blob: b3086cf2702759d1077cb6a014afbd130d8206db [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
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100113/* IGMP reports for link-local multicast groups are enabled by default */
114int sysctl_igmp_llm_reports __read_mostly = 1;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#ifdef CONFIG_IP_MULTICAST
117/* Parameter names and values are taken from igmp-v2-06 draft */
118
Fabian Frederick436f7c22014-11-04 20:52:14 +0100119#define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
120#define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
121#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
122#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
123#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
124#define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126
Fabian Frederick436f7c22014-11-04 20:52:14 +0100127#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Fabian Frederick436f7c22014-11-04 20:52:14 +0100129/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * IGMP specs require to report membership immediately after
131 * joining a group, but we delay the first report by a
132 * small interval. It seems more natural and still does not
133 * contradict to specs provided this delay is small enough.
134 */
135
Herbert Xu42f811b2007-06-04 23:34:44 -0700136#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
139 ((in_dev)->mr_v1_seen && \
140 time_before(jiffies, (in_dev)->mr_v1_seen)))
141#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900142 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700143 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
144 ((in_dev)->mr_v2_seen && \
145 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
William Manleycab70042013-08-06 19:03:13 +0100147static int unsolicited_report_interval(struct in_device *in_dev)
148{
William Manley26900482013-08-06 19:03:15 +0100149 int interval_ms, interval_jiffies;
150
William Manleycab70042013-08-06 19:03:13 +0100151 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100152 interval_ms = IN_DEV_CONF_GET(
153 in_dev,
154 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100155 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100156 interval_ms = IN_DEV_CONF_GET(
157 in_dev,
158 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
159
160 interval_jiffies = msecs_to_jiffies(interval_ms);
161
162 /* _timer functions can't handle a delay of 0 jiffies so ensure
163 * we always return a positive value.
164 */
165 if (interval_jiffies <= 0)
166 interval_jiffies = 1;
167 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700171static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static void igmpv3_clear_delrec(struct in_device *in_dev);
173static int sf_setstate(struct ip_mc_list *pmc);
174static void sf_markstate(struct ip_mc_list *pmc);
175#endif
176static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700177static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
178 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180static void ip_ma_put(struct ip_mc_list *im)
181{
182 if (atomic_dec_and_test(&im->refcnt)) {
183 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800184 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186}
187
David S. Millerd9aa9382010-11-15 08:52:02 -0800188#define for_each_pmc_rcu(in_dev, pmc) \
189 for (pmc = rcu_dereference(in_dev->mc_list); \
190 pmc != NULL; \
191 pmc = rcu_dereference(pmc->next_rcu))
192
193#define for_each_pmc_rtnl(in_dev, pmc) \
194 for (pmc = rtnl_dereference(in_dev->mc_list); \
195 pmc != NULL; \
196 pmc = rtnl_dereference(pmc->next_rcu))
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#ifdef CONFIG_IP_MULTICAST
199
200/*
201 * Timer management
202 */
203
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000204static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 spin_lock_bh(&im->lock);
207 if (del_timer(&im->timer))
208 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800209 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 im->reporter = 0;
211 im->unsolicit_count = 0;
212 spin_unlock_bh(&im->lock);
213}
214
215/* It must be called with locked im->lock */
216static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
217{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500218 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800220 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!mod_timer(&im->timer, jiffies+tv+2))
222 atomic_inc(&im->refcnt);
223}
224
225static void igmp_gq_start_timer(struct in_device *in_dev)
226{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500227 int tv = prandom_u32() % in_dev->mr_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 in_dev->mr_gq_running = 1;
230 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
231 in_dev_hold(in_dev);
232}
233
234static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
235{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500236 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
239 in_dev_hold(in_dev);
240}
241
242static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
243{
244 spin_lock_bh(&im->lock);
245 im->unsolicit_count = 0;
246 if (del_timer(&im->timer)) {
247 if ((long)(im->timer.expires-jiffies) < max_delay) {
248 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800249 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 spin_unlock_bh(&im->lock);
251 return;
252 }
253 atomic_dec(&im->refcnt);
254 }
255 igmp_start_timer(im, max_delay);
256 spin_unlock_bh(&im->lock);
257}
258
259
260/*
261 * Send an IGMP report.
262 */
263
264#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
265
266
267static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
268 int gdeleted, int sdeleted)
269{
270 switch (type) {
271 case IGMPV3_MODE_IS_INCLUDE:
272 case IGMPV3_MODE_IS_EXCLUDE:
273 if (gdeleted || sdeleted)
274 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800275 if (!(pmc->gsquery && !psf->sf_gsresp)) {
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return 1;
278 /* don't include if this source is excluded
279 * in all filters
280 */
281 if (psf->sf_count[MCAST_INCLUDE])
282 return type == IGMPV3_MODE_IS_INCLUDE;
283 return pmc->sfcount[MCAST_EXCLUDE] ==
284 psf->sf_count[MCAST_EXCLUDE];
285 }
286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 case IGMPV3_CHANGE_TO_INCLUDE:
288 if (gdeleted || sdeleted)
289 return 0;
290 return psf->sf_count[MCAST_INCLUDE] != 0;
291 case IGMPV3_CHANGE_TO_EXCLUDE:
292 if (gdeleted || sdeleted)
293 return 0;
294 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
295 psf->sf_count[MCAST_INCLUDE])
296 return 0;
297 return pmc->sfcount[MCAST_EXCLUDE] ==
298 psf->sf_count[MCAST_EXCLUDE];
299 case IGMPV3_ALLOW_NEW_SOURCES:
300 if (gdeleted || !psf->sf_crcount)
301 return 0;
302 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
303 case IGMPV3_BLOCK_OLD_SOURCES:
304 if (pmc->sfmode == MCAST_INCLUDE)
305 return gdeleted || (psf->sf_crcount && sdeleted);
306 return psf->sf_crcount && !gdeleted && !sdeleted;
307 }
308 return 0;
309}
310
311static int
312igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
313{
314 struct ip_sf_list *psf;
315 int scount = 0;
316
Weilong Chenc71151f2013-12-23 14:37:29 +0800317 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
319 continue;
320 scount++;
321 }
322 return scount;
323}
324
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100325static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct sk_buff *skb;
328 struct rtable *rt;
329 struct iphdr *pip;
330 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700331 struct net *net = dev_net(dev);
David S. Miller31e4543d2011-05-03 20:25:42 -0700332 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000333 int hlen = LL_RESERVED_SPACE(dev);
334 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100335 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000337 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000338 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000339 GFP_ATOMIC | __GFP_NOWARN);
340 if (skb)
341 break;
342 size >>= 1;
343 if (size < 256)
344 return NULL;
345 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200346 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
David S. Miller31e4543d2011-05-03 20:25:42 -0700348 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500349 0, 0,
350 IPPROTO_IGMP, 0, dev->ifindex);
351 if (IS_ERR(rt)) {
352 kfree_skb(skb);
353 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Changli Gaod8d1f302010-06-10 23:31:35 -0700356 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 skb->dev = dev;
358
Herbert Xu66088242011-11-18 02:20:04 +0000359 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800360 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300362 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700363 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300364 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 pip->version = 4;
367 pip->ihl = (sizeof(struct iphdr)+4)>>2;
368 pip->tos = 0xc0;
369 pip->frag_off = htons(IP_DF);
370 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700371 pip->daddr = fl4.daddr;
372 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 pip->protocol = IPPROTO_IGMP;
374 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100375 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000376 ((u8 *)&pip[1])[0] = IPOPT_RA;
377 ((u8 *)&pip[1])[1] = 4;
378 ((u8 *)&pip[1])[2] = 0;
379 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700381 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300382 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300383 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
385 pig->resv1 = 0;
386 pig->csum = 0;
387 pig->resv2 = 0;
388 pig->ngrec = 0;
389 return skb;
390}
391
392static int igmpv3_sendpack(struct sk_buff *skb)
393{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300394 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000395 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300397 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Eric W. Biederman33224b12015-10-07 16:48:46 -0500399 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
403{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800404 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
408 int type, struct igmpv3_grec **ppgr)
409{
410 struct net_device *dev = pmc->interface->dev;
411 struct igmpv3_report *pih;
412 struct igmpv3_grec *pgr;
413
414 if (!skb)
415 skb = igmpv3_newpack(dev, dev->mtu);
416 if (!skb)
417 return NULL;
418 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
419 pgr->grec_type = type;
420 pgr->grec_auxwords = 0;
421 pgr->grec_nsrcs = 0;
422 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300423 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 pih->ngrec = htons(ntohs(pih->ngrec)+1);
425 *ppgr = pgr;
426 return skb;
427}
428
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100429#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
432 int type, int gdeleted, int sdeleted)
433{
434 struct net_device *dev = pmc->interface->dev;
435 struct igmpv3_report *pih;
436 struct igmpv3_grec *pgr = NULL;
437 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800438 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (pmc->multiaddr == IGMP_ALL_HOSTS)
441 return skb;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100442 if (ipv4_is_local_multicast(pmc->multiaddr) && !sysctl_igmp_llm_reports)
443 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
446 type == IGMPV3_MODE_IS_EXCLUDE;
447 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
448 type == IGMPV3_CHANGE_TO_EXCLUDE;
449
David L Stevensad125832006-01-18 14:20:56 -0800450 stotal = scount = 0;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
453
David L Stevensad125832006-01-18 14:20:56 -0800454 if (!*psf_list)
455 goto empty_source;
456
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300457 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* EX and TO_EX get a fresh packet, if needed */
460 if (truncate) {
461 if (pih && pih->ngrec &&
462 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
463 if (skb)
464 igmpv3_sendpack(skb);
465 skb = igmpv3_newpack(dev, dev->mtu);
466 }
467 }
468 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800470 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700471 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 psf_next = psf->sf_next;
474
475 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
476 psf_prev = psf;
477 continue;
478 }
479
480 /* clear marks on query responses */
481 if (isquery)
482 psf->sf_gsresp = 0;
483
Al Viro63007722006-09-27 18:31:32 -0700484 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 first*sizeof(struct igmpv3_grec)) {
486 if (truncate && !first)
487 break; /* truncate these */
488 if (pgr)
489 pgr->grec_nsrcs = htons(scount);
490 if (skb)
491 igmpv3_sendpack(skb);
492 skb = igmpv3_newpack(dev, dev->mtu);
493 first = 1;
494 scount = 0;
495 }
496 if (first) {
497 skb = add_grhead(skb, pmc, type, &pgr);
498 first = 0;
499 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800500 if (!skb)
501 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700502 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800504 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
506 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
507 psf->sf_crcount--;
508 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
509 if (psf_prev)
510 psf_prev->sf_next = psf->sf_next;
511 else
512 *psf_list = psf->sf_next;
513 kfree(psf);
514 continue;
515 }
516 }
517 psf_prev = psf;
518 }
David L Stevensad125832006-01-18 14:20:56 -0800519
520empty_source:
521 if (!stotal) {
522 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
523 type == IGMPV3_BLOCK_OLD_SOURCES)
524 return skb;
525 if (pmc->crcount || isquery) {
526 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800527 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800528 igmpv3_sendpack(skb);
529 skb = NULL; /* add_grhead will get a new one */
530 }
531 skb = add_grhead(skb, pmc, type, &pgr);
532 }
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (pgr)
535 pgr->grec_nsrcs = htons(scount);
536
537 if (isquery)
538 pmc->gsquery = 0; /* clear query state on report */
539 return skb;
540}
541
542static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
543{
544 struct sk_buff *skb = NULL;
545 int type;
546
547 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000548 rcu_read_lock();
549 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (pmc->multiaddr == IGMP_ALL_HOSTS)
551 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100552 if (ipv4_is_local_multicast(pmc->multiaddr) &&
553 !sysctl_igmp_llm_reports)
554 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_lock_bh(&pmc->lock);
556 if (pmc->sfcount[MCAST_EXCLUDE])
557 type = IGMPV3_MODE_IS_EXCLUDE;
558 else
559 type = IGMPV3_MODE_IS_INCLUDE;
560 skb = add_grec(skb, pmc, type, 0, 0);
561 spin_unlock_bh(&pmc->lock);
562 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000563 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 } else {
565 spin_lock_bh(&pmc->lock);
566 if (pmc->sfcount[MCAST_EXCLUDE])
567 type = IGMPV3_MODE_IS_EXCLUDE;
568 else
569 type = IGMPV3_MODE_IS_INCLUDE;
570 skb = add_grec(skb, pmc, type, 0, 0);
571 spin_unlock_bh(&pmc->lock);
572 }
573 if (!skb)
574 return 0;
575 return igmpv3_sendpack(skb);
576}
577
578/*
579 * remove zero-count source records from a source filter list
580 */
581static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
582{
583 struct ip_sf_list *psf_prev, *psf_next, *psf;
584
585 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800586 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 psf_next = psf->sf_next;
588 if (psf->sf_crcount == 0) {
589 if (psf_prev)
590 psf_prev->sf_next = psf->sf_next;
591 else
592 *ppsf = psf->sf_next;
593 kfree(psf);
594 } else
595 psf_prev = psf;
596 }
597}
598
599static void igmpv3_send_cr(struct in_device *in_dev)
600{
601 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
602 struct sk_buff *skb = NULL;
603 int type, dtype;
604
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000605 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 spin_lock_bh(&in_dev->mc_tomb_lock);
607
608 /* deleted MCA's */
609 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800610 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 pmc_next = pmc->next;
612 if (pmc->sfmode == MCAST_INCLUDE) {
613 type = IGMPV3_BLOCK_OLD_SOURCES;
614 dtype = IGMPV3_BLOCK_OLD_SOURCES;
615 skb = add_grec(skb, pmc, type, 1, 0);
616 skb = add_grec(skb, pmc, dtype, 1, 1);
617 }
618 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (pmc->sfmode == MCAST_EXCLUDE) {
620 type = IGMPV3_CHANGE_TO_INCLUDE;
621 skb = add_grec(skb, pmc, type, 1, 0);
622 }
David L Stevensad125832006-01-18 14:20:56 -0800623 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (pmc->crcount == 0) {
625 igmpv3_clear_zeros(&pmc->tomb);
626 igmpv3_clear_zeros(&pmc->sources);
627 }
628 }
629 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
630 if (pmc_prev)
631 pmc_prev->next = pmc_next;
632 else
633 in_dev->mc_tomb = pmc_next;
634 in_dev_put(pmc->interface);
635 kfree(pmc);
636 } else
637 pmc_prev = pmc;
638 }
639 spin_unlock_bh(&in_dev->mc_tomb_lock);
640
641 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000642 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 spin_lock_bh(&pmc->lock);
644 if (pmc->sfcount[MCAST_EXCLUDE]) {
645 type = IGMPV3_BLOCK_OLD_SOURCES;
646 dtype = IGMPV3_ALLOW_NEW_SOURCES;
647 } else {
648 type = IGMPV3_ALLOW_NEW_SOURCES;
649 dtype = IGMPV3_BLOCK_OLD_SOURCES;
650 }
651 skb = add_grec(skb, pmc, type, 0, 0);
652 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
653
654 /* filter mode changes */
655 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (pmc->sfmode == MCAST_EXCLUDE)
657 type = IGMPV3_CHANGE_TO_EXCLUDE;
658 else
659 type = IGMPV3_CHANGE_TO_INCLUDE;
660 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800661 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 spin_unlock_bh(&pmc->lock);
664 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000665 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (!skb)
668 return;
669 (void) igmpv3_sendpack(skb);
670}
671
672static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
673 int type)
674{
675 struct sk_buff *skb;
676 struct iphdr *iph;
677 struct igmphdr *ih;
678 struct rtable *rt;
679 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700680 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700681 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e4543d2011-05-03 20:25:42 -0700682 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700683 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000684 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
687 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100688
689 if (ipv4_is_local_multicast(group) && !sysctl_igmp_llm_reports)
690 return 0;
691
692 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 dst = IGMP_ALL_ROUTER;
694 else
695 dst = group;
696
David S. Miller31e4543d2011-05-03 20:25:42 -0700697 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500698 0, 0,
699 IPPROTO_IGMP, 0, dev->ifindex);
700 if (IS_ERR(rt))
701 return -1;
702
Herbert Xu66088242011-11-18 02:20:04 +0000703 hlen = LL_RESERVED_SPACE(dev);
704 tlen = dev->needed_tailroom;
705 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100706 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ip_rt_put(rt);
708 return -1;
709 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200710 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Changli Gaod8d1f302010-06-10 23:31:35 -0700712 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Herbert Xu66088242011-11-18 02:20:04 +0000714 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300716 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700717 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300718 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 iph->version = 4;
721 iph->ihl = (sizeof(struct iphdr)+4)>>2;
722 iph->tos = 0xc0;
723 iph->frag_off = htons(IP_DF);
724 iph->ttl = 1;
725 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700726 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100728 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000729 ((u8 *)&iph[1])[0] = IPOPT_RA;
730 ((u8 *)&iph[1])[1] = 4;
731 ((u8 *)&iph[1])[2] = 0;
732 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800735 ih->type = type;
736 ih->code = 0;
737 ih->csum = 0;
738 ih->group = group;
739 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Eric W. Biederman33224b12015-10-07 16:48:46 -0500741 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744static void igmp_gq_timer_expire(unsigned long data)
745{
746 struct in_device *in_dev = (struct in_device *)data;
747
748 in_dev->mr_gq_running = 0;
749 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700750 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753static void igmp_ifc_timer_expire(unsigned long data)
754{
755 struct in_device *in_dev = (struct in_device *)data;
756
757 igmpv3_send_cr(in_dev);
758 if (in_dev->mr_ifc_count) {
759 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100760 igmp_ifc_start_timer(in_dev,
761 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700763 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766static void igmp_ifc_event(struct in_device *in_dev)
767{
768 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
769 return;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +0200770 in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 igmp_ifc_start_timer(in_dev, 1);
772}
773
774
775static void igmp_timer_expire(unsigned long data)
776{
Weilong Chenc71151f2013-12-23 14:37:29 +0800777 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct in_device *in_dev = im->interface;
779
780 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800781 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (im->unsolicit_count) {
784 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100785 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 im->reporter = 1;
788 spin_unlock(&im->lock);
789
790 if (IGMP_V1_SEEN(in_dev))
791 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
792 else if (IGMP_V2_SEEN(in_dev))
793 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
794 else
795 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
796
797 ip_ma_put(im);
798}
799
David L Stevensad125832006-01-18 14:20:56 -0800800/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700801static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct ip_sf_list *psf;
804 int i, scount;
805
806 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800807 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (scount == nsrcs)
809 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800810 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800811 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000812 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800813 pmc->sfcount[MCAST_EXCLUDE] !=
814 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800815 break;
David L Stevensad125832006-01-18 14:20:56 -0800816 if (srcs[i] == psf->sf_inaddr) {
817 scount++;
818 break;
819 }
820 }
821 }
822 pmc->gsquery = 0;
823 if (scount == nsrcs) /* all sources excluded */
824 return 0;
825 return 1;
826}
827
Al Viroea4d9e72006-09-27 18:30:52 -0700828static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800829{
830 struct ip_sf_list *psf;
831 int i, scount;
832
833 if (pmc->sfmode == MCAST_EXCLUDE)
834 return igmp_xmarksources(pmc, nsrcs, srcs);
835
836 /* mark INCLUDE-mode sources */
837 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800838 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800839 if (scount == nsrcs)
840 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800841 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (srcs[i] == psf->sf_inaddr) {
843 psf->sf_gsresp = 1;
844 scount++;
845 break;
846 }
847 }
David L Stevensad125832006-01-18 14:20:56 -0800848 if (!scount) {
849 pmc->gsquery = 0;
850 return 0;
851 }
852 pmc->gsquery = 1;
853 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Eric Dumazetd679c532012-09-06 20:37:06 +0000856/* return true if packet was dropped */
857static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 struct ip_mc_list *im;
860
861 /* Timers are only set for non-local groups */
862
863 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000864 return false;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100865 if (ipv4_is_local_multicast(group) && !sysctl_igmp_llm_reports)
866 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000868 rcu_read_lock();
869 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (im->multiaddr == group) {
871 igmp_stop_timer(im);
872 break;
873 }
874 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000875 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000876 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Eric Dumazetd679c532012-09-06 20:37:06 +0000879/* return true if packet was dropped */
880static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int len)
882{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300883 struct igmphdr *ih = igmp_hdr(skb);
884 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700886 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 int max_delay;
888 int mark = 0;
889
890
David Stevens5b7c84062010-09-30 14:29:40 +0000891 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (ih->code == 0) {
893 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900894
Fabian Frederick436f7c22014-11-04 20:52:14 +0100895 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 in_dev->mr_v1_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100897 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 group = 0;
899 } else {
900 /* v2 router present */
901 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
902 in_dev->mr_v2_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100903 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 /* cancel the interface change timer */
906 in_dev->mr_ifc_count = 0;
907 if (del_timer(&in_dev->mr_ifc_timer))
908 __in_dev_put(in_dev);
909 /* clear deleted report items */
910 igmpv3_clear_delrec(in_dev);
911 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000912 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c84062010-09-30 14:29:40 +0000913 } else if (IGMP_V1_SEEN(in_dev)) {
914 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100915 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c84062010-09-30 14:29:40 +0000916 group = 0;
917 } else if (IGMP_V2_SEEN(in_dev)) {
918 /* this is a v3 query with v2 queriers present;
919 * Interpretation of the max_delay code is problematic here.
920 * A real v2 host would use ih_code directly, while v3 has a
921 * different encoding. We use the v3 encoding as more likely
922 * to be intended in a v3 query.
923 */
924 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800925 if (!max_delay)
926 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 } else { /* v3 */
928 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000929 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900930
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300931 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900933 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700934 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000935 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300936 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
940 if (!max_delay)
941 max_delay = 1; /* can't mod w/ 0 */
942 in_dev->mr_maxdelay = max_delay;
943 if (ih3->qrv)
944 in_dev->mr_qrv = ih3->qrv;
945 if (!group) { /* general query */
946 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +0200947 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000949 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951 /* mark sources to include, if group & source-specific */
952 mark = ih3->nsrcs != 0;
953 }
954
955 /*
956 * - Start the timers in all of our membership records
957 * that the query applies to for the interface on
958 * which the query arrived excl. those that belong
959 * to a "local" group (224.0.0.X)
960 * - For timers already running check if they need to
961 * be reset.
962 * - Use the igmp->igmp_code field as the maximum
963 * delay possible
964 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000965 rcu_read_lock();
966 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800967 int changed;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (group && group != im->multiaddr)
970 continue;
971 if (im->multiaddr == IGMP_ALL_HOSTS)
972 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100973 if (ipv4_is_local_multicast(im->multiaddr) &&
974 !sysctl_igmp_llm_reports)
975 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 spin_lock_bh(&im->lock);
977 if (im->tm_running)
978 im->gsquery = im->gsquery && mark;
979 else
980 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800981 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900982 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800984 if (changed)
985 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000987 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000988 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000991/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992int igmp_rcv(struct sk_buff *skb)
993{
994 /* This basically follows the spec line by line -- see RFC1112 */
995 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000996 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +0000998 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Ian Morris51456b22015-04-03 09:17:26 +01001000 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001001 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Herbert Xufb286bb2005-11-10 13:01:24 -08001003 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001004 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001005
Tom Herbertde08dc12014-05-07 16:52:10 -07001006 if (skb_checksum_simple_validate(skb))
1007 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001009 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 switch (ih->type) {
1011 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001012 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 case IGMP_HOST_MEMBERSHIP_REPORT:
1015 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001017 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
David Stevens24c69272005-12-02 20:32:59 -08001019 /* don't rely on MC router hearing unicast reports */
1020 if (skb->pkt_type == PACKET_MULTICAST ||
1021 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001022 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 break;
1024 case IGMP_PIM:
1025#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return pim_rcv_v1(skb);
1027#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001028 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 case IGMP_DVMRP:
1030 case IGMP_TRACE:
1031 case IGMP_HOST_LEAVE_MESSAGE:
1032 case IGMP_MTRACE:
1033 case IGMP_MTRACE_RESP:
1034 break;
1035 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001038
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001039drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001040 if (dropped)
1041 kfree_skb(skb);
1042 else
1043 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return 0;
1045}
1046
1047#endif
1048
1049
1050/*
1051 * Add a filter to a device
1052 */
1053
Al Viro63007722006-09-27 18:31:32 -07001054static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 char buf[MAX_ADDR_LEN];
1057 struct net_device *dev = in_dev->dev;
1058
1059 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1060 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001061 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 routine. Something sort of:
1063 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1064 --ANK
1065 */
1066 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001067 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070/*
1071 * Remove a filter from a device
1072 */
1073
Al Viro63007722006-09-27 18:31:32 -07001074static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 char buf[MAX_ADDR_LEN];
1077 struct net_device *dev = in_dev->dev;
1078
1079 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001080 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
1083#ifdef CONFIG_IP_MULTICAST
1084/*
1085 * deleted ip_mc_list manipulation
1086 */
1087static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1088{
1089 struct ip_mc_list *pmc;
1090
1091 /* this is an "ip_mc_list" for convenience; only the fields below
1092 * are actually used. In particular, the refcnt and users are not
1093 * used for management of the delete list. Using the same structure
1094 * for deleted items allows change reports to use common code with
1095 * non-deleted or query-response MCA's.
1096 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001097 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!pmc)
1099 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 spin_lock_bh(&im->lock);
1101 pmc->interface = im->interface;
1102 in_dev_hold(in_dev);
1103 pmc->multiaddr = im->multiaddr;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001104 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 pmc->sfmode = im->sfmode;
1106 if (pmc->sfmode == MCAST_INCLUDE) {
1107 struct ip_sf_list *psf;
1108
1109 pmc->tomb = im->tomb;
1110 pmc->sources = im->sources;
1111 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001112 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 psf->sf_crcount = pmc->crcount;
1114 }
1115 spin_unlock_bh(&im->lock);
1116
1117 spin_lock_bh(&in_dev->mc_tomb_lock);
1118 pmc->next = in_dev->mc_tomb;
1119 in_dev->mc_tomb = pmc;
1120 spin_unlock_bh(&in_dev->mc_tomb_lock);
1121}
1122
Al Viro63007722006-09-27 18:31:32 -07001123static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 struct ip_mc_list *pmc, *pmc_prev;
1126 struct ip_sf_list *psf, *psf_next;
1127
1128 spin_lock_bh(&in_dev->mc_tomb_lock);
1129 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001130 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (pmc->multiaddr == multiaddr)
1132 break;
1133 pmc_prev = pmc;
1134 }
1135 if (pmc) {
1136 if (pmc_prev)
1137 pmc_prev->next = pmc->next;
1138 else
1139 in_dev->mc_tomb = pmc->next;
1140 }
1141 spin_unlock_bh(&in_dev->mc_tomb_lock);
1142 if (pmc) {
Weilong Chenc71151f2013-12-23 14:37:29 +08001143 for (psf = pmc->tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 psf_next = psf->sf_next;
1145 kfree(psf);
1146 }
1147 in_dev_put(pmc->interface);
1148 kfree(pmc);
1149 }
1150}
1151
1152static void igmpv3_clear_delrec(struct in_device *in_dev)
1153{
1154 struct ip_mc_list *pmc, *nextpmc;
1155
1156 spin_lock_bh(&in_dev->mc_tomb_lock);
1157 pmc = in_dev->mc_tomb;
1158 in_dev->mc_tomb = NULL;
1159 spin_unlock_bh(&in_dev->mc_tomb_lock);
1160
1161 for (; pmc; pmc = nextpmc) {
1162 nextpmc = pmc->next;
1163 ip_mc_clear_src(pmc);
1164 in_dev_put(pmc->interface);
1165 kfree(pmc);
1166 }
1167 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001168 rcu_read_lock();
1169 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct ip_sf_list *psf, *psf_next;
1171
1172 spin_lock_bh(&pmc->lock);
1173 psf = pmc->tomb;
1174 pmc->tomb = NULL;
1175 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001176 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 psf_next = psf->sf_next;
1178 kfree(psf);
1179 }
1180 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001181 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183#endif
1184
1185static void igmp_group_dropped(struct ip_mc_list *im)
1186{
1187 struct in_device *in_dev = im->interface;
1188#ifdef CONFIG_IP_MULTICAST
1189 int reporter;
1190#endif
1191
1192 if (im->loaded) {
1193 im->loaded = 0;
1194 ip_mc_filter_del(in_dev, im->multiaddr);
1195 }
1196
1197#ifdef CONFIG_IP_MULTICAST
1198 if (im->multiaddr == IGMP_ALL_HOSTS)
1199 return;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001200 if (ipv4_is_local_multicast(im->multiaddr) && !sysctl_igmp_llm_reports)
1201 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 reporter = im->reporter;
1204 igmp_stop_timer(im);
1205
1206 if (!in_dev->dead) {
1207 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001208 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (IGMP_V2_SEEN(in_dev)) {
1210 if (reporter)
1211 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001212 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 /* IGMPv3 */
1215 igmpv3_add_delrec(in_dev, im);
1216
1217 igmp_ifc_event(in_dev);
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
1222static void igmp_group_added(struct ip_mc_list *im)
1223{
1224 struct in_device *in_dev = im->interface;
1225
1226 if (im->loaded == 0) {
1227 im->loaded = 1;
1228 ip_mc_filter_add(in_dev, im->multiaddr);
1229 }
1230
1231#ifdef CONFIG_IP_MULTICAST
1232 if (im->multiaddr == IGMP_ALL_HOSTS)
1233 return;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001234 if (ipv4_is_local_multicast(im->multiaddr) && !sysctl_igmp_llm_reports)
1235 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 if (in_dev->dead)
1238 return;
1239 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1240 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001241 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 spin_unlock_bh(&im->lock);
1243 return;
1244 }
1245 /* else, v3 */
1246
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001247 im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 igmp_ifc_event(in_dev);
1249#endif
1250}
1251
1252
1253/*
1254 * Multicast list managers
1255 */
1256
Eric Dumazete9897072013-06-07 08:48:57 -07001257static u32 ip_mc_hash(const struct ip_mc_list *im)
1258{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001259 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001260}
1261
1262static void ip_mc_hash_add(struct in_device *in_dev,
1263 struct ip_mc_list *im)
1264{
1265 struct ip_mc_list __rcu **mc_hash;
1266 u32 hash;
1267
1268 mc_hash = rtnl_dereference(in_dev->mc_hash);
1269 if (mc_hash) {
1270 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001271 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001272 rcu_assign_pointer(mc_hash[hash], im);
1273 return;
1274 }
1275
1276 /* do not use a hash table for small number of items */
1277 if (in_dev->mc_count < 4)
1278 return;
1279
1280 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1281 GFP_KERNEL);
1282 if (!mc_hash)
1283 return;
1284
1285 for_each_pmc_rtnl(in_dev, im) {
1286 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001287 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001288 RCU_INIT_POINTER(mc_hash[hash], im);
1289 }
1290
1291 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1292}
1293
1294static void ip_mc_hash_remove(struct in_device *in_dev,
1295 struct ip_mc_list *im)
1296{
1297 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1298 struct ip_mc_list *aux;
1299
1300 if (!mc_hash)
1301 return;
1302 mc_hash += ip_mc_hash(im);
1303 while ((aux = rtnl_dereference(*mc_hash)) != im)
1304 mc_hash = &aux->next_hash;
1305 *mc_hash = im->next_hash;
1306}
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309/*
1310 * A socket has joined a multicast group on device dev.
1311 */
1312
Al Viro8f935bb2006-09-27 18:30:07 -07001313void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 struct ip_mc_list *im;
1316
1317 ASSERT_RTNL();
1318
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001319 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (im->multiaddr == addr) {
1321 im->users++;
1322 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1323 goto out;
1324 }
1325 }
1326
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001327 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (!im)
1329 goto out;
1330
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001331 im->users = 1;
1332 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001334 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /* initial mode is (EX, empty) */
1336 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 atomic_set(&im->refcnt, 1);
1339 spin_lock_init(&im->lock);
1340#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301341 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001342 im->unsolicit_count = sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001344
1345 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001346 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001347 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001348
Eric Dumazete9897072013-06-07 08:48:57 -07001349 ip_mc_hash_add(in_dev, im);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351#ifdef CONFIG_IP_MULTICAST
1352 igmpv3_del_delrec(in_dev, im->multiaddr);
1353#endif
1354 igmp_group_added(im);
1355 if (!in_dev->dead)
1356 ip_rt_multicast_event(in_dev);
1357out:
1358 return;
1359}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001360EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001362static int ip_mc_check_iphdr(struct sk_buff *skb)
1363{
1364 const struct iphdr *iph;
1365 unsigned int len;
1366 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1367
1368 if (!pskb_may_pull(skb, offset))
1369 return -EINVAL;
1370
1371 iph = ip_hdr(skb);
1372
1373 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1374 return -EINVAL;
1375
1376 offset += ip_hdrlen(skb) - sizeof(*iph);
1377
1378 if (!pskb_may_pull(skb, offset))
1379 return -EINVAL;
1380
1381 iph = ip_hdr(skb);
1382
1383 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1384 return -EINVAL;
1385
1386 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1387 if (skb->len < len || len < offset)
1388 return -EINVAL;
1389
1390 skb_set_transport_header(skb, offset);
1391
1392 return 0;
1393}
1394
1395static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1396{
1397 unsigned int len = skb_transport_offset(skb);
1398
1399 len += sizeof(struct igmpv3_report);
1400
1401 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1402}
1403
1404static int ip_mc_check_igmp_query(struct sk_buff *skb)
1405{
1406 unsigned int len = skb_transport_offset(skb);
1407
1408 len += sizeof(struct igmphdr);
1409 if (skb->len < len)
1410 return -EINVAL;
1411
1412 /* IGMPv{1,2}? */
1413 if (skb->len != len) {
1414 /* or IGMPv3? */
1415 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1416 if (skb->len < len || !pskb_may_pull(skb, len))
1417 return -EINVAL;
1418 }
1419
1420 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1421 * all-systems destination addresses (224.0.0.1) for general queries
1422 */
1423 if (!igmp_hdr(skb)->group &&
1424 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1425 return -EINVAL;
1426
1427 return 0;
1428}
1429
1430static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1431{
1432 switch (igmp_hdr(skb)->type) {
1433 case IGMP_HOST_LEAVE_MESSAGE:
1434 case IGMP_HOST_MEMBERSHIP_REPORT:
1435 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1436 /* fall through */
1437 return 0;
1438 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1439 return ip_mc_check_igmp_reportv3(skb);
1440 case IGMP_HOST_MEMBERSHIP_QUERY:
1441 return ip_mc_check_igmp_query(skb);
1442 default:
1443 return -ENOMSG;
1444 }
1445}
1446
1447static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1448{
1449 return skb_checksum_simple_validate(skb);
1450}
1451
1452static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1453
1454{
1455 struct sk_buff *skb_chk;
1456 unsigned int transport_len;
1457 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga5169932015-08-13 05:54:07 +02001458 int ret = -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001459
1460 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1461
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001462 skb_chk = skb_checksum_trimmed(skb, transport_len,
1463 ip_mc_validate_checksum);
1464 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02001465 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001466
Linus Lüssinga5169932015-08-13 05:54:07 +02001467 if (!pskb_may_pull(skb_chk, len))
1468 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001469
1470 ret = ip_mc_check_igmp_msg(skb_chk);
Linus Lüssinga5169932015-08-13 05:54:07 +02001471 if (ret)
1472 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001473
1474 if (skb_trimmed)
1475 *skb_trimmed = skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02001476 /* free now unneeded clone */
1477 else if (skb_chk != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001478 kfree_skb(skb_chk);
1479
Linus Lüssinga5169932015-08-13 05:54:07 +02001480 ret = 0;
1481
1482err:
1483 if (ret && skb_chk && skb_chk != skb)
1484 kfree_skb(skb_chk);
1485
1486 return ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001487}
1488
1489/**
1490 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1491 * @skb: the skb to validate
1492 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1493 *
1494 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001495 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001496 *
1497 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1498 * standard
1499 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1500 * -ENOMEM: A memory allocation failure happened.
1501 *
1502 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1503 * to NULL): After parsing an IGMP packet successfully it will point to
1504 * an skb which has its tail aligned to the IP packet end. This might
1505 * either be the originally provided skb or a trimmed, cloned version if
1506 * the skb frame had data beyond the IP packet. A cloned skb allows us
1507 * to leave the original skb and its full frame unchanged (which might be
1508 * desirable for layer 2 frame jugglers).
1509 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001510 * Caller needs to set the skb network header and free any returned skb if it
1511 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001512 */
1513int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1514{
1515 int ret = ip_mc_check_iphdr(skb);
1516
1517 if (ret < 0)
1518 return ret;
1519
1520 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1521 return -ENOMSG;
1522
1523 return __ip_mc_check_igmp(skb, skb_trimmed);
1524}
1525EXPORT_SYMBOL(ip_mc_check_igmp);
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001528 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001529 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001530static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001531{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001532#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001533 struct ip_mc_list *im;
1534 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001535
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001536 ASSERT_RTNL();
1537
1538 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001539 if (im->multiaddr == IGMP_ALL_HOSTS)
1540 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001541 if (ipv4_is_local_multicast(im->multiaddr) &&
1542 !sysctl_igmp_llm_reports)
1543 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001544
Eric Dumazet866f3b22010-11-18 09:33:19 -08001545 /* a failover is happening and switches
1546 * must be notified immediately
1547 */
1548 if (IGMP_V1_SEEN(in_dev))
1549 type = IGMP_HOST_MEMBERSHIP_REPORT;
1550 else if (IGMP_V2_SEEN(in_dev))
1551 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1552 else
1553 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1554 igmp_send_report(in_dev, im, type);
1555 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001556#endif
1557}
1558
1559/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 * A socket has left a multicast group on device dev
1561 */
1562
Al Viro8f935bb2006-09-27 18:30:07 -07001563void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001565 struct ip_mc_list *i;
1566 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001569
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001570 for (ip = &in_dev->mc_list;
1571 (i = rtnl_dereference(*ip)) != NULL;
1572 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001573 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001575 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001576 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001577 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001579 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 if (!in_dev->dead)
1582 ip_rt_multicast_event(in_dev);
1583
1584 ip_ma_put(i);
1585 return;
1586 }
1587 break;
1588 }
1589 }
1590}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001591EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Moni Shoua75c78502009-09-15 02:37:40 -07001593/* Device changing type */
1594
1595void ip_mc_unmap(struct in_device *in_dev)
1596{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001597 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001598
1599 ASSERT_RTNL();
1600
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001601 for_each_pmc_rtnl(in_dev, pmc)
1602 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001603}
1604
1605void ip_mc_remap(struct in_device *in_dev)
1606{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001607 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001608
1609 ASSERT_RTNL();
1610
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001611 for_each_pmc_rtnl(in_dev, pmc)
1612 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001613}
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615/* Device going down */
1616
1617void ip_mc_down(struct in_device *in_dev)
1618{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001619 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 ASSERT_RTNL();
1622
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001623 for_each_pmc_rtnl(in_dev, pmc)
1624 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626#ifdef CONFIG_IP_MULTICAST
1627 in_dev->mr_ifc_count = 0;
1628 if (del_timer(&in_dev->mr_ifc_timer))
1629 __in_dev_put(in_dev);
1630 in_dev->mr_gq_running = 0;
1631 if (del_timer(&in_dev->mr_gq_timer))
1632 __in_dev_put(in_dev);
1633 igmpv3_clear_delrec(in_dev);
1634#endif
1635
1636 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1637}
1638
1639void ip_mc_init_dev(struct in_device *in_dev)
1640{
1641 ASSERT_RTNL();
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001644 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1645 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001646 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1647 (unsigned long)in_dev);
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001648 in_dev->mr_qrv = sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649#endif
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 spin_lock_init(&in_dev->mc_tomb_lock);
1652}
1653
1654/* Device going up */
1655
1656void ip_mc_up(struct in_device *in_dev)
1657{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001658 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 ASSERT_RTNL();
1661
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001662#ifdef CONFIG_IP_MULTICAST
1663 in_dev->mr_qrv = sysctl_igmp_qrv;
1664#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1666
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001667 for_each_pmc_rtnl(in_dev, pmc)
1668 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
1671/*
1672 * Device is about to be destroyed: clean up.
1673 */
1674
1675void ip_mc_destroy_dev(struct in_device *in_dev)
1676{
1677 struct ip_mc_list *i;
1678
1679 ASSERT_RTNL();
1680
1681 /* Deactivate timers */
1682 ip_mc_down(in_dev);
1683
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001684 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1685 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001686 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001687
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001688 /* We've dropped the groups in ip_mc_down already */
1689 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001694/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001695static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct net_device *dev = NULL;
1698 struct in_device *idev = NULL;
1699
1700 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001701 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return idev;
1703 }
1704 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001705 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (!dev)
1707 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709
David S. Millerb23dd4f2011-03-02 14:31:35 -08001710 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001711 struct rtable *rt = ip_route_output(net,
1712 imr->imr_multiaddr.s_addr,
1713 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001714 if (!IS_ERR(rt)) {
1715 dev = rt->dst.dev;
1716 ip_rt_put(rt);
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 if (dev) {
1720 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001721 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 return idev;
1724}
1725
1726/*
1727 * Join a socket to a group
1728 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001729int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1730int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001731#ifdef CONFIG_IP_MULTICAST
Fabian Frederick436f7c22014-11-04 20:52:14 +01001732int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001736 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct ip_sf_list *psf, *psf_prev;
1739 int rv = 0;
1740
1741 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001742 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (psf->sf_inaddr == *psfsrc)
1744 break;
1745 psf_prev = psf;
1746 }
1747 if (!psf || psf->sf_count[sfmode] == 0) {
1748 /* source filter not found, or count wrong => bug */
1749 return -ESRCH;
1750 }
1751 psf->sf_count[sfmode]--;
1752 if (psf->sf_count[sfmode] == 0) {
1753 ip_rt_multicast_event(pmc->interface);
1754 }
1755 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1756#ifdef CONFIG_IP_MULTICAST
1757 struct in_device *in_dev = pmc->interface;
1758#endif
1759
1760 /* no more filters for this source */
1761 if (psf_prev)
1762 psf_prev->sf_next = psf->sf_next;
1763 else
1764 pmc->sources = psf->sf_next;
1765#ifdef CONFIG_IP_MULTICAST
1766 if (psf->sf_oldin &&
1767 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001768 psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 psf->sf_next = pmc->tomb;
1770 pmc->tomb = psf;
1771 rv = 1;
1772 } else
1773#endif
1774 kfree(psf);
1775 }
1776 return rv;
1777}
1778
1779#ifndef CONFIG_IP_MULTICAST
1780#define igmp_ifc_event(x) do { } while (0)
1781#endif
1782
Al Viro8f935bb2006-09-27 18:30:07 -07001783static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1784 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 struct ip_mc_list *pmc;
1787 int changerec = 0;
1788 int i, err;
1789
1790 if (!in_dev)
1791 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001792 rcu_read_lock();
1793 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (*pmca == pmc->multiaddr)
1795 break;
1796 }
1797 if (!pmc) {
1798 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001799 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return -ESRCH;
1801 }
1802 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001803 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804#ifdef CONFIG_IP_MULTICAST
1805 sf_markstate(pmc);
1806#endif
1807 if (!delta) {
1808 err = -EINVAL;
1809 if (!pmc->sfcount[sfmode])
1810 goto out_unlock;
1811 pmc->sfcount[sfmode]--;
1812 }
1813 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001814 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1816
1817 changerec |= rv > 0;
1818 if (!err && rv < 0)
1819 err = rv;
1820 }
1821 if (pmc->sfmode == MCAST_EXCLUDE &&
1822 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1823 pmc->sfcount[MCAST_INCLUDE]) {
1824#ifdef CONFIG_IP_MULTICAST
1825 struct ip_sf_list *psf;
1826#endif
1827
1828 /* filter mode change */
1829 pmc->sfmode = MCAST_INCLUDE;
1830#ifdef CONFIG_IP_MULTICAST
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001831 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001833 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 psf->sf_crcount = 0;
1835 igmp_ifc_event(pmc->interface);
1836 } else if (sf_setstate(pmc) || changerec) {
1837 igmp_ifc_event(pmc->interface);
1838#endif
1839 }
1840out_unlock:
1841 spin_unlock_bh(&pmc->lock);
1842 return err;
1843}
1844
1845/*
1846 * Add multicast single-source filter to the interface list
1847 */
1848static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001849 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 struct ip_sf_list *psf, *psf_prev;
1852
1853 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001854 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (psf->sf_inaddr == *psfsrc)
1856 break;
1857 psf_prev = psf;
1858 }
1859 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001860 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (!psf)
1862 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 psf->sf_inaddr = *psfsrc;
1864 if (psf_prev) {
1865 psf_prev->sf_next = psf;
1866 } else
1867 pmc->sources = psf;
1868 }
1869 psf->sf_count[sfmode]++;
1870 if (psf->sf_count[sfmode] == 1) {
1871 ip_rt_multicast_event(pmc->interface);
1872 }
1873 return 0;
1874}
1875
1876#ifdef CONFIG_IP_MULTICAST
1877static void sf_markstate(struct ip_mc_list *pmc)
1878{
1879 struct ip_sf_list *psf;
1880 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1881
Weilong Chenc71151f2013-12-23 14:37:29 +08001882 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (pmc->sfcount[MCAST_EXCLUDE]) {
1884 psf->sf_oldin = mca_xcount ==
1885 psf->sf_count[MCAST_EXCLUDE] &&
1886 !psf->sf_count[MCAST_INCLUDE];
1887 } else
1888 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1889}
1890
1891static int sf_setstate(struct ip_mc_list *pmc)
1892{
David L Stevensad125832006-01-18 14:20:56 -08001893 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1895 int qrv = pmc->interface->mr_qrv;
1896 int new_in, rv;
1897
1898 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001899 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (pmc->sfcount[MCAST_EXCLUDE]) {
1901 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1902 !psf->sf_count[MCAST_INCLUDE];
1903 } else
1904 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001905 if (new_in) {
1906 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001907 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001908
Weilong Chenc71151f2013-12-23 14:37:29 +08001909 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001910 if (dpsf->sf_inaddr == psf->sf_inaddr)
1911 break;
1912 prev = dpsf;
1913 }
1914 if (dpsf) {
1915 if (prev)
1916 prev->sf_next = dpsf->sf_next;
1917 else
1918 pmc->tomb = dpsf->sf_next;
1919 kfree(dpsf);
1920 }
1921 psf->sf_crcount = qrv;
1922 rv++;
1923 }
1924 } else if (psf->sf_oldin) {
1925
1926 psf->sf_crcount = 0;
1927 /*
1928 * add or update "delete" records if an active filter
1929 * is now inactive
1930 */
Weilong Chenc71151f2013-12-23 14:37:29 +08001931 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08001932 if (dpsf->sf_inaddr == psf->sf_inaddr)
1933 break;
1934 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001935 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001936 if (!dpsf)
1937 continue;
1938 *dpsf = *psf;
1939 /* pmc->lock held by callers */
1940 dpsf->sf_next = pmc->tomb;
1941 pmc->tomb = dpsf;
1942 }
1943 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 rv++;
1945 }
1946 }
1947 return rv;
1948}
1949#endif
1950
1951/*
1952 * Add multicast source filter list to the interface list
1953 */
Al Viro8f935bb2006-09-27 18:30:07 -07001954static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1955 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 struct ip_mc_list *pmc;
1958 int isexclude;
1959 int i, err;
1960
1961 if (!in_dev)
1962 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001963 rcu_read_lock();
1964 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (*pmca == pmc->multiaddr)
1966 break;
1967 }
1968 if (!pmc) {
1969 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001970 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return -ESRCH;
1972 }
1973 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001974 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976#ifdef CONFIG_IP_MULTICAST
1977 sf_markstate(pmc);
1978#endif
1979 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1980 if (!delta)
1981 pmc->sfcount[sfmode]++;
1982 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001983 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00001984 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (err)
1986 break;
1987 }
1988 if (err) {
1989 int j;
1990
Jun Zhao685f94e62011-11-22 17:19:03 +00001991 if (!delta)
1992 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08001993 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00001994 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1996#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001998 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999#endif
2000
2001 /* filter mode change */
2002 if (pmc->sfcount[MCAST_EXCLUDE])
2003 pmc->sfmode = MCAST_EXCLUDE;
2004 else if (pmc->sfcount[MCAST_INCLUDE])
2005 pmc->sfmode = MCAST_INCLUDE;
2006#ifdef CONFIG_IP_MULTICAST
2007 /* else no filters; keep old mode for reports */
2008
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02002009 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002011 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 psf->sf_crcount = 0;
2013 igmp_ifc_event(in_dev);
2014 } else if (sf_setstate(pmc)) {
2015 igmp_ifc_event(in_dev);
2016#endif
2017 }
2018 spin_unlock_bh(&pmc->lock);
2019 return err;
2020}
2021
2022static void ip_mc_clear_src(struct ip_mc_list *pmc)
2023{
2024 struct ip_sf_list *psf, *nextpsf;
2025
Weilong Chenc71151f2013-12-23 14:37:29 +08002026 for (psf = pmc->tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 nextpsf = psf->sf_next;
2028 kfree(psf);
2029 }
2030 pmc->tomb = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08002031 for (psf = pmc->sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 nextpsf = psf->sf_next;
2033 kfree(psf);
2034 }
2035 pmc->sources = NULL;
2036 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002037 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 pmc->sfcount[MCAST_EXCLUDE] = 1;
2039}
2040
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002041/* Join a multicast group
2042 */
2043
2044int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Al Viro8f935bb2006-09-27 18:30:07 -07002046 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002047 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct in_device *in_dev;
2049 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002050 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002051 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002053 int err;
2054
2055 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Joe Perchesf97c1e02007-12-16 13:45:43 -08002057 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return -EINVAL;
2059
Daniel Lezcano877aced2008-08-13 16:15:57 -07002060 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 err = -ENODEV;
2064 goto done;
2065 }
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002068 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002069 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002070 if (i->multi.imr_multiaddr.s_addr == addr &&
2071 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 count++;
2074 }
2075 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07002076 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002078 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002079 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002080 goto done;
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002083 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 iml->sflist = NULL;
2085 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002086 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return err;
2091}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002092EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2095 struct in_device *in_dev)
2096{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002097 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 int err;
2099
Ian Morris51456b22015-04-03 09:17:26 +01002100 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* any-source empty exclude case */
2102 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2103 iml->sfmode, 0, NULL, 0);
2104 }
2105 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002106 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002107 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002108 /* decrease mem now to avoid the memleak warning */
2109 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002110 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return err;
2112}
2113
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002114int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002117 struct ip_mc_socklist *iml;
2118 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002119 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002120 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002121 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002122 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002123 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Eric Dumazet959d10f2015-02-17 03:19:24 -08002125 ASSERT_RTNL();
2126
Daniel Lezcano877aced2008-08-13 16:15:57 -07002127 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002128 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002129 ret = -ENODEV;
2130 goto out;
2131 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002132 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002133 for (imlp = &inet->mc_list;
2134 (iml = rtnl_dereference(*imlp)) != NULL;
2135 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002136 if (iml->multi.imr_multiaddr.s_addr != group)
2137 continue;
2138 if (ifindex) {
2139 if (iml->multi.imr_ifindex != ifindex)
2140 continue;
2141 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2142 iml->multi.imr_address.s_addr)
2143 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
David L Stevensacd6e002006-08-17 16:27:39 -07002145 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002147 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002148
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002149 if (in_dev)
2150 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002151
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002152 /* decrease mem now to avoid the memleak warning */
2153 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002154 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
dingtianhong52ad3532014-07-02 13:50:48 +08002157out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002158 return ret;
2159}
stephen hemminger193ba922012-10-01 12:32:34 +00002160EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162int ip_mc_source(int add, int omode, struct sock *sk, struct
2163 ip_mreq_source *mreqs, int ifindex)
2164{
2165 int err;
2166 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002167 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 struct ip_mc_socklist *pmc;
2169 struct in_device *in_dev = NULL;
2170 struct inet_sock *inet = inet_sk(sk);
2171 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002172 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002173 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 int i, j, rv;
2175
Joe Perchesf97c1e02007-12-16 13:45:43 -08002176 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return -EINVAL;
2178
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002179 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2182 imr.imr_address.s_addr = mreqs->imr_interface;
2183 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002184 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 if (!in_dev) {
2187 err = -ENODEV;
2188 goto done;
2189 }
2190 err = -EADDRNOTAVAIL;
2191
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002192 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002193 if ((pmc->multi.imr_multiaddr.s_addr ==
2194 imr.imr_multiaddr.s_addr) &&
2195 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 break;
2197 }
David L Stevens917f2f12005-07-08 17:45:16 -07002198 if (!pmc) { /* must have a prior join */
2199 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /* if a source filter was set, must be the same mode as before */
2203 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002204 if (pmc->sfmode != omode) {
2205 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 } else if (pmc->sfmode != omode) {
2209 /* allow mode switches for empty-set filters */
2210 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002211 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 NULL, 0);
2213 pmc->sfmode = omode;
2214 }
2215
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002216 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (!add) {
2218 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002219 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002221 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002223 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (rv == 0)
2225 break;
2226 }
2227 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002228 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
David L Stevens8cdaaa12005-07-08 17:39:23 -07002230 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2231 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2232 leavegroup = 1;
2233 goto done;
2234 }
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002237 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 &mreqs->imr_sourceaddr, 1);
2239
Weilong Chenc71151f2013-12-23 14:37:29 +08002240 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 psl->sl_addr[j-1] = psl->sl_addr[j];
2242 psl->sl_count--;
2243 err = 0;
2244 goto done;
2245 }
2246 /* else, add a new source to the filter */
2247
2248 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2249 err = -ENOBUFS;
2250 goto done;
2251 }
2252 if (!psl || psl->sl_count == psl->sl_max) {
2253 struct ip_sf_socklist *newpsl;
2254 int count = IP_SFBLOCK;
2255
2256 if (psl)
2257 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002258 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (!newpsl) {
2260 err = -ENOBUFS;
2261 goto done;
2262 }
2263 newpsl->sl_max = count;
2264 newpsl->sl_count = count - IP_SFBLOCK;
2265 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002266 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002268 /* decrease mem now to avoid the memleak warning */
2269 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002270 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002272 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002273 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002276 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002278 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (rv == 0)
2280 break;
2281 }
2282 if (rv == 0) /* address already there is an error */
2283 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002284 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 psl->sl_addr[j+1] = psl->sl_addr[j];
2286 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2287 psl->sl_count++;
2288 err = 0;
2289 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002290 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 &mreqs->imr_sourceaddr, 1);
2292done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002293 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002294 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return err;
2296}
2297
2298int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2299{
David L Stevens9951f032005-07-08 17:47:28 -07002300 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002302 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 struct ip_mc_socklist *pmc;
2304 struct in_device *in_dev;
2305 struct inet_sock *inet = inet_sk(sk);
2306 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002307 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002308 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Joe Perchesf97c1e02007-12-16 13:45:43 -08002310 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return -EINVAL;
2312 if (msf->imsf_fmode != MCAST_INCLUDE &&
2313 msf->imsf_fmode != MCAST_EXCLUDE)
2314 return -EINVAL;
2315
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002316 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2319 imr.imr_address.s_addr = msf->imsf_interface;
2320 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002321 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (!in_dev) {
2324 err = -ENODEV;
2325 goto done;
2326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
David L Stevens9951f032005-07-08 17:47:28 -07002328 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2329 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2330 leavegroup = 1;
2331 goto done;
2332 }
2333
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002334 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2336 pmc->multi.imr_ifindex == imr.imr_ifindex)
2337 break;
2338 }
David L Stevens917f2f12005-07-08 17:45:16 -07002339 if (!pmc) { /* must have a prior join */
2340 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002344 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2345 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (!newpsl) {
2347 err = -ENOBUFS;
2348 goto done;
2349 }
2350 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2351 memcpy(newpsl->sl_addr, msf->imsf_slist,
2352 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2353 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2354 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2355 if (err) {
2356 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2357 goto done;
2358 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002359 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002361 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2362 msf->imsf_fmode, 0, NULL, 0);
2363 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002364 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (psl) {
2366 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2367 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002368 /* decrease mem now to avoid the memleak warning */
2369 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002370 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 } else
2372 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2373 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002374 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002376 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377done:
David L Stevens9951f032005-07-08 17:47:28 -07002378 if (leavegroup)
2379 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return err;
2381}
2382
2383int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2384 struct ip_msfilter __user *optval, int __user *optlen)
2385{
2386 int err, len, count, copycount;
2387 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002388 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct ip_mc_socklist *pmc;
2390 struct in_device *in_dev;
2391 struct inet_sock *inet = inet_sk(sk);
2392 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002393 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
WANG Cong87e9f032015-11-03 15:41:16 -08002395 ASSERT_RTNL();
2396
Joe Perchesf97c1e02007-12-16 13:45:43 -08002397 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return -EINVAL;
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2401 imr.imr_address.s_addr = msf->imsf_interface;
2402 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002403 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 if (!in_dev) {
2406 err = -ENODEV;
2407 goto done;
2408 }
2409 err = -EADDRNOTAVAIL;
2410
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002411 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2413 pmc->multi.imr_ifindex == imr.imr_ifindex)
2414 break;
2415 }
2416 if (!pmc) /* must have a prior join */
2417 goto done;
2418 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002419 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (!psl) {
2421 len = 0;
2422 count = 0;
2423 } else {
2424 count = psl->sl_count;
2425 }
2426 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2427 len = copycount * sizeof(psl->sl_addr[0]);
2428 msf->imsf_numsrc = count;
2429 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2430 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2431 return -EFAULT;
2432 }
2433 if (len &&
2434 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2435 return -EFAULT;
2436 return 0;
2437done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return err;
2439}
2440
2441int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2442 struct group_filter __user *optval, int __user *optlen)
2443{
2444 int err, i, count, copycount;
2445 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002446 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 struct ip_mc_socklist *pmc;
2448 struct inet_sock *inet = inet_sk(sk);
2449 struct ip_sf_socklist *psl;
2450
WANG Cong87e9f032015-11-03 15:41:16 -08002451 ASSERT_RTNL();
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 psin = (struct sockaddr_in *)&gsf->gf_group;
2454 if (psin->sin_family != AF_INET)
2455 return -EINVAL;
2456 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002457 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return -EINVAL;
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 err = -EADDRNOTAVAIL;
2461
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002462 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2464 pmc->multi.imr_ifindex == gsf->gf_interface)
2465 break;
2466 }
2467 if (!pmc) /* must have a prior join */
2468 goto done;
2469 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002470 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 count = psl ? psl->sl_count : 0;
2472 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2473 gsf->gf_numsrc = count;
2474 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2475 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2476 return -EFAULT;
2477 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002478 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct sockaddr_storage ss;
2480
2481 psin = (struct sockaddr_in *)&ss;
2482 memset(&ss, 0, sizeof(ss));
2483 psin->sin_family = AF_INET;
2484 psin->sin_addr.s_addr = psl->sl_addr[i];
2485 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2486 return -EFAULT;
2487 }
2488 return 0;
2489done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return err;
2491}
2492
2493/*
2494 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2495 */
Al Viroc0cda062006-09-27 18:31:10 -07002496int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 struct inet_sock *inet = inet_sk(sk);
2499 struct ip_mc_socklist *pmc;
2500 struct ip_sf_socklist *psl;
2501 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002502 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002504 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002505 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002508 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002509 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2511 pmc->multi.imr_ifindex == dif)
2512 break;
2513 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002514 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002516 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002517 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002518 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002520 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Weilong Chenc71151f2013-12-23 14:37:29 +08002522 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (psl->sl_addr[i] == rmt_addr)
2524 break;
2525 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002526 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002528 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002530 goto unlock;
2531 ret = 1;
2532unlock:
2533 rcu_read_unlock();
2534out:
2535 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538/*
2539 * A socket is closing.
2540 */
2541
2542void ip_mc_drop_socket(struct sock *sk)
2543{
2544 struct inet_sock *inet = inet_sk(sk);
2545 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002546 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Ian Morris51456b22015-04-03 09:17:26 +01002548 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return;
2550
2551 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002552 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002555 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002556 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002557 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002558 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002560 /* decrease mem now to avoid the memleak warning */
2561 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002562 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564 rtnl_unlock();
2565}
2566
David S. Millerdbdd9a52011-03-10 16:34:38 -08002567/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002568int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002571 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct ip_sf_list *psf;
2573 int rv = 0;
2574
Eric Dumazete9897072013-06-07 08:48:57 -07002575 mc_hash = rcu_dereference(in_dev->mc_hash);
2576 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002577 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002578
2579 for (im = rcu_dereference(mc_hash[hash]);
2580 im != NULL;
2581 im = rcu_dereference(im->next_hash)) {
2582 if (im->multiaddr == mc_addr)
2583 break;
2584 }
2585 } else {
2586 for_each_pmc_rcu(in_dev, im) {
2587 if (im->multiaddr == mc_addr)
2588 break;
2589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
2591 if (im && proto == IPPROTO_IGMP) {
2592 rv = 1;
2593 } else if (im) {
2594 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002595 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (psf->sf_inaddr == src_addr)
2597 break;
2598 }
2599 if (psf)
2600 rv = psf->sf_count[MCAST_INCLUDE] ||
2601 psf->sf_count[MCAST_EXCLUDE] !=
2602 im->sfcount[MCAST_EXCLUDE];
2603 else
2604 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2605 } else
2606 rv = 1; /* unspecified source; tentatively allow */
2607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return rv;
2609}
2610
2611#if defined(CONFIG_PROC_FS)
2612struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002613 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct net_device *dev;
2615 struct in_device *in_dev;
2616};
2617
2618#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2619
2620static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2621{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002622 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 struct ip_mc_list *im = NULL;
2624 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2625
Pavel Emelianov7562f872007-05-03 15:13:45 -07002626 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002627 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002629
2630 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (!in_dev)
2632 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002633 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (im) {
2635 state->in_dev = in_dev;
2636 break;
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639 return im;
2640}
2641
2642static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2643{
2644 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002645
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002646 im = rcu_dereference(im->next_rcu);
2647 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002648 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (!state->dev) {
2650 state->in_dev = NULL;
2651 break;
2652 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002653 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (!state->in_dev)
2655 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002656 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658 return im;
2659}
2660
2661static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2662{
2663 struct ip_mc_list *im = igmp_mc_get_first(seq);
2664 if (im)
2665 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2666 --pos;
2667 return pos ? NULL : im;
2668}
2669
2670static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002671 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
stephen hemminger61fbab72009-11-10 07:54:55 +00002673 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2675}
2676
2677static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2678{
2679 struct ip_mc_list *im;
2680 if (v == SEQ_START_TOKEN)
2681 im = igmp_mc_get_first(seq);
2682 else
2683 im = igmp_mc_get_next(seq, v);
2684 ++*pos;
2685 return im;
2686}
2687
2688static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002689 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002692
2693 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002695 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
2698static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2699{
2700 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002701 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2703 else {
2704 struct ip_mc_list *im = (struct ip_mc_list *)v;
2705 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2706 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002707 long delta;
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709#ifdef CONFIG_IP_MULTICAST
2710 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2711 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2712 "V3";
2713#else
2714 querier = "NONE";
2715#endif
2716
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002717 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002719 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721
Eric Dumazeta399a802012-08-08 21:13:53 +00002722 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002724 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002726 im->tm_running,
2727 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 im->reporter);
2729 }
2730 return 0;
2731}
2732
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002733static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 .start = igmp_mc_seq_start,
2735 .next = igmp_mc_seq_next,
2736 .stop = igmp_mc_seq_stop,
2737 .show = igmp_mc_seq_show,
2738};
2739
2740static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2741{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002742 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002743 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
Arjan van de Ven9a321442007-02-12 00:55:35 -08002746static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 .owner = THIS_MODULE,
2748 .open = igmp_mc_seq_open,
2749 .read = seq_read,
2750 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002751 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752};
2753
2754struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002755 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct net_device *dev;
2757 struct in_device *idev;
2758 struct ip_mc_list *im;
2759};
2760
2761#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2762
2763static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2764{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002765 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 struct ip_sf_list *psf = NULL;
2767 struct ip_mc_list *im = NULL;
2768 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2769
Pavel Emelianov7562f872007-05-03 15:13:45 -07002770 state->idev = NULL;
2771 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002772 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002774 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002775 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002777 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002778 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 spin_lock_bh(&im->lock);
2780 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002781 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 state->im = im;
2783 state->idev = idev;
2784 break;
2785 }
2786 spin_unlock_bh(&im->lock);
2787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789 return psf;
2790}
2791
2792static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2793{
2794 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2795
2796 psf = psf->sf_next;
2797 while (!psf) {
2798 spin_unlock_bh(&state->im->lock);
2799 state->im = state->im->next;
2800 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002801 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (!state->dev) {
2803 state->idev = NULL;
2804 goto out;
2805 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002806 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (!state->idev)
2808 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002809 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811 if (!state->im)
2812 break;
2813 spin_lock_bh(&state->im->lock);
2814 psf = state->im->sources;
2815 }
2816out:
2817 return psf;
2818}
2819
2820static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2821{
2822 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2823 if (psf)
2824 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2825 --pos;
2826 return pos ? NULL : psf;
2827}
2828
2829static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002830 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
stephen hemminger61fbab72009-11-10 07:54:55 +00002832 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2834}
2835
2836static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2837{
2838 struct ip_sf_list *psf;
2839 if (v == SEQ_START_TOKEN)
2840 psf = igmp_mcf_get_first(seq);
2841 else
2842 psf = igmp_mcf_get_next(seq, v);
2843 ++*pos;
2844 return psf;
2845}
2846
2847static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002848 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
2850 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002851 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 spin_unlock_bh(&state->im->lock);
2853 state->im = NULL;
2854 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002855 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002857 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
2860static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2861{
2862 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2863 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2864
2865 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002866 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 } else {
2868 seq_printf(seq,
2869 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002870 "0x%08x %6lu %6lu\n",
2871 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 ntohl(state->im->multiaddr),
2873 ntohl(psf->sf_inaddr),
2874 psf->sf_count[MCAST_INCLUDE],
2875 psf->sf_count[MCAST_EXCLUDE]);
2876 }
2877 return 0;
2878}
2879
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002880static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 .start = igmp_mcf_seq_start,
2882 .next = igmp_mcf_seq_next,
2883 .stop = igmp_mcf_seq_stop,
2884 .show = igmp_mcf_seq_show,
2885};
2886
2887static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2888{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002889 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002890 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
Arjan van de Ven9a321442007-02-12 00:55:35 -08002893static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 .owner = THIS_MODULE,
2895 .open = igmp_mcf_seq_open,
2896 .read = seq_read,
2897 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002898 .release = seq_release_net,
2899};
2900
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002901static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002902{
2903 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002904 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002905
Gao fengd4beaa62013-02-18 01:34:54 +00002906 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002907 if (!pde)
2908 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002909 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2910 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002911 if (!pde)
2912 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08002913 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2914 SOCK_DGRAM, 0, net);
2915 if (err < 0) {
2916 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2917 err);
2918 goto out_sock;
2919 }
2920
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002921 return 0;
2922
Madhu Challa93a714d2015-02-25 09:58:35 -08002923out_sock:
2924 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002925out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002926 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002927out_igmp:
2928 return -ENOMEM;
2929}
2930
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002931static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002932{
Gao fengece31ff2013-02-18 01:34:56 +00002933 remove_proc_entry("mcfilter", net->proc_net);
2934 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08002935 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002936}
2937
2938static struct pernet_operations igmp_net_ops = {
2939 .init = igmp_net_init,
2940 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941};
WANG Cong72c1d3b2014-01-10 16:09:45 -08002942#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002944static int igmp_netdev_event(struct notifier_block *this,
2945 unsigned long event, void *ptr)
2946{
2947 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2948 struct in_device *in_dev;
2949
2950 switch (event) {
2951 case NETDEV_RESEND_IGMP:
2952 in_dev = __in_dev_get_rtnl(dev);
2953 if (in_dev)
2954 ip_mc_rejoin_groups(in_dev);
2955 break;
2956 default:
2957 break;
2958 }
2959 return NOTIFY_DONE;
2960}
2961
2962static struct notifier_block igmp_notifier = {
2963 .notifier_call = igmp_netdev_event,
2964};
2965
WANG Cong72c1d3b2014-01-10 16:09:45 -08002966int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
WANG Cong72c1d3b2014-01-10 16:09:45 -08002968#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002969 int err;
2970
2971 err = register_pernet_subsys(&igmp_net_ops);
2972 if (err)
2973 return err;
2974 err = register_netdevice_notifier(&igmp_notifier);
2975 if (err)
2976 goto reg_notif_fail;
2977 return 0;
2978
2979reg_notif_fail:
2980 unregister_pernet_subsys(&igmp_net_ops);
2981 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08002982#else
2983 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08002985}