blob: 9fdfd9deac11dde85bc62803068fbe50e45837b8 [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
110#define IP_MAX_MEMBERSHIPS 20
111#define IP_MAX_MSF 10
112
113#ifdef CONFIG_IP_MULTICAST
114/* Parameter names and values are taken from igmp-v2-06 draft */
115
Fabian Frederick436f7c22014-11-04 20:52:14 +0100116#define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
117#define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
118#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
119#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
120#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
121#define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123
Fabian Frederick436f7c22014-11-04 20:52:14 +0100124#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Fabian Frederick436f7c22014-11-04 20:52:14 +0100126/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * IGMP specs require to report membership immediately after
128 * joining a group, but we delay the first report by a
129 * small interval. It seems more natural and still does not
130 * contradict to specs provided this delay is small enough.
131 */
132
Herbert Xu42f811b2007-06-04 23:34:44 -0700133#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900134 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700135 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
136 ((in_dev)->mr_v1_seen && \
137 time_before(jiffies, (in_dev)->mr_v1_seen)))
138#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900139 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700140 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
141 ((in_dev)->mr_v2_seen && \
142 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
William Manleycab70042013-08-06 19:03:13 +0100144static int unsolicited_report_interval(struct in_device *in_dev)
145{
William Manley26900482013-08-06 19:03:15 +0100146 int interval_ms, interval_jiffies;
147
William Manleycab70042013-08-06 19:03:13 +0100148 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100149 interval_ms = IN_DEV_CONF_GET(
150 in_dev,
151 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100152 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100153 interval_ms = IN_DEV_CONF_GET(
154 in_dev,
155 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
156
157 interval_jiffies = msecs_to_jiffies(interval_ms);
158
159 /* _timer functions can't handle a delay of 0 jiffies so ensure
160 * we always return a positive value.
161 */
162 if (interval_jiffies <= 0)
163 interval_jiffies = 1;
164 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700168static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static void igmpv3_clear_delrec(struct in_device *in_dev);
170static int sf_setstate(struct ip_mc_list *pmc);
171static void sf_markstate(struct ip_mc_list *pmc);
172#endif
173static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700174static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
175 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177static void ip_ma_put(struct ip_mc_list *im)
178{
179 if (atomic_dec_and_test(&im->refcnt)) {
180 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800181 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183}
184
David S. Millerd9aa9382010-11-15 08:52:02 -0800185#define for_each_pmc_rcu(in_dev, pmc) \
186 for (pmc = rcu_dereference(in_dev->mc_list); \
187 pmc != NULL; \
188 pmc = rcu_dereference(pmc->next_rcu))
189
190#define for_each_pmc_rtnl(in_dev, pmc) \
191 for (pmc = rtnl_dereference(in_dev->mc_list); \
192 pmc != NULL; \
193 pmc = rtnl_dereference(pmc->next_rcu))
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#ifdef CONFIG_IP_MULTICAST
196
197/*
198 * Timer management
199 */
200
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000201static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 spin_lock_bh(&im->lock);
204 if (del_timer(&im->timer))
205 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800206 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 im->reporter = 0;
208 im->unsolicit_count = 0;
209 spin_unlock_bh(&im->lock);
210}
211
212/* It must be called with locked im->lock */
213static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
214{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500215 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800217 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!mod_timer(&im->timer, jiffies+tv+2))
219 atomic_inc(&im->refcnt);
220}
221
222static void igmp_gq_start_timer(struct in_device *in_dev)
223{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500224 int tv = prandom_u32() % in_dev->mr_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 in_dev->mr_gq_running = 1;
227 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
228 in_dev_hold(in_dev);
229}
230
231static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
232{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500233 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
236 in_dev_hold(in_dev);
237}
238
239static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
240{
241 spin_lock_bh(&im->lock);
242 im->unsolicit_count = 0;
243 if (del_timer(&im->timer)) {
244 if ((long)(im->timer.expires-jiffies) < max_delay) {
245 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800246 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 spin_unlock_bh(&im->lock);
248 return;
249 }
250 atomic_dec(&im->refcnt);
251 }
252 igmp_start_timer(im, max_delay);
253 spin_unlock_bh(&im->lock);
254}
255
256
257/*
258 * Send an IGMP report.
259 */
260
261#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
262
263
264static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
265 int gdeleted, int sdeleted)
266{
267 switch (type) {
268 case IGMPV3_MODE_IS_INCLUDE:
269 case IGMPV3_MODE_IS_EXCLUDE:
270 if (gdeleted || sdeleted)
271 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800272 if (!(pmc->gsquery && !psf->sf_gsresp)) {
273 if (pmc->sfmode == MCAST_INCLUDE)
274 return 1;
275 /* don't include if this source is excluded
276 * in all filters
277 */
278 if (psf->sf_count[MCAST_INCLUDE])
279 return type == IGMPV3_MODE_IS_INCLUDE;
280 return pmc->sfcount[MCAST_EXCLUDE] ==
281 psf->sf_count[MCAST_EXCLUDE];
282 }
283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 case IGMPV3_CHANGE_TO_INCLUDE:
285 if (gdeleted || sdeleted)
286 return 0;
287 return psf->sf_count[MCAST_INCLUDE] != 0;
288 case IGMPV3_CHANGE_TO_EXCLUDE:
289 if (gdeleted || sdeleted)
290 return 0;
291 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
292 psf->sf_count[MCAST_INCLUDE])
293 return 0;
294 return pmc->sfcount[MCAST_EXCLUDE] ==
295 psf->sf_count[MCAST_EXCLUDE];
296 case IGMPV3_ALLOW_NEW_SOURCES:
297 if (gdeleted || !psf->sf_crcount)
298 return 0;
299 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
300 case IGMPV3_BLOCK_OLD_SOURCES:
301 if (pmc->sfmode == MCAST_INCLUDE)
302 return gdeleted || (psf->sf_crcount && sdeleted);
303 return psf->sf_crcount && !gdeleted && !sdeleted;
304 }
305 return 0;
306}
307
308static int
309igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
310{
311 struct ip_sf_list *psf;
312 int scount = 0;
313
Weilong Chenc71151f2013-12-23 14:37:29 +0800314 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
316 continue;
317 scount++;
318 }
319 return scount;
320}
321
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100322static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct sk_buff *skb;
325 struct rtable *rt;
326 struct iphdr *pip;
327 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700328 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700329 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000330 int hlen = LL_RESERVED_SPACE(dev);
331 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100332 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000334 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000335 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000336 GFP_ATOMIC | __GFP_NOWARN);
337 if (skb)
338 break;
339 size >>= 1;
340 if (size < 256)
341 return NULL;
342 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200343 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
David S. Miller31e45432011-05-03 20:25:42 -0700345 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500346 0, 0,
347 IPPROTO_IGMP, 0, dev->ifindex);
348 if (IS_ERR(rt)) {
349 kfree_skb(skb);
350 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Changli Gaod8d1f302010-06-10 23:31:35 -0700353 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 skb->dev = dev;
355
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100356 skb->reserved_tailroom = skb_end_offset(skb) -
357 min(mtu, skb_end_offset(skb));
Herbert Xu66088242011-11-18 02:20:04 +0000358 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300360 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700361 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300362 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 pip->version = 4;
365 pip->ihl = (sizeof(struct iphdr)+4)>>2;
366 pip->tos = 0xc0;
367 pip->frag_off = htons(IP_DF);
368 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700369 pip->daddr = fl4.daddr;
370 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 pip->protocol = IPPROTO_IGMP;
372 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100373 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000374 ((u8 *)&pip[1])[0] = IPOPT_RA;
375 ((u8 *)&pip[1])[1] = 4;
376 ((u8 *)&pip[1])[2] = 0;
377 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700379 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300380 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300381 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
383 pig->resv1 = 0;
384 pig->csum = 0;
385 pig->resv2 = 0;
386 pig->ngrec = 0;
387 return skb;
388}
389
390static int igmpv3_sendpack(struct sk_buff *skb)
391{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300392 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000393 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300395 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Herbert Xuc439cb22008-01-11 19:14:00 -0800397 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
401{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800402 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
406 int type, struct igmpv3_grec **ppgr)
407{
408 struct net_device *dev = pmc->interface->dev;
409 struct igmpv3_report *pih;
410 struct igmpv3_grec *pgr;
411
412 if (!skb)
413 skb = igmpv3_newpack(dev, dev->mtu);
414 if (!skb)
415 return NULL;
416 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
417 pgr->grec_type = type;
418 pgr->grec_auxwords = 0;
419 pgr->grec_nsrcs = 0;
420 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300421 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 pih->ngrec = htons(ntohs(pih->ngrec)+1);
423 *ppgr = pgr;
424 return skb;
425}
426
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100427#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
430 int type, int gdeleted, int sdeleted)
431{
432 struct net_device *dev = pmc->interface->dev;
433 struct igmpv3_report *pih;
434 struct igmpv3_grec *pgr = NULL;
435 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800436 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (pmc->multiaddr == IGMP_ALL_HOSTS)
439 return skb;
440
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;
541 int type;
542
543 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000544 rcu_read_lock();
545 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (pmc->multiaddr == IGMP_ALL_HOSTS)
547 continue;
548 spin_lock_bh(&pmc->lock);
549 if (pmc->sfcount[MCAST_EXCLUDE])
550 type = IGMPV3_MODE_IS_EXCLUDE;
551 else
552 type = IGMPV3_MODE_IS_INCLUDE;
553 skb = add_grec(skb, pmc, type, 0, 0);
554 spin_unlock_bh(&pmc->lock);
555 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000556 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 } else {
558 spin_lock_bh(&pmc->lock);
559 if (pmc->sfcount[MCAST_EXCLUDE])
560 type = IGMPV3_MODE_IS_EXCLUDE;
561 else
562 type = IGMPV3_MODE_IS_INCLUDE;
563 skb = add_grec(skb, pmc, type, 0, 0);
564 spin_unlock_bh(&pmc->lock);
565 }
566 if (!skb)
567 return 0;
568 return igmpv3_sendpack(skb);
569}
570
571/*
572 * remove zero-count source records from a source filter list
573 */
574static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
575{
576 struct ip_sf_list *psf_prev, *psf_next, *psf;
577
578 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800579 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 psf_next = psf->sf_next;
581 if (psf->sf_crcount == 0) {
582 if (psf_prev)
583 psf_prev->sf_next = psf->sf_next;
584 else
585 *ppsf = psf->sf_next;
586 kfree(psf);
587 } else
588 psf_prev = psf;
589 }
590}
591
592static void igmpv3_send_cr(struct in_device *in_dev)
593{
594 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
595 struct sk_buff *skb = NULL;
596 int type, dtype;
597
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000598 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_lock_bh(&in_dev->mc_tomb_lock);
600
601 /* deleted MCA's */
602 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800603 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 pmc_next = pmc->next;
605 if (pmc->sfmode == MCAST_INCLUDE) {
606 type = IGMPV3_BLOCK_OLD_SOURCES;
607 dtype = IGMPV3_BLOCK_OLD_SOURCES;
608 skb = add_grec(skb, pmc, type, 1, 0);
609 skb = add_grec(skb, pmc, dtype, 1, 1);
610 }
611 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (pmc->sfmode == MCAST_EXCLUDE) {
613 type = IGMPV3_CHANGE_TO_INCLUDE;
614 skb = add_grec(skb, pmc, type, 1, 0);
615 }
David L Stevensad125832006-01-18 14:20:56 -0800616 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (pmc->crcount == 0) {
618 igmpv3_clear_zeros(&pmc->tomb);
619 igmpv3_clear_zeros(&pmc->sources);
620 }
621 }
622 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
623 if (pmc_prev)
624 pmc_prev->next = pmc_next;
625 else
626 in_dev->mc_tomb = pmc_next;
627 in_dev_put(pmc->interface);
628 kfree(pmc);
629 } else
630 pmc_prev = pmc;
631 }
632 spin_unlock_bh(&in_dev->mc_tomb_lock);
633
634 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000635 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 spin_lock_bh(&pmc->lock);
637 if (pmc->sfcount[MCAST_EXCLUDE]) {
638 type = IGMPV3_BLOCK_OLD_SOURCES;
639 dtype = IGMPV3_ALLOW_NEW_SOURCES;
640 } else {
641 type = IGMPV3_ALLOW_NEW_SOURCES;
642 dtype = IGMPV3_BLOCK_OLD_SOURCES;
643 }
644 skb = add_grec(skb, pmc, type, 0, 0);
645 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
646
647 /* filter mode changes */
648 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (pmc->sfmode == MCAST_EXCLUDE)
650 type = IGMPV3_CHANGE_TO_EXCLUDE;
651 else
652 type = IGMPV3_CHANGE_TO_INCLUDE;
653 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800654 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 spin_unlock_bh(&pmc->lock);
657 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000658 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (!skb)
661 return;
662 (void) igmpv3_sendpack(skb);
663}
664
665static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
666 int type)
667{
668 struct sk_buff *skb;
669 struct iphdr *iph;
670 struct igmphdr *ih;
671 struct rtable *rt;
672 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700673 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700674 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700675 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700676 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000677 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
680 return igmpv3_send_report(in_dev, pmc);
681 else if (type == IGMP_HOST_LEAVE_MESSAGE)
682 dst = IGMP_ALL_ROUTER;
683 else
684 dst = group;
685
David S. Miller31e45432011-05-03 20:25:42 -0700686 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500687 0, 0,
688 IPPROTO_IGMP, 0, dev->ifindex);
689 if (IS_ERR(rt))
690 return -1;
691
Herbert Xu66088242011-11-18 02:20:04 +0000692 hlen = LL_RESERVED_SPACE(dev);
693 tlen = dev->needed_tailroom;
694 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100695 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ip_rt_put(rt);
697 return -1;
698 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200699 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Changli Gaod8d1f302010-06-10 23:31:35 -0700701 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Herbert Xu66088242011-11-18 02:20:04 +0000703 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300705 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700706 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300707 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 iph->version = 4;
710 iph->ihl = (sizeof(struct iphdr)+4)>>2;
711 iph->tos = 0xc0;
712 iph->frag_off = htons(IP_DF);
713 iph->ttl = 1;
714 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700715 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100717 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000718 ((u8 *)&iph[1])[0] = IPOPT_RA;
719 ((u8 *)&iph[1])[1] = 4;
720 ((u8 *)&iph[1])[2] = 0;
721 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800724 ih->type = type;
725 ih->code = 0;
726 ih->csum = 0;
727 ih->group = group;
728 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Herbert Xuc439cb22008-01-11 19:14:00 -0800730 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733static void igmp_gq_timer_expire(unsigned long data)
734{
735 struct in_device *in_dev = (struct in_device *)data;
736
737 in_dev->mr_gq_running = 0;
738 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700739 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742static void igmp_ifc_timer_expire(unsigned long data)
743{
744 struct in_device *in_dev = (struct in_device *)data;
745
746 igmpv3_send_cr(in_dev);
747 if (in_dev->mr_ifc_count) {
748 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100749 igmp_ifc_start_timer(in_dev,
750 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700752 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static void igmp_ifc_event(struct in_device *in_dev)
756{
757 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
758 return;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +0200759 in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 igmp_ifc_start_timer(in_dev, 1);
761}
762
763
764static void igmp_timer_expire(unsigned long data)
765{
Weilong Chenc71151f2013-12-23 14:37:29 +0800766 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct in_device *in_dev = im->interface;
768
769 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800770 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (im->unsolicit_count) {
773 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100774 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 im->reporter = 1;
777 spin_unlock(&im->lock);
778
779 if (IGMP_V1_SEEN(in_dev))
780 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
781 else if (IGMP_V2_SEEN(in_dev))
782 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
783 else
784 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
785
786 ip_ma_put(im);
787}
788
David L Stevensad125832006-01-18 14:20:56 -0800789/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700790static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct ip_sf_list *psf;
793 int i, scount;
794
795 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800796 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (scount == nsrcs)
798 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800799 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800800 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000801 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800802 pmc->sfcount[MCAST_EXCLUDE] !=
803 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800804 break;
David L Stevensad125832006-01-18 14:20:56 -0800805 if (srcs[i] == psf->sf_inaddr) {
806 scount++;
807 break;
808 }
809 }
810 }
811 pmc->gsquery = 0;
812 if (scount == nsrcs) /* all sources excluded */
813 return 0;
814 return 1;
815}
816
Al Viroea4d9e72006-09-27 18:30:52 -0700817static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800818{
819 struct ip_sf_list *psf;
820 int i, scount;
821
822 if (pmc->sfmode == MCAST_EXCLUDE)
823 return igmp_xmarksources(pmc, nsrcs, srcs);
824
825 /* mark INCLUDE-mode sources */
826 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800827 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800828 if (scount == nsrcs)
829 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800830 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (srcs[i] == psf->sf_inaddr) {
832 psf->sf_gsresp = 1;
833 scount++;
834 break;
835 }
836 }
David L Stevensad125832006-01-18 14:20:56 -0800837 if (!scount) {
838 pmc->gsquery = 0;
839 return 0;
840 }
841 pmc->gsquery = 1;
842 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Eric Dumazetd679c532012-09-06 20:37:06 +0000845/* return true if packet was dropped */
846static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct ip_mc_list *im;
849
850 /* Timers are only set for non-local groups */
851
852 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000853 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000855 rcu_read_lock();
856 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (im->multiaddr == group) {
858 igmp_stop_timer(im);
859 break;
860 }
861 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000862 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000863 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Eric Dumazetd679c532012-09-06 20:37:06 +0000866/* return true if packet was dropped */
867static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 int len)
869{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300870 struct igmphdr *ih = igmp_hdr(skb);
871 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700873 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int max_delay;
875 int mark = 0;
876
877
David Stevens5b7c8402010-09-30 14:29:40 +0000878 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (ih->code == 0) {
880 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900881
Fabian Frederick436f7c22014-11-04 20:52:14 +0100882 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 in_dev->mr_v1_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100884 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 group = 0;
886 } else {
887 /* v2 router present */
888 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
889 in_dev->mr_v2_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100890 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 /* cancel the interface change timer */
893 in_dev->mr_ifc_count = 0;
894 if (del_timer(&in_dev->mr_ifc_timer))
895 __in_dev_put(in_dev);
896 /* clear deleted report items */
897 igmpv3_clear_delrec(in_dev);
898 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000899 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000900 } else if (IGMP_V1_SEEN(in_dev)) {
901 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100902 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000903 group = 0;
904 } else if (IGMP_V2_SEEN(in_dev)) {
905 /* this is a v3 query with v2 queriers present;
906 * Interpretation of the max_delay code is problematic here.
907 * A real v2 host would use ih_code directly, while v3 has a
908 * different encoding. We use the v3 encoding as more likely
909 * to be intended in a v3 query.
910 */
911 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800912 if (!max_delay)
913 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 } else { /* v3 */
915 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000916 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900917
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300918 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900920 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700921 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000922 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300923 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925
926 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
927 if (!max_delay)
928 max_delay = 1; /* can't mod w/ 0 */
929 in_dev->mr_maxdelay = max_delay;
930 if (ih3->qrv)
931 in_dev->mr_qrv = ih3->qrv;
932 if (!group) { /* general query */
933 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +0200934 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000936 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 /* mark sources to include, if group & source-specific */
939 mark = ih3->nsrcs != 0;
940 }
941
942 /*
943 * - Start the timers in all of our membership records
944 * that the query applies to for the interface on
945 * which the query arrived excl. those that belong
946 * to a "local" group (224.0.0.X)
947 * - For timers already running check if they need to
948 * be reset.
949 * - Use the igmp->igmp_code field as the maximum
950 * delay possible
951 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000952 rcu_read_lock();
953 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800954 int changed;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (group && group != im->multiaddr)
957 continue;
958 if (im->multiaddr == IGMP_ALL_HOSTS)
959 continue;
960 spin_lock_bh(&im->lock);
961 if (im->tm_running)
962 im->gsquery = im->gsquery && mark;
963 else
964 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800965 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900966 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800968 if (changed)
969 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000971 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000972 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000975/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976int igmp_rcv(struct sk_buff *skb)
977{
978 /* This basically follows the spec line by line -- see RFC1112 */
979 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000980 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +0000982 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Ian Morris51456b22015-04-03 09:17:26 +0100984 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800985 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Herbert Xufb286bb2005-11-10 13:01:24 -0800987 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000988 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800989
Tom Herbertde08dc12014-05-07 16:52:10 -0700990 if (skb_checksum_simple_validate(skb))
991 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300993 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 switch (ih->type) {
995 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +0000996 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
998 case IGMP_HOST_MEMBERSHIP_REPORT:
999 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001001 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
David Stevens24c69272005-12-02 20:32:59 -08001003 /* don't rely on MC router hearing unicast reports */
1004 if (skb->pkt_type == PACKET_MULTICAST ||
1005 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001006 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008 case IGMP_PIM:
1009#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return pim_rcv_v1(skb);
1011#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001012 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 case IGMP_DVMRP:
1014 case IGMP_TRACE:
1015 case IGMP_HOST_LEAVE_MESSAGE:
1016 case IGMP_MTRACE:
1017 case IGMP_MTRACE_RESP:
1018 break;
1019 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001022
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001023drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001024 if (dropped)
1025 kfree_skb(skb);
1026 else
1027 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 0;
1029}
1030
1031#endif
1032
1033
1034/*
1035 * Add a filter to a device
1036 */
1037
Al Viro63007722006-09-27 18:31:32 -07001038static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 char buf[MAX_ADDR_LEN];
1041 struct net_device *dev = in_dev->dev;
1042
1043 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1044 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001045 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 routine. Something sort of:
1047 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1048 --ANK
1049 */
1050 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001051 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054/*
1055 * Remove a filter from a device
1056 */
1057
Al Viro63007722006-09-27 18:31:32 -07001058static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 char buf[MAX_ADDR_LEN];
1061 struct net_device *dev = in_dev->dev;
1062
1063 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001064 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067#ifdef CONFIG_IP_MULTICAST
1068/*
1069 * deleted ip_mc_list manipulation
1070 */
1071static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1072{
1073 struct ip_mc_list *pmc;
1074
1075 /* this is an "ip_mc_list" for convenience; only the fields below
1076 * are actually used. In particular, the refcnt and users are not
1077 * used for management of the delete list. Using the same structure
1078 * for deleted items allows change reports to use common code with
1079 * non-deleted or query-response MCA's.
1080 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001081 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!pmc)
1083 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 spin_lock_bh(&im->lock);
1085 pmc->interface = im->interface;
1086 in_dev_hold(in_dev);
1087 pmc->multiaddr = im->multiaddr;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001088 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 pmc->sfmode = im->sfmode;
1090 if (pmc->sfmode == MCAST_INCLUDE) {
1091 struct ip_sf_list *psf;
1092
1093 pmc->tomb = im->tomb;
1094 pmc->sources = im->sources;
1095 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001096 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 psf->sf_crcount = pmc->crcount;
1098 }
1099 spin_unlock_bh(&im->lock);
1100
1101 spin_lock_bh(&in_dev->mc_tomb_lock);
1102 pmc->next = in_dev->mc_tomb;
1103 in_dev->mc_tomb = pmc;
1104 spin_unlock_bh(&in_dev->mc_tomb_lock);
1105}
1106
Al Viro63007722006-09-27 18:31:32 -07001107static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 struct ip_mc_list *pmc, *pmc_prev;
1110 struct ip_sf_list *psf, *psf_next;
1111
1112 spin_lock_bh(&in_dev->mc_tomb_lock);
1113 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001114 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (pmc->multiaddr == multiaddr)
1116 break;
1117 pmc_prev = pmc;
1118 }
1119 if (pmc) {
1120 if (pmc_prev)
1121 pmc_prev->next = pmc->next;
1122 else
1123 in_dev->mc_tomb = pmc->next;
1124 }
1125 spin_unlock_bh(&in_dev->mc_tomb_lock);
1126 if (pmc) {
Weilong Chenc71151f2013-12-23 14:37:29 +08001127 for (psf = pmc->tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 psf_next = psf->sf_next;
1129 kfree(psf);
1130 }
1131 in_dev_put(pmc->interface);
1132 kfree(pmc);
1133 }
1134}
1135
1136static void igmpv3_clear_delrec(struct in_device *in_dev)
1137{
1138 struct ip_mc_list *pmc, *nextpmc;
1139
1140 spin_lock_bh(&in_dev->mc_tomb_lock);
1141 pmc = in_dev->mc_tomb;
1142 in_dev->mc_tomb = NULL;
1143 spin_unlock_bh(&in_dev->mc_tomb_lock);
1144
1145 for (; pmc; pmc = nextpmc) {
1146 nextpmc = pmc->next;
1147 ip_mc_clear_src(pmc);
1148 in_dev_put(pmc->interface);
1149 kfree(pmc);
1150 }
1151 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001152 rcu_read_lock();
1153 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct ip_sf_list *psf, *psf_next;
1155
1156 spin_lock_bh(&pmc->lock);
1157 psf = pmc->tomb;
1158 pmc->tomb = NULL;
1159 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001160 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 psf_next = psf->sf_next;
1162 kfree(psf);
1163 }
1164 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001165 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167#endif
1168
1169static void igmp_group_dropped(struct ip_mc_list *im)
1170{
1171 struct in_device *in_dev = im->interface;
1172#ifdef CONFIG_IP_MULTICAST
1173 int reporter;
1174#endif
1175
1176 if (im->loaded) {
1177 im->loaded = 0;
1178 ip_mc_filter_del(in_dev, im->multiaddr);
1179 }
1180
1181#ifdef CONFIG_IP_MULTICAST
1182 if (im->multiaddr == IGMP_ALL_HOSTS)
1183 return;
1184
1185 reporter = im->reporter;
1186 igmp_stop_timer(im);
1187
1188 if (!in_dev->dead) {
1189 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001190 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (IGMP_V2_SEEN(in_dev)) {
1192 if (reporter)
1193 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 /* IGMPv3 */
1197 igmpv3_add_delrec(in_dev, im);
1198
1199 igmp_ifc_event(in_dev);
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204static void igmp_group_added(struct ip_mc_list *im)
1205{
1206 struct in_device *in_dev = im->interface;
1207
1208 if (im->loaded == 0) {
1209 im->loaded = 1;
1210 ip_mc_filter_add(in_dev, im->multiaddr);
1211 }
1212
1213#ifdef CONFIG_IP_MULTICAST
1214 if (im->multiaddr == IGMP_ALL_HOSTS)
1215 return;
1216
1217 if (in_dev->dead)
1218 return;
1219 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1220 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001221 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 spin_unlock_bh(&im->lock);
1223 return;
1224 }
1225 /* else, v3 */
1226
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001227 im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 igmp_ifc_event(in_dev);
1229#endif
1230}
1231
1232
1233/*
1234 * Multicast list managers
1235 */
1236
Eric Dumazete9897072013-06-07 08:48:57 -07001237static u32 ip_mc_hash(const struct ip_mc_list *im)
1238{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001239 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001240}
1241
1242static void ip_mc_hash_add(struct in_device *in_dev,
1243 struct ip_mc_list *im)
1244{
1245 struct ip_mc_list __rcu **mc_hash;
1246 u32 hash;
1247
1248 mc_hash = rtnl_dereference(in_dev->mc_hash);
1249 if (mc_hash) {
1250 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001251 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001252 rcu_assign_pointer(mc_hash[hash], im);
1253 return;
1254 }
1255
1256 /* do not use a hash table for small number of items */
1257 if (in_dev->mc_count < 4)
1258 return;
1259
1260 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1261 GFP_KERNEL);
1262 if (!mc_hash)
1263 return;
1264
1265 for_each_pmc_rtnl(in_dev, im) {
1266 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001267 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001268 RCU_INIT_POINTER(mc_hash[hash], im);
1269 }
1270
1271 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1272}
1273
1274static void ip_mc_hash_remove(struct in_device *in_dev,
1275 struct ip_mc_list *im)
1276{
1277 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1278 struct ip_mc_list *aux;
1279
1280 if (!mc_hash)
1281 return;
1282 mc_hash += ip_mc_hash(im);
1283 while ((aux = rtnl_dereference(*mc_hash)) != im)
1284 mc_hash = &aux->next_hash;
1285 *mc_hash = im->next_hash;
1286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289/*
1290 * A socket has joined a multicast group on device dev.
1291 */
1292
Al Viro8f935bb2006-09-27 18:30:07 -07001293void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 struct ip_mc_list *im;
1296
1297 ASSERT_RTNL();
1298
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001299 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (im->multiaddr == addr) {
1301 im->users++;
1302 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1303 goto out;
1304 }
1305 }
1306
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001307 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (!im)
1309 goto out;
1310
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001311 im->users = 1;
1312 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001314 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* initial mode is (EX, empty) */
1316 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 atomic_set(&im->refcnt, 1);
1319 spin_lock_init(&im->lock);
1320#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301321 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001322 im->unsolicit_count = sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001324
1325 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001326 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001327 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001328
Eric Dumazete9897072013-06-07 08:48:57 -07001329 ip_mc_hash_add(in_dev, im);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331#ifdef CONFIG_IP_MULTICAST
1332 igmpv3_del_delrec(in_dev, im->multiaddr);
1333#endif
1334 igmp_group_added(im);
1335 if (!in_dev->dead)
1336 ip_rt_multicast_event(in_dev);
1337out:
1338 return;
1339}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001340EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001342static int ip_mc_check_iphdr(struct sk_buff *skb)
1343{
1344 const struct iphdr *iph;
1345 unsigned int len;
1346 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1347
1348 if (!pskb_may_pull(skb, offset))
1349 return -EINVAL;
1350
1351 iph = ip_hdr(skb);
1352
1353 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1354 return -EINVAL;
1355
1356 offset += ip_hdrlen(skb) - sizeof(*iph);
1357
1358 if (!pskb_may_pull(skb, offset))
1359 return -EINVAL;
1360
1361 iph = ip_hdr(skb);
1362
1363 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1364 return -EINVAL;
1365
1366 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1367 if (skb->len < len || len < offset)
1368 return -EINVAL;
1369
1370 skb_set_transport_header(skb, offset);
1371
1372 return 0;
1373}
1374
1375static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1376{
1377 unsigned int len = skb_transport_offset(skb);
1378
1379 len += sizeof(struct igmpv3_report);
1380
1381 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1382}
1383
1384static int ip_mc_check_igmp_query(struct sk_buff *skb)
1385{
1386 unsigned int len = skb_transport_offset(skb);
1387
1388 len += sizeof(struct igmphdr);
1389 if (skb->len < len)
1390 return -EINVAL;
1391
1392 /* IGMPv{1,2}? */
1393 if (skb->len != len) {
1394 /* or IGMPv3? */
1395 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1396 if (skb->len < len || !pskb_may_pull(skb, len))
1397 return -EINVAL;
1398 }
1399
1400 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1401 * all-systems destination addresses (224.0.0.1) for general queries
1402 */
1403 if (!igmp_hdr(skb)->group &&
1404 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1405 return -EINVAL;
1406
1407 return 0;
1408}
1409
1410static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1411{
1412 switch (igmp_hdr(skb)->type) {
1413 case IGMP_HOST_LEAVE_MESSAGE:
1414 case IGMP_HOST_MEMBERSHIP_REPORT:
1415 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1416 /* fall through */
1417 return 0;
1418 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1419 return ip_mc_check_igmp_reportv3(skb);
1420 case IGMP_HOST_MEMBERSHIP_QUERY:
1421 return ip_mc_check_igmp_query(skb);
1422 default:
1423 return -ENOMSG;
1424 }
1425}
1426
1427static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1428{
1429 return skb_checksum_simple_validate(skb);
1430}
1431
1432static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1433
1434{
1435 struct sk_buff *skb_chk;
1436 unsigned int transport_len;
1437 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga5169932015-08-13 05:54:07 +02001438 int ret = -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001439
1440 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1441
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001442 skb_chk = skb_checksum_trimmed(skb, transport_len,
1443 ip_mc_validate_checksum);
1444 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02001445 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001446
Linus Lüssinga5169932015-08-13 05:54:07 +02001447 if (!pskb_may_pull(skb_chk, len))
1448 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001449
1450 ret = ip_mc_check_igmp_msg(skb_chk);
Linus Lüssinga5169932015-08-13 05:54:07 +02001451 if (ret)
1452 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001453
1454 if (skb_trimmed)
1455 *skb_trimmed = skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02001456 /* free now unneeded clone */
1457 else if (skb_chk != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001458 kfree_skb(skb_chk);
1459
Linus Lüssinga5169932015-08-13 05:54:07 +02001460 ret = 0;
1461
1462err:
1463 if (ret && skb_chk && skb_chk != skb)
1464 kfree_skb(skb_chk);
1465
1466 return ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001467}
1468
1469/**
1470 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1471 * @skb: the skb to validate
1472 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1473 *
1474 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001475 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001476 *
1477 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1478 * standard
1479 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1480 * -ENOMEM: A memory allocation failure happened.
1481 *
1482 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1483 * to NULL): After parsing an IGMP packet successfully it will point to
1484 * an skb which has its tail aligned to the IP packet end. This might
1485 * either be the originally provided skb or a trimmed, cloned version if
1486 * the skb frame had data beyond the IP packet. A cloned skb allows us
1487 * to leave the original skb and its full frame unchanged (which might be
1488 * desirable for layer 2 frame jugglers).
1489 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001490 * Caller needs to set the skb network header and free any returned skb if it
1491 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001492 */
1493int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1494{
1495 int ret = ip_mc_check_iphdr(skb);
1496
1497 if (ret < 0)
1498 return ret;
1499
1500 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1501 return -ENOMSG;
1502
1503 return __ip_mc_check_igmp(skb, skb_trimmed);
1504}
1505EXPORT_SYMBOL(ip_mc_check_igmp);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001508 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001509 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001510static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001511{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001512#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001513 struct ip_mc_list *im;
1514 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001515
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001516 ASSERT_RTNL();
1517
1518 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001519 if (im->multiaddr == IGMP_ALL_HOSTS)
1520 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001521
Eric Dumazet866f3b22010-11-18 09:33:19 -08001522 /* a failover is happening and switches
1523 * must be notified immediately
1524 */
1525 if (IGMP_V1_SEEN(in_dev))
1526 type = IGMP_HOST_MEMBERSHIP_REPORT;
1527 else if (IGMP_V2_SEEN(in_dev))
1528 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1529 else
1530 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1531 igmp_send_report(in_dev, im, type);
1532 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001533#endif
1534}
1535
1536/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * A socket has left a multicast group on device dev
1538 */
1539
Al Viro8f935bb2006-09-27 18:30:07 -07001540void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001542 struct ip_mc_list *i;
1543 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001546
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001547 for (ip = &in_dev->mc_list;
1548 (i = rtnl_dereference(*ip)) != NULL;
1549 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001550 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001552 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001553 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001554 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001556 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (!in_dev->dead)
1559 ip_rt_multicast_event(in_dev);
1560
1561 ip_ma_put(i);
1562 return;
1563 }
1564 break;
1565 }
1566 }
1567}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001568EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Moni Shoua75c78502009-09-15 02:37:40 -07001570/* Device changing type */
1571
1572void ip_mc_unmap(struct in_device *in_dev)
1573{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001574 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001575
1576 ASSERT_RTNL();
1577
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001578 for_each_pmc_rtnl(in_dev, pmc)
1579 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001580}
1581
1582void ip_mc_remap(struct in_device *in_dev)
1583{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001584 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001585
1586 ASSERT_RTNL();
1587
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001588 for_each_pmc_rtnl(in_dev, pmc)
1589 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592/* Device going down */
1593
1594void ip_mc_down(struct in_device *in_dev)
1595{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001596 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 ASSERT_RTNL();
1599
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001600 for_each_pmc_rtnl(in_dev, pmc)
1601 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603#ifdef CONFIG_IP_MULTICAST
1604 in_dev->mr_ifc_count = 0;
1605 if (del_timer(&in_dev->mr_ifc_timer))
1606 __in_dev_put(in_dev);
1607 in_dev->mr_gq_running = 0;
1608 if (del_timer(&in_dev->mr_gq_timer))
1609 __in_dev_put(in_dev);
1610 igmpv3_clear_delrec(in_dev);
1611#endif
1612
1613 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1614}
1615
1616void ip_mc_init_dev(struct in_device *in_dev)
1617{
1618 ASSERT_RTNL();
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001621 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1622 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001623 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1624 (unsigned long)in_dev);
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001625 in_dev->mr_qrv = sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626#endif
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 spin_lock_init(&in_dev->mc_tomb_lock);
1629}
1630
1631/* Device going up */
1632
1633void ip_mc_up(struct in_device *in_dev)
1634{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001635 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 ASSERT_RTNL();
1638
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001639#ifdef CONFIG_IP_MULTICAST
1640 in_dev->mr_qrv = sysctl_igmp_qrv;
1641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1643
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001644 for_each_pmc_rtnl(in_dev, pmc)
1645 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648/*
1649 * Device is about to be destroyed: clean up.
1650 */
1651
1652void ip_mc_destroy_dev(struct in_device *in_dev)
1653{
1654 struct ip_mc_list *i;
1655
1656 ASSERT_RTNL();
1657
1658 /* Deactivate timers */
1659 ip_mc_down(in_dev);
1660
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001661 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1662 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001663 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001664
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001665 /* We've dropped the groups in ip_mc_down already */
1666 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001671/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001672static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 struct net_device *dev = NULL;
1675 struct in_device *idev = NULL;
1676
1677 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001678 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return idev;
1680 }
1681 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001682 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (!dev)
1684 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
David S. Millerb23dd4f2011-03-02 14:31:35 -08001687 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001688 struct rtable *rt = ip_route_output(net,
1689 imr->imr_multiaddr.s_addr,
1690 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001691 if (!IS_ERR(rt)) {
1692 dev = rt->dst.dev;
1693 ip_rt_put(rt);
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696 if (dev) {
1697 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001698 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 return idev;
1701}
1702
1703/*
1704 * Join a socket to a group
1705 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001706int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1707int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001708#ifdef CONFIG_IP_MULTICAST
Fabian Frederick436f7c22014-11-04 20:52:14 +01001709int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001710#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001713 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 struct ip_sf_list *psf, *psf_prev;
1716 int rv = 0;
1717
1718 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001719 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (psf->sf_inaddr == *psfsrc)
1721 break;
1722 psf_prev = psf;
1723 }
1724 if (!psf || psf->sf_count[sfmode] == 0) {
1725 /* source filter not found, or count wrong => bug */
1726 return -ESRCH;
1727 }
1728 psf->sf_count[sfmode]--;
1729 if (psf->sf_count[sfmode] == 0) {
1730 ip_rt_multicast_event(pmc->interface);
1731 }
1732 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1733#ifdef CONFIG_IP_MULTICAST
1734 struct in_device *in_dev = pmc->interface;
1735#endif
1736
1737 /* no more filters for this source */
1738 if (psf_prev)
1739 psf_prev->sf_next = psf->sf_next;
1740 else
1741 pmc->sources = psf->sf_next;
1742#ifdef CONFIG_IP_MULTICAST
1743 if (psf->sf_oldin &&
1744 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001745 psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 psf->sf_next = pmc->tomb;
1747 pmc->tomb = psf;
1748 rv = 1;
1749 } else
1750#endif
1751 kfree(psf);
1752 }
1753 return rv;
1754}
1755
1756#ifndef CONFIG_IP_MULTICAST
1757#define igmp_ifc_event(x) do { } while (0)
1758#endif
1759
Al Viro8f935bb2006-09-27 18:30:07 -07001760static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1761 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 struct ip_mc_list *pmc;
1764 int changerec = 0;
1765 int i, err;
1766
1767 if (!in_dev)
1768 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001769 rcu_read_lock();
1770 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (*pmca == pmc->multiaddr)
1772 break;
1773 }
1774 if (!pmc) {
1775 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001776 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return -ESRCH;
1778 }
1779 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001780 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781#ifdef CONFIG_IP_MULTICAST
1782 sf_markstate(pmc);
1783#endif
1784 if (!delta) {
1785 err = -EINVAL;
1786 if (!pmc->sfcount[sfmode])
1787 goto out_unlock;
1788 pmc->sfcount[sfmode]--;
1789 }
1790 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001791 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1793
1794 changerec |= rv > 0;
1795 if (!err && rv < 0)
1796 err = rv;
1797 }
1798 if (pmc->sfmode == MCAST_EXCLUDE &&
1799 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1800 pmc->sfcount[MCAST_INCLUDE]) {
1801#ifdef CONFIG_IP_MULTICAST
1802 struct ip_sf_list *psf;
1803#endif
1804
1805 /* filter mode change */
1806 pmc->sfmode = MCAST_INCLUDE;
1807#ifdef CONFIG_IP_MULTICAST
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001808 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001810 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 psf->sf_crcount = 0;
1812 igmp_ifc_event(pmc->interface);
1813 } else if (sf_setstate(pmc) || changerec) {
1814 igmp_ifc_event(pmc->interface);
1815#endif
1816 }
1817out_unlock:
1818 spin_unlock_bh(&pmc->lock);
1819 return err;
1820}
1821
1822/*
1823 * Add multicast single-source filter to the interface list
1824 */
1825static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001826 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 struct ip_sf_list *psf, *psf_prev;
1829
1830 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001831 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (psf->sf_inaddr == *psfsrc)
1833 break;
1834 psf_prev = psf;
1835 }
1836 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001837 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (!psf)
1839 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 psf->sf_inaddr = *psfsrc;
1841 if (psf_prev) {
1842 psf_prev->sf_next = psf;
1843 } else
1844 pmc->sources = psf;
1845 }
1846 psf->sf_count[sfmode]++;
1847 if (psf->sf_count[sfmode] == 1) {
1848 ip_rt_multicast_event(pmc->interface);
1849 }
1850 return 0;
1851}
1852
1853#ifdef CONFIG_IP_MULTICAST
1854static void sf_markstate(struct ip_mc_list *pmc)
1855{
1856 struct ip_sf_list *psf;
1857 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1858
Weilong Chenc71151f2013-12-23 14:37:29 +08001859 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (pmc->sfcount[MCAST_EXCLUDE]) {
1861 psf->sf_oldin = mca_xcount ==
1862 psf->sf_count[MCAST_EXCLUDE] &&
1863 !psf->sf_count[MCAST_INCLUDE];
1864 } else
1865 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1866}
1867
1868static int sf_setstate(struct ip_mc_list *pmc)
1869{
David L Stevensad125832006-01-18 14:20:56 -08001870 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1872 int qrv = pmc->interface->mr_qrv;
1873 int new_in, rv;
1874
1875 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001876 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (pmc->sfcount[MCAST_EXCLUDE]) {
1878 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1879 !psf->sf_count[MCAST_INCLUDE];
1880 } else
1881 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001882 if (new_in) {
1883 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001884 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001885
Weilong Chenc71151f2013-12-23 14:37:29 +08001886 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001887 if (dpsf->sf_inaddr == psf->sf_inaddr)
1888 break;
1889 prev = dpsf;
1890 }
1891 if (dpsf) {
1892 if (prev)
1893 prev->sf_next = dpsf->sf_next;
1894 else
1895 pmc->tomb = dpsf->sf_next;
1896 kfree(dpsf);
1897 }
1898 psf->sf_crcount = qrv;
1899 rv++;
1900 }
1901 } else if (psf->sf_oldin) {
1902
1903 psf->sf_crcount = 0;
1904 /*
1905 * add or update "delete" records if an active filter
1906 * is now inactive
1907 */
Weilong Chenc71151f2013-12-23 14:37:29 +08001908 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08001909 if (dpsf->sf_inaddr == psf->sf_inaddr)
1910 break;
1911 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001912 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001913 if (!dpsf)
1914 continue;
1915 *dpsf = *psf;
1916 /* pmc->lock held by callers */
1917 dpsf->sf_next = pmc->tomb;
1918 pmc->tomb = dpsf;
1919 }
1920 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 rv++;
1922 }
1923 }
1924 return rv;
1925}
1926#endif
1927
1928/*
1929 * Add multicast source filter list to the interface list
1930 */
Al Viro8f935bb2006-09-27 18:30:07 -07001931static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1932 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 struct ip_mc_list *pmc;
1935 int isexclude;
1936 int i, err;
1937
1938 if (!in_dev)
1939 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001940 rcu_read_lock();
1941 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (*pmca == pmc->multiaddr)
1943 break;
1944 }
1945 if (!pmc) {
1946 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001947 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return -ESRCH;
1949 }
1950 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001951 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953#ifdef CONFIG_IP_MULTICAST
1954 sf_markstate(pmc);
1955#endif
1956 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1957 if (!delta)
1958 pmc->sfcount[sfmode]++;
1959 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001960 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00001961 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (err)
1963 break;
1964 }
1965 if (err) {
1966 int j;
1967
Jun Zhao685f94e62011-11-22 17:19:03 +00001968 if (!delta)
1969 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08001970 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00001971 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1973#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001975 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976#endif
1977
1978 /* filter mode change */
1979 if (pmc->sfcount[MCAST_EXCLUDE])
1980 pmc->sfmode = MCAST_EXCLUDE;
1981 else if (pmc->sfcount[MCAST_INCLUDE])
1982 pmc->sfmode = MCAST_INCLUDE;
1983#ifdef CONFIG_IP_MULTICAST
1984 /* else no filters; keep old mode for reports */
1985
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001986 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001988 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 psf->sf_crcount = 0;
1990 igmp_ifc_event(in_dev);
1991 } else if (sf_setstate(pmc)) {
1992 igmp_ifc_event(in_dev);
1993#endif
1994 }
1995 spin_unlock_bh(&pmc->lock);
1996 return err;
1997}
1998
1999static void ip_mc_clear_src(struct ip_mc_list *pmc)
2000{
2001 struct ip_sf_list *psf, *nextpsf;
2002
Weilong Chenc71151f2013-12-23 14:37:29 +08002003 for (psf = pmc->tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 nextpsf = psf->sf_next;
2005 kfree(psf);
2006 }
2007 pmc->tomb = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08002008 for (psf = pmc->sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 nextpsf = psf->sf_next;
2010 kfree(psf);
2011 }
2012 pmc->sources = NULL;
2013 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002014 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 pmc->sfcount[MCAST_EXCLUDE] = 1;
2016}
2017
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002018/* Join a multicast group
2019 */
2020
2021int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Al Viro8f935bb2006-09-27 18:30:07 -07002023 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002024 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 struct in_device *in_dev;
2026 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002027 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002028 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002030 int err;
2031
2032 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Joe Perchesf97c1e02007-12-16 13:45:43 -08002034 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return -EINVAL;
2036
Daniel Lezcano877aced2008-08-13 16:15:57 -07002037 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 err = -ENODEV;
2041 goto done;
2042 }
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002045 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002046 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002047 if (i->multi.imr_multiaddr.s_addr == addr &&
2048 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 count++;
2051 }
2052 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07002053 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002055 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002056 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002057 goto done;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002060 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 iml->sflist = NULL;
2062 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002063 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return err;
2068}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002069EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2072 struct in_device *in_dev)
2073{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002074 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int err;
2076
Ian Morris51456b22015-04-03 09:17:26 +01002077 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 /* any-source empty exclude case */
2079 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2080 iml->sfmode, 0, NULL, 0);
2081 }
2082 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002083 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002084 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002085 /* decrease mem now to avoid the memleak warning */
2086 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002087 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return err;
2089}
2090
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002091int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002094 struct ip_mc_socklist *iml;
2095 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002096 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002097 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002098 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002099 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002100 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Eric Dumazet959d10f2015-02-17 03:19:24 -08002102 ASSERT_RTNL();
2103
Daniel Lezcano877aced2008-08-13 16:15:57 -07002104 in_dev = ip_mc_find_dev(net, imr);
dingtianhong52ad3532014-07-02 13:50:48 +08002105 if (!in_dev) {
2106 ret = -ENODEV;
2107 goto out;
2108 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002109 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002110 for (imlp = &inet->mc_list;
2111 (iml = rtnl_dereference(*imlp)) != NULL;
2112 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002113 if (iml->multi.imr_multiaddr.s_addr != group)
2114 continue;
2115 if (ifindex) {
2116 if (iml->multi.imr_ifindex != ifindex)
2117 continue;
2118 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2119 iml->multi.imr_address.s_addr)
2120 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
David L Stevensacd6e002006-08-17 16:27:39 -07002122 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002124 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002125
dingtianhong52ad3532014-07-02 13:50:48 +08002126 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002127
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002128 /* decrease mem now to avoid the memleak warning */
2129 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002130 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
dingtianhong52ad3532014-07-02 13:50:48 +08002133out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002134 return ret;
2135}
stephen hemminger193ba922012-10-01 12:32:34 +00002136EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138int ip_mc_source(int add, int omode, struct sock *sk, struct
2139 ip_mreq_source *mreqs, int ifindex)
2140{
2141 int err;
2142 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002143 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct ip_mc_socklist *pmc;
2145 struct in_device *in_dev = NULL;
2146 struct inet_sock *inet = inet_sk(sk);
2147 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002148 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002149 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 int i, j, rv;
2151
Joe Perchesf97c1e02007-12-16 13:45:43 -08002152 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return -EINVAL;
2154
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002155 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2158 imr.imr_address.s_addr = mreqs->imr_interface;
2159 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002160 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 if (!in_dev) {
2163 err = -ENODEV;
2164 goto done;
2165 }
2166 err = -EADDRNOTAVAIL;
2167
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002168 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002169 if ((pmc->multi.imr_multiaddr.s_addr ==
2170 imr.imr_multiaddr.s_addr) &&
2171 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 break;
2173 }
David L Stevens917f2f12005-07-08 17:45:16 -07002174 if (!pmc) { /* must have a prior join */
2175 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /* if a source filter was set, must be the same mode as before */
2179 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002180 if (pmc->sfmode != omode) {
2181 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 } else if (pmc->sfmode != omode) {
2185 /* allow mode switches for empty-set filters */
2186 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002187 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 NULL, 0);
2189 pmc->sfmode = omode;
2190 }
2191
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002192 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (!add) {
2194 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002195 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002197 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002199 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (rv == 0)
2201 break;
2202 }
2203 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002204 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
David L Stevens8cdaaa12005-07-08 17:39:23 -07002206 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2207 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2208 leavegroup = 1;
2209 goto done;
2210 }
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002213 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 &mreqs->imr_sourceaddr, 1);
2215
Weilong Chenc71151f2013-12-23 14:37:29 +08002216 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 psl->sl_addr[j-1] = psl->sl_addr[j];
2218 psl->sl_count--;
2219 err = 0;
2220 goto done;
2221 }
2222 /* else, add a new source to the filter */
2223
2224 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2225 err = -ENOBUFS;
2226 goto done;
2227 }
2228 if (!psl || psl->sl_count == psl->sl_max) {
2229 struct ip_sf_socklist *newpsl;
2230 int count = IP_SFBLOCK;
2231
2232 if (psl)
2233 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002234 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (!newpsl) {
2236 err = -ENOBUFS;
2237 goto done;
2238 }
2239 newpsl->sl_max = count;
2240 newpsl->sl_count = count - IP_SFBLOCK;
2241 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002242 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002244 /* decrease mem now to avoid the memleak warning */
2245 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002246 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002248 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002249 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002252 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002254 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (rv == 0)
2256 break;
2257 }
2258 if (rv == 0) /* address already there is an error */
2259 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002260 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 psl->sl_addr[j+1] = psl->sl_addr[j];
2262 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2263 psl->sl_count++;
2264 err = 0;
2265 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002266 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 &mreqs->imr_sourceaddr, 1);
2268done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002269 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002270 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return err;
2272}
2273
2274int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2275{
David L Stevens9951f032005-07-08 17:47:28 -07002276 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002278 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct ip_mc_socklist *pmc;
2280 struct in_device *in_dev;
2281 struct inet_sock *inet = inet_sk(sk);
2282 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002283 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002284 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Joe Perchesf97c1e02007-12-16 13:45:43 -08002286 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return -EINVAL;
2288 if (msf->imsf_fmode != MCAST_INCLUDE &&
2289 msf->imsf_fmode != MCAST_EXCLUDE)
2290 return -EINVAL;
2291
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002292 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2295 imr.imr_address.s_addr = msf->imsf_interface;
2296 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002297 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 if (!in_dev) {
2300 err = -ENODEV;
2301 goto done;
2302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
David L Stevens9951f032005-07-08 17:47:28 -07002304 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2305 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2306 leavegroup = 1;
2307 goto done;
2308 }
2309
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002310 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2312 pmc->multi.imr_ifindex == imr.imr_ifindex)
2313 break;
2314 }
David L Stevens917f2f12005-07-08 17:45:16 -07002315 if (!pmc) { /* must have a prior join */
2316 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002320 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2321 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (!newpsl) {
2323 err = -ENOBUFS;
2324 goto done;
2325 }
2326 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2327 memcpy(newpsl->sl_addr, msf->imsf_slist,
2328 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2329 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2330 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2331 if (err) {
2332 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2333 goto done;
2334 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002335 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002337 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2338 msf->imsf_fmode, 0, NULL, 0);
2339 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002340 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (psl) {
2342 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2343 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002344 /* decrease mem now to avoid the memleak warning */
2345 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002346 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 } else
2348 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2349 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002350 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002352 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353done:
David L Stevens9951f032005-07-08 17:47:28 -07002354 if (leavegroup)
2355 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return err;
2357}
2358
2359int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2360 struct ip_msfilter __user *optval, int __user *optlen)
2361{
2362 int err, len, count, copycount;
2363 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002364 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct ip_mc_socklist *pmc;
2366 struct in_device *in_dev;
2367 struct inet_sock *inet = inet_sk(sk);
2368 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002369 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Joe Perchesf97c1e02007-12-16 13:45:43 -08002371 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return -EINVAL;
2373
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002374 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2377 imr.imr_address.s_addr = msf->imsf_interface;
2378 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002379 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 if (!in_dev) {
2382 err = -ENODEV;
2383 goto done;
2384 }
2385 err = -EADDRNOTAVAIL;
2386
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002387 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2389 pmc->multi.imr_ifindex == imr.imr_ifindex)
2390 break;
2391 }
2392 if (!pmc) /* must have a prior join */
2393 goto done;
2394 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002395 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002396 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (!psl) {
2398 len = 0;
2399 count = 0;
2400 } else {
2401 count = psl->sl_count;
2402 }
2403 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2404 len = copycount * sizeof(psl->sl_addr[0]);
2405 msf->imsf_numsrc = count;
2406 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2407 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2408 return -EFAULT;
2409 }
2410 if (len &&
2411 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2412 return -EFAULT;
2413 return 0;
2414done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002415 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return err;
2417}
2418
2419int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2420 struct group_filter __user *optval, int __user *optlen)
2421{
2422 int err, i, count, copycount;
2423 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002424 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 struct ip_mc_socklist *pmc;
2426 struct inet_sock *inet = inet_sk(sk);
2427 struct ip_sf_socklist *psl;
2428
2429 psin = (struct sockaddr_in *)&gsf->gf_group;
2430 if (psin->sin_family != AF_INET)
2431 return -EINVAL;
2432 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002433 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return -EINVAL;
2435
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002436 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 err = -EADDRNOTAVAIL;
2439
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002440 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2442 pmc->multi.imr_ifindex == gsf->gf_interface)
2443 break;
2444 }
2445 if (!pmc) /* must have a prior join */
2446 goto done;
2447 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002448 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002449 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 count = psl ? psl->sl_count : 0;
2451 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2452 gsf->gf_numsrc = count;
2453 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2454 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2455 return -EFAULT;
2456 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002457 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 struct sockaddr_storage ss;
2459
2460 psin = (struct sockaddr_in *)&ss;
2461 memset(&ss, 0, sizeof(ss));
2462 psin->sin_family = AF_INET;
2463 psin->sin_addr.s_addr = psl->sl_addr[i];
2464 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2465 return -EFAULT;
2466 }
2467 return 0;
2468done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002469 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 return err;
2471}
2472
2473/*
2474 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2475 */
Al Viroc0cda062006-09-27 18:31:10 -07002476int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 struct inet_sock *inet = inet_sk(sk);
2479 struct ip_mc_socklist *pmc;
2480 struct ip_sf_socklist *psl;
2481 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002482 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002484 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002485 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002488 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002489 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2491 pmc->multi.imr_ifindex == dif)
2492 break;
2493 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002494 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002496 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002497 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002498 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002500 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Weilong Chenc71151f2013-12-23 14:37:29 +08002502 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (psl->sl_addr[i] == rmt_addr)
2504 break;
2505 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002506 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002508 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002510 goto unlock;
2511 ret = 1;
2512unlock:
2513 rcu_read_unlock();
2514out:
2515 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
2518/*
2519 * A socket is closing.
2520 */
2521
2522void ip_mc_drop_socket(struct sock *sk)
2523{
2524 struct inet_sock *inet = inet_sk(sk);
2525 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002526 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Ian Morris51456b22015-04-03 09:17:26 +01002528 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return;
2530
2531 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002532 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002535 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002536 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002537 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002538 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002540 /* decrease mem now to avoid the memleak warning */
2541 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002542 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 rtnl_unlock();
2545}
2546
David S. Millerdbdd9a52011-03-10 16:34:38 -08002547/* called with rcu_read_lock() */
2548int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002551 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 struct ip_sf_list *psf;
2553 int rv = 0;
2554
Eric Dumazete9897072013-06-07 08:48:57 -07002555 mc_hash = rcu_dereference(in_dev->mc_hash);
2556 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002557 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002558
2559 for (im = rcu_dereference(mc_hash[hash]);
2560 im != NULL;
2561 im = rcu_dereference(im->next_hash)) {
2562 if (im->multiaddr == mc_addr)
2563 break;
2564 }
2565 } else {
2566 for_each_pmc_rcu(in_dev, im) {
2567 if (im->multiaddr == mc_addr)
2568 break;
2569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 if (im && proto == IPPROTO_IGMP) {
2572 rv = 1;
2573 } else if (im) {
2574 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002575 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (psf->sf_inaddr == src_addr)
2577 break;
2578 }
2579 if (psf)
2580 rv = psf->sf_count[MCAST_INCLUDE] ||
2581 psf->sf_count[MCAST_EXCLUDE] !=
2582 im->sfcount[MCAST_EXCLUDE];
2583 else
2584 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2585 } else
2586 rv = 1; /* unspecified source; tentatively allow */
2587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return rv;
2589}
2590
2591#if defined(CONFIG_PROC_FS)
2592struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002593 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 struct net_device *dev;
2595 struct in_device *in_dev;
2596};
2597
2598#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2599
2600static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2601{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002602 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 struct ip_mc_list *im = NULL;
2604 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2605
Pavel Emelianov7562f872007-05-03 15:13:45 -07002606 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002607 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002609
2610 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (!in_dev)
2612 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002613 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (im) {
2615 state->in_dev = in_dev;
2616 break;
2617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
2619 return im;
2620}
2621
2622static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2623{
2624 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002625
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002626 im = rcu_dereference(im->next_rcu);
2627 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002628 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (!state->dev) {
2630 state->in_dev = NULL;
2631 break;
2632 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002633 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (!state->in_dev)
2635 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002636 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
2638 return im;
2639}
2640
2641static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2642{
2643 struct ip_mc_list *im = igmp_mc_get_first(seq);
2644 if (im)
2645 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2646 --pos;
2647 return pos ? NULL : im;
2648}
2649
2650static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002651 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
stephen hemminger61fbab72009-11-10 07:54:55 +00002653 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2655}
2656
2657static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2658{
2659 struct ip_mc_list *im;
2660 if (v == SEQ_START_TOKEN)
2661 im = igmp_mc_get_first(seq);
2662 else
2663 im = igmp_mc_get_next(seq, v);
2664 ++*pos;
2665 return im;
2666}
2667
2668static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002669 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002672
2673 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002675 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
2678static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2679{
2680 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002681 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2683 else {
2684 struct ip_mc_list *im = (struct ip_mc_list *)v;
2685 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2686 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002687 long delta;
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689#ifdef CONFIG_IP_MULTICAST
2690 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2691 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2692 "V3";
2693#else
2694 querier = "NONE";
2695#endif
2696
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002697 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002699 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
2701
Eric Dumazeta399a802012-08-08 21:13:53 +00002702 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002704 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002706 im->tm_running,
2707 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 im->reporter);
2709 }
2710 return 0;
2711}
2712
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002713static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 .start = igmp_mc_seq_start,
2715 .next = igmp_mc_seq_next,
2716 .stop = igmp_mc_seq_stop,
2717 .show = igmp_mc_seq_show,
2718};
2719
2720static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2721{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002722 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002723 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Arjan van de Ven9a321442007-02-12 00:55:35 -08002726static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 .owner = THIS_MODULE,
2728 .open = igmp_mc_seq_open,
2729 .read = seq_read,
2730 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002731 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732};
2733
2734struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002735 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 struct net_device *dev;
2737 struct in_device *idev;
2738 struct ip_mc_list *im;
2739};
2740
2741#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2742
2743static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2744{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002745 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 struct ip_sf_list *psf = NULL;
2747 struct ip_mc_list *im = NULL;
2748 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2749
Pavel Emelianov7562f872007-05-03 15:13:45 -07002750 state->idev = NULL;
2751 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002752 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002754 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002755 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002757 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002758 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 spin_lock_bh(&im->lock);
2760 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002761 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 state->im = im;
2763 state->idev = idev;
2764 break;
2765 }
2766 spin_unlock_bh(&im->lock);
2767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
2769 return psf;
2770}
2771
2772static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2773{
2774 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2775
2776 psf = psf->sf_next;
2777 while (!psf) {
2778 spin_unlock_bh(&state->im->lock);
2779 state->im = state->im->next;
2780 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002781 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (!state->dev) {
2783 state->idev = NULL;
2784 goto out;
2785 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002786 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (!state->idev)
2788 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002789 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791 if (!state->im)
2792 break;
2793 spin_lock_bh(&state->im->lock);
2794 psf = state->im->sources;
2795 }
2796out:
2797 return psf;
2798}
2799
2800static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2801{
2802 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2803 if (psf)
2804 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2805 --pos;
2806 return pos ? NULL : psf;
2807}
2808
2809static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002810 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
stephen hemminger61fbab72009-11-10 07:54:55 +00002812 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2814}
2815
2816static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2817{
2818 struct ip_sf_list *psf;
2819 if (v == SEQ_START_TOKEN)
2820 psf = igmp_mcf_get_first(seq);
2821 else
2822 psf = igmp_mcf_get_next(seq, v);
2823 ++*pos;
2824 return psf;
2825}
2826
2827static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002828 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002831 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 spin_unlock_bh(&state->im->lock);
2833 state->im = NULL;
2834 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002835 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002837 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2841{
2842 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2843 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2844
2845 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002846 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 } else {
2848 seq_printf(seq,
2849 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002850 "0x%08x %6lu %6lu\n",
2851 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 ntohl(state->im->multiaddr),
2853 ntohl(psf->sf_inaddr),
2854 psf->sf_count[MCAST_INCLUDE],
2855 psf->sf_count[MCAST_EXCLUDE]);
2856 }
2857 return 0;
2858}
2859
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002860static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 .start = igmp_mcf_seq_start,
2862 .next = igmp_mcf_seq_next,
2863 .stop = igmp_mcf_seq_stop,
2864 .show = igmp_mcf_seq_show,
2865};
2866
2867static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2868{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002869 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002870 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
Arjan van de Ven9a321442007-02-12 00:55:35 -08002873static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 .owner = THIS_MODULE,
2875 .open = igmp_mcf_seq_open,
2876 .read = seq_read,
2877 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002878 .release = seq_release_net,
2879};
2880
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002881static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002882{
2883 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002884 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002885
Gao fengd4beaa62013-02-18 01:34:54 +00002886 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002887 if (!pde)
2888 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002889 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2890 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002891 if (!pde)
2892 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08002893 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2894 SOCK_DGRAM, 0, net);
2895 if (err < 0) {
2896 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2897 err);
2898 goto out_sock;
2899 }
2900
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002901 return 0;
2902
Madhu Challa93a714d2015-02-25 09:58:35 -08002903out_sock:
2904 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002905out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002906 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002907out_igmp:
2908 return -ENOMEM;
2909}
2910
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002911static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002912{
Gao fengece31ff2013-02-18 01:34:56 +00002913 remove_proc_entry("mcfilter", net->proc_net);
2914 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08002915 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002916}
2917
2918static struct pernet_operations igmp_net_ops = {
2919 .init = igmp_net_init,
2920 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921};
WANG Cong72c1d3b2014-01-10 16:09:45 -08002922#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002924static int igmp_netdev_event(struct notifier_block *this,
2925 unsigned long event, void *ptr)
2926{
2927 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2928 struct in_device *in_dev;
2929
2930 switch (event) {
2931 case NETDEV_RESEND_IGMP:
2932 in_dev = __in_dev_get_rtnl(dev);
2933 if (in_dev)
2934 ip_mc_rejoin_groups(in_dev);
2935 break;
2936 default:
2937 break;
2938 }
2939 return NOTIFY_DONE;
2940}
2941
2942static struct notifier_block igmp_notifier = {
2943 .notifier_call = igmp_netdev_event,
2944};
2945
WANG Cong72c1d3b2014-01-10 16:09:45 -08002946int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
WANG Cong72c1d3b2014-01-10 16:09:45 -08002948#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002949 int err;
2950
2951 err = register_pernet_subsys(&igmp_net_ops);
2952 if (err)
2953 return err;
2954 err = register_netdevice_notifier(&igmp_notifier);
2955 if (err)
2956 goto reg_notif_fail;
2957 return 0;
2958
2959reg_notif_fail:
2960 unregister_pernet_subsys(&igmp_net_ops);
2961 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08002962#else
2963 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08002965}