blob: caf2f1101d027b7b6e8d9683887e16c7bd4a8438 [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 Torvalds7c0f6ba2016-12-24 11:46:01 -080075#include <linux/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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020092
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020093#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020094#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <net/ip.h>
96#include <net/protocol.h>
97#include <net/route.h>
98#include <net/sock.h>
99#include <net/checksum.h>
Madhu Challa93a714d2015-02-25 09:58:35 -0800100#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <linux/netfilter_ipv4.h>
102#ifdef CONFIG_IP_MROUTE
103#include <linux/mroute.h>
104#endif
105#ifdef CONFIG_PROC_FS
106#include <linux/proc_fs.h>
107#include <linux/seq_file.h>
108#endif
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#ifdef CONFIG_IP_MULTICAST
111/* Parameter names and values are taken from igmp-v2-06 draft */
112
Fabian Frederick436f7c22014-11-04 20:52:14 +0100113#define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
114#define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
115#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
116#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
117#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
118#define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120
Fabian Frederick436f7c22014-11-04 20:52:14 +0100121#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Fabian Frederick436f7c22014-11-04 20:52:14 +0100123/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * IGMP specs require to report membership immediately after
125 * joining a group, but we delay the first report by a
126 * small interval. It seems more natural and still does not
127 * contradict to specs provided this delay is small enough.
128 */
129
Herbert Xu42f811b2007-06-04 23:34:44 -0700130#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900131 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700132 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
133 ((in_dev)->mr_v1_seen && \
134 time_before(jiffies, (in_dev)->mr_v1_seen)))
135#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900136 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700137 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
William Manleycab70042013-08-06 19:03:13 +0100141static int unsolicited_report_interval(struct in_device *in_dev)
142{
William Manley26900482013-08-06 19:03:15 +0100143 int interval_ms, interval_jiffies;
144
William Manleycab70042013-08-06 19:03:13 +0100145 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100146 interval_ms = IN_DEV_CONF_GET(
147 in_dev,
148 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100149 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100150 interval_ms = IN_DEV_CONF_GET(
151 in_dev,
152 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
153
154 interval_jiffies = msecs_to_jiffies(interval_ms);
155
156 /* _timer functions can't handle a delay of 0 jiffies so ensure
157 * we always return a positive value.
158 */
159 if (interval_jiffies <= 0)
160 interval_jiffies = 1;
161 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Hangbin Liu24803f32016-11-14 16:16:28 +0800165static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void igmpv3_clear_delrec(struct in_device *in_dev);
167static int sf_setstate(struct ip_mc_list *pmc);
168static void sf_markstate(struct ip_mc_list *pmc);
169#endif
170static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700171static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
172 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174static void ip_ma_put(struct ip_mc_list *im)
175{
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300176 if (refcount_dec_and_test(&im->refcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800178 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180}
181
David S. Millerd9aa9382010-11-15 08:52:02 -0800182#define for_each_pmc_rcu(in_dev, pmc) \
183 for (pmc = rcu_dereference(in_dev->mc_list); \
184 pmc != NULL; \
185 pmc = rcu_dereference(pmc->next_rcu))
186
187#define for_each_pmc_rtnl(in_dev, pmc) \
188 for (pmc = rtnl_dereference(in_dev->mc_list); \
189 pmc != NULL; \
190 pmc = rtnl_dereference(pmc->next_rcu))
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#ifdef CONFIG_IP_MULTICAST
193
194/*
195 * Timer management
196 */
197
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000198static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 spin_lock_bh(&im->lock);
201 if (del_timer(&im->timer))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300202 refcount_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800203 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 im->reporter = 0;
205 im->unsolicit_count = 0;
206 spin_unlock_bh(&im->lock);
207}
208
209/* It must be called with locked im->lock */
210static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
211{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500212 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800214 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!mod_timer(&im->timer, jiffies+tv+2))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300216 refcount_inc(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static void igmp_gq_start_timer(struct in_device *in_dev)
220{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500221 int tv = prandom_u32() % in_dev->mr_maxdelay;
Michal Tesar7ababb72017-01-02 14:38:36 +0100222 unsigned long exp = jiffies + tv + 2;
223
224 if (in_dev->mr_gq_running &&
225 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
226 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 in_dev->mr_gq_running = 1;
Michal Tesar7ababb72017-01-02 14:38:36 +0100229 if (!mod_timer(&in_dev->mr_gq_timer, exp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 in_dev_hold(in_dev);
231}
232
233static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
234{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500235 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
238 in_dev_hold(in_dev);
239}
240
241static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
242{
243 spin_lock_bh(&im->lock);
244 im->unsolicit_count = 0;
245 if (del_timer(&im->timer)) {
246 if ((long)(im->timer.expires-jiffies) < max_delay) {
247 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800248 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 spin_unlock_bh(&im->lock);
250 return;
251 }
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300252 refcount_dec(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 igmp_start_timer(im, max_delay);
255 spin_unlock_bh(&im->lock);
256}
257
258
259/*
260 * Send an IGMP report.
261 */
262
263#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
264
265
266static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
267 int gdeleted, int sdeleted)
268{
269 switch (type) {
270 case IGMPV3_MODE_IS_INCLUDE:
271 case IGMPV3_MODE_IS_EXCLUDE:
272 if (gdeleted || sdeleted)
273 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800274 if (!(pmc->gsquery && !psf->sf_gsresp)) {
275 if (pmc->sfmode == MCAST_INCLUDE)
276 return 1;
277 /* don't include if this source is excluded
278 * in all filters
279 */
280 if (psf->sf_count[MCAST_INCLUDE])
281 return type == IGMPV3_MODE_IS_INCLUDE;
282 return pmc->sfcount[MCAST_EXCLUDE] ==
283 psf->sf_count[MCAST_EXCLUDE];
284 }
285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 case IGMPV3_CHANGE_TO_INCLUDE:
287 if (gdeleted || sdeleted)
288 return 0;
289 return psf->sf_count[MCAST_INCLUDE] != 0;
290 case IGMPV3_CHANGE_TO_EXCLUDE:
291 if (gdeleted || sdeleted)
292 return 0;
293 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
294 psf->sf_count[MCAST_INCLUDE])
295 return 0;
296 return pmc->sfcount[MCAST_EXCLUDE] ==
297 psf->sf_count[MCAST_EXCLUDE];
298 case IGMPV3_ALLOW_NEW_SOURCES:
299 if (gdeleted || !psf->sf_crcount)
300 return 0;
301 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
302 case IGMPV3_BLOCK_OLD_SOURCES:
303 if (pmc->sfmode == MCAST_INCLUDE)
304 return gdeleted || (psf->sf_crcount && sdeleted);
305 return psf->sf_crcount && !gdeleted && !sdeleted;
306 }
307 return 0;
308}
309
310static int
311igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
312{
313 struct ip_sf_list *psf;
314 int scount = 0;
315
Weilong Chenc71151f2013-12-23 14:37:29 +0800316 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
318 continue;
319 scount++;
320 }
321 return scount;
322}
323
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100324static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct sk_buff *skb;
327 struct rtable *rt;
328 struct iphdr *pip;
329 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700330 struct net *net = dev_net(dev);
David S. Miller31e4543d2011-05-03 20:25:42 -0700331 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000332 int hlen = LL_RESERVED_SPACE(dev);
333 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100334 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000336 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000337 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000338 GFP_ATOMIC | __GFP_NOWARN);
339 if (skb)
340 break;
341 size >>= 1;
342 if (size < 256)
343 return NULL;
344 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200345 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
David S. Miller31e4543d2011-05-03 20:25:42 -0700347 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500348 0, 0,
349 IPPROTO_IGMP, 0, dev->ifindex);
350 if (IS_ERR(rt)) {
351 kfree_skb(skb);
352 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Changli Gaod8d1f302010-06-10 23:31:35 -0700355 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 skb->dev = dev;
357
Herbert Xu66088242011-11-18 02:20:04 +0000358 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800359 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300361 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700362 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300363 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 pip->version = 4;
366 pip->ihl = (sizeof(struct iphdr)+4)>>2;
367 pip->tos = 0xc0;
368 pip->frag_off = htons(IP_DF);
369 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700370 pip->daddr = fl4.daddr;
371 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 pip->protocol = IPPROTO_IGMP;
373 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100374 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000375 ((u8 *)&pip[1])[0] = IPOPT_RA;
376 ((u8 *)&pip[1])[1] = 4;
377 ((u8 *)&pip[1])[2] = 0;
378 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700380 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300381 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300382 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
384 pig->resv1 = 0;
385 pig->csum = 0;
386 pig->resv2 = 0;
387 pig->ngrec = 0;
388 return skb;
389}
390
391static int igmpv3_sendpack(struct sk_buff *skb)
392{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300393 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000394 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300396 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Eric W. Biederman33224b12015-10-07 16:48:46 -0500398 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
402{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800403 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
407 int type, struct igmpv3_grec **ppgr)
408{
409 struct net_device *dev = pmc->interface->dev;
410 struct igmpv3_report *pih;
411 struct igmpv3_grec *pgr;
412
413 if (!skb)
414 skb = igmpv3_newpack(dev, dev->mtu);
415 if (!skb)
416 return NULL;
Johannes Berg4df864c2017-06-16 14:29:21 +0200417 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 pgr->grec_type = type;
419 pgr->grec_auxwords = 0;
420 pgr->grec_nsrcs = 0;
421 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300422 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 pih->ngrec = htons(ntohs(pih->ngrec)+1);
424 *ppgr = pgr;
425 return skb;
426}
427
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100428#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
431 int type, int gdeleted, int sdeleted)
432{
433 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200434 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct igmpv3_report *pih;
436 struct igmpv3_grec *pgr = NULL;
437 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800438 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (pmc->multiaddr == IGMP_ALL_HOSTS)
441 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200442 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100443 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
446 type == IGMPV3_MODE_IS_EXCLUDE;
447 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
448 type == IGMPV3_CHANGE_TO_EXCLUDE;
449
David L Stevensad125832006-01-18 14:20:56 -0800450 stotal = scount = 0;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
453
David L Stevensad125832006-01-18 14:20:56 -0800454 if (!*psf_list)
455 goto empty_source;
456
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300457 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* EX and TO_EX get a fresh packet, if needed */
460 if (truncate) {
461 if (pih && pih->ngrec &&
462 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
463 if (skb)
464 igmpv3_sendpack(skb);
465 skb = igmpv3_newpack(dev, dev->mtu);
466 }
467 }
468 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800470 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700471 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 psf_next = psf->sf_next;
474
475 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
476 psf_prev = psf;
477 continue;
478 }
479
Hangbin Liua0525172016-08-02 18:02:57 +0800480 /* Based on RFC3376 5.1. Should not send source-list change
481 * records when there is a filter mode change.
482 */
483 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
484 (!gdeleted && pmc->crcount)) &&
485 (type == IGMPV3_ALLOW_NEW_SOURCES ||
486 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
487 goto decrease_sf_crcount;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* clear marks on query responses */
490 if (isquery)
491 psf->sf_gsresp = 0;
492
Al Viro63007722006-09-27 18:31:32 -0700493 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 first*sizeof(struct igmpv3_grec)) {
495 if (truncate && !first)
496 break; /* truncate these */
497 if (pgr)
498 pgr->grec_nsrcs = htons(scount);
499 if (skb)
500 igmpv3_sendpack(skb);
501 skb = igmpv3_newpack(dev, dev->mtu);
502 first = 1;
503 scount = 0;
504 }
505 if (first) {
506 skb = add_grhead(skb, pmc, type, &pgr);
507 first = 0;
508 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800509 if (!skb)
510 return NULL;
Johannes Berg4df864c2017-06-16 14:29:21 +0200511 psrc = skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800513 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
515 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
Hangbin Liua0525172016-08-02 18:02:57 +0800516decrease_sf_crcount:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 psf->sf_crcount--;
518 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
519 if (psf_prev)
520 psf_prev->sf_next = psf->sf_next;
521 else
522 *psf_list = psf->sf_next;
523 kfree(psf);
524 continue;
525 }
526 }
527 psf_prev = psf;
528 }
David L Stevensad125832006-01-18 14:20:56 -0800529
530empty_source:
531 if (!stotal) {
532 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
533 type == IGMPV3_BLOCK_OLD_SOURCES)
534 return skb;
535 if (pmc->crcount || isquery) {
536 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800537 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800538 igmpv3_sendpack(skb);
539 skb = NULL; /* add_grhead will get a new one */
540 }
541 skb = add_grhead(skb, pmc, type, &pgr);
542 }
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (pgr)
545 pgr->grec_nsrcs = htons(scount);
546
547 if (isquery)
548 pmc->gsquery = 0; /* clear query state on report */
549 return skb;
550}
551
552static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
553{
554 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200555 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int type;
557
558 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000559 rcu_read_lock();
560 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (pmc->multiaddr == IGMP_ALL_HOSTS)
562 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100563 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200564 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100565 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_lock_bh(&pmc->lock);
567 if (pmc->sfcount[MCAST_EXCLUDE])
568 type = IGMPV3_MODE_IS_EXCLUDE;
569 else
570 type = IGMPV3_MODE_IS_INCLUDE;
571 skb = add_grec(skb, pmc, type, 0, 0);
572 spin_unlock_bh(&pmc->lock);
573 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000574 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 } else {
576 spin_lock_bh(&pmc->lock);
577 if (pmc->sfcount[MCAST_EXCLUDE])
578 type = IGMPV3_MODE_IS_EXCLUDE;
579 else
580 type = IGMPV3_MODE_IS_INCLUDE;
581 skb = add_grec(skb, pmc, type, 0, 0);
582 spin_unlock_bh(&pmc->lock);
583 }
584 if (!skb)
585 return 0;
586 return igmpv3_sendpack(skb);
587}
588
589/*
590 * remove zero-count source records from a source filter list
591 */
592static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
593{
594 struct ip_sf_list *psf_prev, *psf_next, *psf;
595
596 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800597 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 psf_next = psf->sf_next;
599 if (psf->sf_crcount == 0) {
600 if (psf_prev)
601 psf_prev->sf_next = psf->sf_next;
602 else
603 *ppsf = psf->sf_next;
604 kfree(psf);
605 } else
606 psf_prev = psf;
607 }
608}
609
610static void igmpv3_send_cr(struct in_device *in_dev)
611{
612 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
613 struct sk_buff *skb = NULL;
614 int type, dtype;
615
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000616 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 spin_lock_bh(&in_dev->mc_tomb_lock);
618
619 /* deleted MCA's */
620 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800621 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 pmc_next = pmc->next;
623 if (pmc->sfmode == MCAST_INCLUDE) {
624 type = IGMPV3_BLOCK_OLD_SOURCES;
625 dtype = IGMPV3_BLOCK_OLD_SOURCES;
626 skb = add_grec(skb, pmc, type, 1, 0);
627 skb = add_grec(skb, pmc, dtype, 1, 1);
628 }
629 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (pmc->sfmode == MCAST_EXCLUDE) {
631 type = IGMPV3_CHANGE_TO_INCLUDE;
632 skb = add_grec(skb, pmc, type, 1, 0);
633 }
David L Stevensad125832006-01-18 14:20:56 -0800634 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (pmc->crcount == 0) {
636 igmpv3_clear_zeros(&pmc->tomb);
637 igmpv3_clear_zeros(&pmc->sources);
638 }
639 }
640 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
641 if (pmc_prev)
642 pmc_prev->next = pmc_next;
643 else
644 in_dev->mc_tomb = pmc_next;
645 in_dev_put(pmc->interface);
646 kfree(pmc);
647 } else
648 pmc_prev = pmc;
649 }
650 spin_unlock_bh(&in_dev->mc_tomb_lock);
651
652 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000653 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 spin_lock_bh(&pmc->lock);
655 if (pmc->sfcount[MCAST_EXCLUDE]) {
656 type = IGMPV3_BLOCK_OLD_SOURCES;
657 dtype = IGMPV3_ALLOW_NEW_SOURCES;
658 } else {
659 type = IGMPV3_ALLOW_NEW_SOURCES;
660 dtype = IGMPV3_BLOCK_OLD_SOURCES;
661 }
662 skb = add_grec(skb, pmc, type, 0, 0);
663 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
664
665 /* filter mode changes */
666 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (pmc->sfmode == MCAST_EXCLUDE)
668 type = IGMPV3_CHANGE_TO_EXCLUDE;
669 else
670 type = IGMPV3_CHANGE_TO_INCLUDE;
671 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800672 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674 spin_unlock_bh(&pmc->lock);
675 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000676 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (!skb)
679 return;
680 (void) igmpv3_sendpack(skb);
681}
682
683static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
684 int type)
685{
686 struct sk_buff *skb;
687 struct iphdr *iph;
688 struct igmphdr *ih;
689 struct rtable *rt;
690 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700691 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700692 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e4543d2011-05-03 20:25:42 -0700693 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700694 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000695 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
698 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100699
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200700 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100701 return 0;
702
703 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 dst = IGMP_ALL_ROUTER;
705 else
706 dst = group;
707
David S. Miller31e4543d2011-05-03 20:25:42 -0700708 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500709 0, 0,
710 IPPROTO_IGMP, 0, dev->ifindex);
711 if (IS_ERR(rt))
712 return -1;
713
Herbert Xu66088242011-11-18 02:20:04 +0000714 hlen = LL_RESERVED_SPACE(dev);
715 tlen = dev->needed_tailroom;
716 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100717 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 ip_rt_put(rt);
719 return -1;
720 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200721 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Changli Gaod8d1f302010-06-10 23:31:35 -0700723 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Herbert Xu66088242011-11-18 02:20:04 +0000725 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300727 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700728 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300729 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 iph->version = 4;
732 iph->ihl = (sizeof(struct iphdr)+4)>>2;
733 iph->tos = 0xc0;
734 iph->frag_off = htons(IP_DF);
735 iph->ttl = 1;
736 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700737 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100739 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000740 ((u8 *)&iph[1])[0] = IPOPT_RA;
741 ((u8 *)&iph[1])[1] = 4;
742 ((u8 *)&iph[1])[2] = 0;
743 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Johannes Berg4df864c2017-06-16 14:29:21 +0200745 ih = skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800746 ih->type = type;
747 ih->code = 0;
748 ih->csum = 0;
749 ih->group = group;
750 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Eric W. Biederman33224b12015-10-07 16:48:46 -0500752 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static void igmp_gq_timer_expire(unsigned long data)
756{
757 struct in_device *in_dev = (struct in_device *)data;
758
759 in_dev->mr_gq_running = 0;
760 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700761 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764static void igmp_ifc_timer_expire(unsigned long data)
765{
766 struct in_device *in_dev = (struct in_device *)data;
767
768 igmpv3_send_cr(in_dev);
769 if (in_dev->mr_ifc_count) {
770 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100771 igmp_ifc_start_timer(in_dev,
772 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700774 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777static void igmp_ifc_event(struct in_device *in_dev)
778{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200779 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
781 return;
Nikolay Borisov165094a2016-02-08 23:29:24 +0200782 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 igmp_ifc_start_timer(in_dev, 1);
784}
785
786
787static void igmp_timer_expire(unsigned long data)
788{
Weilong Chenc71151f2013-12-23 14:37:29 +0800789 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct in_device *in_dev = im->interface;
791
792 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800793 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (im->unsolicit_count) {
796 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100797 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 im->reporter = 1;
800 spin_unlock(&im->lock);
801
802 if (IGMP_V1_SEEN(in_dev))
803 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
804 else if (IGMP_V2_SEEN(in_dev))
805 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
806 else
807 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
808
809 ip_ma_put(im);
810}
811
David L Stevensad125832006-01-18 14:20:56 -0800812/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700813static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct ip_sf_list *psf;
816 int i, scount;
817
818 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800819 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (scount == nsrcs)
821 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800822 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800823 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000824 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800825 pmc->sfcount[MCAST_EXCLUDE] !=
826 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800827 break;
David L Stevensad125832006-01-18 14:20:56 -0800828 if (srcs[i] == psf->sf_inaddr) {
829 scount++;
830 break;
831 }
832 }
833 }
834 pmc->gsquery = 0;
835 if (scount == nsrcs) /* all sources excluded */
836 return 0;
837 return 1;
838}
839
Al Viroea4d9e72006-09-27 18:30:52 -0700840static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800841{
842 struct ip_sf_list *psf;
843 int i, scount;
844
845 if (pmc->sfmode == MCAST_EXCLUDE)
846 return igmp_xmarksources(pmc, nsrcs, srcs);
847
848 /* mark INCLUDE-mode sources */
849 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800850 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800851 if (scount == nsrcs)
852 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800853 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (srcs[i] == psf->sf_inaddr) {
855 psf->sf_gsresp = 1;
856 scount++;
857 break;
858 }
859 }
David L Stevensad125832006-01-18 14:20:56 -0800860 if (!scount) {
861 pmc->gsquery = 0;
862 return 0;
863 }
864 pmc->gsquery = 1;
865 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Eric Dumazetd679c532012-09-06 20:37:06 +0000868/* return true if packet was dropped */
869static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200872 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* Timers are only set for non-local groups */
875
876 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000877 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200878 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100879 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000881 rcu_read_lock();
882 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (im->multiaddr == group) {
884 igmp_stop_timer(im);
885 break;
886 }
887 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000888 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000889 return false;
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_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int len)
895{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300896 struct igmphdr *ih = igmp_hdr(skb);
897 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700899 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 int max_delay;
901 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200902 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904
David Stevens5b7c8402010-09-30 14:29:40 +0000905 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (ih->code == 0) {
907 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900908
Fabian Frederick436f7c22014-11-04 20:52:14 +0100909 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 in_dev->mr_v1_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100911 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 group = 0;
913 } else {
914 /* v2 router present */
915 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
916 in_dev->mr_v2_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100917 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 /* cancel the interface change timer */
920 in_dev->mr_ifc_count = 0;
921 if (del_timer(&in_dev->mr_ifc_timer))
922 __in_dev_put(in_dev);
923 /* clear deleted report items */
924 igmpv3_clear_delrec(in_dev);
925 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000926 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000927 } else if (IGMP_V1_SEEN(in_dev)) {
928 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100929 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000930 group = 0;
931 } else if (IGMP_V2_SEEN(in_dev)) {
932 /* this is a v3 query with v2 queriers present;
933 * Interpretation of the max_delay code is problematic here.
934 * A real v2 host would use ih_code directly, while v3 has a
935 * different encoding. We use the v3 encoding as more likely
936 * to be intended in a v3 query.
937 */
938 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800939 if (!max_delay)
940 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 } else { /* v3 */
942 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000943 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900944
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300945 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900947 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700948 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000949 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300950 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
953 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
954 if (!max_delay)
955 max_delay = 1; /* can't mod w/ 0 */
956 in_dev->mr_maxdelay = max_delay;
957 if (ih3->qrv)
958 in_dev->mr_qrv = ih3->qrv;
959 if (!group) { /* general query */
960 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +0200961 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000963 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 /* mark sources to include, if group & source-specific */
966 mark = ih3->nsrcs != 0;
967 }
968
969 /*
970 * - Start the timers in all of our membership records
971 * that the query applies to for the interface on
972 * which the query arrived excl. those that belong
973 * to a "local" group (224.0.0.X)
974 * - For timers already running check if they need to
975 * be reset.
976 * - Use the igmp->igmp_code field as the maximum
977 * delay possible
978 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000979 rcu_read_lock();
980 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800981 int changed;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (group && group != im->multiaddr)
984 continue;
985 if (im->multiaddr == IGMP_ALL_HOSTS)
986 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100987 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200988 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100989 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 spin_lock_bh(&im->lock);
991 if (im->tm_running)
992 im->gsquery = im->gsquery && mark;
993 else
994 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800995 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900996 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800998 if (changed)
999 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001001 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +00001002 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001005/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006int igmp_rcv(struct sk_buff *skb)
1007{
1008 /* This basically follows the spec line by line -- see RFC1112 */
1009 struct igmphdr *ih;
David Ahernc7b725b2017-08-15 18:38:42 -07001010 struct net_device *dev = skb->dev;
1011 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001013 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
David Ahernc7b725b2017-08-15 18:38:42 -07001015 if (netif_is_l3_master(dev)) {
1016 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1017 if (!dev)
1018 goto drop;
1019 }
1020
1021 in_dev = __in_dev_get_rcu(dev);
Ian Morris51456b22015-04-03 09:17:26 +01001022 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001023 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Herbert Xufb286bb2005-11-10 13:01:24 -08001025 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001026 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001027
Tom Herbertde08dc12014-05-07 16:52:10 -07001028 if (skb_checksum_simple_validate(skb))
1029 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001031 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 switch (ih->type) {
1033 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001034 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 break;
1036 case IGMP_HOST_MEMBERSHIP_REPORT:
1037 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001039 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
David Stevens24c69272005-12-02 20:32:59 -08001041 /* don't rely on MC router hearing unicast reports */
1042 if (skb->pkt_type == PACKET_MULTICAST ||
1043 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001044 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 break;
1046 case IGMP_PIM:
1047#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return pim_rcv_v1(skb);
1049#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001050 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 case IGMP_DVMRP:
1052 case IGMP_TRACE:
1053 case IGMP_HOST_LEAVE_MESSAGE:
1054 case IGMP_MTRACE:
1055 case IGMP_MTRACE_RESP:
1056 break;
1057 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001058 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001060
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001061drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001062 if (dropped)
1063 kfree_skb(skb);
1064 else
1065 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067}
1068
1069#endif
1070
1071
1072/*
1073 * Add a filter to a device
1074 */
1075
Al Viro63007722006-09-27 18:31:32 -07001076static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 char buf[MAX_ADDR_LEN];
1079 struct net_device *dev = in_dev->dev;
1080
1081 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1082 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001083 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 routine. Something sort of:
1085 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1086 --ANK
1087 */
1088 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001089 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092/*
1093 * Remove a filter from a device
1094 */
1095
Al Viro63007722006-09-27 18:31:32 -07001096static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 char buf[MAX_ADDR_LEN];
1099 struct net_device *dev = in_dev->dev;
1100
1101 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001102 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105#ifdef CONFIG_IP_MULTICAST
1106/*
1107 * deleted ip_mc_list manipulation
1108 */
1109static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1110{
1111 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001112 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /* this is an "ip_mc_list" for convenience; only the fields below
1115 * are actually used. In particular, the refcnt and users are not
1116 * used for management of the delete list. Using the same structure
1117 * for deleted items allows change reports to use common code with
1118 * non-deleted or query-response MCA's.
1119 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001120 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!pmc)
1122 return;
WANG Congb4846fc2017-06-20 10:46:27 -07001123 spin_lock_init(&pmc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 spin_lock_bh(&im->lock);
1125 pmc->interface = im->interface;
1126 in_dev_hold(in_dev);
1127 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001128 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 pmc->sfmode = im->sfmode;
1130 if (pmc->sfmode == MCAST_INCLUDE) {
1131 struct ip_sf_list *psf;
1132
1133 pmc->tomb = im->tomb;
1134 pmc->sources = im->sources;
1135 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001136 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 psf->sf_crcount = pmc->crcount;
1138 }
1139 spin_unlock_bh(&im->lock);
1140
1141 spin_lock_bh(&in_dev->mc_tomb_lock);
1142 pmc->next = in_dev->mc_tomb;
1143 in_dev->mc_tomb = pmc;
1144 spin_unlock_bh(&in_dev->mc_tomb_lock);
1145}
1146
Hangbin Liu24803f32016-11-14 16:16:28 +08001147/*
1148 * restore ip_mc_list deleted records
1149 */
1150static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001153 struct ip_sf_list *psf;
1154 struct net *net = dev_net(in_dev->dev);
1155 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 spin_lock_bh(&in_dev->mc_tomb_lock);
1158 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001159 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (pmc->multiaddr == multiaddr)
1161 break;
1162 pmc_prev = pmc;
1163 }
1164 if (pmc) {
1165 if (pmc_prev)
1166 pmc_prev->next = pmc->next;
1167 else
1168 in_dev->mc_tomb = pmc->next;
1169 }
1170 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001171
1172 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001174 im->interface = pmc->interface;
1175 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1176 im->sfmode = pmc->sfmode;
1177 if (pmc->sfmode == MCAST_INCLUDE) {
1178 im->tomb = pmc->tomb;
1179 im->sources = pmc->sources;
1180 for (psf = im->sources; psf; psf = psf->sf_next)
1181 psf->sf_crcount = im->crcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 in_dev_put(pmc->interface);
Hangbin Liu9c8bb162017-02-08 21:16:45 +08001184 kfree(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001186 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Hangbin Liu24803f32016-11-14 16:16:28 +08001189/*
1190 * flush ip_mc_list deleted records
1191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static void igmpv3_clear_delrec(struct in_device *in_dev)
1193{
1194 struct ip_mc_list *pmc, *nextpmc;
1195
1196 spin_lock_bh(&in_dev->mc_tomb_lock);
1197 pmc = in_dev->mc_tomb;
1198 in_dev->mc_tomb = NULL;
1199 spin_unlock_bh(&in_dev->mc_tomb_lock);
1200
1201 for (; pmc; pmc = nextpmc) {
1202 nextpmc = pmc->next;
1203 ip_mc_clear_src(pmc);
1204 in_dev_put(pmc->interface);
1205 kfree(pmc);
1206 }
1207 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001208 rcu_read_lock();
1209 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 struct ip_sf_list *psf, *psf_next;
1211
1212 spin_lock_bh(&pmc->lock);
1213 psf = pmc->tomb;
1214 pmc->tomb = NULL;
1215 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001216 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 psf_next = psf->sf_next;
1218 kfree(psf);
1219 }
1220 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001221 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223#endif
1224
1225static void igmp_group_dropped(struct ip_mc_list *im)
1226{
1227 struct in_device *in_dev = im->interface;
1228#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001229 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int reporter;
1231#endif
1232
1233 if (im->loaded) {
1234 im->loaded = 0;
1235 ip_mc_filter_del(in_dev, im->multiaddr);
1236 }
1237
1238#ifdef CONFIG_IP_MULTICAST
1239 if (im->multiaddr == IGMP_ALL_HOSTS)
1240 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001241 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001242 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 reporter = im->reporter;
1245 igmp_stop_timer(im);
1246
1247 if (!in_dev->dead) {
1248 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001249 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (IGMP_V2_SEEN(in_dev)) {
1251 if (reporter)
1252 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001253 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 /* IGMPv3 */
1256 igmpv3_add_delrec(in_dev, im);
1257
1258 igmp_ifc_event(in_dev);
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263static void igmp_group_added(struct ip_mc_list *im)
1264{
1265 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001266#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001267 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (im->loaded == 0) {
1271 im->loaded = 1;
1272 ip_mc_filter_add(in_dev, im->multiaddr);
1273 }
1274
1275#ifdef CONFIG_IP_MULTICAST
1276 if (im->multiaddr == IGMP_ALL_HOSTS)
1277 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001278 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (in_dev->dead)
1282 return;
1283 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1284 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001285 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 spin_unlock_bh(&im->lock);
1287 return;
1288 }
1289 /* else, v3 */
1290
Nikolay Borisov165094a2016-02-08 23:29:24 +02001291 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 igmp_ifc_event(in_dev);
1293#endif
1294}
1295
1296
1297/*
1298 * Multicast list managers
1299 */
1300
Eric Dumazete9897072013-06-07 08:48:57 -07001301static u32 ip_mc_hash(const struct ip_mc_list *im)
1302{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001303 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001304}
1305
1306static void ip_mc_hash_add(struct in_device *in_dev,
1307 struct ip_mc_list *im)
1308{
1309 struct ip_mc_list __rcu **mc_hash;
1310 u32 hash;
1311
1312 mc_hash = rtnl_dereference(in_dev->mc_hash);
1313 if (mc_hash) {
1314 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001315 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001316 rcu_assign_pointer(mc_hash[hash], im);
1317 return;
1318 }
1319
1320 /* do not use a hash table for small number of items */
1321 if (in_dev->mc_count < 4)
1322 return;
1323
1324 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1325 GFP_KERNEL);
1326 if (!mc_hash)
1327 return;
1328
1329 for_each_pmc_rtnl(in_dev, im) {
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_INIT_POINTER(mc_hash[hash], im);
1333 }
1334
1335 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1336}
1337
1338static void ip_mc_hash_remove(struct in_device *in_dev,
1339 struct ip_mc_list *im)
1340{
1341 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1342 struct ip_mc_list *aux;
1343
1344 if (!mc_hash)
1345 return;
1346 mc_hash += ip_mc_hash(im);
1347 while ((aux = rtnl_dereference(*mc_hash)) != im)
1348 mc_hash = &aux->next_hash;
1349 *mc_hash = im->next_hash;
1350}
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353/*
1354 * A socket has joined a multicast group on device dev.
1355 */
1356
Al Viro8f935bb2006-09-27 18:30:07 -07001357void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct ip_mc_list *im;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001360#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001361 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 ASSERT_RTNL();
1365
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001366 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (im->multiaddr == addr) {
1368 im->users++;
1369 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1370 goto out;
1371 }
1372 }
1373
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001374 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!im)
1376 goto out;
1377
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001378 im->users = 1;
1379 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001381 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* initial mode is (EX, empty) */
1383 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 im->sfcount[MCAST_EXCLUDE] = 1;
Reshetova, Elena8851ab52017-06-30 13:08:02 +03001385 refcount_set(&im->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 spin_lock_init(&im->lock);
1387#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301388 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001389 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001391
1392 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001393 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001394 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001395
Eric Dumazete9897072013-06-07 08:48:57 -07001396 ip_mc_hash_add(in_dev, im);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001399 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400#endif
1401 igmp_group_added(im);
1402 if (!in_dev->dead)
1403 ip_rt_multicast_event(in_dev);
1404out:
1405 return;
1406}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001407EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001409static int ip_mc_check_iphdr(struct sk_buff *skb)
1410{
1411 const struct iphdr *iph;
1412 unsigned int len;
1413 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1414
1415 if (!pskb_may_pull(skb, offset))
1416 return -EINVAL;
1417
1418 iph = ip_hdr(skb);
1419
1420 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1421 return -EINVAL;
1422
1423 offset += ip_hdrlen(skb) - sizeof(*iph);
1424
1425 if (!pskb_may_pull(skb, offset))
1426 return -EINVAL;
1427
1428 iph = ip_hdr(skb);
1429
1430 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1431 return -EINVAL;
1432
1433 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1434 if (skb->len < len || len < offset)
1435 return -EINVAL;
1436
1437 skb_set_transport_header(skb, offset);
1438
1439 return 0;
1440}
1441
1442static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1443{
1444 unsigned int len = skb_transport_offset(skb);
1445
1446 len += sizeof(struct igmpv3_report);
1447
1448 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1449}
1450
1451static int ip_mc_check_igmp_query(struct sk_buff *skb)
1452{
1453 unsigned int len = skb_transport_offset(skb);
1454
1455 len += sizeof(struct igmphdr);
1456 if (skb->len < len)
1457 return -EINVAL;
1458
1459 /* IGMPv{1,2}? */
1460 if (skb->len != len) {
1461 /* or IGMPv3? */
1462 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1463 if (skb->len < len || !pskb_may_pull(skb, len))
1464 return -EINVAL;
1465 }
1466
1467 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1468 * all-systems destination addresses (224.0.0.1) for general queries
1469 */
1470 if (!igmp_hdr(skb)->group &&
1471 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1472 return -EINVAL;
1473
1474 return 0;
1475}
1476
1477static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1478{
1479 switch (igmp_hdr(skb)->type) {
1480 case IGMP_HOST_LEAVE_MESSAGE:
1481 case IGMP_HOST_MEMBERSHIP_REPORT:
1482 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1483 /* fall through */
1484 return 0;
1485 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1486 return ip_mc_check_igmp_reportv3(skb);
1487 case IGMP_HOST_MEMBERSHIP_QUERY:
1488 return ip_mc_check_igmp_query(skb);
1489 default:
1490 return -ENOMSG;
1491 }
1492}
1493
1494static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1495{
1496 return skb_checksum_simple_validate(skb);
1497}
1498
1499static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1500
1501{
1502 struct sk_buff *skb_chk;
1503 unsigned int transport_len;
1504 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga5169932015-08-13 05:54:07 +02001505 int ret = -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001506
1507 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1508
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001509 skb_chk = skb_checksum_trimmed(skb, transport_len,
1510 ip_mc_validate_checksum);
1511 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02001512 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001513
Linus Lüssinga5169932015-08-13 05:54:07 +02001514 if (!pskb_may_pull(skb_chk, len))
1515 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001516
1517 ret = ip_mc_check_igmp_msg(skb_chk);
Linus Lüssinga5169932015-08-13 05:54:07 +02001518 if (ret)
1519 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001520
1521 if (skb_trimmed)
1522 *skb_trimmed = skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02001523 /* free now unneeded clone */
1524 else if (skb_chk != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001525 kfree_skb(skb_chk);
1526
Linus Lüssinga5169932015-08-13 05:54:07 +02001527 ret = 0;
1528
1529err:
1530 if (ret && skb_chk && skb_chk != skb)
1531 kfree_skb(skb_chk);
1532
1533 return ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001534}
1535
1536/**
1537 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1538 * @skb: the skb to validate
1539 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1540 *
1541 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001542 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001543 *
1544 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1545 * standard
1546 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1547 * -ENOMEM: A memory allocation failure happened.
1548 *
1549 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1550 * to NULL): After parsing an IGMP packet successfully it will point to
1551 * an skb which has its tail aligned to the IP packet end. This might
1552 * either be the originally provided skb or a trimmed, cloned version if
1553 * the skb frame had data beyond the IP packet. A cloned skb allows us
1554 * to leave the original skb and its full frame unchanged (which might be
1555 * desirable for layer 2 frame jugglers).
1556 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001557 * Caller needs to set the skb network header and free any returned skb if it
1558 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001559 */
1560int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1561{
1562 int ret = ip_mc_check_iphdr(skb);
1563
1564 if (ret < 0)
1565 return ret;
1566
1567 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1568 return -ENOMSG;
1569
1570 return __ip_mc_check_igmp(skb, skb_trimmed);
1571}
1572EXPORT_SYMBOL(ip_mc_check_igmp);
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001575 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001576 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001577static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001578{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001579#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001580 struct ip_mc_list *im;
1581 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001582 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001583
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001584 ASSERT_RTNL();
1585
1586 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001587 if (im->multiaddr == IGMP_ALL_HOSTS)
1588 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001589 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001590 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001591 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001592
Eric Dumazet866f3b22010-11-18 09:33:19 -08001593 /* a failover is happening and switches
1594 * must be notified immediately
1595 */
1596 if (IGMP_V1_SEEN(in_dev))
1597 type = IGMP_HOST_MEMBERSHIP_REPORT;
1598 else if (IGMP_V2_SEEN(in_dev))
1599 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1600 else
1601 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1602 igmp_send_report(in_dev, im, type);
1603 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001604#endif
1605}
1606
1607/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 * A socket has left a multicast group on device dev
1609 */
1610
Al Viro8f935bb2006-09-27 18:30:07 -07001611void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001613 struct ip_mc_list *i;
1614 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001617
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001618 for (ip = &in_dev->mc_list;
1619 (i = rtnl_dereference(*ip)) != NULL;
1620 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001621 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001623 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001624 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001625 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001627 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 if (!in_dev->dead)
1630 ip_rt_multicast_event(in_dev);
1631
1632 ip_ma_put(i);
1633 return;
1634 }
1635 break;
1636 }
1637 }
1638}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001639EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Moni Shoua75c78502009-09-15 02:37:40 -07001641/* Device changing type */
1642
1643void ip_mc_unmap(struct in_device *in_dev)
1644{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001645 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001646
1647 ASSERT_RTNL();
1648
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001649 for_each_pmc_rtnl(in_dev, pmc)
1650 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001651}
1652
1653void ip_mc_remap(struct in_device *in_dev)
1654{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001655 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001656
1657 ASSERT_RTNL();
1658
Hangbin Liu24803f32016-11-14 16:16:28 +08001659 for_each_pmc_rtnl(in_dev, pmc) {
1660#ifdef CONFIG_IP_MULTICAST
1661 igmpv3_del_delrec(in_dev, pmc);
1662#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001663 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001664 }
Moni Shoua75c78502009-09-15 02:37:40 -07001665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/* Device going down */
1668
1669void ip_mc_down(struct in_device *in_dev)
1670{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001671 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 ASSERT_RTNL();
1674
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001675 for_each_pmc_rtnl(in_dev, pmc)
1676 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678#ifdef CONFIG_IP_MULTICAST
1679 in_dev->mr_ifc_count = 0;
1680 if (del_timer(&in_dev->mr_ifc_timer))
1681 __in_dev_put(in_dev);
1682 in_dev->mr_gq_running = 0;
1683 if (del_timer(&in_dev->mr_gq_timer))
1684 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685#endif
1686
1687 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1688}
1689
1690void ip_mc_init_dev(struct in_device *in_dev)
1691{
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001692#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001693 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001694#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 ASSERT_RTNL();
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001698 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1699 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001700 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1701 (unsigned long)in_dev);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001702 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703#endif
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 spin_lock_init(&in_dev->mc_tomb_lock);
1706}
1707
1708/* Device going up */
1709
1710void ip_mc_up(struct in_device *in_dev)
1711{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001712 struct ip_mc_list *pmc;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001713#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001714 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001715#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 ASSERT_RTNL();
1718
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001719#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001720 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001721#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1723
Hangbin Liu24803f32016-11-14 16:16:28 +08001724 for_each_pmc_rtnl(in_dev, pmc) {
1725#ifdef CONFIG_IP_MULTICAST
1726 igmpv3_del_delrec(in_dev, pmc);
1727#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001728 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732/*
1733 * Device is about to be destroyed: clean up.
1734 */
1735
1736void ip_mc_destroy_dev(struct in_device *in_dev)
1737{
1738 struct ip_mc_list *i;
1739
1740 ASSERT_RTNL();
1741
1742 /* Deactivate timers */
1743 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001744#ifdef CONFIG_IP_MULTICAST
1745 igmpv3_clear_delrec(in_dev);
1746#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001748 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1749 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001750 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001755/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001756static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 struct net_device *dev = NULL;
1759 struct in_device *idev = NULL;
1760
1761 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001762 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return idev;
1764 }
1765 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001766 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (!dev)
1768 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770
David S. Millerb23dd4f2011-03-02 14:31:35 -08001771 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001772 struct rtable *rt = ip_route_output(net,
1773 imr->imr_multiaddr.s_addr,
1774 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001775 if (!IS_ERR(rt)) {
1776 dev = rt->dst.dev;
1777 ip_rt_put(rt);
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 if (dev) {
1781 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001782 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 return idev;
1785}
1786
1787/*
1788 * Join a socket to a group
1789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001792 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 struct ip_sf_list *psf, *psf_prev;
1795 int rv = 0;
1796
1797 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001798 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (psf->sf_inaddr == *psfsrc)
1800 break;
1801 psf_prev = psf;
1802 }
1803 if (!psf || psf->sf_count[sfmode] == 0) {
1804 /* source filter not found, or count wrong => bug */
1805 return -ESRCH;
1806 }
1807 psf->sf_count[sfmode]--;
1808 if (psf->sf_count[sfmode] == 0) {
1809 ip_rt_multicast_event(pmc->interface);
1810 }
1811 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1812#ifdef CONFIG_IP_MULTICAST
1813 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001814 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815#endif
1816
1817 /* no more filters for this source */
1818 if (psf_prev)
1819 psf_prev->sf_next = psf->sf_next;
1820 else
1821 pmc->sources = psf->sf_next;
1822#ifdef CONFIG_IP_MULTICAST
1823 if (psf->sf_oldin &&
1824 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001825 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 psf->sf_next = pmc->tomb;
1827 pmc->tomb = psf;
1828 rv = 1;
1829 } else
1830#endif
1831 kfree(psf);
1832 }
1833 return rv;
1834}
1835
1836#ifndef CONFIG_IP_MULTICAST
1837#define igmp_ifc_event(x) do { } while (0)
1838#endif
1839
Al Viro8f935bb2006-09-27 18:30:07 -07001840static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1841 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct ip_mc_list *pmc;
1844 int changerec = 0;
1845 int i, err;
1846
1847 if (!in_dev)
1848 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001849 rcu_read_lock();
1850 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (*pmca == pmc->multiaddr)
1852 break;
1853 }
1854 if (!pmc) {
1855 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001856 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return -ESRCH;
1858 }
1859 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001860 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861#ifdef CONFIG_IP_MULTICAST
1862 sf_markstate(pmc);
1863#endif
1864 if (!delta) {
1865 err = -EINVAL;
1866 if (!pmc->sfcount[sfmode])
1867 goto out_unlock;
1868 pmc->sfcount[sfmode]--;
1869 }
1870 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001871 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1873
1874 changerec |= rv > 0;
1875 if (!err && rv < 0)
1876 err = rv;
1877 }
1878 if (pmc->sfmode == MCAST_EXCLUDE &&
1879 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1880 pmc->sfcount[MCAST_INCLUDE]) {
1881#ifdef CONFIG_IP_MULTICAST
1882 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001883 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884#endif
1885
1886 /* filter mode change */
1887 pmc->sfmode = MCAST_INCLUDE;
1888#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001889 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001891 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 psf->sf_crcount = 0;
1893 igmp_ifc_event(pmc->interface);
1894 } else if (sf_setstate(pmc) || changerec) {
1895 igmp_ifc_event(pmc->interface);
1896#endif
1897 }
1898out_unlock:
1899 spin_unlock_bh(&pmc->lock);
1900 return err;
1901}
1902
1903/*
1904 * Add multicast single-source filter to the interface list
1905 */
1906static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001907 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct ip_sf_list *psf, *psf_prev;
1910
1911 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001912 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (psf->sf_inaddr == *psfsrc)
1914 break;
1915 psf_prev = psf;
1916 }
1917 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001918 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (!psf)
1920 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 psf->sf_inaddr = *psfsrc;
1922 if (psf_prev) {
1923 psf_prev->sf_next = psf;
1924 } else
1925 pmc->sources = psf;
1926 }
1927 psf->sf_count[sfmode]++;
1928 if (psf->sf_count[sfmode] == 1) {
1929 ip_rt_multicast_event(pmc->interface);
1930 }
1931 return 0;
1932}
1933
1934#ifdef CONFIG_IP_MULTICAST
1935static void sf_markstate(struct ip_mc_list *pmc)
1936{
1937 struct ip_sf_list *psf;
1938 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1939
Weilong Chenc71151f2013-12-23 14:37:29 +08001940 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (pmc->sfcount[MCAST_EXCLUDE]) {
1942 psf->sf_oldin = mca_xcount ==
1943 psf->sf_count[MCAST_EXCLUDE] &&
1944 !psf->sf_count[MCAST_INCLUDE];
1945 } else
1946 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1947}
1948
1949static int sf_setstate(struct ip_mc_list *pmc)
1950{
David L Stevensad125832006-01-18 14:20:56 -08001951 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1953 int qrv = pmc->interface->mr_qrv;
1954 int new_in, rv;
1955
1956 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001957 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (pmc->sfcount[MCAST_EXCLUDE]) {
1959 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1960 !psf->sf_count[MCAST_INCLUDE];
1961 } else
1962 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001963 if (new_in) {
1964 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001965 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001966
Weilong Chenc71151f2013-12-23 14:37:29 +08001967 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001968 if (dpsf->sf_inaddr == psf->sf_inaddr)
1969 break;
1970 prev = dpsf;
1971 }
1972 if (dpsf) {
1973 if (prev)
1974 prev->sf_next = dpsf->sf_next;
1975 else
1976 pmc->tomb = dpsf->sf_next;
1977 kfree(dpsf);
1978 }
1979 psf->sf_crcount = qrv;
1980 rv++;
1981 }
1982 } else if (psf->sf_oldin) {
1983
1984 psf->sf_crcount = 0;
1985 /*
1986 * add or update "delete" records if an active filter
1987 * is now inactive
1988 */
Weilong Chenc71151f2013-12-23 14:37:29 +08001989 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08001990 if (dpsf->sf_inaddr == psf->sf_inaddr)
1991 break;
1992 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001993 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001994 if (!dpsf)
1995 continue;
1996 *dpsf = *psf;
1997 /* pmc->lock held by callers */
1998 dpsf->sf_next = pmc->tomb;
1999 pmc->tomb = dpsf;
2000 }
2001 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 rv++;
2003 }
2004 }
2005 return rv;
2006}
2007#endif
2008
2009/*
2010 * Add multicast source filter list to the interface list
2011 */
Al Viro8f935bb2006-09-27 18:30:07 -07002012static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2013 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 struct ip_mc_list *pmc;
2016 int isexclude;
2017 int i, err;
2018
2019 if (!in_dev)
2020 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002021 rcu_read_lock();
2022 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (*pmca == pmc->multiaddr)
2024 break;
2025 }
2026 if (!pmc) {
2027 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002028 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return -ESRCH;
2030 }
2031 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002032 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034#ifdef CONFIG_IP_MULTICAST
2035 sf_markstate(pmc);
2036#endif
2037 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2038 if (!delta)
2039 pmc->sfcount[sfmode]++;
2040 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002041 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002042 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (err)
2044 break;
2045 }
2046 if (err) {
2047 int j;
2048
Jun Zhao685f94e62011-11-22 17:19:03 +00002049 if (!delta)
2050 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002051 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002052 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2054#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002056 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002057 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058#endif
2059
2060 /* filter mode change */
2061 if (pmc->sfcount[MCAST_EXCLUDE])
2062 pmc->sfmode = MCAST_EXCLUDE;
2063 else if (pmc->sfcount[MCAST_INCLUDE])
2064 pmc->sfmode = MCAST_INCLUDE;
2065#ifdef CONFIG_IP_MULTICAST
2066 /* else no filters; keep old mode for reports */
2067
Nikolay Borisov165094a2016-02-08 23:29:24 +02002068 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002070 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 psf->sf_crcount = 0;
2072 igmp_ifc_event(in_dev);
2073 } else if (sf_setstate(pmc)) {
2074 igmp_ifc_event(in_dev);
2075#endif
2076 }
2077 spin_unlock_bh(&pmc->lock);
2078 return err;
2079}
2080
2081static void ip_mc_clear_src(struct ip_mc_list *pmc)
2082{
WANG Congc38b7d32017-06-12 09:52:26 -07002083 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
WANG Congc38b7d32017-06-12 09:52:26 -07002085 spin_lock_bh(&pmc->lock);
2086 tomb = pmc->tomb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 pmc->tomb = NULL;
WANG Congc38b7d32017-06-12 09:52:26 -07002088 sources = pmc->sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 pmc->sources = NULL;
2090 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002091 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 pmc->sfcount[MCAST_EXCLUDE] = 1;
WANG Congc38b7d32017-06-12 09:52:26 -07002093 spin_unlock_bh(&pmc->lock);
2094
2095 for (psf = tomb; psf; psf = nextpsf) {
2096 nextpsf = psf->sf_next;
2097 kfree(psf);
2098 }
2099 for (psf = sources; psf; psf = nextpsf) {
2100 nextpsf = psf->sf_next;
2101 kfree(psf);
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002105/* Join a multicast group
2106 */
2107
2108int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Al Viro8f935bb2006-09-27 18:30:07 -07002110 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002111 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct in_device *in_dev;
2113 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002114 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002115 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002117 int err;
2118
2119 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Joe Perchesf97c1e02007-12-16 13:45:43 -08002121 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return -EINVAL;
2123
Daniel Lezcano877aced2008-08-13 16:15:57 -07002124 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 err = -ENODEV;
2128 goto done;
2129 }
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002132 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002133 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002134 if (i->multi.imr_multiaddr.s_addr == addr &&
2135 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 count++;
2138 }
2139 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002140 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002142 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002143 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002144 goto done;
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002147 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 iml->sflist = NULL;
2149 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002150 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return err;
2155}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002156EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2159 struct in_device *in_dev)
2160{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002161 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int err;
2163
Ian Morris51456b22015-04-03 09:17:26 +01002164 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* any-source empty exclude case */
2166 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2167 iml->sfmode, 0, NULL, 0);
2168 }
2169 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002170 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002171 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002172 /* decrease mem now to avoid the memleak warning */
2173 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002174 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return err;
2176}
2177
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002178int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002181 struct ip_mc_socklist *iml;
2182 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002183 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002184 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002185 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002186 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002187 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Eric Dumazet959d10f2015-02-17 03:19:24 -08002189 ASSERT_RTNL();
2190
Daniel Lezcano877aced2008-08-13 16:15:57 -07002191 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002192 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002193 ret = -ENODEV;
2194 goto out;
2195 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002196 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002197 for (imlp = &inet->mc_list;
2198 (iml = rtnl_dereference(*imlp)) != NULL;
2199 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002200 if (iml->multi.imr_multiaddr.s_addr != group)
2201 continue;
2202 if (ifindex) {
2203 if (iml->multi.imr_ifindex != ifindex)
2204 continue;
2205 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2206 iml->multi.imr_address.s_addr)
2207 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
David L Stevensacd6e002006-08-17 16:27:39 -07002209 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002211 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002212
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002213 if (in_dev)
2214 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002215
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002216 /* decrease mem now to avoid the memleak warning */
2217 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002218 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
dingtianhong52ad3532014-07-02 13:50:48 +08002221out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002222 return ret;
2223}
stephen hemminger193ba922012-10-01 12:32:34 +00002224EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226int ip_mc_source(int add, int omode, struct sock *sk, struct
2227 ip_mreq_source *mreqs, int ifindex)
2228{
2229 int err;
2230 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002231 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 struct ip_mc_socklist *pmc;
2233 struct in_device *in_dev = NULL;
2234 struct inet_sock *inet = inet_sk(sk);
2235 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002236 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002237 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int i, j, rv;
2239
Joe Perchesf97c1e02007-12-16 13:45:43 -08002240 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return -EINVAL;
2242
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002243 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2246 imr.imr_address.s_addr = mreqs->imr_interface;
2247 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002248 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 if (!in_dev) {
2251 err = -ENODEV;
2252 goto done;
2253 }
2254 err = -EADDRNOTAVAIL;
2255
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002256 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002257 if ((pmc->multi.imr_multiaddr.s_addr ==
2258 imr.imr_multiaddr.s_addr) &&
2259 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 break;
2261 }
David L Stevens917f2f12005-07-08 17:45:16 -07002262 if (!pmc) { /* must have a prior join */
2263 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /* if a source filter was set, must be the same mode as before */
2267 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002268 if (pmc->sfmode != omode) {
2269 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 } else if (pmc->sfmode != omode) {
2273 /* allow mode switches for empty-set filters */
2274 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002275 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 NULL, 0);
2277 pmc->sfmode = omode;
2278 }
2279
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002280 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (!add) {
2282 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002283 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002285 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002287 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (rv == 0)
2289 break;
2290 }
2291 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002292 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
David L Stevens8cdaaa12005-07-08 17:39:23 -07002294 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2295 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2296 leavegroup = 1;
2297 goto done;
2298 }
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002301 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 &mreqs->imr_sourceaddr, 1);
2303
Weilong Chenc71151f2013-12-23 14:37:29 +08002304 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 psl->sl_addr[j-1] = psl->sl_addr[j];
2306 psl->sl_count--;
2307 err = 0;
2308 goto done;
2309 }
2310 /* else, add a new source to the filter */
2311
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002312 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 err = -ENOBUFS;
2314 goto done;
2315 }
2316 if (!psl || psl->sl_count == psl->sl_max) {
2317 struct ip_sf_socklist *newpsl;
2318 int count = IP_SFBLOCK;
2319
2320 if (psl)
2321 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002322 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!newpsl) {
2324 err = -ENOBUFS;
2325 goto done;
2326 }
2327 newpsl->sl_max = count;
2328 newpsl->sl_count = count - IP_SFBLOCK;
2329 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002330 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002332 /* decrease mem now to avoid the memleak warning */
2333 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002334 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002336 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002337 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002340 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002342 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (rv == 0)
2344 break;
2345 }
2346 if (rv == 0) /* address already there is an error */
2347 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002348 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 psl->sl_addr[j+1] = psl->sl_addr[j];
2350 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2351 psl->sl_count++;
2352 err = 0;
2353 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002354 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 &mreqs->imr_sourceaddr, 1);
2356done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002357 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002358 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return err;
2360}
2361
2362int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2363{
David L Stevens9951f032005-07-08 17:47:28 -07002364 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002366 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct ip_mc_socklist *pmc;
2368 struct in_device *in_dev;
2369 struct inet_sock *inet = inet_sk(sk);
2370 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002371 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002372 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Joe Perchesf97c1e02007-12-16 13:45:43 -08002374 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return -EINVAL;
2376 if (msf->imsf_fmode != MCAST_INCLUDE &&
2377 msf->imsf_fmode != MCAST_EXCLUDE)
2378 return -EINVAL;
2379
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002380 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2383 imr.imr_address.s_addr = msf->imsf_interface;
2384 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002385 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 if (!in_dev) {
2388 err = -ENODEV;
2389 goto done;
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
David L Stevens9951f032005-07-08 17:47:28 -07002392 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2393 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2394 leavegroup = 1;
2395 goto done;
2396 }
2397
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002398 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2400 pmc->multi.imr_ifindex == imr.imr_ifindex)
2401 break;
2402 }
David L Stevens917f2f12005-07-08 17:45:16 -07002403 if (!pmc) { /* must have a prior join */
2404 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002408 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2409 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (!newpsl) {
2411 err = -ENOBUFS;
2412 goto done;
2413 }
2414 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2415 memcpy(newpsl->sl_addr, msf->imsf_slist,
2416 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2417 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2418 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2419 if (err) {
2420 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2421 goto done;
2422 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002423 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002425 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2426 msf->imsf_fmode, 0, NULL, 0);
2427 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002428 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (psl) {
2430 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2431 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002432 /* decrease mem now to avoid the memleak warning */
2433 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002434 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 } else
2436 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2437 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002438 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002440 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441done:
David L Stevens9951f032005-07-08 17:47:28 -07002442 if (leavegroup)
2443 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return err;
2445}
2446
2447int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2448 struct ip_msfilter __user *optval, int __user *optlen)
2449{
2450 int err, len, count, copycount;
2451 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002452 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct ip_mc_socklist *pmc;
2454 struct in_device *in_dev;
2455 struct inet_sock *inet = inet_sk(sk);
2456 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002457 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
WANG Cong87e9f032015-11-03 15:41:16 -08002459 ASSERT_RTNL();
2460
Joe Perchesf97c1e02007-12-16 13:45:43 -08002461 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return -EINVAL;
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2465 imr.imr_address.s_addr = msf->imsf_interface;
2466 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002467 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 if (!in_dev) {
2470 err = -ENODEV;
2471 goto done;
2472 }
2473 err = -EADDRNOTAVAIL;
2474
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002475 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2477 pmc->multi.imr_ifindex == imr.imr_ifindex)
2478 break;
2479 }
2480 if (!pmc) /* must have a prior join */
2481 goto done;
2482 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002483 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (!psl) {
2485 len = 0;
2486 count = 0;
2487 } else {
2488 count = psl->sl_count;
2489 }
2490 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2491 len = copycount * sizeof(psl->sl_addr[0]);
2492 msf->imsf_numsrc = count;
2493 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2494 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2495 return -EFAULT;
2496 }
2497 if (len &&
2498 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2499 return -EFAULT;
2500 return 0;
2501done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return err;
2503}
2504
2505int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2506 struct group_filter __user *optval, int __user *optlen)
2507{
2508 int err, i, count, copycount;
2509 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002510 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct ip_mc_socklist *pmc;
2512 struct inet_sock *inet = inet_sk(sk);
2513 struct ip_sf_socklist *psl;
2514
WANG Cong87e9f032015-11-03 15:41:16 -08002515 ASSERT_RTNL();
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 psin = (struct sockaddr_in *)&gsf->gf_group;
2518 if (psin->sin_family != AF_INET)
2519 return -EINVAL;
2520 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002521 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return -EINVAL;
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 err = -EADDRNOTAVAIL;
2525
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002526 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2528 pmc->multi.imr_ifindex == gsf->gf_interface)
2529 break;
2530 }
2531 if (!pmc) /* must have a prior join */
2532 goto done;
2533 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002534 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 count = psl ? psl->sl_count : 0;
2536 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2537 gsf->gf_numsrc = count;
2538 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2539 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2540 return -EFAULT;
2541 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002542 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct sockaddr_storage ss;
2544
2545 psin = (struct sockaddr_in *)&ss;
2546 memset(&ss, 0, sizeof(ss));
2547 psin->sin_family = AF_INET;
2548 psin->sin_addr.s_addr = psl->sl_addr[i];
2549 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2550 return -EFAULT;
2551 }
2552 return 0;
2553done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return err;
2555}
2556
2557/*
2558 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2559 */
Al Viroc0cda062006-09-27 18:31:10 -07002560int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 struct inet_sock *inet = inet_sk(sk);
2563 struct ip_mc_socklist *pmc;
2564 struct ip_sf_socklist *psl;
2565 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002566 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002568 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002569 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002572 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002573 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2575 pmc->multi.imr_ifindex == dif)
2576 break;
2577 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002578 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002580 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002581 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002582 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002584 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Weilong Chenc71151f2013-12-23 14:37:29 +08002586 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (psl->sl_addr[i] == rmt_addr)
2588 break;
2589 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002590 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002592 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002594 goto unlock;
2595 ret = 1;
2596unlock:
2597 rcu_read_unlock();
2598out:
2599 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
2602/*
2603 * A socket is closing.
2604 */
2605
2606void ip_mc_drop_socket(struct sock *sk)
2607{
2608 struct inet_sock *inet = inet_sk(sk);
2609 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002610 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Ian Morris51456b22015-04-03 09:17:26 +01002612 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return;
2614
2615 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002616 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002619 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002620 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002621 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002622 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002624 /* decrease mem now to avoid the memleak warning */
2625 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002626 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
2628 rtnl_unlock();
2629}
2630
David S. Millerdbdd9a52011-03-10 16:34:38 -08002631/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002632int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002635 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 struct ip_sf_list *psf;
2637 int rv = 0;
2638
Eric Dumazete9897072013-06-07 08:48:57 -07002639 mc_hash = rcu_dereference(in_dev->mc_hash);
2640 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002641 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002642
2643 for (im = rcu_dereference(mc_hash[hash]);
2644 im != NULL;
2645 im = rcu_dereference(im->next_hash)) {
2646 if (im->multiaddr == mc_addr)
2647 break;
2648 }
2649 } else {
2650 for_each_pmc_rcu(in_dev, im) {
2651 if (im->multiaddr == mc_addr)
2652 break;
2653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
2655 if (im && proto == IPPROTO_IGMP) {
2656 rv = 1;
2657 } else if (im) {
2658 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002659 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 if (psf->sf_inaddr == src_addr)
2661 break;
2662 }
2663 if (psf)
2664 rv = psf->sf_count[MCAST_INCLUDE] ||
2665 psf->sf_count[MCAST_EXCLUDE] !=
2666 im->sfcount[MCAST_EXCLUDE];
2667 else
2668 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2669 } else
2670 rv = 1; /* unspecified source; tentatively allow */
2671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return rv;
2673}
2674
2675#if defined(CONFIG_PROC_FS)
2676struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002677 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct net_device *dev;
2679 struct in_device *in_dev;
2680};
2681
2682#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2683
2684static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2685{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002686 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 struct ip_mc_list *im = NULL;
2688 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2689
Pavel Emelianov7562f872007-05-03 15:13:45 -07002690 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002691 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002693
2694 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (!in_dev)
2696 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002697 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (im) {
2699 state->in_dev = in_dev;
2700 break;
2701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
2703 return im;
2704}
2705
2706static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2707{
2708 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002709
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002710 im = rcu_dereference(im->next_rcu);
2711 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002712 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (!state->dev) {
2714 state->in_dev = NULL;
2715 break;
2716 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002717 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (!state->in_dev)
2719 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002720 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
2722 return im;
2723}
2724
2725static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2726{
2727 struct ip_mc_list *im = igmp_mc_get_first(seq);
2728 if (im)
2729 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2730 --pos;
2731 return pos ? NULL : im;
2732}
2733
2734static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002735 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
stephen hemminger61fbab72009-11-10 07:54:55 +00002737 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2739}
2740
2741static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2742{
2743 struct ip_mc_list *im;
2744 if (v == SEQ_START_TOKEN)
2745 im = igmp_mc_get_first(seq);
2746 else
2747 im = igmp_mc_get_next(seq, v);
2748 ++*pos;
2749 return im;
2750}
2751
2752static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002753 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002756
2757 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002759 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760}
2761
2762static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2763{
2764 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002765 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2767 else {
2768 struct ip_mc_list *im = (struct ip_mc_list *)v;
2769 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2770 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002771 long delta;
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773#ifdef CONFIG_IP_MULTICAST
2774 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2775 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2776 "V3";
2777#else
2778 querier = "NONE";
2779#endif
2780
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002781 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002783 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785
Eric Dumazeta399a802012-08-08 21:13:53 +00002786 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002788 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002790 im->tm_running,
2791 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 im->reporter);
2793 }
2794 return 0;
2795}
2796
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002797static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 .start = igmp_mc_seq_start,
2799 .next = igmp_mc_seq_next,
2800 .stop = igmp_mc_seq_stop,
2801 .show = igmp_mc_seq_show,
2802};
2803
2804static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2805{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002806 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002807 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
Arjan van de Ven9a321442007-02-12 00:55:35 -08002810static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 .owner = THIS_MODULE,
2812 .open = igmp_mc_seq_open,
2813 .read = seq_read,
2814 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002815 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816};
2817
2818struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002819 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 struct net_device *dev;
2821 struct in_device *idev;
2822 struct ip_mc_list *im;
2823};
2824
2825#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2826
2827static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2828{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002829 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 struct ip_sf_list *psf = NULL;
2831 struct ip_mc_list *im = NULL;
2832 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2833
Pavel Emelianov7562f872007-05-03 15:13:45 -07002834 state->idev = NULL;
2835 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002836 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002838 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002839 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002841 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002842 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 spin_lock_bh(&im->lock);
2844 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002845 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 state->im = im;
2847 state->idev = idev;
2848 break;
2849 }
2850 spin_unlock_bh(&im->lock);
2851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853 return psf;
2854}
2855
2856static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2857{
2858 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2859
2860 psf = psf->sf_next;
2861 while (!psf) {
2862 spin_unlock_bh(&state->im->lock);
2863 state->im = state->im->next;
2864 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002865 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (!state->dev) {
2867 state->idev = NULL;
2868 goto out;
2869 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002870 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (!state->idev)
2872 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002873 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
2875 if (!state->im)
2876 break;
2877 spin_lock_bh(&state->im->lock);
2878 psf = state->im->sources;
2879 }
2880out:
2881 return psf;
2882}
2883
2884static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2885{
2886 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2887 if (psf)
2888 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2889 --pos;
2890 return pos ? NULL : psf;
2891}
2892
2893static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002894 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
stephen hemminger61fbab72009-11-10 07:54:55 +00002896 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2898}
2899
2900static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2901{
2902 struct ip_sf_list *psf;
2903 if (v == SEQ_START_TOKEN)
2904 psf = igmp_mcf_get_first(seq);
2905 else
2906 psf = igmp_mcf_get_next(seq, v);
2907 ++*pos;
2908 return psf;
2909}
2910
2911static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002912 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002915 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 spin_unlock_bh(&state->im->lock);
2917 state->im = NULL;
2918 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002919 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002921 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
2924static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2925{
2926 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2927 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2928
2929 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002930 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 } else {
2932 seq_printf(seq,
2933 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002934 "0x%08x %6lu %6lu\n",
2935 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 ntohl(state->im->multiaddr),
2937 ntohl(psf->sf_inaddr),
2938 psf->sf_count[MCAST_INCLUDE],
2939 psf->sf_count[MCAST_EXCLUDE]);
2940 }
2941 return 0;
2942}
2943
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002944static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 .start = igmp_mcf_seq_start,
2946 .next = igmp_mcf_seq_next,
2947 .stop = igmp_mcf_seq_stop,
2948 .show = igmp_mcf_seq_show,
2949};
2950
2951static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2952{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002953 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002954 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
Arjan van de Ven9a321442007-02-12 00:55:35 -08002957static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 .owner = THIS_MODULE,
2959 .open = igmp_mcf_seq_open,
2960 .read = seq_read,
2961 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002962 .release = seq_release_net,
2963};
2964
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002965static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002966{
2967 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002968 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002969
Gao fengd4beaa62013-02-18 01:34:54 +00002970 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002971 if (!pde)
2972 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002973 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2974 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002975 if (!pde)
2976 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08002977 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2978 SOCK_DGRAM, 0, net);
2979 if (err < 0) {
2980 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2981 err);
2982 goto out_sock;
2983 }
2984
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002985 return 0;
2986
Madhu Challa93a714d2015-02-25 09:58:35 -08002987out_sock:
2988 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002989out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002990 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002991out_igmp:
2992 return -ENOMEM;
2993}
2994
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002995static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002996{
Gao fengece31ff2013-02-18 01:34:56 +00002997 remove_proc_entry("mcfilter", net->proc_net);
2998 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08002999 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003000}
3001
3002static struct pernet_operations igmp_net_ops = {
3003 .init = igmp_net_init,
3004 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005};
WANG Cong72c1d3b2014-01-10 16:09:45 -08003006#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003008static int igmp_netdev_event(struct notifier_block *this,
3009 unsigned long event, void *ptr)
3010{
3011 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3012 struct in_device *in_dev;
3013
3014 switch (event) {
3015 case NETDEV_RESEND_IGMP:
3016 in_dev = __in_dev_get_rtnl(dev);
3017 if (in_dev)
3018 ip_mc_rejoin_groups(in_dev);
3019 break;
3020 default:
3021 break;
3022 }
3023 return NOTIFY_DONE;
3024}
3025
3026static struct notifier_block igmp_notifier = {
3027 .notifier_call = igmp_netdev_event,
3028};
3029
WANG Cong72c1d3b2014-01-10 16:09:45 -08003030int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003032#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003033 int err;
3034
3035 err = register_pernet_subsys(&igmp_net_ops);
3036 if (err)
3037 return err;
3038 err = register_netdevice_notifier(&igmp_notifier);
3039 if (err)
3040 goto reg_notif_fail;
3041 return 0;
3042
3043reg_notif_fail:
3044 unregister_pernet_subsys(&igmp_net_ops);
3045 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003046#else
3047 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003049}