blob: 44fd86de2823dd17de16276a8ec01b190e69b8b4 [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{
176 if (atomic_dec_and_test(&im->refcnt)) {
177 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))
202 atomic_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))
216 atomic_inc(&im->refcnt);
217}
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 }
252 atomic_dec(&im->refcnt);
253 }
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;
417 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
418 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;
Al Viro63007722006-09-27 18:31:32 -0700511 psrc = (__be32 *)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
745 ih = (struct igmphdr *)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;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001010 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001012 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Ian Morris51456b22015-04-03 09:17:26 +01001014 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001015 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Herbert Xufb286bb2005-11-10 13:01:24 -08001017 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001018 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001019
Tom Herbertde08dc12014-05-07 16:52:10 -07001020 if (skb_checksum_simple_validate(skb))
1021 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001023 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 switch (ih->type) {
1025 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001026 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
1028 case IGMP_HOST_MEMBERSHIP_REPORT:
1029 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001031 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
David Stevens24c69272005-12-02 20:32:59 -08001033 /* don't rely on MC router hearing unicast reports */
1034 if (skb->pkt_type == PACKET_MULTICAST ||
1035 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001036 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 break;
1038 case IGMP_PIM:
1039#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return pim_rcv_v1(skb);
1041#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001042 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 case IGMP_DVMRP:
1044 case IGMP_TRACE:
1045 case IGMP_HOST_LEAVE_MESSAGE:
1046 case IGMP_MTRACE:
1047 case IGMP_MTRACE_RESP:
1048 break;
1049 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001050 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001052
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001053drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001054 if (dropped)
1055 kfree_skb(skb);
1056 else
1057 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 0;
1059}
1060
1061#endif
1062
1063
1064/*
1065 * Add a filter to a device
1066 */
1067
Al Viro63007722006-09-27 18:31:32 -07001068static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 char buf[MAX_ADDR_LEN];
1071 struct net_device *dev = in_dev->dev;
1072
1073 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1074 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001075 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 routine. Something sort of:
1077 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1078 --ANK
1079 */
1080 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001081 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084/*
1085 * Remove a filter from a device
1086 */
1087
Al Viro63007722006-09-27 18:31:32 -07001088static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 char buf[MAX_ADDR_LEN];
1091 struct net_device *dev = in_dev->dev;
1092
1093 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001094 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097#ifdef CONFIG_IP_MULTICAST
1098/*
1099 * deleted ip_mc_list manipulation
1100 */
1101static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1102{
1103 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001104 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 /* this is an "ip_mc_list" for convenience; only the fields below
1107 * are actually used. In particular, the refcnt and users are not
1108 * used for management of the delete list. Using the same structure
1109 * for deleted items allows change reports to use common code with
1110 * non-deleted or query-response MCA's.
1111 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001112 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!pmc)
1114 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 spin_lock_bh(&im->lock);
1116 pmc->interface = im->interface;
1117 in_dev_hold(in_dev);
1118 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001119 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 pmc->sfmode = im->sfmode;
1121 if (pmc->sfmode == MCAST_INCLUDE) {
1122 struct ip_sf_list *psf;
1123
1124 pmc->tomb = im->tomb;
1125 pmc->sources = im->sources;
1126 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001127 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 psf->sf_crcount = pmc->crcount;
1129 }
1130 spin_unlock_bh(&im->lock);
1131
1132 spin_lock_bh(&in_dev->mc_tomb_lock);
1133 pmc->next = in_dev->mc_tomb;
1134 in_dev->mc_tomb = pmc;
1135 spin_unlock_bh(&in_dev->mc_tomb_lock);
1136}
1137
Hangbin Liu24803f32016-11-14 16:16:28 +08001138/*
1139 * restore ip_mc_list deleted records
1140 */
1141static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001144 struct ip_sf_list *psf;
1145 struct net *net = dev_net(in_dev->dev);
1146 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 spin_lock_bh(&in_dev->mc_tomb_lock);
1149 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001150 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (pmc->multiaddr == multiaddr)
1152 break;
1153 pmc_prev = pmc;
1154 }
1155 if (pmc) {
1156 if (pmc_prev)
1157 pmc_prev->next = pmc->next;
1158 else
1159 in_dev->mc_tomb = pmc->next;
1160 }
1161 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001162
1163 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001165 im->interface = pmc->interface;
1166 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1167 im->sfmode = pmc->sfmode;
1168 if (pmc->sfmode == MCAST_INCLUDE) {
1169 im->tomb = pmc->tomb;
1170 im->sources = pmc->sources;
1171 for (psf = im->sources; psf; psf = psf->sf_next)
1172 psf->sf_crcount = im->crcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 in_dev_put(pmc->interface);
Hangbin Liu9c8bb162017-02-08 21:16:45 +08001175 kfree(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001177 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Hangbin Liu24803f32016-11-14 16:16:28 +08001180/*
1181 * flush ip_mc_list deleted records
1182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183static void igmpv3_clear_delrec(struct in_device *in_dev)
1184{
1185 struct ip_mc_list *pmc, *nextpmc;
1186
1187 spin_lock_bh(&in_dev->mc_tomb_lock);
1188 pmc = in_dev->mc_tomb;
1189 in_dev->mc_tomb = NULL;
1190 spin_unlock_bh(&in_dev->mc_tomb_lock);
1191
1192 for (; pmc; pmc = nextpmc) {
1193 nextpmc = pmc->next;
1194 ip_mc_clear_src(pmc);
1195 in_dev_put(pmc->interface);
1196 kfree(pmc);
1197 }
1198 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001199 rcu_read_lock();
1200 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 struct ip_sf_list *psf, *psf_next;
1202
1203 spin_lock_bh(&pmc->lock);
1204 psf = pmc->tomb;
1205 pmc->tomb = NULL;
1206 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001207 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 psf_next = psf->sf_next;
1209 kfree(psf);
1210 }
1211 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001212 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214#endif
1215
1216static void igmp_group_dropped(struct ip_mc_list *im)
1217{
1218 struct in_device *in_dev = im->interface;
1219#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001220 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int reporter;
1222#endif
1223
1224 if (im->loaded) {
1225 im->loaded = 0;
1226 ip_mc_filter_del(in_dev, im->multiaddr);
1227 }
1228
1229#ifdef CONFIG_IP_MULTICAST
1230 if (im->multiaddr == IGMP_ALL_HOSTS)
1231 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001232 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001233 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 reporter = im->reporter;
1236 igmp_stop_timer(im);
1237
1238 if (!in_dev->dead) {
1239 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001240 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (IGMP_V2_SEEN(in_dev)) {
1242 if (reporter)
1243 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001244 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 /* IGMPv3 */
1247 igmpv3_add_delrec(in_dev, im);
1248
1249 igmp_ifc_event(in_dev);
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254static void igmp_group_added(struct ip_mc_list *im)
1255{
1256 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001257#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001258 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (im->loaded == 0) {
1262 im->loaded = 1;
1263 ip_mc_filter_add(in_dev, im->multiaddr);
1264 }
1265
1266#ifdef CONFIG_IP_MULTICAST
1267 if (im->multiaddr == IGMP_ALL_HOSTS)
1268 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001269 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001270 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (in_dev->dead)
1273 return;
1274 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1275 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001276 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 spin_unlock_bh(&im->lock);
1278 return;
1279 }
1280 /* else, v3 */
1281
Nikolay Borisov165094a2016-02-08 23:29:24 +02001282 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 igmp_ifc_event(in_dev);
1284#endif
1285}
1286
1287
1288/*
1289 * Multicast list managers
1290 */
1291
Eric Dumazete9897072013-06-07 08:48:57 -07001292static u32 ip_mc_hash(const struct ip_mc_list *im)
1293{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001294 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001295}
1296
1297static void ip_mc_hash_add(struct in_device *in_dev,
1298 struct ip_mc_list *im)
1299{
1300 struct ip_mc_list __rcu **mc_hash;
1301 u32 hash;
1302
1303 mc_hash = rtnl_dereference(in_dev->mc_hash);
1304 if (mc_hash) {
1305 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001306 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001307 rcu_assign_pointer(mc_hash[hash], im);
1308 return;
1309 }
1310
1311 /* do not use a hash table for small number of items */
1312 if (in_dev->mc_count < 4)
1313 return;
1314
1315 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1316 GFP_KERNEL);
1317 if (!mc_hash)
1318 return;
1319
1320 for_each_pmc_rtnl(in_dev, im) {
1321 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001322 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001323 RCU_INIT_POINTER(mc_hash[hash], im);
1324 }
1325
1326 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1327}
1328
1329static void ip_mc_hash_remove(struct in_device *in_dev,
1330 struct ip_mc_list *im)
1331{
1332 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1333 struct ip_mc_list *aux;
1334
1335 if (!mc_hash)
1336 return;
1337 mc_hash += ip_mc_hash(im);
1338 while ((aux = rtnl_dereference(*mc_hash)) != im)
1339 mc_hash = &aux->next_hash;
1340 *mc_hash = im->next_hash;
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344/*
1345 * A socket has joined a multicast group on device dev.
1346 */
1347
Al Viro8f935bb2006-09-27 18:30:07 -07001348void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct ip_mc_list *im;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001351#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001352 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 ASSERT_RTNL();
1356
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001357 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (im->multiaddr == addr) {
1359 im->users++;
1360 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1361 goto out;
1362 }
1363 }
1364
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001365 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!im)
1367 goto out;
1368
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001369 im->users = 1;
1370 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001372 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /* initial mode is (EX, empty) */
1374 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 atomic_set(&im->refcnt, 1);
1377 spin_lock_init(&im->lock);
1378#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301379 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001380 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001382
1383 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001384 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001385 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001386
Eric Dumazete9897072013-06-07 08:48:57 -07001387 ip_mc_hash_add(in_dev, im);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001390 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391#endif
1392 igmp_group_added(im);
1393 if (!in_dev->dead)
1394 ip_rt_multicast_event(in_dev);
1395out:
1396 return;
1397}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001398EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001400static int ip_mc_check_iphdr(struct sk_buff *skb)
1401{
1402 const struct iphdr *iph;
1403 unsigned int len;
1404 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1405
1406 if (!pskb_may_pull(skb, offset))
1407 return -EINVAL;
1408
1409 iph = ip_hdr(skb);
1410
1411 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1412 return -EINVAL;
1413
1414 offset += ip_hdrlen(skb) - sizeof(*iph);
1415
1416 if (!pskb_may_pull(skb, offset))
1417 return -EINVAL;
1418
1419 iph = ip_hdr(skb);
1420
1421 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1422 return -EINVAL;
1423
1424 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1425 if (skb->len < len || len < offset)
1426 return -EINVAL;
1427
1428 skb_set_transport_header(skb, offset);
1429
1430 return 0;
1431}
1432
1433static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1434{
1435 unsigned int len = skb_transport_offset(skb);
1436
1437 len += sizeof(struct igmpv3_report);
1438
1439 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1440}
1441
1442static int ip_mc_check_igmp_query(struct sk_buff *skb)
1443{
1444 unsigned int len = skb_transport_offset(skb);
1445
1446 len += sizeof(struct igmphdr);
1447 if (skb->len < len)
1448 return -EINVAL;
1449
1450 /* IGMPv{1,2}? */
1451 if (skb->len != len) {
1452 /* or IGMPv3? */
1453 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1454 if (skb->len < len || !pskb_may_pull(skb, len))
1455 return -EINVAL;
1456 }
1457
1458 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1459 * all-systems destination addresses (224.0.0.1) for general queries
1460 */
1461 if (!igmp_hdr(skb)->group &&
1462 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1463 return -EINVAL;
1464
1465 return 0;
1466}
1467
1468static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1469{
1470 switch (igmp_hdr(skb)->type) {
1471 case IGMP_HOST_LEAVE_MESSAGE:
1472 case IGMP_HOST_MEMBERSHIP_REPORT:
1473 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1474 /* fall through */
1475 return 0;
1476 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1477 return ip_mc_check_igmp_reportv3(skb);
1478 case IGMP_HOST_MEMBERSHIP_QUERY:
1479 return ip_mc_check_igmp_query(skb);
1480 default:
1481 return -ENOMSG;
1482 }
1483}
1484
1485static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1486{
1487 return skb_checksum_simple_validate(skb);
1488}
1489
1490static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1491
1492{
1493 struct sk_buff *skb_chk;
1494 unsigned int transport_len;
1495 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga5169932015-08-13 05:54:07 +02001496 int ret = -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001497
1498 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1499
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001500 skb_chk = skb_checksum_trimmed(skb, transport_len,
1501 ip_mc_validate_checksum);
1502 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02001503 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001504
Linus Lüssinga5169932015-08-13 05:54:07 +02001505 if (!pskb_may_pull(skb_chk, len))
1506 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001507
1508 ret = ip_mc_check_igmp_msg(skb_chk);
Linus Lüssinga5169932015-08-13 05:54:07 +02001509 if (ret)
1510 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001511
1512 if (skb_trimmed)
1513 *skb_trimmed = skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02001514 /* free now unneeded clone */
1515 else if (skb_chk != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001516 kfree_skb(skb_chk);
1517
Linus Lüssinga5169932015-08-13 05:54:07 +02001518 ret = 0;
1519
1520err:
1521 if (ret && skb_chk && skb_chk != skb)
1522 kfree_skb(skb_chk);
1523
1524 return ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001525}
1526
1527/**
1528 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1529 * @skb: the skb to validate
1530 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1531 *
1532 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001533 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001534 *
1535 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1536 * standard
1537 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1538 * -ENOMEM: A memory allocation failure happened.
1539 *
1540 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1541 * to NULL): After parsing an IGMP packet successfully it will point to
1542 * an skb which has its tail aligned to the IP packet end. This might
1543 * either be the originally provided skb or a trimmed, cloned version if
1544 * the skb frame had data beyond the IP packet. A cloned skb allows us
1545 * to leave the original skb and its full frame unchanged (which might be
1546 * desirable for layer 2 frame jugglers).
1547 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001548 * Caller needs to set the skb network header and free any returned skb if it
1549 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001550 */
1551int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1552{
1553 int ret = ip_mc_check_iphdr(skb);
1554
1555 if (ret < 0)
1556 return ret;
1557
1558 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1559 return -ENOMSG;
1560
1561 return __ip_mc_check_igmp(skb, skb_trimmed);
1562}
1563EXPORT_SYMBOL(ip_mc_check_igmp);
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001566 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001567 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001568static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001569{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001570#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001571 struct ip_mc_list *im;
1572 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001573 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001574
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001575 ASSERT_RTNL();
1576
1577 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001578 if (im->multiaddr == IGMP_ALL_HOSTS)
1579 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001580 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001581 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001582 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001583
Eric Dumazet866f3b22010-11-18 09:33:19 -08001584 /* a failover is happening and switches
1585 * must be notified immediately
1586 */
1587 if (IGMP_V1_SEEN(in_dev))
1588 type = IGMP_HOST_MEMBERSHIP_REPORT;
1589 else if (IGMP_V2_SEEN(in_dev))
1590 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1591 else
1592 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1593 igmp_send_report(in_dev, im, type);
1594 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001595#endif
1596}
1597
1598/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 * A socket has left a multicast group on device dev
1600 */
1601
Al Viro8f935bb2006-09-27 18:30:07 -07001602void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001604 struct ip_mc_list *i;
1605 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001608
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001609 for (ip = &in_dev->mc_list;
1610 (i = rtnl_dereference(*ip)) != NULL;
1611 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001612 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001614 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001615 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001616 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001618 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if (!in_dev->dead)
1621 ip_rt_multicast_event(in_dev);
1622
1623 ip_ma_put(i);
1624 return;
1625 }
1626 break;
1627 }
1628 }
1629}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001630EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Moni Shoua75c78502009-09-15 02:37:40 -07001632/* Device changing type */
1633
1634void ip_mc_unmap(struct in_device *in_dev)
1635{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001636 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001637
1638 ASSERT_RTNL();
1639
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001640 for_each_pmc_rtnl(in_dev, pmc)
1641 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001642}
1643
1644void ip_mc_remap(struct in_device *in_dev)
1645{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001646 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001647
1648 ASSERT_RTNL();
1649
Hangbin Liu24803f32016-11-14 16:16:28 +08001650 for_each_pmc_rtnl(in_dev, pmc) {
1651#ifdef CONFIG_IP_MULTICAST
1652 igmpv3_del_delrec(in_dev, pmc);
1653#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001654 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001655 }
Moni Shoua75c78502009-09-15 02:37:40 -07001656}
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658/* Device going down */
1659
1660void ip_mc_down(struct in_device *in_dev)
1661{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001662 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 ASSERT_RTNL();
1665
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001666 for_each_pmc_rtnl(in_dev, pmc)
1667 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669#ifdef CONFIG_IP_MULTICAST
1670 in_dev->mr_ifc_count = 0;
1671 if (del_timer(&in_dev->mr_ifc_timer))
1672 __in_dev_put(in_dev);
1673 in_dev->mr_gq_running = 0;
1674 if (del_timer(&in_dev->mr_gq_timer))
1675 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676#endif
1677
1678 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1679}
1680
1681void ip_mc_init_dev(struct in_device *in_dev)
1682{
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001683#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001684 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001685#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 ASSERT_RTNL();
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001689 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1690 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001691 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1692 (unsigned long)in_dev);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001693 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694#endif
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 spin_lock_init(&in_dev->mc_tomb_lock);
1697}
1698
1699/* Device going up */
1700
1701void ip_mc_up(struct in_device *in_dev)
1702{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001703 struct ip_mc_list *pmc;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001704#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001705 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001706#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 ASSERT_RTNL();
1709
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001710#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001711 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1714
Hangbin Liu24803f32016-11-14 16:16:28 +08001715 for_each_pmc_rtnl(in_dev, pmc) {
1716#ifdef CONFIG_IP_MULTICAST
1717 igmpv3_del_delrec(in_dev, pmc);
1718#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001719 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
1723/*
1724 * Device is about to be destroyed: clean up.
1725 */
1726
1727void ip_mc_destroy_dev(struct in_device *in_dev)
1728{
1729 struct ip_mc_list *i;
1730
1731 ASSERT_RTNL();
1732
1733 /* Deactivate timers */
1734 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001735#ifdef CONFIG_IP_MULTICAST
1736 igmpv3_clear_delrec(in_dev);
1737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001739 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1740 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001741 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001746/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001747static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct net_device *dev = NULL;
1750 struct in_device *idev = NULL;
1751
1752 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001753 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return idev;
1755 }
1756 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001757 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (!dev)
1759 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761
David S. Millerb23dd4f2011-03-02 14:31:35 -08001762 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001763 struct rtable *rt = ip_route_output(net,
1764 imr->imr_multiaddr.s_addr,
1765 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001766 if (!IS_ERR(rt)) {
1767 dev = rt->dst.dev;
1768 ip_rt_put(rt);
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771 if (dev) {
1772 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001773 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775 return idev;
1776}
1777
1778/*
1779 * Join a socket to a group
1780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001783 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct ip_sf_list *psf, *psf_prev;
1786 int rv = 0;
1787
1788 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001789 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (psf->sf_inaddr == *psfsrc)
1791 break;
1792 psf_prev = psf;
1793 }
1794 if (!psf || psf->sf_count[sfmode] == 0) {
1795 /* source filter not found, or count wrong => bug */
1796 return -ESRCH;
1797 }
1798 psf->sf_count[sfmode]--;
1799 if (psf->sf_count[sfmode] == 0) {
1800 ip_rt_multicast_event(pmc->interface);
1801 }
1802 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1803#ifdef CONFIG_IP_MULTICAST
1804 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001805 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806#endif
1807
1808 /* no more filters for this source */
1809 if (psf_prev)
1810 psf_prev->sf_next = psf->sf_next;
1811 else
1812 pmc->sources = psf->sf_next;
1813#ifdef CONFIG_IP_MULTICAST
1814 if (psf->sf_oldin &&
1815 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001816 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 psf->sf_next = pmc->tomb;
1818 pmc->tomb = psf;
1819 rv = 1;
1820 } else
1821#endif
1822 kfree(psf);
1823 }
1824 return rv;
1825}
1826
1827#ifndef CONFIG_IP_MULTICAST
1828#define igmp_ifc_event(x) do { } while (0)
1829#endif
1830
Al Viro8f935bb2006-09-27 18:30:07 -07001831static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1832 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 struct ip_mc_list *pmc;
1835 int changerec = 0;
1836 int i, err;
1837
1838 if (!in_dev)
1839 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001840 rcu_read_lock();
1841 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (*pmca == pmc->multiaddr)
1843 break;
1844 }
1845 if (!pmc) {
1846 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001847 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return -ESRCH;
1849 }
1850 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001851 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852#ifdef CONFIG_IP_MULTICAST
1853 sf_markstate(pmc);
1854#endif
1855 if (!delta) {
1856 err = -EINVAL;
1857 if (!pmc->sfcount[sfmode])
1858 goto out_unlock;
1859 pmc->sfcount[sfmode]--;
1860 }
1861 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001862 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1864
1865 changerec |= rv > 0;
1866 if (!err && rv < 0)
1867 err = rv;
1868 }
1869 if (pmc->sfmode == MCAST_EXCLUDE &&
1870 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1871 pmc->sfcount[MCAST_INCLUDE]) {
1872#ifdef CONFIG_IP_MULTICAST
1873 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001874 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875#endif
1876
1877 /* filter mode change */
1878 pmc->sfmode = MCAST_INCLUDE;
1879#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001880 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001882 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 psf->sf_crcount = 0;
1884 igmp_ifc_event(pmc->interface);
1885 } else if (sf_setstate(pmc) || changerec) {
1886 igmp_ifc_event(pmc->interface);
1887#endif
1888 }
1889out_unlock:
1890 spin_unlock_bh(&pmc->lock);
1891 return err;
1892}
1893
1894/*
1895 * Add multicast single-source filter to the interface list
1896 */
1897static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001898 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 struct ip_sf_list *psf, *psf_prev;
1901
1902 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001903 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (psf->sf_inaddr == *psfsrc)
1905 break;
1906 psf_prev = psf;
1907 }
1908 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001909 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (!psf)
1911 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 psf->sf_inaddr = *psfsrc;
1913 if (psf_prev) {
1914 psf_prev->sf_next = psf;
1915 } else
1916 pmc->sources = psf;
1917 }
1918 psf->sf_count[sfmode]++;
1919 if (psf->sf_count[sfmode] == 1) {
1920 ip_rt_multicast_event(pmc->interface);
1921 }
1922 return 0;
1923}
1924
1925#ifdef CONFIG_IP_MULTICAST
1926static void sf_markstate(struct ip_mc_list *pmc)
1927{
1928 struct ip_sf_list *psf;
1929 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1930
Weilong Chenc71151f2013-12-23 14:37:29 +08001931 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (pmc->sfcount[MCAST_EXCLUDE]) {
1933 psf->sf_oldin = mca_xcount ==
1934 psf->sf_count[MCAST_EXCLUDE] &&
1935 !psf->sf_count[MCAST_INCLUDE];
1936 } else
1937 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1938}
1939
1940static int sf_setstate(struct ip_mc_list *pmc)
1941{
David L Stevensad125832006-01-18 14:20:56 -08001942 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1944 int qrv = pmc->interface->mr_qrv;
1945 int new_in, rv;
1946
1947 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001948 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (pmc->sfcount[MCAST_EXCLUDE]) {
1950 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1951 !psf->sf_count[MCAST_INCLUDE];
1952 } else
1953 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001954 if (new_in) {
1955 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001956 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001957
Weilong Chenc71151f2013-12-23 14:37:29 +08001958 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001959 if (dpsf->sf_inaddr == psf->sf_inaddr)
1960 break;
1961 prev = dpsf;
1962 }
1963 if (dpsf) {
1964 if (prev)
1965 prev->sf_next = dpsf->sf_next;
1966 else
1967 pmc->tomb = dpsf->sf_next;
1968 kfree(dpsf);
1969 }
1970 psf->sf_crcount = qrv;
1971 rv++;
1972 }
1973 } else if (psf->sf_oldin) {
1974
1975 psf->sf_crcount = 0;
1976 /*
1977 * add or update "delete" records if an active filter
1978 * is now inactive
1979 */
Weilong Chenc71151f2013-12-23 14:37:29 +08001980 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08001981 if (dpsf->sf_inaddr == psf->sf_inaddr)
1982 break;
1983 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001984 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001985 if (!dpsf)
1986 continue;
1987 *dpsf = *psf;
1988 /* pmc->lock held by callers */
1989 dpsf->sf_next = pmc->tomb;
1990 pmc->tomb = dpsf;
1991 }
1992 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 rv++;
1994 }
1995 }
1996 return rv;
1997}
1998#endif
1999
2000/*
2001 * Add multicast source filter list to the interface list
2002 */
Al Viro8f935bb2006-09-27 18:30:07 -07002003static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2004 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 struct ip_mc_list *pmc;
2007 int isexclude;
2008 int i, err;
2009
2010 if (!in_dev)
2011 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002012 rcu_read_lock();
2013 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (*pmca == pmc->multiaddr)
2015 break;
2016 }
2017 if (!pmc) {
2018 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002019 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return -ESRCH;
2021 }
2022 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002023 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025#ifdef CONFIG_IP_MULTICAST
2026 sf_markstate(pmc);
2027#endif
2028 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2029 if (!delta)
2030 pmc->sfcount[sfmode]++;
2031 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002032 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002033 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (err)
2035 break;
2036 }
2037 if (err) {
2038 int j;
2039
Jun Zhao685f94e62011-11-22 17:19:03 +00002040 if (!delta)
2041 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002042 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002043 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2045#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002047 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002048 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049#endif
2050
2051 /* filter mode change */
2052 if (pmc->sfcount[MCAST_EXCLUDE])
2053 pmc->sfmode = MCAST_EXCLUDE;
2054 else if (pmc->sfcount[MCAST_INCLUDE])
2055 pmc->sfmode = MCAST_INCLUDE;
2056#ifdef CONFIG_IP_MULTICAST
2057 /* else no filters; keep old mode for reports */
2058
Nikolay Borisov165094a2016-02-08 23:29:24 +02002059 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002061 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 psf->sf_crcount = 0;
2063 igmp_ifc_event(in_dev);
2064 } else if (sf_setstate(pmc)) {
2065 igmp_ifc_event(in_dev);
2066#endif
2067 }
2068 spin_unlock_bh(&pmc->lock);
2069 return err;
2070}
2071
2072static void ip_mc_clear_src(struct ip_mc_list *pmc)
2073{
2074 struct ip_sf_list *psf, *nextpsf;
2075
Weilong Chenc71151f2013-12-23 14:37:29 +08002076 for (psf = pmc->tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 nextpsf = psf->sf_next;
2078 kfree(psf);
2079 }
2080 pmc->tomb = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08002081 for (psf = pmc->sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 nextpsf = psf->sf_next;
2083 kfree(psf);
2084 }
2085 pmc->sources = NULL;
2086 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002087 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 pmc->sfcount[MCAST_EXCLUDE] = 1;
2089}
2090
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002091/* Join a multicast group
2092 */
2093
2094int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Al Viro8f935bb2006-09-27 18:30:07 -07002096 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002097 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct in_device *in_dev;
2099 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002100 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002101 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002103 int err;
2104
2105 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Joe Perchesf97c1e02007-12-16 13:45:43 -08002107 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return -EINVAL;
2109
Daniel Lezcano877aced2008-08-13 16:15:57 -07002110 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 err = -ENODEV;
2114 goto done;
2115 }
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002118 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002119 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002120 if (i->multi.imr_multiaddr.s_addr == addr &&
2121 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 count++;
2124 }
2125 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002126 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002128 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002129 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002130 goto done;
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002133 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 iml->sflist = NULL;
2135 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002136 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return err;
2141}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002142EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2145 struct in_device *in_dev)
2146{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002147 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 int err;
2149
Ian Morris51456b22015-04-03 09:17:26 +01002150 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /* any-source empty exclude case */
2152 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2153 iml->sfmode, 0, NULL, 0);
2154 }
2155 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002156 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002157 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002158 /* decrease mem now to avoid the memleak warning */
2159 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002160 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return err;
2162}
2163
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002164int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002167 struct ip_mc_socklist *iml;
2168 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002169 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002170 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002171 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002172 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002173 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Eric Dumazet959d10f2015-02-17 03:19:24 -08002175 ASSERT_RTNL();
2176
Daniel Lezcano877aced2008-08-13 16:15:57 -07002177 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002178 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002179 ret = -ENODEV;
2180 goto out;
2181 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002182 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002183 for (imlp = &inet->mc_list;
2184 (iml = rtnl_dereference(*imlp)) != NULL;
2185 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002186 if (iml->multi.imr_multiaddr.s_addr != group)
2187 continue;
2188 if (ifindex) {
2189 if (iml->multi.imr_ifindex != ifindex)
2190 continue;
2191 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2192 iml->multi.imr_address.s_addr)
2193 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
David L Stevensacd6e002006-08-17 16:27:39 -07002195 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002197 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002198
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002199 if (in_dev)
2200 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002201
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002202 /* decrease mem now to avoid the memleak warning */
2203 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002204 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
dingtianhong52ad3532014-07-02 13:50:48 +08002207out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002208 return ret;
2209}
stephen hemminger193ba922012-10-01 12:32:34 +00002210EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212int ip_mc_source(int add, int omode, struct sock *sk, struct
2213 ip_mreq_source *mreqs, int ifindex)
2214{
2215 int err;
2216 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002217 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct ip_mc_socklist *pmc;
2219 struct in_device *in_dev = NULL;
2220 struct inet_sock *inet = inet_sk(sk);
2221 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002222 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002223 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 int i, j, rv;
2225
Joe Perchesf97c1e02007-12-16 13:45:43 -08002226 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return -EINVAL;
2228
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002229 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2232 imr.imr_address.s_addr = mreqs->imr_interface;
2233 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002234 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 if (!in_dev) {
2237 err = -ENODEV;
2238 goto done;
2239 }
2240 err = -EADDRNOTAVAIL;
2241
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002242 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002243 if ((pmc->multi.imr_multiaddr.s_addr ==
2244 imr.imr_multiaddr.s_addr) &&
2245 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 break;
2247 }
David L Stevens917f2f12005-07-08 17:45:16 -07002248 if (!pmc) { /* must have a prior join */
2249 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /* if a source filter was set, must be the same mode as before */
2253 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002254 if (pmc->sfmode != omode) {
2255 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 } else if (pmc->sfmode != omode) {
2259 /* allow mode switches for empty-set filters */
2260 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002261 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 NULL, 0);
2263 pmc->sfmode = omode;
2264 }
2265
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002266 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (!add) {
2268 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002269 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002271 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002273 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (rv == 0)
2275 break;
2276 }
2277 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002278 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
David L Stevens8cdaaa12005-07-08 17:39:23 -07002280 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2281 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2282 leavegroup = 1;
2283 goto done;
2284 }
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002287 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 &mreqs->imr_sourceaddr, 1);
2289
Weilong Chenc71151f2013-12-23 14:37:29 +08002290 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 psl->sl_addr[j-1] = psl->sl_addr[j];
2292 psl->sl_count--;
2293 err = 0;
2294 goto done;
2295 }
2296 /* else, add a new source to the filter */
2297
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002298 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 err = -ENOBUFS;
2300 goto done;
2301 }
2302 if (!psl || psl->sl_count == psl->sl_max) {
2303 struct ip_sf_socklist *newpsl;
2304 int count = IP_SFBLOCK;
2305
2306 if (psl)
2307 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002308 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (!newpsl) {
2310 err = -ENOBUFS;
2311 goto done;
2312 }
2313 newpsl->sl_max = count;
2314 newpsl->sl_count = count - IP_SFBLOCK;
2315 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002316 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002318 /* decrease mem now to avoid the memleak warning */
2319 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002320 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002322 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002323 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002326 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002328 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (rv == 0)
2330 break;
2331 }
2332 if (rv == 0) /* address already there is an error */
2333 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002334 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 psl->sl_addr[j+1] = psl->sl_addr[j];
2336 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2337 psl->sl_count++;
2338 err = 0;
2339 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002340 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 &mreqs->imr_sourceaddr, 1);
2342done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002343 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002344 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return err;
2346}
2347
2348int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2349{
David L Stevens9951f032005-07-08 17:47:28 -07002350 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002352 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 struct ip_mc_socklist *pmc;
2354 struct in_device *in_dev;
2355 struct inet_sock *inet = inet_sk(sk);
2356 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002357 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002358 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Joe Perchesf97c1e02007-12-16 13:45:43 -08002360 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return -EINVAL;
2362 if (msf->imsf_fmode != MCAST_INCLUDE &&
2363 msf->imsf_fmode != MCAST_EXCLUDE)
2364 return -EINVAL;
2365
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002366 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2369 imr.imr_address.s_addr = msf->imsf_interface;
2370 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002371 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 if (!in_dev) {
2374 err = -ENODEV;
2375 goto done;
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
David L Stevens9951f032005-07-08 17:47:28 -07002378 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2379 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2380 leavegroup = 1;
2381 goto done;
2382 }
2383
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002384 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2386 pmc->multi.imr_ifindex == imr.imr_ifindex)
2387 break;
2388 }
David L Stevens917f2f12005-07-08 17:45:16 -07002389 if (!pmc) { /* must have a prior join */
2390 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002394 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2395 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (!newpsl) {
2397 err = -ENOBUFS;
2398 goto done;
2399 }
2400 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2401 memcpy(newpsl->sl_addr, msf->imsf_slist,
2402 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2403 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2404 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2405 if (err) {
2406 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2407 goto done;
2408 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002409 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002411 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2412 msf->imsf_fmode, 0, NULL, 0);
2413 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002414 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (psl) {
2416 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2417 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002418 /* decrease mem now to avoid the memleak warning */
2419 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002420 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 } else
2422 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2423 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002424 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002426 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427done:
David L Stevens9951f032005-07-08 17:47:28 -07002428 if (leavegroup)
2429 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 return err;
2431}
2432
2433int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2434 struct ip_msfilter __user *optval, int __user *optlen)
2435{
2436 int err, len, count, copycount;
2437 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002438 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct ip_mc_socklist *pmc;
2440 struct in_device *in_dev;
2441 struct inet_sock *inet = inet_sk(sk);
2442 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002443 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
WANG Cong87e9f032015-11-03 15:41:16 -08002445 ASSERT_RTNL();
2446
Joe Perchesf97c1e02007-12-16 13:45:43 -08002447 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return -EINVAL;
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2451 imr.imr_address.s_addr = msf->imsf_interface;
2452 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002453 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 if (!in_dev) {
2456 err = -ENODEV;
2457 goto done;
2458 }
2459 err = -EADDRNOTAVAIL;
2460
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002461 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2463 pmc->multi.imr_ifindex == imr.imr_ifindex)
2464 break;
2465 }
2466 if (!pmc) /* must have a prior join */
2467 goto done;
2468 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002469 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (!psl) {
2471 len = 0;
2472 count = 0;
2473 } else {
2474 count = psl->sl_count;
2475 }
2476 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2477 len = copycount * sizeof(psl->sl_addr[0]);
2478 msf->imsf_numsrc = count;
2479 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2480 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2481 return -EFAULT;
2482 }
2483 if (len &&
2484 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2485 return -EFAULT;
2486 return 0;
2487done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return err;
2489}
2490
2491int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2492 struct group_filter __user *optval, int __user *optlen)
2493{
2494 int err, i, count, copycount;
2495 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002496 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 struct ip_mc_socklist *pmc;
2498 struct inet_sock *inet = inet_sk(sk);
2499 struct ip_sf_socklist *psl;
2500
WANG Cong87e9f032015-11-03 15:41:16 -08002501 ASSERT_RTNL();
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 psin = (struct sockaddr_in *)&gsf->gf_group;
2504 if (psin->sin_family != AF_INET)
2505 return -EINVAL;
2506 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002507 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return -EINVAL;
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 err = -EADDRNOTAVAIL;
2511
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002512 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2514 pmc->multi.imr_ifindex == gsf->gf_interface)
2515 break;
2516 }
2517 if (!pmc) /* must have a prior join */
2518 goto done;
2519 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002520 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 count = psl ? psl->sl_count : 0;
2522 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2523 gsf->gf_numsrc = count;
2524 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2525 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2526 return -EFAULT;
2527 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002528 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct sockaddr_storage ss;
2530
2531 psin = (struct sockaddr_in *)&ss;
2532 memset(&ss, 0, sizeof(ss));
2533 psin->sin_family = AF_INET;
2534 psin->sin_addr.s_addr = psl->sl_addr[i];
2535 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2536 return -EFAULT;
2537 }
2538 return 0;
2539done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return err;
2541}
2542
2543/*
2544 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2545 */
Al Viroc0cda062006-09-27 18:31:10 -07002546int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548 struct inet_sock *inet = inet_sk(sk);
2549 struct ip_mc_socklist *pmc;
2550 struct ip_sf_socklist *psl;
2551 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002552 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002554 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002555 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002558 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002559 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2561 pmc->multi.imr_ifindex == dif)
2562 break;
2563 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002564 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002566 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002567 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002568 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002570 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Weilong Chenc71151f2013-12-23 14:37:29 +08002572 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (psl->sl_addr[i] == rmt_addr)
2574 break;
2575 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002576 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002578 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002580 goto unlock;
2581 ret = 1;
2582unlock:
2583 rcu_read_unlock();
2584out:
2585 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
2588/*
2589 * A socket is closing.
2590 */
2591
2592void ip_mc_drop_socket(struct sock *sk)
2593{
2594 struct inet_sock *inet = inet_sk(sk);
2595 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002596 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Ian Morris51456b22015-04-03 09:17:26 +01002598 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return;
2600
2601 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002602 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002605 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002606 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002607 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002608 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002610 /* decrease mem now to avoid the memleak warning */
2611 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002612 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614 rtnl_unlock();
2615}
2616
David S. Millerdbdd9a52011-03-10 16:34:38 -08002617/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002618int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
2620 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002621 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct ip_sf_list *psf;
2623 int rv = 0;
2624
Eric Dumazete9897072013-06-07 08:48:57 -07002625 mc_hash = rcu_dereference(in_dev->mc_hash);
2626 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002627 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002628
2629 for (im = rcu_dereference(mc_hash[hash]);
2630 im != NULL;
2631 im = rcu_dereference(im->next_hash)) {
2632 if (im->multiaddr == mc_addr)
2633 break;
2634 }
2635 } else {
2636 for_each_pmc_rcu(in_dev, im) {
2637 if (im->multiaddr == mc_addr)
2638 break;
2639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
2641 if (im && proto == IPPROTO_IGMP) {
2642 rv = 1;
2643 } else if (im) {
2644 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002645 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (psf->sf_inaddr == src_addr)
2647 break;
2648 }
2649 if (psf)
2650 rv = psf->sf_count[MCAST_INCLUDE] ||
2651 psf->sf_count[MCAST_EXCLUDE] !=
2652 im->sfcount[MCAST_EXCLUDE];
2653 else
2654 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2655 } else
2656 rv = 1; /* unspecified source; tentatively allow */
2657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return rv;
2659}
2660
2661#if defined(CONFIG_PROC_FS)
2662struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002663 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 struct net_device *dev;
2665 struct in_device *in_dev;
2666};
2667
2668#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2669
2670static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2671{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002672 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct ip_mc_list *im = NULL;
2674 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2675
Pavel Emelianov7562f872007-05-03 15:13:45 -07002676 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002677 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002679
2680 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (!in_dev)
2682 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002683 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (im) {
2685 state->in_dev = in_dev;
2686 break;
2687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
2689 return im;
2690}
2691
2692static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2693{
2694 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002695
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002696 im = rcu_dereference(im->next_rcu);
2697 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002698 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (!state->dev) {
2700 state->in_dev = NULL;
2701 break;
2702 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002703 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (!state->in_dev)
2705 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002706 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
2708 return im;
2709}
2710
2711static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2712{
2713 struct ip_mc_list *im = igmp_mc_get_first(seq);
2714 if (im)
2715 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2716 --pos;
2717 return pos ? NULL : im;
2718}
2719
2720static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002721 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
stephen hemminger61fbab72009-11-10 07:54:55 +00002723 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2725}
2726
2727static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2728{
2729 struct ip_mc_list *im;
2730 if (v == SEQ_START_TOKEN)
2731 im = igmp_mc_get_first(seq);
2732 else
2733 im = igmp_mc_get_next(seq, v);
2734 ++*pos;
2735 return im;
2736}
2737
2738static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002739 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
2741 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002742
2743 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002745 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
2748static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2749{
2750 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002751 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2753 else {
2754 struct ip_mc_list *im = (struct ip_mc_list *)v;
2755 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2756 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002757 long delta;
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759#ifdef CONFIG_IP_MULTICAST
2760 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2761 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2762 "V3";
2763#else
2764 querier = "NONE";
2765#endif
2766
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002767 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002769 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
2771
Eric Dumazeta399a802012-08-08 21:13:53 +00002772 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002774 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002776 im->tm_running,
2777 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 im->reporter);
2779 }
2780 return 0;
2781}
2782
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002783static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 .start = igmp_mc_seq_start,
2785 .next = igmp_mc_seq_next,
2786 .stop = igmp_mc_seq_stop,
2787 .show = igmp_mc_seq_show,
2788};
2789
2790static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2791{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002792 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002793 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
Arjan van de Ven9a321442007-02-12 00:55:35 -08002796static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 .owner = THIS_MODULE,
2798 .open = igmp_mc_seq_open,
2799 .read = seq_read,
2800 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002801 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802};
2803
2804struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002805 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 struct net_device *dev;
2807 struct in_device *idev;
2808 struct ip_mc_list *im;
2809};
2810
2811#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2812
2813static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2814{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002815 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct ip_sf_list *psf = NULL;
2817 struct ip_mc_list *im = NULL;
2818 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2819
Pavel Emelianov7562f872007-05-03 15:13:45 -07002820 state->idev = NULL;
2821 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002822 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002824 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002825 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002827 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002828 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 spin_lock_bh(&im->lock);
2830 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002831 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 state->im = im;
2833 state->idev = idev;
2834 break;
2835 }
2836 spin_unlock_bh(&im->lock);
2837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
2839 return psf;
2840}
2841
2842static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2843{
2844 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2845
2846 psf = psf->sf_next;
2847 while (!psf) {
2848 spin_unlock_bh(&state->im->lock);
2849 state->im = state->im->next;
2850 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002851 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (!state->dev) {
2853 state->idev = NULL;
2854 goto out;
2855 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002856 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (!state->idev)
2858 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002859 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
2861 if (!state->im)
2862 break;
2863 spin_lock_bh(&state->im->lock);
2864 psf = state->im->sources;
2865 }
2866out:
2867 return psf;
2868}
2869
2870static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2871{
2872 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2873 if (psf)
2874 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2875 --pos;
2876 return pos ? NULL : psf;
2877}
2878
2879static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002880 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
stephen hemminger61fbab72009-11-10 07:54:55 +00002882 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2884}
2885
2886static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2887{
2888 struct ip_sf_list *psf;
2889 if (v == SEQ_START_TOKEN)
2890 psf = igmp_mcf_get_first(seq);
2891 else
2892 psf = igmp_mcf_get_next(seq, v);
2893 ++*pos;
2894 return psf;
2895}
2896
2897static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002898 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002901 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 spin_unlock_bh(&state->im->lock);
2903 state->im = NULL;
2904 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002905 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002907 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
2909
2910static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2911{
2912 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2913 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2914
2915 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002916 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 } else {
2918 seq_printf(seq,
2919 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002920 "0x%08x %6lu %6lu\n",
2921 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 ntohl(state->im->multiaddr),
2923 ntohl(psf->sf_inaddr),
2924 psf->sf_count[MCAST_INCLUDE],
2925 psf->sf_count[MCAST_EXCLUDE]);
2926 }
2927 return 0;
2928}
2929
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002930static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 .start = igmp_mcf_seq_start,
2932 .next = igmp_mcf_seq_next,
2933 .stop = igmp_mcf_seq_stop,
2934 .show = igmp_mcf_seq_show,
2935};
2936
2937static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2938{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002939 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002940 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941}
2942
Arjan van de Ven9a321442007-02-12 00:55:35 -08002943static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 .owner = THIS_MODULE,
2945 .open = igmp_mcf_seq_open,
2946 .read = seq_read,
2947 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002948 .release = seq_release_net,
2949};
2950
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002951static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002952{
2953 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002954 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002955
Gao fengd4beaa62013-02-18 01:34:54 +00002956 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002957 if (!pde)
2958 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002959 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2960 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002961 if (!pde)
2962 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08002963 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2964 SOCK_DGRAM, 0, net);
2965 if (err < 0) {
2966 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2967 err);
2968 goto out_sock;
2969 }
2970
Nikolay Borisovdcd87992016-02-15 12:11:28 +02002971 /* Sysctl initialization */
2972 net->ipv4.sysctl_igmp_max_memberships = 20;
2973 net->ipv4.sysctl_igmp_max_msf = 10;
2974 /* IGMP reports for link-local multicast groups are enabled by default */
2975 net->ipv4.sysctl_igmp_llm_reports = 1;
2976 net->ipv4.sysctl_igmp_qrv = 2;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002977 return 0;
2978
Madhu Challa93a714d2015-02-25 09:58:35 -08002979out_sock:
2980 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002981out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002982 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002983out_igmp:
2984 return -ENOMEM;
2985}
2986
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002987static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002988{
Gao fengece31ff2013-02-18 01:34:56 +00002989 remove_proc_entry("mcfilter", net->proc_net);
2990 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08002991 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002992}
2993
2994static struct pernet_operations igmp_net_ops = {
2995 .init = igmp_net_init,
2996 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997};
WANG Cong72c1d3b2014-01-10 16:09:45 -08002998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003000static int igmp_netdev_event(struct notifier_block *this,
3001 unsigned long event, void *ptr)
3002{
3003 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3004 struct in_device *in_dev;
3005
3006 switch (event) {
3007 case NETDEV_RESEND_IGMP:
3008 in_dev = __in_dev_get_rtnl(dev);
3009 if (in_dev)
3010 ip_mc_rejoin_groups(in_dev);
3011 break;
3012 default:
3013 break;
3014 }
3015 return NOTIFY_DONE;
3016}
3017
3018static struct notifier_block igmp_notifier = {
3019 .notifier_call = igmp_netdev_event,
3020};
3021
WANG Cong72c1d3b2014-01-10 16:09:45 -08003022int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003024#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003025 int err;
3026
3027 err = register_pernet_subsys(&igmp_net_ops);
3028 if (err)
3029 return err;
3030 err = register_netdevice_notifier(&igmp_notifier);
3031 if (err)
3032 goto reg_notif_fail;
3033 return 0;
3034
3035reg_notif_fail:
3036 unregister_pernet_subsys(&igmp_net_ops);
3037 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003038#else
3039 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003041}