blob: 7c95335bf85eee105dfe65bfcf9a8448dc83305f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
3 *
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
6 *
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Authors:
Alan Cox113aa832008-10-13 19:01:08 -070012 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * Fixes:
20 *
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
23 * functions.
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
32 * points.
33 *
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090036 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050 * memberships but was being deleted,
51 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * from %p with timer not initialized\n"
53 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090054 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
58 * true (960216).
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
70 * Vinay Kulkarni
71 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/types.h>
77#include <linux/kernel.h>
78#include <linux/jiffies.h>
79#include <linux/string.h>
80#include <linux/socket.h>
81#include <linux/sockios.h>
82#include <linux/in.h>
83#include <linux/inet.h>
84#include <linux/netdevice.h>
85#include <linux/skbuff.h>
86#include <linux/inetdevice.h>
87#include <linux/igmp.h>
88#include <linux/if_arp.h>
89#include <linux/rtnetlink.h>
90#include <linux/times.h>
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +020091#include <linux/pkt_sched.h>
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);
Al Viro63007722006-09-27 18:31:32 -0700165static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 in_dev->mr_gq_running = 1;
224 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
225 in_dev_hold(in_dev);
226}
227
228static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
229{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500230 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
233 in_dev_hold(in_dev);
234}
235
236static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
237{
238 spin_lock_bh(&im->lock);
239 im->unsolicit_count = 0;
240 if (del_timer(&im->timer)) {
241 if ((long)(im->timer.expires-jiffies) < max_delay) {
242 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800243 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 spin_unlock_bh(&im->lock);
245 return;
246 }
247 atomic_dec(&im->refcnt);
248 }
249 igmp_start_timer(im, max_delay);
250 spin_unlock_bh(&im->lock);
251}
252
253
254/*
255 * Send an IGMP report.
256 */
257
258#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
259
260
261static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
262 int gdeleted, int sdeleted)
263{
264 switch (type) {
265 case IGMPV3_MODE_IS_INCLUDE:
266 case IGMPV3_MODE_IS_EXCLUDE:
267 if (gdeleted || sdeleted)
268 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800269 if (!(pmc->gsquery && !psf->sf_gsresp)) {
270 if (pmc->sfmode == MCAST_INCLUDE)
271 return 1;
272 /* don't include if this source is excluded
273 * in all filters
274 */
275 if (psf->sf_count[MCAST_INCLUDE])
276 return type == IGMPV3_MODE_IS_INCLUDE;
277 return pmc->sfcount[MCAST_EXCLUDE] ==
278 psf->sf_count[MCAST_EXCLUDE];
279 }
280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 case IGMPV3_CHANGE_TO_INCLUDE:
282 if (gdeleted || sdeleted)
283 return 0;
284 return psf->sf_count[MCAST_INCLUDE] != 0;
285 case IGMPV3_CHANGE_TO_EXCLUDE:
286 if (gdeleted || sdeleted)
287 return 0;
288 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
289 psf->sf_count[MCAST_INCLUDE])
290 return 0;
291 return pmc->sfcount[MCAST_EXCLUDE] ==
292 psf->sf_count[MCAST_EXCLUDE];
293 case IGMPV3_ALLOW_NEW_SOURCES:
294 if (gdeleted || !psf->sf_crcount)
295 return 0;
296 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
297 case IGMPV3_BLOCK_OLD_SOURCES:
298 if (pmc->sfmode == MCAST_INCLUDE)
299 return gdeleted || (psf->sf_crcount && sdeleted);
300 return psf->sf_crcount && !gdeleted && !sdeleted;
301 }
302 return 0;
303}
304
305static int
306igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
307{
308 struct ip_sf_list *psf;
309 int scount = 0;
310
Weilong Chenc71151f2013-12-23 14:37:29 +0800311 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
313 continue;
314 scount++;
315 }
316 return scount;
317}
318
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100319static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 struct sk_buff *skb;
322 struct rtable *rt;
323 struct iphdr *pip;
324 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700325 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700326 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000327 int hlen = LL_RESERVED_SPACE(dev);
328 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100329 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000331 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000332 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000333 GFP_ATOMIC | __GFP_NOWARN);
334 if (skb)
335 break;
336 size >>= 1;
337 if (size < 256)
338 return NULL;
339 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200340 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
David S. Miller31e45432011-05-03 20:25:42 -0700342 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500343 0, 0,
344 IPPROTO_IGMP, 0, dev->ifindex);
345 if (IS_ERR(rt)) {
346 kfree_skb(skb);
347 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Changli Gaod8d1f302010-06-10 23:31:35 -0700350 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 skb->dev = dev;
352
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100353 skb->reserved_tailroom = skb_end_offset(skb) -
354 min(mtu, skb_end_offset(skb));
Herbert Xu66088242011-11-18 02:20:04 +0000355 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300357 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700358 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300359 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 pip->version = 4;
362 pip->ihl = (sizeof(struct iphdr)+4)>>2;
363 pip->tos = 0xc0;
364 pip->frag_off = htons(IP_DF);
365 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700366 pip->daddr = fl4.daddr;
367 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 pip->protocol = IPPROTO_IGMP;
369 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100370 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000371 ((u8 *)&pip[1])[0] = IPOPT_RA;
372 ((u8 *)&pip[1])[1] = 4;
373 ((u8 *)&pip[1])[2] = 0;
374 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700376 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300377 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300378 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
380 pig->resv1 = 0;
381 pig->csum = 0;
382 pig->resv2 = 0;
383 pig->ngrec = 0;
384 return skb;
385}
386
387static int igmpv3_sendpack(struct sk_buff *skb)
388{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300389 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000390 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300392 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Eric W. Biederman33224b12015-10-07 16:48:46 -0500394 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
398{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800399 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
403 int type, struct igmpv3_grec **ppgr)
404{
405 struct net_device *dev = pmc->interface->dev;
406 struct igmpv3_report *pih;
407 struct igmpv3_grec *pgr;
408
409 if (!skb)
410 skb = igmpv3_newpack(dev, dev->mtu);
411 if (!skb)
412 return NULL;
413 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
414 pgr->grec_type = type;
415 pgr->grec_auxwords = 0;
416 pgr->grec_nsrcs = 0;
417 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300418 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 pih->ngrec = htons(ntohs(pih->ngrec)+1);
420 *ppgr = pgr;
421 return skb;
422}
423
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100424#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
427 int type, int gdeleted, int sdeleted)
428{
429 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200430 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct igmpv3_report *pih;
432 struct igmpv3_grec *pgr = NULL;
433 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800434 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (pmc->multiaddr == IGMP_ALL_HOSTS)
437 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200438 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100439 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
442 type == IGMPV3_MODE_IS_EXCLUDE;
443 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
444 type == IGMPV3_CHANGE_TO_EXCLUDE;
445
David L Stevensad125832006-01-18 14:20:56 -0800446 stotal = scount = 0;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
449
David L Stevensad125832006-01-18 14:20:56 -0800450 if (!*psf_list)
451 goto empty_source;
452
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300453 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* EX and TO_EX get a fresh packet, if needed */
456 if (truncate) {
457 if (pih && pih->ngrec &&
458 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
459 if (skb)
460 igmpv3_sendpack(skb);
461 skb = igmpv3_newpack(dev, dev->mtu);
462 }
463 }
464 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800466 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700467 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 psf_next = psf->sf_next;
470
471 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
472 psf_prev = psf;
473 continue;
474 }
475
476 /* clear marks on query responses */
477 if (isquery)
478 psf->sf_gsresp = 0;
479
Al Viro63007722006-09-27 18:31:32 -0700480 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 first*sizeof(struct igmpv3_grec)) {
482 if (truncate && !first)
483 break; /* truncate these */
484 if (pgr)
485 pgr->grec_nsrcs = htons(scount);
486 if (skb)
487 igmpv3_sendpack(skb);
488 skb = igmpv3_newpack(dev, dev->mtu);
489 first = 1;
490 scount = 0;
491 }
492 if (first) {
493 skb = add_grhead(skb, pmc, type, &pgr);
494 first = 0;
495 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800496 if (!skb)
497 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700498 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800500 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
502 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
503 psf->sf_crcount--;
504 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
505 if (psf_prev)
506 psf_prev->sf_next = psf->sf_next;
507 else
508 *psf_list = psf->sf_next;
509 kfree(psf);
510 continue;
511 }
512 }
513 psf_prev = psf;
514 }
David L Stevensad125832006-01-18 14:20:56 -0800515
516empty_source:
517 if (!stotal) {
518 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
519 type == IGMPV3_BLOCK_OLD_SOURCES)
520 return skb;
521 if (pmc->crcount || isquery) {
522 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800523 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800524 igmpv3_sendpack(skb);
525 skb = NULL; /* add_grhead will get a new one */
526 }
527 skb = add_grhead(skb, pmc, type, &pgr);
528 }
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (pgr)
531 pgr->grec_nsrcs = htons(scount);
532
533 if (isquery)
534 pmc->gsquery = 0; /* clear query state on report */
535 return skb;
536}
537
538static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
539{
540 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200541 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 int type;
543
544 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000545 rcu_read_lock();
546 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (pmc->multiaddr == IGMP_ALL_HOSTS)
548 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100549 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200550 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100551 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 spin_lock_bh(&pmc->lock);
553 if (pmc->sfcount[MCAST_EXCLUDE])
554 type = IGMPV3_MODE_IS_EXCLUDE;
555 else
556 type = IGMPV3_MODE_IS_INCLUDE;
557 skb = add_grec(skb, pmc, type, 0, 0);
558 spin_unlock_bh(&pmc->lock);
559 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000560 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else {
562 spin_lock_bh(&pmc->lock);
563 if (pmc->sfcount[MCAST_EXCLUDE])
564 type = IGMPV3_MODE_IS_EXCLUDE;
565 else
566 type = IGMPV3_MODE_IS_INCLUDE;
567 skb = add_grec(skb, pmc, type, 0, 0);
568 spin_unlock_bh(&pmc->lock);
569 }
570 if (!skb)
571 return 0;
572 return igmpv3_sendpack(skb);
573}
574
575/*
576 * remove zero-count source records from a source filter list
577 */
578static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
579{
580 struct ip_sf_list *psf_prev, *psf_next, *psf;
581
582 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800583 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 psf_next = psf->sf_next;
585 if (psf->sf_crcount == 0) {
586 if (psf_prev)
587 psf_prev->sf_next = psf->sf_next;
588 else
589 *ppsf = psf->sf_next;
590 kfree(psf);
591 } else
592 psf_prev = psf;
593 }
594}
595
596static void igmpv3_send_cr(struct in_device *in_dev)
597{
598 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
599 struct sk_buff *skb = NULL;
600 int type, dtype;
601
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000602 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 spin_lock_bh(&in_dev->mc_tomb_lock);
604
605 /* deleted MCA's */
606 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800607 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 pmc_next = pmc->next;
609 if (pmc->sfmode == MCAST_INCLUDE) {
610 type = IGMPV3_BLOCK_OLD_SOURCES;
611 dtype = IGMPV3_BLOCK_OLD_SOURCES;
612 skb = add_grec(skb, pmc, type, 1, 0);
613 skb = add_grec(skb, pmc, dtype, 1, 1);
614 }
615 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (pmc->sfmode == MCAST_EXCLUDE) {
617 type = IGMPV3_CHANGE_TO_INCLUDE;
618 skb = add_grec(skb, pmc, type, 1, 0);
619 }
David L Stevensad125832006-01-18 14:20:56 -0800620 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (pmc->crcount == 0) {
622 igmpv3_clear_zeros(&pmc->tomb);
623 igmpv3_clear_zeros(&pmc->sources);
624 }
625 }
626 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
627 if (pmc_prev)
628 pmc_prev->next = pmc_next;
629 else
630 in_dev->mc_tomb = pmc_next;
631 in_dev_put(pmc->interface);
632 kfree(pmc);
633 } else
634 pmc_prev = pmc;
635 }
636 spin_unlock_bh(&in_dev->mc_tomb_lock);
637
638 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000639 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 spin_lock_bh(&pmc->lock);
641 if (pmc->sfcount[MCAST_EXCLUDE]) {
642 type = IGMPV3_BLOCK_OLD_SOURCES;
643 dtype = IGMPV3_ALLOW_NEW_SOURCES;
644 } else {
645 type = IGMPV3_ALLOW_NEW_SOURCES;
646 dtype = IGMPV3_BLOCK_OLD_SOURCES;
647 }
648 skb = add_grec(skb, pmc, type, 0, 0);
649 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
650
651 /* filter mode changes */
652 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (pmc->sfmode == MCAST_EXCLUDE)
654 type = IGMPV3_CHANGE_TO_EXCLUDE;
655 else
656 type = IGMPV3_CHANGE_TO_INCLUDE;
657 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800658 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 spin_unlock_bh(&pmc->lock);
661 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000662 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (!skb)
665 return;
666 (void) igmpv3_sendpack(skb);
667}
668
669static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
670 int type)
671{
672 struct sk_buff *skb;
673 struct iphdr *iph;
674 struct igmphdr *ih;
675 struct rtable *rt;
676 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700677 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700678 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700679 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700680 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000681 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
684 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100685
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200686 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100687 return 0;
688
689 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 dst = IGMP_ALL_ROUTER;
691 else
692 dst = group;
693
David S. Miller31e45432011-05-03 20:25:42 -0700694 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500695 0, 0,
696 IPPROTO_IGMP, 0, dev->ifindex);
697 if (IS_ERR(rt))
698 return -1;
699
Herbert Xu66088242011-11-18 02:20:04 +0000700 hlen = LL_RESERVED_SPACE(dev);
701 tlen = dev->needed_tailroom;
702 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100703 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ip_rt_put(rt);
705 return -1;
706 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200707 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Changli Gaod8d1f302010-06-10 23:31:35 -0700709 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Herbert Xu66088242011-11-18 02:20:04 +0000711 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300713 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700714 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300715 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 iph->version = 4;
718 iph->ihl = (sizeof(struct iphdr)+4)>>2;
719 iph->tos = 0xc0;
720 iph->frag_off = htons(IP_DF);
721 iph->ttl = 1;
722 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700723 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100725 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000726 ((u8 *)&iph[1])[0] = IPOPT_RA;
727 ((u8 *)&iph[1])[1] = 4;
728 ((u8 *)&iph[1])[2] = 0;
729 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800732 ih->type = type;
733 ih->code = 0;
734 ih->csum = 0;
735 ih->group = group;
736 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Eric W. Biederman33224b12015-10-07 16:48:46 -0500738 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741static void igmp_gq_timer_expire(unsigned long data)
742{
743 struct in_device *in_dev = (struct in_device *)data;
744
745 in_dev->mr_gq_running = 0;
746 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700747 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750static void igmp_ifc_timer_expire(unsigned long data)
751{
752 struct in_device *in_dev = (struct in_device *)data;
753
754 igmpv3_send_cr(in_dev);
755 if (in_dev->mr_ifc_count) {
756 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100757 igmp_ifc_start_timer(in_dev,
758 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700760 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763static void igmp_ifc_event(struct in_device *in_dev)
764{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200765 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
767 return;
Nikolay Borisov165094a2016-02-08 23:29:24 +0200768 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 igmp_ifc_start_timer(in_dev, 1);
770}
771
772
773static void igmp_timer_expire(unsigned long data)
774{
Weilong Chenc71151f2013-12-23 14:37:29 +0800775 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct in_device *in_dev = im->interface;
777
778 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800779 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (im->unsolicit_count) {
782 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100783 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785 im->reporter = 1;
786 spin_unlock(&im->lock);
787
788 if (IGMP_V1_SEEN(in_dev))
789 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
790 else if (IGMP_V2_SEEN(in_dev))
791 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
792 else
793 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
794
795 ip_ma_put(im);
796}
797
David L Stevensad125832006-01-18 14:20:56 -0800798/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700799static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct ip_sf_list *psf;
802 int i, scount;
803
804 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800805 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (scount == nsrcs)
807 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800808 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800809 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000810 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800811 pmc->sfcount[MCAST_EXCLUDE] !=
812 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800813 break;
David L Stevensad125832006-01-18 14:20:56 -0800814 if (srcs[i] == psf->sf_inaddr) {
815 scount++;
816 break;
817 }
818 }
819 }
820 pmc->gsquery = 0;
821 if (scount == nsrcs) /* all sources excluded */
822 return 0;
823 return 1;
824}
825
Al Viroea4d9e72006-09-27 18:30:52 -0700826static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800827{
828 struct ip_sf_list *psf;
829 int i, scount;
830
831 if (pmc->sfmode == MCAST_EXCLUDE)
832 return igmp_xmarksources(pmc, nsrcs, srcs);
833
834 /* mark INCLUDE-mode sources */
835 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800836 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800837 if (scount == nsrcs)
838 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800839 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (srcs[i] == psf->sf_inaddr) {
841 psf->sf_gsresp = 1;
842 scount++;
843 break;
844 }
845 }
David L Stevensad125832006-01-18 14:20:56 -0800846 if (!scount) {
847 pmc->gsquery = 0;
848 return 0;
849 }
850 pmc->gsquery = 1;
851 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Eric Dumazetd679c532012-09-06 20:37:06 +0000854/* return true if packet was dropped */
855static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200858 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* Timers are only set for non-local groups */
861
862 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000863 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200864 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100865 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000867 rcu_read_lock();
868 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (im->multiaddr == group) {
870 igmp_stop_timer(im);
871 break;
872 }
873 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000874 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000875 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Eric Dumazetd679c532012-09-06 20:37:06 +0000878/* return true if packet was dropped */
879static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int len)
881{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300882 struct igmphdr *ih = igmp_hdr(skb);
883 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700885 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int max_delay;
887 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200888 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890
David Stevens5b7c8402010-09-30 14:29:40 +0000891 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (ih->code == 0) {
893 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900894
Fabian Frederick436f7c22014-11-04 20:52:14 +0100895 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 in_dev->mr_v1_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100897 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 group = 0;
899 } else {
900 /* v2 router present */
901 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
902 in_dev->mr_v2_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100903 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 /* cancel the interface change timer */
906 in_dev->mr_ifc_count = 0;
907 if (del_timer(&in_dev->mr_ifc_timer))
908 __in_dev_put(in_dev);
909 /* clear deleted report items */
910 igmpv3_clear_delrec(in_dev);
911 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000912 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000913 } else if (IGMP_V1_SEEN(in_dev)) {
914 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100915 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000916 group = 0;
917 } else if (IGMP_V2_SEEN(in_dev)) {
918 /* this is a v3 query with v2 queriers present;
919 * Interpretation of the max_delay code is problematic here.
920 * A real v2 host would use ih_code directly, while v3 has a
921 * different encoding. We use the v3 encoding as more likely
922 * to be intended in a v3 query.
923 */
924 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800925 if (!max_delay)
926 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 } else { /* v3 */
928 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000929 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900930
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300931 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900933 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700934 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000935 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300936 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
940 if (!max_delay)
941 max_delay = 1; /* can't mod w/ 0 */
942 in_dev->mr_maxdelay = max_delay;
943 if (ih3->qrv)
944 in_dev->mr_qrv = ih3->qrv;
945 if (!group) { /* general query */
946 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +0200947 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000949 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951 /* mark sources to include, if group & source-specific */
952 mark = ih3->nsrcs != 0;
953 }
954
955 /*
956 * - Start the timers in all of our membership records
957 * that the query applies to for the interface on
958 * which the query arrived excl. those that belong
959 * to a "local" group (224.0.0.X)
960 * - For timers already running check if they need to
961 * be reset.
962 * - Use the igmp->igmp_code field as the maximum
963 * delay possible
964 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000965 rcu_read_lock();
966 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800967 int changed;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (group && group != im->multiaddr)
970 continue;
971 if (im->multiaddr == IGMP_ALL_HOSTS)
972 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100973 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200974 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100975 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 spin_lock_bh(&im->lock);
977 if (im->tm_running)
978 im->gsquery = im->gsquery && mark;
979 else
980 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800981 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900982 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800984 if (changed)
985 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000987 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000988 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000991/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992int igmp_rcv(struct sk_buff *skb)
993{
994 /* This basically follows the spec line by line -- see RFC1112 */
995 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000996 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +0000998 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Ian Morris51456b22015-04-03 09:17:26 +01001000 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001001 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Herbert Xufb286bb2005-11-10 13:01:24 -08001003 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001004 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001005
Tom Herbertde08dc12014-05-07 16:52:10 -07001006 if (skb_checksum_simple_validate(skb))
1007 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001009 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 switch (ih->type) {
1011 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001012 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 case IGMP_HOST_MEMBERSHIP_REPORT:
1015 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001017 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
David Stevens24c69272005-12-02 20:32:59 -08001019 /* don't rely on MC router hearing unicast reports */
1020 if (skb->pkt_type == PACKET_MULTICAST ||
1021 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001022 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 break;
1024 case IGMP_PIM:
1025#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return pim_rcv_v1(skb);
1027#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001028 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 case IGMP_DVMRP:
1030 case IGMP_TRACE:
1031 case IGMP_HOST_LEAVE_MESSAGE:
1032 case IGMP_MTRACE:
1033 case IGMP_MTRACE_RESP:
1034 break;
1035 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001038
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001039drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001040 if (dropped)
1041 kfree_skb(skb);
1042 else
1043 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return 0;
1045}
1046
1047#endif
1048
1049
1050/*
1051 * Add a filter to a device
1052 */
1053
Al Viro63007722006-09-27 18:31:32 -07001054static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 char buf[MAX_ADDR_LEN];
1057 struct net_device *dev = in_dev->dev;
1058
1059 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1060 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001061 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 routine. Something sort of:
1063 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1064 --ANK
1065 */
1066 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001067 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070/*
1071 * Remove a filter from a device
1072 */
1073
Al Viro63007722006-09-27 18:31:32 -07001074static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 char buf[MAX_ADDR_LEN];
1077 struct net_device *dev = in_dev->dev;
1078
1079 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001080 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
1083#ifdef CONFIG_IP_MULTICAST
1084/*
1085 * deleted ip_mc_list manipulation
1086 */
1087static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1088{
1089 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001090 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /* this is an "ip_mc_list" for convenience; only the fields below
1093 * are actually used. In particular, the refcnt and users are not
1094 * used for management of the delete list. Using the same structure
1095 * for deleted items allows change reports to use common code with
1096 * non-deleted or query-response MCA's.
1097 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001098 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (!pmc)
1100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 spin_lock_bh(&im->lock);
1102 pmc->interface = im->interface;
1103 in_dev_hold(in_dev);
1104 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001105 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 pmc->sfmode = im->sfmode;
1107 if (pmc->sfmode == MCAST_INCLUDE) {
1108 struct ip_sf_list *psf;
1109
1110 pmc->tomb = im->tomb;
1111 pmc->sources = im->sources;
1112 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001113 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 psf->sf_crcount = pmc->crcount;
1115 }
1116 spin_unlock_bh(&im->lock);
1117
1118 spin_lock_bh(&in_dev->mc_tomb_lock);
1119 pmc->next = in_dev->mc_tomb;
1120 in_dev->mc_tomb = pmc;
1121 spin_unlock_bh(&in_dev->mc_tomb_lock);
1122}
1123
Al Viro63007722006-09-27 18:31:32 -07001124static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct ip_mc_list *pmc, *pmc_prev;
1127 struct ip_sf_list *psf, *psf_next;
1128
1129 spin_lock_bh(&in_dev->mc_tomb_lock);
1130 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001131 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (pmc->multiaddr == multiaddr)
1133 break;
1134 pmc_prev = pmc;
1135 }
1136 if (pmc) {
1137 if (pmc_prev)
1138 pmc_prev->next = pmc->next;
1139 else
1140 in_dev->mc_tomb = pmc->next;
1141 }
1142 spin_unlock_bh(&in_dev->mc_tomb_lock);
1143 if (pmc) {
Weilong Chenc71151f2013-12-23 14:37:29 +08001144 for (psf = pmc->tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 psf_next = psf->sf_next;
1146 kfree(psf);
1147 }
1148 in_dev_put(pmc->interface);
1149 kfree(pmc);
1150 }
1151}
1152
1153static void igmpv3_clear_delrec(struct in_device *in_dev)
1154{
1155 struct ip_mc_list *pmc, *nextpmc;
1156
1157 spin_lock_bh(&in_dev->mc_tomb_lock);
1158 pmc = in_dev->mc_tomb;
1159 in_dev->mc_tomb = NULL;
1160 spin_unlock_bh(&in_dev->mc_tomb_lock);
1161
1162 for (; pmc; pmc = nextpmc) {
1163 nextpmc = pmc->next;
1164 ip_mc_clear_src(pmc);
1165 in_dev_put(pmc->interface);
1166 kfree(pmc);
1167 }
1168 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001169 rcu_read_lock();
1170 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct ip_sf_list *psf, *psf_next;
1172
1173 spin_lock_bh(&pmc->lock);
1174 psf = pmc->tomb;
1175 pmc->tomb = NULL;
1176 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001177 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 psf_next = psf->sf_next;
1179 kfree(psf);
1180 }
1181 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001182 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184#endif
1185
1186static void igmp_group_dropped(struct ip_mc_list *im)
1187{
1188 struct in_device *in_dev = im->interface;
1189#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001190 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int reporter;
1192#endif
1193
1194 if (im->loaded) {
1195 im->loaded = 0;
1196 ip_mc_filter_del(in_dev, im->multiaddr);
1197 }
1198
1199#ifdef CONFIG_IP_MULTICAST
1200 if (im->multiaddr == IGMP_ALL_HOSTS)
1201 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001202 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 reporter = im->reporter;
1206 igmp_stop_timer(im);
1207
1208 if (!in_dev->dead) {
1209 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (IGMP_V2_SEEN(in_dev)) {
1212 if (reporter)
1213 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001214 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 /* IGMPv3 */
1217 igmpv3_add_delrec(in_dev, im);
1218
1219 igmp_ifc_event(in_dev);
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224static void igmp_group_added(struct ip_mc_list *im)
1225{
1226 struct in_device *in_dev = im->interface;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001227 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (im->loaded == 0) {
1230 im->loaded = 1;
1231 ip_mc_filter_add(in_dev, im->multiaddr);
1232 }
1233
1234#ifdef CONFIG_IP_MULTICAST
1235 if (im->multiaddr == IGMP_ALL_HOSTS)
1236 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001237 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001238 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (in_dev->dead)
1241 return;
1242 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1243 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001244 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 spin_unlock_bh(&im->lock);
1246 return;
1247 }
1248 /* else, v3 */
1249
Nikolay Borisov165094a2016-02-08 23:29:24 +02001250 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 igmp_ifc_event(in_dev);
1252#endif
1253}
1254
1255
1256/*
1257 * Multicast list managers
1258 */
1259
Eric Dumazete9897072013-06-07 08:48:57 -07001260static u32 ip_mc_hash(const struct ip_mc_list *im)
1261{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001262 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001263}
1264
1265static void ip_mc_hash_add(struct in_device *in_dev,
1266 struct ip_mc_list *im)
1267{
1268 struct ip_mc_list __rcu **mc_hash;
1269 u32 hash;
1270
1271 mc_hash = rtnl_dereference(in_dev->mc_hash);
1272 if (mc_hash) {
1273 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001274 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001275 rcu_assign_pointer(mc_hash[hash], im);
1276 return;
1277 }
1278
1279 /* do not use a hash table for small number of items */
1280 if (in_dev->mc_count < 4)
1281 return;
1282
1283 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1284 GFP_KERNEL);
1285 if (!mc_hash)
1286 return;
1287
1288 for_each_pmc_rtnl(in_dev, im) {
1289 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001290 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001291 RCU_INIT_POINTER(mc_hash[hash], im);
1292 }
1293
1294 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1295}
1296
1297static void ip_mc_hash_remove(struct in_device *in_dev,
1298 struct ip_mc_list *im)
1299{
1300 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1301 struct ip_mc_list *aux;
1302
1303 if (!mc_hash)
1304 return;
1305 mc_hash += ip_mc_hash(im);
1306 while ((aux = rtnl_dereference(*mc_hash)) != im)
1307 mc_hash = &aux->next_hash;
1308 *mc_hash = im->next_hash;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312/*
1313 * A socket has joined a multicast group on device dev.
1314 */
1315
Al Viro8f935bb2006-09-27 18:30:07 -07001316void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct ip_mc_list *im;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001319 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 ASSERT_RTNL();
1322
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001323 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (im->multiaddr == addr) {
1325 im->users++;
1326 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1327 goto out;
1328 }
1329 }
1330
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001331 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (!im)
1333 goto out;
1334
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001335 im->users = 1;
1336 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001338 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* initial mode is (EX, empty) */
1340 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 atomic_set(&im->refcnt, 1);
1343 spin_lock_init(&im->lock);
1344#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301345 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001346 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001348
1349 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001350 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001351 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001352
Eric Dumazete9897072013-06-07 08:48:57 -07001353 ip_mc_hash_add(in_dev, im);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355#ifdef CONFIG_IP_MULTICAST
1356 igmpv3_del_delrec(in_dev, im->multiaddr);
1357#endif
1358 igmp_group_added(im);
1359 if (!in_dev->dead)
1360 ip_rt_multicast_event(in_dev);
1361out:
1362 return;
1363}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001364EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001366static int ip_mc_check_iphdr(struct sk_buff *skb)
1367{
1368 const struct iphdr *iph;
1369 unsigned int len;
1370 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1371
1372 if (!pskb_may_pull(skb, offset))
1373 return -EINVAL;
1374
1375 iph = ip_hdr(skb);
1376
1377 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1378 return -EINVAL;
1379
1380 offset += ip_hdrlen(skb) - sizeof(*iph);
1381
1382 if (!pskb_may_pull(skb, offset))
1383 return -EINVAL;
1384
1385 iph = ip_hdr(skb);
1386
1387 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1388 return -EINVAL;
1389
1390 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1391 if (skb->len < len || len < offset)
1392 return -EINVAL;
1393
1394 skb_set_transport_header(skb, offset);
1395
1396 return 0;
1397}
1398
1399static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1400{
1401 unsigned int len = skb_transport_offset(skb);
1402
1403 len += sizeof(struct igmpv3_report);
1404
1405 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1406}
1407
1408static int ip_mc_check_igmp_query(struct sk_buff *skb)
1409{
1410 unsigned int len = skb_transport_offset(skb);
1411
1412 len += sizeof(struct igmphdr);
1413 if (skb->len < len)
1414 return -EINVAL;
1415
1416 /* IGMPv{1,2}? */
1417 if (skb->len != len) {
1418 /* or IGMPv3? */
1419 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1420 if (skb->len < len || !pskb_may_pull(skb, len))
1421 return -EINVAL;
1422 }
1423
1424 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1425 * all-systems destination addresses (224.0.0.1) for general queries
1426 */
1427 if (!igmp_hdr(skb)->group &&
1428 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1429 return -EINVAL;
1430
1431 return 0;
1432}
1433
1434static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1435{
1436 switch (igmp_hdr(skb)->type) {
1437 case IGMP_HOST_LEAVE_MESSAGE:
1438 case IGMP_HOST_MEMBERSHIP_REPORT:
1439 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1440 /* fall through */
1441 return 0;
1442 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1443 return ip_mc_check_igmp_reportv3(skb);
1444 case IGMP_HOST_MEMBERSHIP_QUERY:
1445 return ip_mc_check_igmp_query(skb);
1446 default:
1447 return -ENOMSG;
1448 }
1449}
1450
1451static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1452{
1453 return skb_checksum_simple_validate(skb);
1454}
1455
1456static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1457
1458{
1459 struct sk_buff *skb_chk;
1460 unsigned int transport_len;
1461 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga5169932015-08-13 05:54:07 +02001462 int ret = -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001463
1464 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1465
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001466 skb_chk = skb_checksum_trimmed(skb, transport_len,
1467 ip_mc_validate_checksum);
1468 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02001469 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001470
Linus Lüssinga5169932015-08-13 05:54:07 +02001471 if (!pskb_may_pull(skb_chk, len))
1472 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001473
1474 ret = ip_mc_check_igmp_msg(skb_chk);
Linus Lüssinga5169932015-08-13 05:54:07 +02001475 if (ret)
1476 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001477
1478 if (skb_trimmed)
1479 *skb_trimmed = skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02001480 /* free now unneeded clone */
1481 else if (skb_chk != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001482 kfree_skb(skb_chk);
1483
Linus Lüssinga5169932015-08-13 05:54:07 +02001484 ret = 0;
1485
1486err:
1487 if (ret && skb_chk && skb_chk != skb)
1488 kfree_skb(skb_chk);
1489
1490 return ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001491}
1492
1493/**
1494 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1495 * @skb: the skb to validate
1496 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1497 *
1498 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001499 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001500 *
1501 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1502 * standard
1503 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1504 * -ENOMEM: A memory allocation failure happened.
1505 *
1506 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1507 * to NULL): After parsing an IGMP packet successfully it will point to
1508 * an skb which has its tail aligned to the IP packet end. This might
1509 * either be the originally provided skb or a trimmed, cloned version if
1510 * the skb frame had data beyond the IP packet. A cloned skb allows us
1511 * to leave the original skb and its full frame unchanged (which might be
1512 * desirable for layer 2 frame jugglers).
1513 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001514 * Caller needs to set the skb network header and free any returned skb if it
1515 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001516 */
1517int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1518{
1519 int ret = ip_mc_check_iphdr(skb);
1520
1521 if (ret < 0)
1522 return ret;
1523
1524 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1525 return -ENOMSG;
1526
1527 return __ip_mc_check_igmp(skb, skb_trimmed);
1528}
1529EXPORT_SYMBOL(ip_mc_check_igmp);
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001532 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001533 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001534static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001535{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001536#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001537 struct ip_mc_list *im;
1538 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001539 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001540
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001541 ASSERT_RTNL();
1542
1543 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001544 if (im->multiaddr == IGMP_ALL_HOSTS)
1545 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001546 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001547 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001548 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001549
Eric Dumazet866f3b22010-11-18 09:33:19 -08001550 /* a failover is happening and switches
1551 * must be notified immediately
1552 */
1553 if (IGMP_V1_SEEN(in_dev))
1554 type = IGMP_HOST_MEMBERSHIP_REPORT;
1555 else if (IGMP_V2_SEEN(in_dev))
1556 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1557 else
1558 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1559 igmp_send_report(in_dev, im, type);
1560 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001561#endif
1562}
1563
1564/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 * A socket has left a multicast group on device dev
1566 */
1567
Al Viro8f935bb2006-09-27 18:30:07 -07001568void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001570 struct ip_mc_list *i;
1571 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001574
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001575 for (ip = &in_dev->mc_list;
1576 (i = rtnl_dereference(*ip)) != NULL;
1577 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001578 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001580 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001581 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001582 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001584 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 if (!in_dev->dead)
1587 ip_rt_multicast_event(in_dev);
1588
1589 ip_ma_put(i);
1590 return;
1591 }
1592 break;
1593 }
1594 }
1595}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001596EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Moni Shoua75c78502009-09-15 02:37:40 -07001598/* Device changing type */
1599
1600void ip_mc_unmap(struct in_device *in_dev)
1601{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001602 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001603
1604 ASSERT_RTNL();
1605
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001606 for_each_pmc_rtnl(in_dev, pmc)
1607 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001608}
1609
1610void ip_mc_remap(struct in_device *in_dev)
1611{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001612 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001613
1614 ASSERT_RTNL();
1615
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001616 for_each_pmc_rtnl(in_dev, pmc)
1617 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001618}
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620/* Device going down */
1621
1622void ip_mc_down(struct in_device *in_dev)
1623{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001624 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 ASSERT_RTNL();
1627
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001628 for_each_pmc_rtnl(in_dev, pmc)
1629 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631#ifdef CONFIG_IP_MULTICAST
1632 in_dev->mr_ifc_count = 0;
1633 if (del_timer(&in_dev->mr_ifc_timer))
1634 __in_dev_put(in_dev);
1635 in_dev->mr_gq_running = 0;
1636 if (del_timer(&in_dev->mr_gq_timer))
1637 __in_dev_put(in_dev);
1638 igmpv3_clear_delrec(in_dev);
1639#endif
1640
1641 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1642}
1643
1644void ip_mc_init_dev(struct in_device *in_dev)
1645{
Nikolay Borisov165094a2016-02-08 23:29:24 +02001646 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 ASSERT_RTNL();
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001650 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1651 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001652 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1653 (unsigned long)in_dev);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001654 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655#endif
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 spin_lock_init(&in_dev->mc_tomb_lock);
1658}
1659
1660/* Device going up */
1661
1662void ip_mc_up(struct in_device *in_dev)
1663{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001664 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001665 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 ASSERT_RTNL();
1668
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001669#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001670 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001671#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1673
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001674 for_each_pmc_rtnl(in_dev, pmc)
1675 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
1678/*
1679 * Device is about to be destroyed: clean up.
1680 */
1681
1682void ip_mc_destroy_dev(struct in_device *in_dev)
1683{
1684 struct ip_mc_list *i;
1685
1686 ASSERT_RTNL();
1687
1688 /* Deactivate timers */
1689 ip_mc_down(in_dev);
1690
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001691 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1692 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001693 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001694
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001695 /* We've dropped the groups in ip_mc_down already */
1696 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001701/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001702static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 struct net_device *dev = NULL;
1705 struct in_device *idev = NULL;
1706
1707 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001708 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return idev;
1710 }
1711 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001712 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (!dev)
1714 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
David S. Millerb23dd4f2011-03-02 14:31:35 -08001717 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001718 struct rtable *rt = ip_route_output(net,
1719 imr->imr_multiaddr.s_addr,
1720 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001721 if (!IS_ERR(rt)) {
1722 dev = rt->dst.dev;
1723 ip_rt_put(rt);
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726 if (dev) {
1727 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001728 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730 return idev;
1731}
1732
1733/*
1734 * Join a socket to a group
1735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001738 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 struct ip_sf_list *psf, *psf_prev;
1741 int rv = 0;
1742
1743 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001744 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (psf->sf_inaddr == *psfsrc)
1746 break;
1747 psf_prev = psf;
1748 }
1749 if (!psf || psf->sf_count[sfmode] == 0) {
1750 /* source filter not found, or count wrong => bug */
1751 return -ESRCH;
1752 }
1753 psf->sf_count[sfmode]--;
1754 if (psf->sf_count[sfmode] == 0) {
1755 ip_rt_multicast_event(pmc->interface);
1756 }
1757 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1758#ifdef CONFIG_IP_MULTICAST
1759 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001760 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761#endif
1762
1763 /* no more filters for this source */
1764 if (psf_prev)
1765 psf_prev->sf_next = psf->sf_next;
1766 else
1767 pmc->sources = psf->sf_next;
1768#ifdef CONFIG_IP_MULTICAST
1769 if (psf->sf_oldin &&
1770 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001771 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 psf->sf_next = pmc->tomb;
1773 pmc->tomb = psf;
1774 rv = 1;
1775 } else
1776#endif
1777 kfree(psf);
1778 }
1779 return rv;
1780}
1781
1782#ifndef CONFIG_IP_MULTICAST
1783#define igmp_ifc_event(x) do { } while (0)
1784#endif
1785
Al Viro8f935bb2006-09-27 18:30:07 -07001786static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1787 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 struct ip_mc_list *pmc;
1790 int changerec = 0;
1791 int i, err;
1792
1793 if (!in_dev)
1794 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001795 rcu_read_lock();
1796 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (*pmca == pmc->multiaddr)
1798 break;
1799 }
1800 if (!pmc) {
1801 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001802 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return -ESRCH;
1804 }
1805 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001806 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#ifdef CONFIG_IP_MULTICAST
1808 sf_markstate(pmc);
1809#endif
1810 if (!delta) {
1811 err = -EINVAL;
1812 if (!pmc->sfcount[sfmode])
1813 goto out_unlock;
1814 pmc->sfcount[sfmode]--;
1815 }
1816 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001817 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1819
1820 changerec |= rv > 0;
1821 if (!err && rv < 0)
1822 err = rv;
1823 }
1824 if (pmc->sfmode == MCAST_EXCLUDE &&
1825 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1826 pmc->sfcount[MCAST_INCLUDE]) {
1827#ifdef CONFIG_IP_MULTICAST
1828 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001829 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830#endif
1831
1832 /* filter mode change */
1833 pmc->sfmode = MCAST_INCLUDE;
1834#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001835 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001837 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 psf->sf_crcount = 0;
1839 igmp_ifc_event(pmc->interface);
1840 } else if (sf_setstate(pmc) || changerec) {
1841 igmp_ifc_event(pmc->interface);
1842#endif
1843 }
1844out_unlock:
1845 spin_unlock_bh(&pmc->lock);
1846 return err;
1847}
1848
1849/*
1850 * Add multicast single-source filter to the interface list
1851 */
1852static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001853 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 struct ip_sf_list *psf, *psf_prev;
1856
1857 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001858 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (psf->sf_inaddr == *psfsrc)
1860 break;
1861 psf_prev = psf;
1862 }
1863 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001864 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (!psf)
1866 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 psf->sf_inaddr = *psfsrc;
1868 if (psf_prev) {
1869 psf_prev->sf_next = psf;
1870 } else
1871 pmc->sources = psf;
1872 }
1873 psf->sf_count[sfmode]++;
1874 if (psf->sf_count[sfmode] == 1) {
1875 ip_rt_multicast_event(pmc->interface);
1876 }
1877 return 0;
1878}
1879
1880#ifdef CONFIG_IP_MULTICAST
1881static void sf_markstate(struct ip_mc_list *pmc)
1882{
1883 struct ip_sf_list *psf;
1884 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1885
Weilong Chenc71151f2013-12-23 14:37:29 +08001886 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (pmc->sfcount[MCAST_EXCLUDE]) {
1888 psf->sf_oldin = mca_xcount ==
1889 psf->sf_count[MCAST_EXCLUDE] &&
1890 !psf->sf_count[MCAST_INCLUDE];
1891 } else
1892 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1893}
1894
1895static int sf_setstate(struct ip_mc_list *pmc)
1896{
David L Stevensad125832006-01-18 14:20:56 -08001897 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1899 int qrv = pmc->interface->mr_qrv;
1900 int new_in, rv;
1901
1902 rv = 0;
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 (pmc->sfcount[MCAST_EXCLUDE]) {
1905 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1906 !psf->sf_count[MCAST_INCLUDE];
1907 } else
1908 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001909 if (new_in) {
1910 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001911 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001912
Weilong Chenc71151f2013-12-23 14:37:29 +08001913 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001914 if (dpsf->sf_inaddr == psf->sf_inaddr)
1915 break;
1916 prev = dpsf;
1917 }
1918 if (dpsf) {
1919 if (prev)
1920 prev->sf_next = dpsf->sf_next;
1921 else
1922 pmc->tomb = dpsf->sf_next;
1923 kfree(dpsf);
1924 }
1925 psf->sf_crcount = qrv;
1926 rv++;
1927 }
1928 } else if (psf->sf_oldin) {
1929
1930 psf->sf_crcount = 0;
1931 /*
1932 * add or update "delete" records if an active filter
1933 * is now inactive
1934 */
Weilong Chenc71151f2013-12-23 14:37:29 +08001935 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08001936 if (dpsf->sf_inaddr == psf->sf_inaddr)
1937 break;
1938 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001939 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001940 if (!dpsf)
1941 continue;
1942 *dpsf = *psf;
1943 /* pmc->lock held by callers */
1944 dpsf->sf_next = pmc->tomb;
1945 pmc->tomb = dpsf;
1946 }
1947 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 rv++;
1949 }
1950 }
1951 return rv;
1952}
1953#endif
1954
1955/*
1956 * Add multicast source filter list to the interface list
1957 */
Al Viro8f935bb2006-09-27 18:30:07 -07001958static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1959 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 struct ip_mc_list *pmc;
1962 int isexclude;
1963 int i, err;
1964
1965 if (!in_dev)
1966 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001967 rcu_read_lock();
1968 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (*pmca == pmc->multiaddr)
1970 break;
1971 }
1972 if (!pmc) {
1973 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001974 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return -ESRCH;
1976 }
1977 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001978 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980#ifdef CONFIG_IP_MULTICAST
1981 sf_markstate(pmc);
1982#endif
1983 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1984 if (!delta)
1985 pmc->sfcount[sfmode]++;
1986 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001987 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00001988 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (err)
1990 break;
1991 }
1992 if (err) {
1993 int j;
1994
Jun Zhao685f94e62011-11-22 17:19:03 +00001995 if (!delta)
1996 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08001997 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00001998 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2000#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002002 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002003 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004#endif
2005
2006 /* filter mode change */
2007 if (pmc->sfcount[MCAST_EXCLUDE])
2008 pmc->sfmode = MCAST_EXCLUDE;
2009 else if (pmc->sfcount[MCAST_INCLUDE])
2010 pmc->sfmode = MCAST_INCLUDE;
2011#ifdef CONFIG_IP_MULTICAST
2012 /* else no filters; keep old mode for reports */
2013
Nikolay Borisov165094a2016-02-08 23:29:24 +02002014 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002016 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 psf->sf_crcount = 0;
2018 igmp_ifc_event(in_dev);
2019 } else if (sf_setstate(pmc)) {
2020 igmp_ifc_event(in_dev);
2021#endif
2022 }
2023 spin_unlock_bh(&pmc->lock);
2024 return err;
2025}
2026
2027static void ip_mc_clear_src(struct ip_mc_list *pmc)
2028{
2029 struct ip_sf_list *psf, *nextpsf;
2030
Weilong Chenc71151f2013-12-23 14:37:29 +08002031 for (psf = pmc->tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 nextpsf = psf->sf_next;
2033 kfree(psf);
2034 }
2035 pmc->tomb = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08002036 for (psf = pmc->sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 nextpsf = psf->sf_next;
2038 kfree(psf);
2039 }
2040 pmc->sources = NULL;
2041 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002042 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 pmc->sfcount[MCAST_EXCLUDE] = 1;
2044}
2045
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002046/* Join a multicast group
2047 */
2048
2049int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Al Viro8f935bb2006-09-27 18:30:07 -07002051 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002052 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 struct in_device *in_dev;
2054 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002055 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002056 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002058 int err;
2059
2060 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Joe Perchesf97c1e02007-12-16 13:45:43 -08002062 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EINVAL;
2064
Daniel Lezcano877aced2008-08-13 16:15:57 -07002065 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 err = -ENODEV;
2069 goto done;
2070 }
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002073 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002074 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002075 if (i->multi.imr_multiaddr.s_addr == addr &&
2076 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 count++;
2079 }
2080 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002081 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002083 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002084 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002085 goto done;
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002088 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 iml->sflist = NULL;
2090 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002091 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return err;
2096}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002097EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2100 struct in_device *in_dev)
2101{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002102 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 int err;
2104
Ian Morris51456b22015-04-03 09:17:26 +01002105 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* any-source empty exclude case */
2107 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2108 iml->sfmode, 0, NULL, 0);
2109 }
2110 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002111 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002112 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002113 /* decrease mem now to avoid the memleak warning */
2114 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002115 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return err;
2117}
2118
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002119int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002122 struct ip_mc_socklist *iml;
2123 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002124 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002125 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002126 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002127 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002128 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Eric Dumazet959d10f2015-02-17 03:19:24 -08002130 ASSERT_RTNL();
2131
Daniel Lezcano877aced2008-08-13 16:15:57 -07002132 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002133 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002134 ret = -ENODEV;
2135 goto out;
2136 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002137 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002138 for (imlp = &inet->mc_list;
2139 (iml = rtnl_dereference(*imlp)) != NULL;
2140 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002141 if (iml->multi.imr_multiaddr.s_addr != group)
2142 continue;
2143 if (ifindex) {
2144 if (iml->multi.imr_ifindex != ifindex)
2145 continue;
2146 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2147 iml->multi.imr_address.s_addr)
2148 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David L Stevensacd6e002006-08-17 16:27:39 -07002150 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002152 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002153
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002154 if (in_dev)
2155 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002156
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002157 /* decrease mem now to avoid the memleak warning */
2158 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002159 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
dingtianhong52ad3532014-07-02 13:50:48 +08002162out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002163 return ret;
2164}
stephen hemminger193ba922012-10-01 12:32:34 +00002165EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167int ip_mc_source(int add, int omode, struct sock *sk, struct
2168 ip_mreq_source *mreqs, int ifindex)
2169{
2170 int err;
2171 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002172 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 struct ip_mc_socklist *pmc;
2174 struct in_device *in_dev = NULL;
2175 struct inet_sock *inet = inet_sk(sk);
2176 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002177 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002178 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 int i, j, rv;
2180
Joe Perchesf97c1e02007-12-16 13:45:43 -08002181 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return -EINVAL;
2183
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002184 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2187 imr.imr_address.s_addr = mreqs->imr_interface;
2188 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002189 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 if (!in_dev) {
2192 err = -ENODEV;
2193 goto done;
2194 }
2195 err = -EADDRNOTAVAIL;
2196
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002197 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002198 if ((pmc->multi.imr_multiaddr.s_addr ==
2199 imr.imr_multiaddr.s_addr) &&
2200 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 break;
2202 }
David L Stevens917f2f12005-07-08 17:45:16 -07002203 if (!pmc) { /* must have a prior join */
2204 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /* if a source filter was set, must be the same mode as before */
2208 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002209 if (pmc->sfmode != omode) {
2210 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 } else if (pmc->sfmode != omode) {
2214 /* allow mode switches for empty-set filters */
2215 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002216 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 NULL, 0);
2218 pmc->sfmode = omode;
2219 }
2220
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002221 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (!add) {
2223 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002224 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002226 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002228 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (rv == 0)
2230 break;
2231 }
2232 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002233 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
David L Stevens8cdaaa12005-07-08 17:39:23 -07002235 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2236 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2237 leavegroup = 1;
2238 goto done;
2239 }
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002242 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 &mreqs->imr_sourceaddr, 1);
2244
Weilong Chenc71151f2013-12-23 14:37:29 +08002245 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 psl->sl_addr[j-1] = psl->sl_addr[j];
2247 psl->sl_count--;
2248 err = 0;
2249 goto done;
2250 }
2251 /* else, add a new source to the filter */
2252
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002253 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 err = -ENOBUFS;
2255 goto done;
2256 }
2257 if (!psl || psl->sl_count == psl->sl_max) {
2258 struct ip_sf_socklist *newpsl;
2259 int count = IP_SFBLOCK;
2260
2261 if (psl)
2262 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002263 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (!newpsl) {
2265 err = -ENOBUFS;
2266 goto done;
2267 }
2268 newpsl->sl_max = count;
2269 newpsl->sl_count = count - IP_SFBLOCK;
2270 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002271 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002273 /* decrease mem now to avoid the memleak warning */
2274 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002275 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002277 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002278 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002281 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002283 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (rv == 0)
2285 break;
2286 }
2287 if (rv == 0) /* address already there is an error */
2288 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002289 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 psl->sl_addr[j+1] = psl->sl_addr[j];
2291 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2292 psl->sl_count++;
2293 err = 0;
2294 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002295 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 &mreqs->imr_sourceaddr, 1);
2297done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002298 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002299 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return err;
2301}
2302
2303int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2304{
David L Stevens9951f032005-07-08 17:47:28 -07002305 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002307 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 struct ip_mc_socklist *pmc;
2309 struct in_device *in_dev;
2310 struct inet_sock *inet = inet_sk(sk);
2311 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002312 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002313 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Joe Perchesf97c1e02007-12-16 13:45:43 -08002315 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return -EINVAL;
2317 if (msf->imsf_fmode != MCAST_INCLUDE &&
2318 msf->imsf_fmode != MCAST_EXCLUDE)
2319 return -EINVAL;
2320
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002321 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2324 imr.imr_address.s_addr = msf->imsf_interface;
2325 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002326 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 if (!in_dev) {
2329 err = -ENODEV;
2330 goto done;
2331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
David L Stevens9951f032005-07-08 17:47:28 -07002333 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2334 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2335 leavegroup = 1;
2336 goto done;
2337 }
2338
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002339 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2341 pmc->multi.imr_ifindex == imr.imr_ifindex)
2342 break;
2343 }
David L Stevens917f2f12005-07-08 17:45:16 -07002344 if (!pmc) { /* must have a prior join */
2345 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002349 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2350 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (!newpsl) {
2352 err = -ENOBUFS;
2353 goto done;
2354 }
2355 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2356 memcpy(newpsl->sl_addr, msf->imsf_slist,
2357 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2358 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2359 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2360 if (err) {
2361 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2362 goto done;
2363 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002364 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002366 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2367 msf->imsf_fmode, 0, NULL, 0);
2368 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002369 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (psl) {
2371 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2372 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002373 /* decrease mem now to avoid the memleak warning */
2374 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002375 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 } else
2377 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2378 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002379 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002381 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382done:
David L Stevens9951f032005-07-08 17:47:28 -07002383 if (leavegroup)
2384 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return err;
2386}
2387
2388int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2389 struct ip_msfilter __user *optval, int __user *optlen)
2390{
2391 int err, len, count, copycount;
2392 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002393 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct ip_mc_socklist *pmc;
2395 struct in_device *in_dev;
2396 struct inet_sock *inet = inet_sk(sk);
2397 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002398 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
WANG Cong87e9f032015-11-03 15:41:16 -08002400 ASSERT_RTNL();
2401
Joe Perchesf97c1e02007-12-16 13:45:43 -08002402 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return -EINVAL;
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2406 imr.imr_address.s_addr = msf->imsf_interface;
2407 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002408 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 if (!in_dev) {
2411 err = -ENODEV;
2412 goto done;
2413 }
2414 err = -EADDRNOTAVAIL;
2415
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002416 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2418 pmc->multi.imr_ifindex == imr.imr_ifindex)
2419 break;
2420 }
2421 if (!pmc) /* must have a prior join */
2422 goto done;
2423 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002424 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (!psl) {
2426 len = 0;
2427 count = 0;
2428 } else {
2429 count = psl->sl_count;
2430 }
2431 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2432 len = copycount * sizeof(psl->sl_addr[0]);
2433 msf->imsf_numsrc = count;
2434 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2435 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2436 return -EFAULT;
2437 }
2438 if (len &&
2439 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2440 return -EFAULT;
2441 return 0;
2442done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return err;
2444}
2445
2446int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2447 struct group_filter __user *optval, int __user *optlen)
2448{
2449 int err, i, count, copycount;
2450 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002451 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct ip_mc_socklist *pmc;
2453 struct inet_sock *inet = inet_sk(sk);
2454 struct ip_sf_socklist *psl;
2455
WANG Cong87e9f032015-11-03 15:41:16 -08002456 ASSERT_RTNL();
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 psin = (struct sockaddr_in *)&gsf->gf_group;
2459 if (psin->sin_family != AF_INET)
2460 return -EINVAL;
2461 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002462 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return -EINVAL;
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 err = -EADDRNOTAVAIL;
2466
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002467 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2469 pmc->multi.imr_ifindex == gsf->gf_interface)
2470 break;
2471 }
2472 if (!pmc) /* must have a prior join */
2473 goto done;
2474 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002475 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 count = psl ? psl->sl_count : 0;
2477 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2478 gsf->gf_numsrc = count;
2479 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2480 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2481 return -EFAULT;
2482 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002483 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct sockaddr_storage ss;
2485
2486 psin = (struct sockaddr_in *)&ss;
2487 memset(&ss, 0, sizeof(ss));
2488 psin->sin_family = AF_INET;
2489 psin->sin_addr.s_addr = psl->sl_addr[i];
2490 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2491 return -EFAULT;
2492 }
2493 return 0;
2494done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return err;
2496}
2497
2498/*
2499 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2500 */
Al Viroc0cda062006-09-27 18:31:10 -07002501int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503 struct inet_sock *inet = inet_sk(sk);
2504 struct ip_mc_socklist *pmc;
2505 struct ip_sf_socklist *psl;
2506 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002507 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002509 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002510 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002511 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002513 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002514 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2516 pmc->multi.imr_ifindex == dif)
2517 break;
2518 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002519 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002521 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002522 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002523 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002525 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Weilong Chenc71151f2013-12-23 14:37:29 +08002527 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (psl->sl_addr[i] == rmt_addr)
2529 break;
2530 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002531 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002533 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002535 goto unlock;
2536 ret = 1;
2537unlock:
2538 rcu_read_unlock();
2539out:
2540 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
2543/*
2544 * A socket is closing.
2545 */
2546
2547void ip_mc_drop_socket(struct sock *sk)
2548{
2549 struct inet_sock *inet = inet_sk(sk);
2550 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002551 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Ian Morris51456b22015-04-03 09:17:26 +01002553 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return;
2555
2556 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002557 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002560 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002561 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002562 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002563 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002565 /* decrease mem now to avoid the memleak warning */
2566 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002567 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569 rtnl_unlock();
2570}
2571
David S. Millerdbdd9a52011-03-10 16:34:38 -08002572/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002573int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
2575 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002576 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct ip_sf_list *psf;
2578 int rv = 0;
2579
Eric Dumazete9897072013-06-07 08:48:57 -07002580 mc_hash = rcu_dereference(in_dev->mc_hash);
2581 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002582 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002583
2584 for (im = rcu_dereference(mc_hash[hash]);
2585 im != NULL;
2586 im = rcu_dereference(im->next_hash)) {
2587 if (im->multiaddr == mc_addr)
2588 break;
2589 }
2590 } else {
2591 for_each_pmc_rcu(in_dev, im) {
2592 if (im->multiaddr == mc_addr)
2593 break;
2594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 if (im && proto == IPPROTO_IGMP) {
2597 rv = 1;
2598 } else if (im) {
2599 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002600 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if (psf->sf_inaddr == src_addr)
2602 break;
2603 }
2604 if (psf)
2605 rv = psf->sf_count[MCAST_INCLUDE] ||
2606 psf->sf_count[MCAST_EXCLUDE] !=
2607 im->sfcount[MCAST_EXCLUDE];
2608 else
2609 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2610 } else
2611 rv = 1; /* unspecified source; tentatively allow */
2612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return rv;
2614}
2615
2616#if defined(CONFIG_PROC_FS)
2617struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002618 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 struct net_device *dev;
2620 struct in_device *in_dev;
2621};
2622
2623#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2624
2625static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2626{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002627 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 struct ip_mc_list *im = NULL;
2629 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2630
Pavel Emelianov7562f872007-05-03 15:13:45 -07002631 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002632 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002634
2635 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (!in_dev)
2637 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002638 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (im) {
2640 state->in_dev = in_dev;
2641 break;
2642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
2644 return im;
2645}
2646
2647static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2648{
2649 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002650
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002651 im = rcu_dereference(im->next_rcu);
2652 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002653 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (!state->dev) {
2655 state->in_dev = NULL;
2656 break;
2657 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002658 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (!state->in_dev)
2660 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002661 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 return im;
2664}
2665
2666static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2667{
2668 struct ip_mc_list *im = igmp_mc_get_first(seq);
2669 if (im)
2670 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2671 --pos;
2672 return pos ? NULL : im;
2673}
2674
2675static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002676 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
stephen hemminger61fbab72009-11-10 07:54:55 +00002678 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2680}
2681
2682static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2683{
2684 struct ip_mc_list *im;
2685 if (v == SEQ_START_TOKEN)
2686 im = igmp_mc_get_first(seq);
2687 else
2688 im = igmp_mc_get_next(seq, v);
2689 ++*pos;
2690 return im;
2691}
2692
2693static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002694 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
2696 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002697
2698 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
2703static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2704{
2705 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002706 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2708 else {
2709 struct ip_mc_list *im = (struct ip_mc_list *)v;
2710 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2711 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002712 long delta;
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714#ifdef CONFIG_IP_MULTICAST
2715 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2716 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2717 "V3";
2718#else
2719 querier = "NONE";
2720#endif
2721
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002722 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002724 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726
Eric Dumazeta399a802012-08-08 21:13:53 +00002727 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002729 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002731 im->tm_running,
2732 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 im->reporter);
2734 }
2735 return 0;
2736}
2737
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002738static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 .start = igmp_mc_seq_start,
2740 .next = igmp_mc_seq_next,
2741 .stop = igmp_mc_seq_stop,
2742 .show = igmp_mc_seq_show,
2743};
2744
2745static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2746{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002747 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002748 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
Arjan van de Ven9a321442007-02-12 00:55:35 -08002751static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 .owner = THIS_MODULE,
2753 .open = igmp_mc_seq_open,
2754 .read = seq_read,
2755 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002756 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757};
2758
2759struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002760 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 struct net_device *dev;
2762 struct in_device *idev;
2763 struct ip_mc_list *im;
2764};
2765
2766#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2767
2768static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2769{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002770 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 struct ip_sf_list *psf = NULL;
2772 struct ip_mc_list *im = NULL;
2773 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2774
Pavel Emelianov7562f872007-05-03 15:13:45 -07002775 state->idev = NULL;
2776 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002777 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002779 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002780 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002782 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002783 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 spin_lock_bh(&im->lock);
2785 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002786 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 state->im = im;
2788 state->idev = idev;
2789 break;
2790 }
2791 spin_unlock_bh(&im->lock);
2792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794 return psf;
2795}
2796
2797static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2798{
2799 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2800
2801 psf = psf->sf_next;
2802 while (!psf) {
2803 spin_unlock_bh(&state->im->lock);
2804 state->im = state->im->next;
2805 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002806 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (!state->dev) {
2808 state->idev = NULL;
2809 goto out;
2810 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002811 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (!state->idev)
2813 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002814 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
2816 if (!state->im)
2817 break;
2818 spin_lock_bh(&state->im->lock);
2819 psf = state->im->sources;
2820 }
2821out:
2822 return psf;
2823}
2824
2825static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2826{
2827 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2828 if (psf)
2829 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2830 --pos;
2831 return pos ? NULL : psf;
2832}
2833
2834static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002835 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
stephen hemminger61fbab72009-11-10 07:54:55 +00002837 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2839}
2840
2841static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2842{
2843 struct ip_sf_list *psf;
2844 if (v == SEQ_START_TOKEN)
2845 psf = igmp_mcf_get_first(seq);
2846 else
2847 psf = igmp_mcf_get_next(seq, v);
2848 ++*pos;
2849 return psf;
2850}
2851
2852static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002853 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002856 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 spin_unlock_bh(&state->im->lock);
2858 state->im = NULL;
2859 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002860 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002862 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
2865static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2866{
2867 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2868 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2869
2870 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002871 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 } else {
2873 seq_printf(seq,
2874 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002875 "0x%08x %6lu %6lu\n",
2876 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 ntohl(state->im->multiaddr),
2878 ntohl(psf->sf_inaddr),
2879 psf->sf_count[MCAST_INCLUDE],
2880 psf->sf_count[MCAST_EXCLUDE]);
2881 }
2882 return 0;
2883}
2884
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002885static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 .start = igmp_mcf_seq_start,
2887 .next = igmp_mcf_seq_next,
2888 .stop = igmp_mcf_seq_stop,
2889 .show = igmp_mcf_seq_show,
2890};
2891
2892static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2893{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002894 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002895 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
Arjan van de Ven9a321442007-02-12 00:55:35 -08002898static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 .owner = THIS_MODULE,
2900 .open = igmp_mcf_seq_open,
2901 .read = seq_read,
2902 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002903 .release = seq_release_net,
2904};
2905
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002906static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002907{
2908 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002909 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002910
Gao fengd4beaa62013-02-18 01:34:54 +00002911 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002912 if (!pde)
2913 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002914 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2915 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002916 if (!pde)
2917 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08002918 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2919 SOCK_DGRAM, 0, net);
2920 if (err < 0) {
2921 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2922 err);
2923 goto out_sock;
2924 }
2925
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002926 return 0;
2927
Madhu Challa93a714d2015-02-25 09:58:35 -08002928out_sock:
2929 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002930out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002931 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002932out_igmp:
2933 return -ENOMEM;
2934}
2935
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002936static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002937{
Gao fengece31ff2013-02-18 01:34:56 +00002938 remove_proc_entry("mcfilter", net->proc_net);
2939 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08002940 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002941}
2942
2943static struct pernet_operations igmp_net_ops = {
2944 .init = igmp_net_init,
2945 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946};
WANG Cong72c1d3b2014-01-10 16:09:45 -08002947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002949static int igmp_netdev_event(struct notifier_block *this,
2950 unsigned long event, void *ptr)
2951{
2952 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2953 struct in_device *in_dev;
2954
2955 switch (event) {
2956 case NETDEV_RESEND_IGMP:
2957 in_dev = __in_dev_get_rtnl(dev);
2958 if (in_dev)
2959 ip_mc_rejoin_groups(in_dev);
2960 break;
2961 default:
2962 break;
2963 }
2964 return NOTIFY_DONE;
2965}
2966
2967static struct notifier_block igmp_notifier = {
2968 .notifier_call = igmp_netdev_event,
2969};
2970
WANG Cong72c1d3b2014-01-10 16:09:45 -08002971int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
WANG Cong72c1d3b2014-01-10 16:09:45 -08002973#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002974 int err;
2975
2976 err = register_pernet_subsys(&igmp_net_ops);
2977 if (err)
2978 return err;
2979 err = register_netdevice_notifier(&igmp_notifier);
2980 if (err)
2981 goto reg_notif_fail;
2982 return 0;
2983
2984reg_notif_fail:
2985 unregister_pernet_subsys(&igmp_net_ops);
2986 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08002987#else
2988 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08002990}