blob: 9c7a4cea1628bbbb17608a7af3809515a867e1cf [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#ifdef CONFIG_IP_MULTICAST
194
195/*
196 * Timer management
197 */
198
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000199static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 spin_lock_bh(&im->lock);
202 if (del_timer(&im->timer))
203 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800204 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 im->reporter = 0;
206 im->unsolicit_count = 0;
207 spin_unlock_bh(&im->lock);
208}
209
210/* It must be called with locked im->lock */
211static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
212{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500213 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800215 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!mod_timer(&im->timer, jiffies+tv+2))
217 atomic_inc(&im->refcnt);
218}
219
220static void igmp_gq_start_timer(struct in_device *in_dev)
221{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500222 int tv = prandom_u32() % in_dev->mr_maxdelay;
Michal Tesarfe1e13c2017-01-02 14:38:36 +0100223 unsigned long exp = jiffies + tv + 2;
224
225 if (in_dev->mr_gq_running &&
226 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
227 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 in_dev->mr_gq_running = 1;
Michal Tesarfe1e13c2017-01-02 14:38:36 +0100230 if (!mod_timer(&in_dev->mr_gq_timer, exp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 in_dev_hold(in_dev);
232}
233
234static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
235{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500236 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
239 in_dev_hold(in_dev);
240}
241
242static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
243{
244 spin_lock_bh(&im->lock);
245 im->unsolicit_count = 0;
246 if (del_timer(&im->timer)) {
247 if ((long)(im->timer.expires-jiffies) < max_delay) {
248 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800249 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 spin_unlock_bh(&im->lock);
251 return;
252 }
253 atomic_dec(&im->refcnt);
254 }
255 igmp_start_timer(im, max_delay);
256 spin_unlock_bh(&im->lock);
257}
258
259
260/*
261 * Send an IGMP report.
262 */
263
264#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
265
266
267static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
268 int gdeleted, int sdeleted)
269{
270 switch (type) {
271 case IGMPV3_MODE_IS_INCLUDE:
272 case IGMPV3_MODE_IS_EXCLUDE:
273 if (gdeleted || sdeleted)
274 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800275 if (!(pmc->gsquery && !psf->sf_gsresp)) {
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return 1;
278 /* don't include if this source is excluded
279 * in all filters
280 */
281 if (psf->sf_count[MCAST_INCLUDE])
282 return type == IGMPV3_MODE_IS_INCLUDE;
283 return pmc->sfcount[MCAST_EXCLUDE] ==
284 psf->sf_count[MCAST_EXCLUDE];
285 }
286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 case IGMPV3_CHANGE_TO_INCLUDE:
288 if (gdeleted || sdeleted)
289 return 0;
290 return psf->sf_count[MCAST_INCLUDE] != 0;
291 case IGMPV3_CHANGE_TO_EXCLUDE:
292 if (gdeleted || sdeleted)
293 return 0;
294 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
295 psf->sf_count[MCAST_INCLUDE])
296 return 0;
297 return pmc->sfcount[MCAST_EXCLUDE] ==
298 psf->sf_count[MCAST_EXCLUDE];
299 case IGMPV3_ALLOW_NEW_SOURCES:
300 if (gdeleted || !psf->sf_crcount)
301 return 0;
302 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
303 case IGMPV3_BLOCK_OLD_SOURCES:
304 if (pmc->sfmode == MCAST_INCLUDE)
305 return gdeleted || (psf->sf_crcount && sdeleted);
306 return psf->sf_crcount && !gdeleted && !sdeleted;
307 }
308 return 0;
309}
310
311static int
312igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
313{
314 struct ip_sf_list *psf;
315 int scount = 0;
316
Weilong Chenc71151f2013-12-23 14:37:29 +0800317 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
319 continue;
320 scount++;
321 }
322 return scount;
323}
324
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800325/* source address selection per RFC 3376 section 4.2.13 */
326static __be32 igmpv3_get_srcaddr(struct net_device *dev,
327 const struct flowi4 *fl4)
328{
329 struct in_device *in_dev = __in_dev_get_rcu(dev);
330
331 if (!in_dev)
332 return htonl(INADDR_ANY);
333
334 for_ifa(in_dev) {
Felix Fietkau0ae16962018-01-19 11:50:46 +0100335 if (fl4->saddr == ifa->ifa_local)
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800336 return fl4->saddr;
337 } endfor_ifa(in_dev);
338
339 return htonl(INADDR_ANY);
340}
341
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100342static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct sk_buff *skb;
345 struct rtable *rt;
346 struct iphdr *pip;
347 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700348 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700349 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000350 int hlen = LL_RESERVED_SPACE(dev);
351 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100352 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000354 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000355 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000356 GFP_ATOMIC | __GFP_NOWARN);
357 if (skb)
358 break;
359 size >>= 1;
360 if (size < 256)
361 return NULL;
362 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200363 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
David S. Miller31e45432011-05-03 20:25:42 -0700365 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500366 0, 0,
367 IPPROTO_IGMP, 0, dev->ifindex);
368 if (IS_ERR(rt)) {
369 kfree_skb(skb);
370 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Changli Gaod8d1f302010-06-10 23:31:35 -0700373 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 skb->dev = dev;
375
Herbert Xu66088242011-11-18 02:20:04 +0000376 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800377 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300379 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700380 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300381 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 pip->version = 4;
384 pip->ihl = (sizeof(struct iphdr)+4)>>2;
385 pip->tos = 0xc0;
386 pip->frag_off = htons(IP_DF);
387 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700388 pip->daddr = fl4.daddr;
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800389 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 pip->protocol = IPPROTO_IGMP;
391 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100392 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000393 ((u8 *)&pip[1])[0] = IPOPT_RA;
394 ((u8 *)&pip[1])[1] = 4;
395 ((u8 *)&pip[1])[2] = 0;
396 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700398 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300399 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300400 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
402 pig->resv1 = 0;
403 pig->csum = 0;
404 pig->resv2 = 0;
405 pig->ngrec = 0;
406 return skb;
407}
408
409static int igmpv3_sendpack(struct sk_buff *skb)
410{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300411 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000412 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300414 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Eric W. Biederman33224b12015-10-07 16:48:46 -0500416 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
420{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800421 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800425 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct net_device *dev = pmc->interface->dev;
428 struct igmpv3_report *pih;
429 struct igmpv3_grec *pgr;
430
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800431 if (!skb) {
432 skb = igmpv3_newpack(dev, mtu);
433 if (!skb)
434 return NULL;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
437 pgr->grec_type = type;
438 pgr->grec_auxwords = 0;
439 pgr->grec_nsrcs = 0;
440 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300441 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 pih->ngrec = htons(ntohs(pih->ngrec)+1);
443 *ppgr = pgr;
444 return skb;
445}
446
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100447#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
450 int type, int gdeleted, int sdeleted)
451{
452 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200453 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct igmpv3_report *pih;
455 struct igmpv3_grec *pgr = NULL;
456 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800457 int scount, stotal, first, isquery, truncate;
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800458 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (pmc->multiaddr == IGMP_ALL_HOSTS)
461 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200462 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100463 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800465 mtu = READ_ONCE(dev->mtu);
466 if (mtu < IPV4_MIN_MTU)
467 return skb;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
470 type == IGMPV3_MODE_IS_EXCLUDE;
471 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
472 type == IGMPV3_CHANGE_TO_EXCLUDE;
473
David L Stevensad125832006-01-18 14:20:56 -0800474 stotal = scount = 0;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
477
David L Stevensad125832006-01-18 14:20:56 -0800478 if (!*psf_list)
479 goto empty_source;
480
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300481 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* EX and TO_EX get a fresh packet, if needed */
484 if (truncate) {
485 if (pih && pih->ngrec &&
486 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
487 if (skb)
488 igmpv3_sendpack(skb);
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800489 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 }
492 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800494 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700495 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 psf_next = psf->sf_next;
498
499 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
500 psf_prev = psf;
501 continue;
502 }
503
Hangbin Liua0525172016-08-02 18:02:57 +0800504 /* Based on RFC3376 5.1. Should not send source-list change
505 * records when there is a filter mode change.
506 */
507 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
508 (!gdeleted && pmc->crcount)) &&
509 (type == IGMPV3_ALLOW_NEW_SOURCES ||
510 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
511 goto decrease_sf_crcount;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* clear marks on query responses */
514 if (isquery)
515 psf->sf_gsresp = 0;
516
Al Viro63007722006-09-27 18:31:32 -0700517 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 first*sizeof(struct igmpv3_grec)) {
519 if (truncate && !first)
520 break; /* truncate these */
521 if (pgr)
522 pgr->grec_nsrcs = htons(scount);
523 if (skb)
524 igmpv3_sendpack(skb);
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800525 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 first = 1;
527 scount = 0;
528 }
529 if (first) {
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800530 skb = add_grhead(skb, pmc, type, &pgr, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 first = 0;
532 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800533 if (!skb)
534 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700535 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800537 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
539 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
Hangbin Liua0525172016-08-02 18:02:57 +0800540decrease_sf_crcount:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 psf->sf_crcount--;
542 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
543 if (psf_prev)
544 psf_prev->sf_next = psf->sf_next;
545 else
546 *psf_list = psf->sf_next;
547 kfree(psf);
548 continue;
549 }
550 }
551 psf_prev = psf;
552 }
David L Stevensad125832006-01-18 14:20:56 -0800553
554empty_source:
555 if (!stotal) {
556 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
557 type == IGMPV3_BLOCK_OLD_SOURCES)
558 return skb;
559 if (pmc->crcount || isquery) {
560 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800561 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800562 igmpv3_sendpack(skb);
563 skb = NULL; /* add_grhead will get a new one */
564 }
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800565 skb = add_grhead(skb, pmc, type, &pgr, mtu);
David L Stevensad125832006-01-18 14:20:56 -0800566 }
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (pgr)
569 pgr->grec_nsrcs = htons(scount);
570
571 if (isquery)
572 pmc->gsquery = 0; /* clear query state on report */
573 return skb;
574}
575
576static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
577{
578 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200579 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int type;
581
582 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000583 rcu_read_lock();
584 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (pmc->multiaddr == IGMP_ALL_HOSTS)
586 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100587 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200588 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100589 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock_bh(&pmc->lock);
591 if (pmc->sfcount[MCAST_EXCLUDE])
592 type = IGMPV3_MODE_IS_EXCLUDE;
593 else
594 type = IGMPV3_MODE_IS_INCLUDE;
595 skb = add_grec(skb, pmc, type, 0, 0);
596 spin_unlock_bh(&pmc->lock);
597 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000598 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else {
600 spin_lock_bh(&pmc->lock);
601 if (pmc->sfcount[MCAST_EXCLUDE])
602 type = IGMPV3_MODE_IS_EXCLUDE;
603 else
604 type = IGMPV3_MODE_IS_INCLUDE;
605 skb = add_grec(skb, pmc, type, 0, 0);
606 spin_unlock_bh(&pmc->lock);
607 }
608 if (!skb)
609 return 0;
610 return igmpv3_sendpack(skb);
611}
612
613/*
614 * remove zero-count source records from a source filter list
615 */
616static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
617{
618 struct ip_sf_list *psf_prev, *psf_next, *psf;
619
620 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800621 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 psf_next = psf->sf_next;
623 if (psf->sf_crcount == 0) {
624 if (psf_prev)
625 psf_prev->sf_next = psf->sf_next;
626 else
627 *ppsf = psf->sf_next;
628 kfree(psf);
629 } else
630 psf_prev = psf;
631 }
632}
633
634static void igmpv3_send_cr(struct in_device *in_dev)
635{
636 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
637 struct sk_buff *skb = NULL;
638 int type, dtype;
639
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000640 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 spin_lock_bh(&in_dev->mc_tomb_lock);
642
643 /* deleted MCA's */
644 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800645 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 pmc_next = pmc->next;
647 if (pmc->sfmode == MCAST_INCLUDE) {
648 type = IGMPV3_BLOCK_OLD_SOURCES;
649 dtype = IGMPV3_BLOCK_OLD_SOURCES;
650 skb = add_grec(skb, pmc, type, 1, 0);
651 skb = add_grec(skb, pmc, dtype, 1, 1);
652 }
653 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (pmc->sfmode == MCAST_EXCLUDE) {
655 type = IGMPV3_CHANGE_TO_INCLUDE;
656 skb = add_grec(skb, pmc, type, 1, 0);
657 }
David L Stevensad125832006-01-18 14:20:56 -0800658 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (pmc->crcount == 0) {
660 igmpv3_clear_zeros(&pmc->tomb);
661 igmpv3_clear_zeros(&pmc->sources);
662 }
663 }
664 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
665 if (pmc_prev)
666 pmc_prev->next = pmc_next;
667 else
668 in_dev->mc_tomb = pmc_next;
669 in_dev_put(pmc->interface);
670 kfree(pmc);
671 } else
672 pmc_prev = pmc;
673 }
674 spin_unlock_bh(&in_dev->mc_tomb_lock);
675
676 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000677 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 spin_lock_bh(&pmc->lock);
679 if (pmc->sfcount[MCAST_EXCLUDE]) {
680 type = IGMPV3_BLOCK_OLD_SOURCES;
681 dtype = IGMPV3_ALLOW_NEW_SOURCES;
682 } else {
683 type = IGMPV3_ALLOW_NEW_SOURCES;
684 dtype = IGMPV3_BLOCK_OLD_SOURCES;
685 }
686 skb = add_grec(skb, pmc, type, 0, 0);
687 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
688
689 /* filter mode changes */
690 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (pmc->sfmode == MCAST_EXCLUDE)
692 type = IGMPV3_CHANGE_TO_EXCLUDE;
693 else
694 type = IGMPV3_CHANGE_TO_INCLUDE;
695 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800696 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 spin_unlock_bh(&pmc->lock);
699 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (!skb)
703 return;
704 (void) igmpv3_sendpack(skb);
705}
706
707static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
708 int type)
709{
710 struct sk_buff *skb;
711 struct iphdr *iph;
712 struct igmphdr *ih;
713 struct rtable *rt;
714 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700715 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700716 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700717 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700718 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000719 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
722 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100723
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200724 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100725 return 0;
726
727 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 dst = IGMP_ALL_ROUTER;
729 else
730 dst = group;
731
David S. Miller31e45432011-05-03 20:25:42 -0700732 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500733 0, 0,
734 IPPROTO_IGMP, 0, dev->ifindex);
735 if (IS_ERR(rt))
736 return -1;
737
Herbert Xu66088242011-11-18 02:20:04 +0000738 hlen = LL_RESERVED_SPACE(dev);
739 tlen = dev->needed_tailroom;
740 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100741 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ip_rt_put(rt);
743 return -1;
744 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200745 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Changli Gaod8d1f302010-06-10 23:31:35 -0700747 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Herbert Xu66088242011-11-18 02:20:04 +0000749 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300751 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700752 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300753 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 iph->version = 4;
756 iph->ihl = (sizeof(struct iphdr)+4)>>2;
757 iph->tos = 0xc0;
758 iph->frag_off = htons(IP_DF);
759 iph->ttl = 1;
760 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700761 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100763 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000764 ((u8 *)&iph[1])[0] = IPOPT_RA;
765 ((u8 *)&iph[1])[1] = 4;
766 ((u8 *)&iph[1])[2] = 0;
767 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800770 ih->type = type;
771 ih->code = 0;
772 ih->csum = 0;
773 ih->group = group;
774 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Eric W. Biederman33224b12015-10-07 16:48:46 -0500776 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779static void igmp_gq_timer_expire(unsigned long data)
780{
781 struct in_device *in_dev = (struct in_device *)data;
782
783 in_dev->mr_gq_running = 0;
784 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700785 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788static void igmp_ifc_timer_expire(unsigned long data)
789{
790 struct in_device *in_dev = (struct in_device *)data;
791
792 igmpv3_send_cr(in_dev);
793 if (in_dev->mr_ifc_count) {
794 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100795 igmp_ifc_start_timer(in_dev,
796 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700798 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801static void igmp_ifc_event(struct in_device *in_dev)
802{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200803 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
805 return;
Nikolay Borisov165094a2016-02-08 23:29:24 +0200806 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 igmp_ifc_start_timer(in_dev, 1);
808}
809
810
811static void igmp_timer_expire(unsigned long data)
812{
Weilong Chenc71151f2013-12-23 14:37:29 +0800813 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct in_device *in_dev = im->interface;
815
816 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800817 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (im->unsolicit_count) {
820 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100821 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 im->reporter = 1;
824 spin_unlock(&im->lock);
825
826 if (IGMP_V1_SEEN(in_dev))
827 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
828 else if (IGMP_V2_SEEN(in_dev))
829 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
830 else
831 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
832
833 ip_ma_put(im);
834}
835
David L Stevensad125832006-01-18 14:20:56 -0800836/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700837static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct ip_sf_list *psf;
840 int i, scount;
841
842 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800843 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (scount == nsrcs)
845 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800846 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800847 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000848 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800849 pmc->sfcount[MCAST_EXCLUDE] !=
850 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800851 break;
David L Stevensad125832006-01-18 14:20:56 -0800852 if (srcs[i] == psf->sf_inaddr) {
853 scount++;
854 break;
855 }
856 }
857 }
858 pmc->gsquery = 0;
859 if (scount == nsrcs) /* all sources excluded */
860 return 0;
861 return 1;
862}
863
Al Viroea4d9e72006-09-27 18:30:52 -0700864static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800865{
866 struct ip_sf_list *psf;
867 int i, scount;
868
869 if (pmc->sfmode == MCAST_EXCLUDE)
870 return igmp_xmarksources(pmc, nsrcs, srcs);
871
872 /* mark INCLUDE-mode sources */
873 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800874 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800875 if (scount == nsrcs)
876 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800877 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (srcs[i] == psf->sf_inaddr) {
879 psf->sf_gsresp = 1;
880 scount++;
881 break;
882 }
883 }
David L Stevensad125832006-01-18 14:20:56 -0800884 if (!scount) {
885 pmc->gsquery = 0;
886 return 0;
887 }
888 pmc->gsquery = 1;
889 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Eric Dumazetd679c532012-09-06 20:37:06 +0000892/* return true if packet was dropped */
893static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200896 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /* Timers are only set for non-local groups */
899
900 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000901 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200902 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100903 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000905 rcu_read_lock();
906 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (im->multiaddr == group) {
908 igmp_stop_timer(im);
909 break;
910 }
911 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000912 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000913 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Eric Dumazetd679c532012-09-06 20:37:06 +0000916/* return true if packet was dropped */
917static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int len)
919{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300920 struct igmphdr *ih = igmp_hdr(skb);
921 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700923 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int max_delay;
925 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200926 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928
David Stevens5b7c8402010-09-30 14:29:40 +0000929 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (ih->code == 0) {
931 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900932
Fabian Frederick436f7c22014-11-04 20:52:14 +0100933 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 in_dev->mr_v1_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100935 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 group = 0;
937 } else {
938 /* v2 router present */
939 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
940 in_dev->mr_v2_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100941 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 /* cancel the interface change timer */
944 in_dev->mr_ifc_count = 0;
945 if (del_timer(&in_dev->mr_ifc_timer))
946 __in_dev_put(in_dev);
947 /* clear deleted report items */
948 igmpv3_clear_delrec(in_dev);
949 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000950 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000951 } else if (IGMP_V1_SEEN(in_dev)) {
952 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100953 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000954 group = 0;
955 } else if (IGMP_V2_SEEN(in_dev)) {
956 /* this is a v3 query with v2 queriers present;
957 * Interpretation of the max_delay code is problematic here.
958 * A real v2 host would use ih_code directly, while v3 has a
959 * different encoding. We use the v3 encoding as more likely
960 * to be intended in a v3 query.
961 */
962 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800963 if (!max_delay)
964 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 } else { /* v3 */
966 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000967 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900968
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300969 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900971 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700972 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000973 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300974 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
978 if (!max_delay)
979 max_delay = 1; /* can't mod w/ 0 */
980 in_dev->mr_maxdelay = max_delay;
981 if (ih3->qrv)
982 in_dev->mr_qrv = ih3->qrv;
983 if (!group) { /* general query */
984 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +0200985 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000987 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 /* mark sources to include, if group & source-specific */
990 mark = ih3->nsrcs != 0;
991 }
992
993 /*
994 * - Start the timers in all of our membership records
995 * that the query applies to for the interface on
996 * which the query arrived excl. those that belong
997 * to a "local" group (224.0.0.X)
998 * - For timers already running check if they need to
999 * be reset.
1000 * - Use the igmp->igmp_code field as the maximum
1001 * delay possible
1002 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001003 rcu_read_lock();
1004 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -08001005 int changed;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (group && group != im->multiaddr)
1008 continue;
1009 if (im->multiaddr == IGMP_ALL_HOSTS)
1010 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001011 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001012 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001013 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 spin_lock_bh(&im->lock);
1015 if (im->tm_running)
1016 im->gsquery = im->gsquery && mark;
1017 else
1018 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -08001019 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001020 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -08001022 if (changed)
1023 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001025 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +00001026 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001029/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030int igmp_rcv(struct sk_buff *skb)
1031{
1032 /* This basically follows the spec line by line -- see RFC1112 */
1033 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001034 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001036 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Ian Morris51456b22015-04-03 09:17:26 +01001038 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001039 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Herbert Xufb286bb2005-11-10 13:01:24 -08001041 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001042 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001043
Tom Herbertde08dc12014-05-07 16:52:10 -07001044 if (skb_checksum_simple_validate(skb))
1045 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001047 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 switch (ih->type) {
1049 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001050 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 break;
1052 case IGMP_HOST_MEMBERSHIP_REPORT:
1053 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001055 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 break;
David Stevens24c69272005-12-02 20:32:59 -08001057 /* don't rely on MC router hearing unicast reports */
1058 if (skb->pkt_type == PACKET_MULTICAST ||
1059 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001060 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 break;
1062 case IGMP_PIM:
1063#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return pim_rcv_v1(skb);
1065#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001066 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 case IGMP_DVMRP:
1068 case IGMP_TRACE:
1069 case IGMP_HOST_LEAVE_MESSAGE:
1070 case IGMP_MTRACE:
1071 case IGMP_MTRACE_RESP:
1072 break;
1073 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001076
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001077drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001078 if (dropped)
1079 kfree_skb(skb);
1080 else
1081 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
1083}
1084
1085#endif
1086
1087
1088/*
1089 * Add a filter to a device
1090 */
1091
Al Viro63007722006-09-27 18:31:32 -07001092static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 char buf[MAX_ADDR_LEN];
1095 struct net_device *dev = in_dev->dev;
1096
1097 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1098 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001099 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 routine. Something sort of:
1101 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1102 --ANK
1103 */
1104 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001105 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108/*
1109 * Remove a filter from a device
1110 */
1111
Al Viro63007722006-09-27 18:31:32 -07001112static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 char buf[MAX_ADDR_LEN];
1115 struct net_device *dev = in_dev->dev;
1116
1117 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001118 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121#ifdef CONFIG_IP_MULTICAST
1122/*
1123 * deleted ip_mc_list manipulation
1124 */
1125static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1126{
1127 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001128 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /* this is an "ip_mc_list" for convenience; only the fields below
1131 * are actually used. In particular, the refcnt and users are not
1132 * used for management of the delete list. Using the same structure
1133 * for deleted items allows change reports to use common code with
1134 * non-deleted or query-response MCA's.
1135 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001136 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!pmc)
1138 return;
WANG Congcac2a9b2017-06-20 10:46:27 -07001139 spin_lock_init(&pmc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 spin_lock_bh(&im->lock);
1141 pmc->interface = im->interface;
1142 in_dev_hold(in_dev);
1143 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001144 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 pmc->sfmode = im->sfmode;
1146 if (pmc->sfmode == MCAST_INCLUDE) {
1147 struct ip_sf_list *psf;
1148
1149 pmc->tomb = im->tomb;
1150 pmc->sources = im->sources;
1151 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001152 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 psf->sf_crcount = pmc->crcount;
1154 }
1155 spin_unlock_bh(&im->lock);
1156
1157 spin_lock_bh(&in_dev->mc_tomb_lock);
1158 pmc->next = in_dev->mc_tomb;
1159 in_dev->mc_tomb = pmc;
1160 spin_unlock_bh(&in_dev->mc_tomb_lock);
1161}
1162
Hangbin Liu24803f32016-11-14 16:16:28 +08001163/*
1164 * restore ip_mc_list deleted records
1165 */
1166static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001169 struct ip_sf_list *psf;
1170 struct net *net = dev_net(in_dev->dev);
1171 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 spin_lock_bh(&in_dev->mc_tomb_lock);
1174 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001175 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (pmc->multiaddr == multiaddr)
1177 break;
1178 pmc_prev = pmc;
1179 }
1180 if (pmc) {
1181 if (pmc_prev)
1182 pmc_prev->next = pmc->next;
1183 else
1184 in_dev->mc_tomb = pmc->next;
1185 }
1186 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001187
1188 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001190 im->interface = pmc->interface;
1191 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1192 im->sfmode = pmc->sfmode;
1193 if (pmc->sfmode == MCAST_INCLUDE) {
1194 im->tomb = pmc->tomb;
1195 im->sources = pmc->sources;
1196 for (psf = im->sources; psf; psf = psf->sf_next)
1197 psf->sf_crcount = im->crcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199 in_dev_put(pmc->interface);
Hangbin Liu16a3fbe2017-02-08 21:16:45 +08001200 kfree(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001202 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Hangbin Liu24803f32016-11-14 16:16:28 +08001205/*
1206 * flush ip_mc_list deleted records
1207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208static void igmpv3_clear_delrec(struct in_device *in_dev)
1209{
1210 struct ip_mc_list *pmc, *nextpmc;
1211
1212 spin_lock_bh(&in_dev->mc_tomb_lock);
1213 pmc = in_dev->mc_tomb;
1214 in_dev->mc_tomb = NULL;
1215 spin_unlock_bh(&in_dev->mc_tomb_lock);
1216
1217 for (; pmc; pmc = nextpmc) {
1218 nextpmc = pmc->next;
1219 ip_mc_clear_src(pmc);
1220 in_dev_put(pmc->interface);
1221 kfree(pmc);
1222 }
1223 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001224 rcu_read_lock();
1225 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 struct ip_sf_list *psf, *psf_next;
1227
1228 spin_lock_bh(&pmc->lock);
1229 psf = pmc->tomb;
1230 pmc->tomb = NULL;
1231 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001232 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 psf_next = psf->sf_next;
1234 kfree(psf);
1235 }
1236 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001237 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239#endif
1240
1241static void igmp_group_dropped(struct ip_mc_list *im)
1242{
1243 struct in_device *in_dev = im->interface;
1244#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001245 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int reporter;
1247#endif
1248
1249 if (im->loaded) {
1250 im->loaded = 0;
1251 ip_mc_filter_del(in_dev, im->multiaddr);
1252 }
1253
1254#ifdef CONFIG_IP_MULTICAST
1255 if (im->multiaddr == IGMP_ALL_HOSTS)
1256 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001257 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001258 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 reporter = im->reporter;
1261 igmp_stop_timer(im);
1262
1263 if (!in_dev->dead) {
1264 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001265 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (IGMP_V2_SEEN(in_dev)) {
1267 if (reporter)
1268 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001269 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 /* IGMPv3 */
1272 igmpv3_add_delrec(in_dev, im);
1273
1274 igmp_ifc_event(in_dev);
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279static void igmp_group_added(struct ip_mc_list *im)
1280{
1281 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001282#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001283 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001284#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (im->loaded == 0) {
1287 im->loaded = 1;
1288 ip_mc_filter_add(in_dev, im->multiaddr);
1289 }
1290
1291#ifdef CONFIG_IP_MULTICAST
1292 if (im->multiaddr == IGMP_ALL_HOSTS)
1293 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001294 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001295 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (in_dev->dead)
1298 return;
1299 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1300 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001301 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 spin_unlock_bh(&im->lock);
1303 return;
1304 }
1305 /* else, v3 */
1306
Nikolay Borisov165094a2016-02-08 23:29:24 +02001307 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 igmp_ifc_event(in_dev);
1309#endif
1310}
1311
1312
1313/*
1314 * Multicast list managers
1315 */
1316
Eric Dumazete9897072013-06-07 08:48:57 -07001317static u32 ip_mc_hash(const struct ip_mc_list *im)
1318{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001319 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001320}
1321
1322static void ip_mc_hash_add(struct in_device *in_dev,
1323 struct ip_mc_list *im)
1324{
1325 struct ip_mc_list __rcu **mc_hash;
1326 u32 hash;
1327
1328 mc_hash = rtnl_dereference(in_dev->mc_hash);
1329 if (mc_hash) {
1330 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001331 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001332 rcu_assign_pointer(mc_hash[hash], im);
1333 return;
1334 }
1335
1336 /* do not use a hash table for small number of items */
1337 if (in_dev->mc_count < 4)
1338 return;
1339
1340 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1341 GFP_KERNEL);
1342 if (!mc_hash)
1343 return;
1344
1345 for_each_pmc_rtnl(in_dev, im) {
1346 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001347 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001348 RCU_INIT_POINTER(mc_hash[hash], im);
1349 }
1350
1351 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1352}
1353
1354static void ip_mc_hash_remove(struct in_device *in_dev,
1355 struct ip_mc_list *im)
1356{
1357 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1358 struct ip_mc_list *aux;
1359
1360 if (!mc_hash)
1361 return;
1362 mc_hash += ip_mc_hash(im);
1363 while ((aux = rtnl_dereference(*mc_hash)) != im)
1364 mc_hash = &aux->next_hash;
1365 *mc_hash = im->next_hash;
1366}
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369/*
1370 * A socket has joined a multicast group on device dev.
1371 */
1372
Al Viro8f935bb2006-09-27 18:30:07 -07001373void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct ip_mc_list *im;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001376#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001377 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001378#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 ASSERT_RTNL();
1381
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001382 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (im->multiaddr == addr) {
1384 im->users++;
1385 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1386 goto out;
1387 }
1388 }
1389
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001390 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!im)
1392 goto out;
1393
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001394 im->users = 1;
1395 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001397 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* initial mode is (EX, empty) */
1399 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 atomic_set(&im->refcnt, 1);
1402 spin_lock_init(&im->lock);
1403#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301404 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001405 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001407
1408 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001409 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001410 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001411
Eric Dumazete9897072013-06-07 08:48:57 -07001412 ip_mc_hash_add(in_dev, im);
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001415 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416#endif
1417 igmp_group_added(im);
1418 if (!in_dev->dead)
1419 ip_rt_multicast_event(in_dev);
1420out:
1421 return;
1422}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001423EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001425static int ip_mc_check_iphdr(struct sk_buff *skb)
1426{
1427 const struct iphdr *iph;
1428 unsigned int len;
1429 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1430
1431 if (!pskb_may_pull(skb, offset))
1432 return -EINVAL;
1433
1434 iph = ip_hdr(skb);
1435
1436 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1437 return -EINVAL;
1438
1439 offset += ip_hdrlen(skb) - sizeof(*iph);
1440
1441 if (!pskb_may_pull(skb, offset))
1442 return -EINVAL;
1443
1444 iph = ip_hdr(skb);
1445
1446 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1447 return -EINVAL;
1448
1449 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1450 if (skb->len < len || len < offset)
1451 return -EINVAL;
1452
1453 skb_set_transport_header(skb, offset);
1454
1455 return 0;
1456}
1457
1458static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1459{
1460 unsigned int len = skb_transport_offset(skb);
1461
1462 len += sizeof(struct igmpv3_report);
1463
1464 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1465}
1466
1467static int ip_mc_check_igmp_query(struct sk_buff *skb)
1468{
1469 unsigned int len = skb_transport_offset(skb);
1470
1471 len += sizeof(struct igmphdr);
1472 if (skb->len < len)
1473 return -EINVAL;
1474
1475 /* IGMPv{1,2}? */
1476 if (skb->len != len) {
1477 /* or IGMPv3? */
1478 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1479 if (skb->len < len || !pskb_may_pull(skb, len))
1480 return -EINVAL;
1481 }
1482
1483 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1484 * all-systems destination addresses (224.0.0.1) for general queries
1485 */
1486 if (!igmp_hdr(skb)->group &&
1487 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1488 return -EINVAL;
1489
1490 return 0;
1491}
1492
1493static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1494{
1495 switch (igmp_hdr(skb)->type) {
1496 case IGMP_HOST_LEAVE_MESSAGE:
1497 case IGMP_HOST_MEMBERSHIP_REPORT:
1498 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1499 /* fall through */
1500 return 0;
1501 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1502 return ip_mc_check_igmp_reportv3(skb);
1503 case IGMP_HOST_MEMBERSHIP_QUERY:
1504 return ip_mc_check_igmp_query(skb);
1505 default:
1506 return -ENOMSG;
1507 }
1508}
1509
1510static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1511{
1512 return skb_checksum_simple_validate(skb);
1513}
1514
1515static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1516
1517{
1518 struct sk_buff *skb_chk;
1519 unsigned int transport_len;
1520 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga5169932015-08-13 05:54:07 +02001521 int ret = -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001522
1523 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1524
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001525 skb_chk = skb_checksum_trimmed(skb, transport_len,
1526 ip_mc_validate_checksum);
1527 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02001528 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001529
Linus Lüssinga5169932015-08-13 05:54:07 +02001530 if (!pskb_may_pull(skb_chk, len))
1531 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001532
1533 ret = ip_mc_check_igmp_msg(skb_chk);
Linus Lüssinga5169932015-08-13 05:54:07 +02001534 if (ret)
1535 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001536
1537 if (skb_trimmed)
1538 *skb_trimmed = skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02001539 /* free now unneeded clone */
1540 else if (skb_chk != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001541 kfree_skb(skb_chk);
1542
Linus Lüssinga5169932015-08-13 05:54:07 +02001543 ret = 0;
1544
1545err:
1546 if (ret && skb_chk && skb_chk != skb)
1547 kfree_skb(skb_chk);
1548
1549 return ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001550}
1551
1552/**
1553 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1554 * @skb: the skb to validate
1555 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1556 *
1557 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001558 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001559 *
1560 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1561 * standard
1562 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1563 * -ENOMEM: A memory allocation failure happened.
1564 *
1565 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1566 * to NULL): After parsing an IGMP packet successfully it will point to
1567 * an skb which has its tail aligned to the IP packet end. This might
1568 * either be the originally provided skb or a trimmed, cloned version if
1569 * the skb frame had data beyond the IP packet. A cloned skb allows us
1570 * to leave the original skb and its full frame unchanged (which might be
1571 * desirable for layer 2 frame jugglers).
1572 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001573 * Caller needs to set the skb network header and free any returned skb if it
1574 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001575 */
1576int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1577{
1578 int ret = ip_mc_check_iphdr(skb);
1579
1580 if (ret < 0)
1581 return ret;
1582
1583 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1584 return -ENOMSG;
1585
1586 return __ip_mc_check_igmp(skb, skb_trimmed);
1587}
1588EXPORT_SYMBOL(ip_mc_check_igmp);
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001591 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001592 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001593static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001594{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001595#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001596 struct ip_mc_list *im;
1597 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001598 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001599
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001600 ASSERT_RTNL();
1601
1602 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001603 if (im->multiaddr == IGMP_ALL_HOSTS)
1604 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001605 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001606 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001607 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001608
Eric Dumazet866f3b22010-11-18 09:33:19 -08001609 /* a failover is happening and switches
1610 * must be notified immediately
1611 */
1612 if (IGMP_V1_SEEN(in_dev))
1613 type = IGMP_HOST_MEMBERSHIP_REPORT;
1614 else if (IGMP_V2_SEEN(in_dev))
1615 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1616 else
1617 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1618 igmp_send_report(in_dev, im, type);
1619 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001620#endif
1621}
1622
1623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * A socket has left a multicast group on device dev
1625 */
1626
Al Viro8f935bb2006-09-27 18:30:07 -07001627void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001629 struct ip_mc_list *i;
1630 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001633
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001634 for (ip = &in_dev->mc_list;
1635 (i = rtnl_dereference(*ip)) != NULL;
1636 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001637 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001639 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001640 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001641 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001643 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 if (!in_dev->dead)
1646 ip_rt_multicast_event(in_dev);
1647
1648 ip_ma_put(i);
1649 return;
1650 }
1651 break;
1652 }
1653 }
1654}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001655EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Moni Shoua75c78502009-09-15 02:37:40 -07001657/* Device changing type */
1658
1659void ip_mc_unmap(struct in_device *in_dev)
1660{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001661 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001662
1663 ASSERT_RTNL();
1664
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001665 for_each_pmc_rtnl(in_dev, pmc)
1666 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001667}
1668
1669void ip_mc_remap(struct in_device *in_dev)
1670{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001671 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001672
1673 ASSERT_RTNL();
1674
Hangbin Liu24803f32016-11-14 16:16:28 +08001675 for_each_pmc_rtnl(in_dev, pmc) {
1676#ifdef CONFIG_IP_MULTICAST
1677 igmpv3_del_delrec(in_dev, pmc);
1678#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001679 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001680 }
Moni Shoua75c78502009-09-15 02:37:40 -07001681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683/* Device going down */
1684
1685void ip_mc_down(struct in_device *in_dev)
1686{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001687 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 ASSERT_RTNL();
1690
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001691 for_each_pmc_rtnl(in_dev, pmc)
1692 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694#ifdef CONFIG_IP_MULTICAST
1695 in_dev->mr_ifc_count = 0;
1696 if (del_timer(&in_dev->mr_ifc_timer))
1697 __in_dev_put(in_dev);
1698 in_dev->mr_gq_running = 0;
1699 if (del_timer(&in_dev->mr_gq_timer))
1700 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701#endif
1702
1703 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1704}
1705
1706void ip_mc_init_dev(struct in_device *in_dev)
1707{
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001708#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001709 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001710#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 ASSERT_RTNL();
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001714 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1715 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001716 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1717 (unsigned long)in_dev);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001718 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719#endif
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 spin_lock_init(&in_dev->mc_tomb_lock);
1722}
1723
1724/* Device going up */
1725
1726void ip_mc_up(struct in_device *in_dev)
1727{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001728 struct ip_mc_list *pmc;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001729#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001730 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 ASSERT_RTNL();
1734
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001735#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001736 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1739
Hangbin Liu24803f32016-11-14 16:16:28 +08001740 for_each_pmc_rtnl(in_dev, pmc) {
1741#ifdef CONFIG_IP_MULTICAST
1742 igmpv3_del_delrec(in_dev, pmc);
1743#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001744 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
1748/*
1749 * Device is about to be destroyed: clean up.
1750 */
1751
1752void ip_mc_destroy_dev(struct in_device *in_dev)
1753{
1754 struct ip_mc_list *i;
1755
1756 ASSERT_RTNL();
1757
1758 /* Deactivate timers */
1759 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001760#ifdef CONFIG_IP_MULTICAST
1761 igmpv3_clear_delrec(in_dev);
1762#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001764 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1765 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001766 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001771/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001772static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct net_device *dev = NULL;
1775 struct in_device *idev = NULL;
1776
1777 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001778 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return idev;
1780 }
1781 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001782 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (!dev)
1784 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786
David S. Millerb23dd4f2011-03-02 14:31:35 -08001787 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001788 struct rtable *rt = ip_route_output(net,
1789 imr->imr_multiaddr.s_addr,
1790 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001791 if (!IS_ERR(rt)) {
1792 dev = rt->dst.dev;
1793 ip_rt_put(rt);
1794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796 if (dev) {
1797 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001798 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 return idev;
1801}
1802
1803/*
1804 * Join a socket to a group
1805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001808 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 struct ip_sf_list *psf, *psf_prev;
1811 int rv = 0;
1812
1813 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001814 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (psf->sf_inaddr == *psfsrc)
1816 break;
1817 psf_prev = psf;
1818 }
1819 if (!psf || psf->sf_count[sfmode] == 0) {
1820 /* source filter not found, or count wrong => bug */
1821 return -ESRCH;
1822 }
1823 psf->sf_count[sfmode]--;
1824 if (psf->sf_count[sfmode] == 0) {
1825 ip_rt_multicast_event(pmc->interface);
1826 }
1827 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1828#ifdef CONFIG_IP_MULTICAST
1829 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001830 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831#endif
1832
1833 /* no more filters for this source */
1834 if (psf_prev)
1835 psf_prev->sf_next = psf->sf_next;
1836 else
1837 pmc->sources = psf->sf_next;
1838#ifdef CONFIG_IP_MULTICAST
1839 if (psf->sf_oldin &&
1840 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001841 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 psf->sf_next = pmc->tomb;
1843 pmc->tomb = psf;
1844 rv = 1;
1845 } else
1846#endif
1847 kfree(psf);
1848 }
1849 return rv;
1850}
1851
1852#ifndef CONFIG_IP_MULTICAST
1853#define igmp_ifc_event(x) do { } while (0)
1854#endif
1855
Al Viro8f935bb2006-09-27 18:30:07 -07001856static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1857 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 struct ip_mc_list *pmc;
1860 int changerec = 0;
1861 int i, err;
1862
1863 if (!in_dev)
1864 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001865 rcu_read_lock();
1866 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (*pmca == pmc->multiaddr)
1868 break;
1869 }
1870 if (!pmc) {
1871 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001872 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return -ESRCH;
1874 }
1875 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001876 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877#ifdef CONFIG_IP_MULTICAST
1878 sf_markstate(pmc);
1879#endif
1880 if (!delta) {
1881 err = -EINVAL;
1882 if (!pmc->sfcount[sfmode])
1883 goto out_unlock;
1884 pmc->sfcount[sfmode]--;
1885 }
1886 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001887 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1889
1890 changerec |= rv > 0;
1891 if (!err && rv < 0)
1892 err = rv;
1893 }
1894 if (pmc->sfmode == MCAST_EXCLUDE &&
1895 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1896 pmc->sfcount[MCAST_INCLUDE]) {
1897#ifdef CONFIG_IP_MULTICAST
1898 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001899 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900#endif
1901
1902 /* filter mode change */
1903 pmc->sfmode = MCAST_INCLUDE;
1904#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001905 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001907 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 psf->sf_crcount = 0;
1909 igmp_ifc_event(pmc->interface);
1910 } else if (sf_setstate(pmc) || changerec) {
1911 igmp_ifc_event(pmc->interface);
1912#endif
1913 }
1914out_unlock:
1915 spin_unlock_bh(&pmc->lock);
1916 return err;
1917}
1918
1919/*
1920 * Add multicast single-source filter to the interface list
1921 */
1922static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001923 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 struct ip_sf_list *psf, *psf_prev;
1926
1927 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001928 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (psf->sf_inaddr == *psfsrc)
1930 break;
1931 psf_prev = psf;
1932 }
1933 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001934 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (!psf)
1936 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 psf->sf_inaddr = *psfsrc;
1938 if (psf_prev) {
1939 psf_prev->sf_next = psf;
1940 } else
1941 pmc->sources = psf;
1942 }
1943 psf->sf_count[sfmode]++;
1944 if (psf->sf_count[sfmode] == 1) {
1945 ip_rt_multicast_event(pmc->interface);
1946 }
1947 return 0;
1948}
1949
1950#ifdef CONFIG_IP_MULTICAST
1951static void sf_markstate(struct ip_mc_list *pmc)
1952{
1953 struct ip_sf_list *psf;
1954 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1955
Weilong Chenc71151f2013-12-23 14:37:29 +08001956 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (pmc->sfcount[MCAST_EXCLUDE]) {
1958 psf->sf_oldin = mca_xcount ==
1959 psf->sf_count[MCAST_EXCLUDE] &&
1960 !psf->sf_count[MCAST_INCLUDE];
1961 } else
1962 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1963}
1964
1965static int sf_setstate(struct ip_mc_list *pmc)
1966{
David L Stevensad125832006-01-18 14:20:56 -08001967 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1969 int qrv = pmc->interface->mr_qrv;
1970 int new_in, rv;
1971
1972 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001973 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (pmc->sfcount[MCAST_EXCLUDE]) {
1975 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1976 !psf->sf_count[MCAST_INCLUDE];
1977 } else
1978 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001979 if (new_in) {
1980 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001981 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001982
Weilong Chenc71151f2013-12-23 14:37:29 +08001983 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001984 if (dpsf->sf_inaddr == psf->sf_inaddr)
1985 break;
1986 prev = dpsf;
1987 }
1988 if (dpsf) {
1989 if (prev)
1990 prev->sf_next = dpsf->sf_next;
1991 else
1992 pmc->tomb = dpsf->sf_next;
1993 kfree(dpsf);
1994 }
1995 psf->sf_crcount = qrv;
1996 rv++;
1997 }
1998 } else if (psf->sf_oldin) {
1999
2000 psf->sf_crcount = 0;
2001 /*
2002 * add or update "delete" records if an active filter
2003 * is now inactive
2004 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002005 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08002006 if (dpsf->sf_inaddr == psf->sf_inaddr)
2007 break;
2008 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00002009 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08002010 if (!dpsf)
2011 continue;
2012 *dpsf = *psf;
2013 /* pmc->lock held by callers */
2014 dpsf->sf_next = pmc->tomb;
2015 pmc->tomb = dpsf;
2016 }
2017 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 rv++;
2019 }
2020 }
2021 return rv;
2022}
2023#endif
2024
2025/*
2026 * Add multicast source filter list to the interface list
2027 */
Al Viro8f935bb2006-09-27 18:30:07 -07002028static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2029 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 struct ip_mc_list *pmc;
2032 int isexclude;
2033 int i, err;
2034
2035 if (!in_dev)
2036 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002037 rcu_read_lock();
2038 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (*pmca == pmc->multiaddr)
2040 break;
2041 }
2042 if (!pmc) {
2043 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002044 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return -ESRCH;
2046 }
2047 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002048 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050#ifdef CONFIG_IP_MULTICAST
2051 sf_markstate(pmc);
2052#endif
2053 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2054 if (!delta)
2055 pmc->sfcount[sfmode]++;
2056 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002057 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002058 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (err)
2060 break;
2061 }
2062 if (err) {
2063 int j;
2064
Jun Zhao685f94e62011-11-22 17:19:03 +00002065 if (!delta)
2066 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002067 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002068 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2070#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002072 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002073 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074#endif
2075
2076 /* filter mode change */
2077 if (pmc->sfcount[MCAST_EXCLUDE])
2078 pmc->sfmode = MCAST_EXCLUDE;
2079 else if (pmc->sfcount[MCAST_INCLUDE])
2080 pmc->sfmode = MCAST_INCLUDE;
2081#ifdef CONFIG_IP_MULTICAST
2082 /* else no filters; keep old mode for reports */
2083
Nikolay Borisov165094a2016-02-08 23:29:24 +02002084 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002086 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 psf->sf_crcount = 0;
2088 igmp_ifc_event(in_dev);
2089 } else if (sf_setstate(pmc)) {
2090 igmp_ifc_event(in_dev);
2091#endif
2092 }
2093 spin_unlock_bh(&pmc->lock);
2094 return err;
2095}
2096
2097static void ip_mc_clear_src(struct ip_mc_list *pmc)
2098{
WANG Congecd66272017-06-12 09:52:26 -07002099 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
WANG Congecd66272017-06-12 09:52:26 -07002101 spin_lock_bh(&pmc->lock);
2102 tomb = pmc->tomb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 pmc->tomb = NULL;
WANG Congecd66272017-06-12 09:52:26 -07002104 sources = pmc->sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 pmc->sources = NULL;
2106 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002107 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 pmc->sfcount[MCAST_EXCLUDE] = 1;
WANG Congecd66272017-06-12 09:52:26 -07002109 spin_unlock_bh(&pmc->lock);
2110
2111 for (psf = tomb; psf; psf = nextpsf) {
2112 nextpsf = psf->sf_next;
2113 kfree(psf);
2114 }
2115 for (psf = sources; psf; psf = nextpsf) {
2116 nextpsf = psf->sf_next;
2117 kfree(psf);
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002121/* Join a multicast group
2122 */
2123
2124int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Al Viro8f935bb2006-09-27 18:30:07 -07002126 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002127 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct in_device *in_dev;
2129 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002130 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002131 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002133 int err;
2134
2135 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Joe Perchesf97c1e02007-12-16 13:45:43 -08002137 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return -EINVAL;
2139
Daniel Lezcano877aced2008-08-13 16:15:57 -07002140 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 err = -ENODEV;
2144 goto done;
2145 }
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002148 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002149 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002150 if (i->multi.imr_multiaddr.s_addr == addr &&
2151 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 count++;
2154 }
2155 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002156 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002158 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002159 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002160 goto done;
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002163 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 iml->sflist = NULL;
2165 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002166 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return err;
2171}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002172EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2175 struct in_device *in_dev)
2176{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002177 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 int err;
2179
Ian Morris51456b22015-04-03 09:17:26 +01002180 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /* any-source empty exclude case */
2182 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2183 iml->sfmode, 0, NULL, 0);
2184 }
2185 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002186 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002187 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002188 /* decrease mem now to avoid the memleak warning */
2189 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002190 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return err;
2192}
2193
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002194int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002197 struct ip_mc_socklist *iml;
2198 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002199 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002200 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002201 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002202 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002203 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Eric Dumazet959d10f2015-02-17 03:19:24 -08002205 ASSERT_RTNL();
2206
Daniel Lezcano877aced2008-08-13 16:15:57 -07002207 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002208 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002209 ret = -ENODEV;
2210 goto out;
2211 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002212 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002213 for (imlp = &inet->mc_list;
2214 (iml = rtnl_dereference(*imlp)) != NULL;
2215 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002216 if (iml->multi.imr_multiaddr.s_addr != group)
2217 continue;
2218 if (ifindex) {
2219 if (iml->multi.imr_ifindex != ifindex)
2220 continue;
2221 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2222 iml->multi.imr_address.s_addr)
2223 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
David L Stevensacd6e002006-08-17 16:27:39 -07002225 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002227 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002228
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002229 if (in_dev)
2230 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002231
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002232 /* decrease mem now to avoid the memleak warning */
2233 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002234 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
dingtianhong52ad3532014-07-02 13:50:48 +08002237out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002238 return ret;
2239}
stephen hemminger193ba922012-10-01 12:32:34 +00002240EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242int ip_mc_source(int add, int omode, struct sock *sk, struct
2243 ip_mreq_source *mreqs, int ifindex)
2244{
2245 int err;
2246 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002247 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 struct ip_mc_socklist *pmc;
2249 struct in_device *in_dev = NULL;
2250 struct inet_sock *inet = inet_sk(sk);
2251 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002252 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002253 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 int i, j, rv;
2255
Joe Perchesf97c1e02007-12-16 13:45:43 -08002256 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return -EINVAL;
2258
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002259 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2262 imr.imr_address.s_addr = mreqs->imr_interface;
2263 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002264 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 if (!in_dev) {
2267 err = -ENODEV;
2268 goto done;
2269 }
2270 err = -EADDRNOTAVAIL;
2271
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002272 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002273 if ((pmc->multi.imr_multiaddr.s_addr ==
2274 imr.imr_multiaddr.s_addr) &&
2275 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 break;
2277 }
David L Stevens917f2f12005-07-08 17:45:16 -07002278 if (!pmc) { /* must have a prior join */
2279 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 /* if a source filter was set, must be the same mode as before */
2283 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002284 if (pmc->sfmode != omode) {
2285 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 } else if (pmc->sfmode != omode) {
2289 /* allow mode switches for empty-set filters */
2290 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002291 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 NULL, 0);
2293 pmc->sfmode = omode;
2294 }
2295
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002296 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (!add) {
2298 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002299 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002301 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002303 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (rv == 0)
2305 break;
2306 }
2307 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002308 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
David L Stevens8cdaaa12005-07-08 17:39:23 -07002310 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2311 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2312 leavegroup = 1;
2313 goto done;
2314 }
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002317 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 &mreqs->imr_sourceaddr, 1);
2319
Weilong Chenc71151f2013-12-23 14:37:29 +08002320 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 psl->sl_addr[j-1] = psl->sl_addr[j];
2322 psl->sl_count--;
2323 err = 0;
2324 goto done;
2325 }
2326 /* else, add a new source to the filter */
2327
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002328 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 err = -ENOBUFS;
2330 goto done;
2331 }
2332 if (!psl || psl->sl_count == psl->sl_max) {
2333 struct ip_sf_socklist *newpsl;
2334 int count = IP_SFBLOCK;
2335
2336 if (psl)
2337 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002338 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (!newpsl) {
2340 err = -ENOBUFS;
2341 goto done;
2342 }
2343 newpsl->sl_max = count;
2344 newpsl->sl_count = count - IP_SFBLOCK;
2345 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002346 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002348 /* decrease mem now to avoid the memleak warning */
2349 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002350 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002352 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002353 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002356 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002358 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (rv == 0)
2360 break;
2361 }
2362 if (rv == 0) /* address already there is an error */
2363 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002364 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 psl->sl_addr[j+1] = psl->sl_addr[j];
2366 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2367 psl->sl_count++;
2368 err = 0;
2369 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002370 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 &mreqs->imr_sourceaddr, 1);
2372done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002373 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002374 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return err;
2376}
2377
2378int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2379{
David L Stevens9951f032005-07-08 17:47:28 -07002380 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002382 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 struct ip_mc_socklist *pmc;
2384 struct in_device *in_dev;
2385 struct inet_sock *inet = inet_sk(sk);
2386 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002387 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002388 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Joe Perchesf97c1e02007-12-16 13:45:43 -08002390 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return -EINVAL;
2392 if (msf->imsf_fmode != MCAST_INCLUDE &&
2393 msf->imsf_fmode != MCAST_EXCLUDE)
2394 return -EINVAL;
2395
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002396 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2399 imr.imr_address.s_addr = msf->imsf_interface;
2400 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002401 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 if (!in_dev) {
2404 err = -ENODEV;
2405 goto done;
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
David L Stevens9951f032005-07-08 17:47:28 -07002408 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2409 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2410 leavegroup = 1;
2411 goto done;
2412 }
2413
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002414 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2416 pmc->multi.imr_ifindex == imr.imr_ifindex)
2417 break;
2418 }
David L Stevens917f2f12005-07-08 17:45:16 -07002419 if (!pmc) { /* must have a prior join */
2420 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002424 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2425 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (!newpsl) {
2427 err = -ENOBUFS;
2428 goto done;
2429 }
2430 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2431 memcpy(newpsl->sl_addr, msf->imsf_slist,
2432 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2433 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2434 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2435 if (err) {
2436 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2437 goto done;
2438 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002439 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002441 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2442 msf->imsf_fmode, 0, NULL, 0);
2443 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002444 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (psl) {
2446 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2447 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002448 /* decrease mem now to avoid the memleak warning */
2449 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002450 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 } else
2452 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2453 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002454 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002456 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457done:
David L Stevens9951f032005-07-08 17:47:28 -07002458 if (leavegroup)
2459 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return err;
2461}
2462
2463int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2464 struct ip_msfilter __user *optval, int __user *optlen)
2465{
2466 int err, len, count, copycount;
2467 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002468 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 struct ip_mc_socklist *pmc;
2470 struct in_device *in_dev;
2471 struct inet_sock *inet = inet_sk(sk);
2472 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002473 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
WANG Cong87e9f032015-11-03 15:41:16 -08002475 ASSERT_RTNL();
2476
Joe Perchesf97c1e02007-12-16 13:45:43 -08002477 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return -EINVAL;
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2481 imr.imr_address.s_addr = msf->imsf_interface;
2482 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002483 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 if (!in_dev) {
2486 err = -ENODEV;
2487 goto done;
2488 }
2489 err = -EADDRNOTAVAIL;
2490
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002491 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2493 pmc->multi.imr_ifindex == imr.imr_ifindex)
2494 break;
2495 }
2496 if (!pmc) /* must have a prior join */
2497 goto done;
2498 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002499 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (!psl) {
2501 len = 0;
2502 count = 0;
2503 } else {
2504 count = psl->sl_count;
2505 }
2506 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2507 len = copycount * sizeof(psl->sl_addr[0]);
2508 msf->imsf_numsrc = count;
2509 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2510 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2511 return -EFAULT;
2512 }
2513 if (len &&
2514 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2515 return -EFAULT;
2516 return 0;
2517done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return err;
2519}
2520
2521int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2522 struct group_filter __user *optval, int __user *optlen)
2523{
2524 int err, i, count, copycount;
2525 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002526 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 struct ip_mc_socklist *pmc;
2528 struct inet_sock *inet = inet_sk(sk);
2529 struct ip_sf_socklist *psl;
2530
WANG Cong87e9f032015-11-03 15:41:16 -08002531 ASSERT_RTNL();
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 psin = (struct sockaddr_in *)&gsf->gf_group;
2534 if (psin->sin_family != AF_INET)
2535 return -EINVAL;
2536 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002537 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return -EINVAL;
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 err = -EADDRNOTAVAIL;
2541
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002542 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2544 pmc->multi.imr_ifindex == gsf->gf_interface)
2545 break;
2546 }
2547 if (!pmc) /* must have a prior join */
2548 goto done;
2549 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002550 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 count = psl ? psl->sl_count : 0;
2552 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2553 gsf->gf_numsrc = count;
2554 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2555 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2556 return -EFAULT;
2557 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002558 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 struct sockaddr_storage ss;
2560
2561 psin = (struct sockaddr_in *)&ss;
2562 memset(&ss, 0, sizeof(ss));
2563 psin->sin_family = AF_INET;
2564 psin->sin_addr.s_addr = psl->sl_addr[i];
2565 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2566 return -EFAULT;
2567 }
2568 return 0;
2569done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return err;
2571}
2572
2573/*
2574 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2575 */
Al Viroc0cda062006-09-27 18:31:10 -07002576int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 struct inet_sock *inet = inet_sk(sk);
2579 struct ip_mc_socklist *pmc;
2580 struct ip_sf_socklist *psl;
2581 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002582 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002584 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002585 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002586 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002588 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002589 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2591 pmc->multi.imr_ifindex == dif)
2592 break;
2593 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002594 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002596 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002597 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002598 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002600 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Weilong Chenc71151f2013-12-23 14:37:29 +08002602 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (psl->sl_addr[i] == rmt_addr)
2604 break;
2605 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002606 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002608 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002610 goto unlock;
2611 ret = 1;
2612unlock:
2613 rcu_read_unlock();
2614out:
2615 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
2618/*
2619 * A socket is closing.
2620 */
2621
2622void ip_mc_drop_socket(struct sock *sk)
2623{
2624 struct inet_sock *inet = inet_sk(sk);
2625 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002626 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Ian Morris51456b22015-04-03 09:17:26 +01002628 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return;
2630
2631 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002632 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002635 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002636 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002637 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002638 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002640 /* decrease mem now to avoid the memleak warning */
2641 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002642 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
2644 rtnl_unlock();
2645}
2646
David S. Millerdbdd9a52011-03-10 16:34:38 -08002647/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002648int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
2650 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002651 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct ip_sf_list *psf;
2653 int rv = 0;
2654
Eric Dumazete9897072013-06-07 08:48:57 -07002655 mc_hash = rcu_dereference(in_dev->mc_hash);
2656 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002657 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002658
2659 for (im = rcu_dereference(mc_hash[hash]);
2660 im != NULL;
2661 im = rcu_dereference(im->next_hash)) {
2662 if (im->multiaddr == mc_addr)
2663 break;
2664 }
2665 } else {
2666 for_each_pmc_rcu(in_dev, im) {
2667 if (im->multiaddr == mc_addr)
2668 break;
2669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
2671 if (im && proto == IPPROTO_IGMP) {
2672 rv = 1;
2673 } else if (im) {
2674 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002675 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (psf->sf_inaddr == src_addr)
2677 break;
2678 }
2679 if (psf)
2680 rv = psf->sf_count[MCAST_INCLUDE] ||
2681 psf->sf_count[MCAST_EXCLUDE] !=
2682 im->sfcount[MCAST_EXCLUDE];
2683 else
2684 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2685 } else
2686 rv = 1; /* unspecified source; tentatively allow */
2687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return rv;
2689}
2690
2691#if defined(CONFIG_PROC_FS)
2692struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002693 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct net_device *dev;
2695 struct in_device *in_dev;
2696};
2697
2698#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2699
2700static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2701{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002702 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 struct ip_mc_list *im = NULL;
2704 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2705
Pavel Emelianov7562f872007-05-03 15:13:45 -07002706 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002707 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002709
2710 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (!in_dev)
2712 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002713 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (im) {
2715 state->in_dev = in_dev;
2716 break;
2717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719 return im;
2720}
2721
2722static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2723{
2724 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002725
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002726 im = rcu_dereference(im->next_rcu);
2727 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002728 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 if (!state->dev) {
2730 state->in_dev = NULL;
2731 break;
2732 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002733 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (!state->in_dev)
2735 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002736 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
2738 return im;
2739}
2740
2741static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2742{
2743 struct ip_mc_list *im = igmp_mc_get_first(seq);
2744 if (im)
2745 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2746 --pos;
2747 return pos ? NULL : im;
2748}
2749
2750static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002751 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
stephen hemminger61fbab72009-11-10 07:54:55 +00002753 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2755}
2756
2757static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2758{
2759 struct ip_mc_list *im;
2760 if (v == SEQ_START_TOKEN)
2761 im = igmp_mc_get_first(seq);
2762 else
2763 im = igmp_mc_get_next(seq, v);
2764 ++*pos;
2765 return im;
2766}
2767
2768static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002769 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
2771 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002772
2773 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002775 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
2778static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2779{
2780 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002781 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2783 else {
2784 struct ip_mc_list *im = (struct ip_mc_list *)v;
2785 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2786 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002787 long delta;
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789#ifdef CONFIG_IP_MULTICAST
2790 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2791 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2792 "V3";
2793#else
2794 querier = "NONE";
2795#endif
2796
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002797 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002799 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
2801
Eric Dumazeta399a802012-08-08 21:13:53 +00002802 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002804 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002806 im->tm_running,
2807 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 im->reporter);
2809 }
2810 return 0;
2811}
2812
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002813static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 .start = igmp_mc_seq_start,
2815 .next = igmp_mc_seq_next,
2816 .stop = igmp_mc_seq_stop,
2817 .show = igmp_mc_seq_show,
2818};
2819
2820static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2821{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002822 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002823 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824}
2825
Arjan van de Ven9a321442007-02-12 00:55:35 -08002826static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 .owner = THIS_MODULE,
2828 .open = igmp_mc_seq_open,
2829 .read = seq_read,
2830 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002831 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832};
2833
2834struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002835 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 struct net_device *dev;
2837 struct in_device *idev;
2838 struct ip_mc_list *im;
2839};
2840
2841#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2842
2843static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2844{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002845 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct ip_sf_list *psf = NULL;
2847 struct ip_mc_list *im = NULL;
2848 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2849
Pavel Emelianov7562f872007-05-03 15:13:45 -07002850 state->idev = NULL;
2851 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002852 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002854 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002855 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002857 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002858 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 spin_lock_bh(&im->lock);
2860 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002861 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 state->im = im;
2863 state->idev = idev;
2864 break;
2865 }
2866 spin_unlock_bh(&im->lock);
2867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
2869 return psf;
2870}
2871
2872static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2873{
2874 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2875
2876 psf = psf->sf_next;
2877 while (!psf) {
2878 spin_unlock_bh(&state->im->lock);
2879 state->im = state->im->next;
2880 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002881 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (!state->dev) {
2883 state->idev = NULL;
2884 goto out;
2885 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002886 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (!state->idev)
2888 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002889 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891 if (!state->im)
2892 break;
2893 spin_lock_bh(&state->im->lock);
2894 psf = state->im->sources;
2895 }
2896out:
2897 return psf;
2898}
2899
2900static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2901{
2902 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2903 if (psf)
2904 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2905 --pos;
2906 return pos ? NULL : psf;
2907}
2908
2909static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002910 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
stephen hemminger61fbab72009-11-10 07:54:55 +00002912 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2914}
2915
2916static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2917{
2918 struct ip_sf_list *psf;
2919 if (v == SEQ_START_TOKEN)
2920 psf = igmp_mcf_get_first(seq);
2921 else
2922 psf = igmp_mcf_get_next(seq, v);
2923 ++*pos;
2924 return psf;
2925}
2926
2927static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002928 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002931 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 spin_unlock_bh(&state->im->lock);
2933 state->im = NULL;
2934 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002935 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002937 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
2940static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2941{
2942 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2943 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2944
2945 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002946 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 } else {
2948 seq_printf(seq,
2949 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002950 "0x%08x %6lu %6lu\n",
2951 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 ntohl(state->im->multiaddr),
2953 ntohl(psf->sf_inaddr),
2954 psf->sf_count[MCAST_INCLUDE],
2955 psf->sf_count[MCAST_EXCLUDE]);
2956 }
2957 return 0;
2958}
2959
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002960static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 .start = igmp_mcf_seq_start,
2962 .next = igmp_mcf_seq_next,
2963 .stop = igmp_mcf_seq_stop,
2964 .show = igmp_mcf_seq_show,
2965};
2966
2967static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2968{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002969 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002970 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
Arjan van de Ven9a321442007-02-12 00:55:35 -08002973static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 .owner = THIS_MODULE,
2975 .open = igmp_mcf_seq_open,
2976 .read = seq_read,
2977 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002978 .release = seq_release_net,
2979};
2980
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002981static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002982{
2983 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002984 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002985
Gao fengd4beaa62013-02-18 01:34:54 +00002986 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002987 if (!pde)
2988 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002989 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2990 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002991 if (!pde)
2992 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08002993 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2994 SOCK_DGRAM, 0, net);
2995 if (err < 0) {
2996 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2997 err);
2998 goto out_sock;
2999 }
3000
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003001 return 0;
3002
Madhu Challa93a714d2015-02-25 09:58:35 -08003003out_sock:
3004 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003005out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00003006 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003007out_igmp:
3008 return -ENOMEM;
3009}
3010
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003011static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003012{
Gao fengece31ff2013-02-18 01:34:56 +00003013 remove_proc_entry("mcfilter", net->proc_net);
3014 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08003015 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003016}
3017
3018static struct pernet_operations igmp_net_ops = {
3019 .init = igmp_net_init,
3020 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021};
WANG Cong72c1d3b2014-01-10 16:09:45 -08003022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003024static int igmp_netdev_event(struct notifier_block *this,
3025 unsigned long event, void *ptr)
3026{
3027 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3028 struct in_device *in_dev;
3029
3030 switch (event) {
3031 case NETDEV_RESEND_IGMP:
3032 in_dev = __in_dev_get_rtnl(dev);
3033 if (in_dev)
3034 ip_mc_rejoin_groups(in_dev);
3035 break;
3036 default:
3037 break;
3038 }
3039 return NOTIFY_DONE;
3040}
3041
3042static struct notifier_block igmp_notifier = {
3043 .notifier_call = igmp_netdev_event,
3044};
3045
WANG Cong72c1d3b2014-01-10 16:09:45 -08003046int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003048#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003049 int err;
3050
3051 err = register_pernet_subsys(&igmp_net_ops);
3052 if (err)
3053 return err;
3054 err = register_netdevice_notifier(&igmp_notifier);
3055 if (err)
3056 goto reg_notif_fail;
3057 return 0;
3058
3059reg_notif_fail:
3060 unregister_pernet_subsys(&igmp_net_ops);
3061 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003062#else
3063 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003065}