blob: 4b1172d73e0309766d6d1a70473dd4c14d2c6fc0 [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>
100#include <linux/netfilter_ipv4.h>
101#ifdef CONFIG_IP_MROUTE
102#include <linux/mroute.h>
103#endif
104#ifdef CONFIG_PROC_FS
105#include <linux/proc_fs.h>
106#include <linux/seq_file.h>
107#endif
108
109#define IP_MAX_MEMBERSHIPS 20
110#define IP_MAX_MSF 10
111
112#ifdef CONFIG_IP_MULTICAST
113/* Parameter names and values are taken from igmp-v2-06 draft */
114
Fabian Frederick436f7c22014-11-04 20:52:14 +0100115#define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
116#define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
117#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
118#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
119#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
120#define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122
Fabian Frederick436f7c22014-11-04 20:52:14 +0100123#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Fabian Frederick436f7c22014-11-04 20:52:14 +0100125/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
130 */
131
Herbert Xu42f811b2007-06-04 23:34:44 -0700132#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900133 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700134 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135 ((in_dev)->mr_v1_seen && \
136 time_before(jiffies, (in_dev)->mr_v1_seen)))
137#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900138 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140 ((in_dev)->mr_v2_seen && \
141 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
William Manleycab70042013-08-06 19:03:13 +0100143static int unsolicited_report_interval(struct in_device *in_dev)
144{
William Manley26900482013-08-06 19:03:15 +0100145 int interval_ms, interval_jiffies;
146
William Manleycab70042013-08-06 19:03:13 +0100147 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100148 interval_ms = IN_DEV_CONF_GET(
149 in_dev,
150 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100151 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100152 interval_ms = IN_DEV_CONF_GET(
153 in_dev,
154 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
155
156 interval_jiffies = msecs_to_jiffies(interval_ms);
157
158 /* _timer functions can't handle a delay of 0 jiffies so ensure
159 * we always return a positive value.
160 */
161 if (interval_jiffies <= 0)
162 interval_jiffies = 1;
163 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700167static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static void igmpv3_clear_delrec(struct in_device *in_dev);
169static int sf_setstate(struct ip_mc_list *pmc);
170static void sf_markstate(struct ip_mc_list *pmc);
171#endif
172static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700173static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
174 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176static void ip_ma_put(struct ip_mc_list *im)
177{
178 if (atomic_dec_and_test(&im->refcnt)) {
179 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800180 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182}
183
David S. Millerd9aa9382010-11-15 08:52:02 -0800184#define for_each_pmc_rcu(in_dev, pmc) \
185 for (pmc = rcu_dereference(in_dev->mc_list); \
186 pmc != NULL; \
187 pmc = rcu_dereference(pmc->next_rcu))
188
189#define for_each_pmc_rtnl(in_dev, pmc) \
190 for (pmc = rtnl_dereference(in_dev->mc_list); \
191 pmc != NULL; \
192 pmc = rtnl_dereference(pmc->next_rcu))
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#ifdef CONFIG_IP_MULTICAST
195
196/*
197 * Timer management
198 */
199
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000200static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 spin_lock_bh(&im->lock);
203 if (del_timer(&im->timer))
204 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800205 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 im->reporter = 0;
207 im->unsolicit_count = 0;
208 spin_unlock_bh(&im->lock);
209}
210
211/* It must be called with locked im->lock */
212static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
213{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500214 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800216 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!mod_timer(&im->timer, jiffies+tv+2))
218 atomic_inc(&im->refcnt);
219}
220
221static void igmp_gq_start_timer(struct in_device *in_dev)
222{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500223 int tv = prandom_u32() % in_dev->mr_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 in_dev->mr_gq_running = 1;
226 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
227 in_dev_hold(in_dev);
228}
229
230static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
231{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500232 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
235 in_dev_hold(in_dev);
236}
237
238static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
239{
240 spin_lock_bh(&im->lock);
241 im->unsolicit_count = 0;
242 if (del_timer(&im->timer)) {
243 if ((long)(im->timer.expires-jiffies) < max_delay) {
244 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800245 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spin_unlock_bh(&im->lock);
247 return;
248 }
249 atomic_dec(&im->refcnt);
250 }
251 igmp_start_timer(im, max_delay);
252 spin_unlock_bh(&im->lock);
253}
254
255
256/*
257 * Send an IGMP report.
258 */
259
260#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
261
262
263static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
264 int gdeleted, int sdeleted)
265{
266 switch (type) {
267 case IGMPV3_MODE_IS_INCLUDE:
268 case IGMPV3_MODE_IS_EXCLUDE:
269 if (gdeleted || sdeleted)
270 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800271 if (!(pmc->gsquery && !psf->sf_gsresp)) {
272 if (pmc->sfmode == MCAST_INCLUDE)
273 return 1;
274 /* don't include if this source is excluded
275 * in all filters
276 */
277 if (psf->sf_count[MCAST_INCLUDE])
278 return type == IGMPV3_MODE_IS_INCLUDE;
279 return pmc->sfcount[MCAST_EXCLUDE] ==
280 psf->sf_count[MCAST_EXCLUDE];
281 }
282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 case IGMPV3_CHANGE_TO_INCLUDE:
284 if (gdeleted || sdeleted)
285 return 0;
286 return psf->sf_count[MCAST_INCLUDE] != 0;
287 case IGMPV3_CHANGE_TO_EXCLUDE:
288 if (gdeleted || sdeleted)
289 return 0;
290 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
291 psf->sf_count[MCAST_INCLUDE])
292 return 0;
293 return pmc->sfcount[MCAST_EXCLUDE] ==
294 psf->sf_count[MCAST_EXCLUDE];
295 case IGMPV3_ALLOW_NEW_SOURCES:
296 if (gdeleted || !psf->sf_crcount)
297 return 0;
298 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
299 case IGMPV3_BLOCK_OLD_SOURCES:
300 if (pmc->sfmode == MCAST_INCLUDE)
301 return gdeleted || (psf->sf_crcount && sdeleted);
302 return psf->sf_crcount && !gdeleted && !sdeleted;
303 }
304 return 0;
305}
306
307static int
308igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
309{
310 struct ip_sf_list *psf;
311 int scount = 0;
312
Weilong Chenc71151f2013-12-23 14:37:29 +0800313 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
315 continue;
316 scount++;
317 }
318 return scount;
319}
320
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100321static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct sk_buff *skb;
324 struct rtable *rt;
325 struct iphdr *pip;
326 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700327 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700328 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000329 int hlen = LL_RESERVED_SPACE(dev);
330 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100331 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000333 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000334 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000335 GFP_ATOMIC | __GFP_NOWARN);
336 if (skb)
337 break;
338 size >>= 1;
339 if (size < 256)
340 return NULL;
341 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200342 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
David S. Miller31e45432011-05-03 20:25:42 -0700344 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500345 0, 0,
346 IPPROTO_IGMP, 0, dev->ifindex);
347 if (IS_ERR(rt)) {
348 kfree_skb(skb);
349 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Changli Gaod8d1f302010-06-10 23:31:35 -0700352 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 skb->dev = dev;
354
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100355 skb->reserved_tailroom = skb_end_offset(skb) -
356 min(mtu, skb_end_offset(skb));
Herbert Xu66088242011-11-18 02:20:04 +0000357 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300359 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700360 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300361 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 pip->version = 4;
364 pip->ihl = (sizeof(struct iphdr)+4)>>2;
365 pip->tos = 0xc0;
366 pip->frag_off = htons(IP_DF);
367 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700368 pip->daddr = fl4.daddr;
369 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 pip->protocol = IPPROTO_IGMP;
371 pip->tot_len = 0; /* filled in later */
Eric Dumazet73f156a2014-06-02 05:26:03 -0700372 ip_select_ident(skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000373 ((u8 *)&pip[1])[0] = IPOPT_RA;
374 ((u8 *)&pip[1])[1] = 4;
375 ((u8 *)&pip[1])[2] = 0;
376 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700378 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300379 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300380 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
382 pig->resv1 = 0;
383 pig->csum = 0;
384 pig->resv2 = 0;
385 pig->ngrec = 0;
386 return skb;
387}
388
389static int igmpv3_sendpack(struct sk_buff *skb)
390{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300391 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000392 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300394 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Herbert Xuc439cb22008-01-11 19:14:00 -0800396 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
400{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800401 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
405 int type, struct igmpv3_grec **ppgr)
406{
407 struct net_device *dev = pmc->interface->dev;
408 struct igmpv3_report *pih;
409 struct igmpv3_grec *pgr;
410
411 if (!skb)
412 skb = igmpv3_newpack(dev, dev->mtu);
413 if (!skb)
414 return NULL;
415 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
416 pgr->grec_type = type;
417 pgr->grec_auxwords = 0;
418 pgr->grec_nsrcs = 0;
419 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300420 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 pih->ngrec = htons(ntohs(pih->ngrec)+1);
422 *ppgr = pgr;
423 return skb;
424}
425
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100426#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
429 int type, int gdeleted, int sdeleted)
430{
431 struct net_device *dev = pmc->interface->dev;
432 struct igmpv3_report *pih;
433 struct igmpv3_grec *pgr = NULL;
434 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800435 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (pmc->multiaddr == IGMP_ALL_HOSTS)
438 return skb;
439
440 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
441 type == IGMPV3_MODE_IS_EXCLUDE;
442 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
443 type == IGMPV3_CHANGE_TO_EXCLUDE;
444
David L Stevensad125832006-01-18 14:20:56 -0800445 stotal = scount = 0;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
448
David L Stevensad125832006-01-18 14:20:56 -0800449 if (!*psf_list)
450 goto empty_source;
451
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300452 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /* EX and TO_EX get a fresh packet, if needed */
455 if (truncate) {
456 if (pih && pih->ngrec &&
457 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
458 if (skb)
459 igmpv3_sendpack(skb);
460 skb = igmpv3_newpack(dev, dev->mtu);
461 }
462 }
463 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800465 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700466 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 psf_next = psf->sf_next;
469
470 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
471 psf_prev = psf;
472 continue;
473 }
474
475 /* clear marks on query responses */
476 if (isquery)
477 psf->sf_gsresp = 0;
478
Al Viro63007722006-09-27 18:31:32 -0700479 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 first*sizeof(struct igmpv3_grec)) {
481 if (truncate && !first)
482 break; /* truncate these */
483 if (pgr)
484 pgr->grec_nsrcs = htons(scount);
485 if (skb)
486 igmpv3_sendpack(skb);
487 skb = igmpv3_newpack(dev, dev->mtu);
488 first = 1;
489 scount = 0;
490 }
491 if (first) {
492 skb = add_grhead(skb, pmc, type, &pgr);
493 first = 0;
494 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800495 if (!skb)
496 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700497 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800499 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
501 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
502 psf->sf_crcount--;
503 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
504 if (psf_prev)
505 psf_prev->sf_next = psf->sf_next;
506 else
507 *psf_list = psf->sf_next;
508 kfree(psf);
509 continue;
510 }
511 }
512 psf_prev = psf;
513 }
David L Stevensad125832006-01-18 14:20:56 -0800514
515empty_source:
516 if (!stotal) {
517 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
518 type == IGMPV3_BLOCK_OLD_SOURCES)
519 return skb;
520 if (pmc->crcount || isquery) {
521 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800522 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800523 igmpv3_sendpack(skb);
524 skb = NULL; /* add_grhead will get a new one */
525 }
526 skb = add_grhead(skb, pmc, type, &pgr);
527 }
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (pgr)
530 pgr->grec_nsrcs = htons(scount);
531
532 if (isquery)
533 pmc->gsquery = 0; /* clear query state on report */
534 return skb;
535}
536
537static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
538{
539 struct sk_buff *skb = NULL;
540 int type;
541
542 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000543 rcu_read_lock();
544 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (pmc->multiaddr == IGMP_ALL_HOSTS)
546 continue;
547 spin_lock_bh(&pmc->lock);
548 if (pmc->sfcount[MCAST_EXCLUDE])
549 type = IGMPV3_MODE_IS_EXCLUDE;
550 else
551 type = IGMPV3_MODE_IS_INCLUDE;
552 skb = add_grec(skb, pmc, type, 0, 0);
553 spin_unlock_bh(&pmc->lock);
554 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000555 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 } else {
557 spin_lock_bh(&pmc->lock);
558 if (pmc->sfcount[MCAST_EXCLUDE])
559 type = IGMPV3_MODE_IS_EXCLUDE;
560 else
561 type = IGMPV3_MODE_IS_INCLUDE;
562 skb = add_grec(skb, pmc, type, 0, 0);
563 spin_unlock_bh(&pmc->lock);
564 }
565 if (!skb)
566 return 0;
567 return igmpv3_sendpack(skb);
568}
569
570/*
571 * remove zero-count source records from a source filter list
572 */
573static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
574{
575 struct ip_sf_list *psf_prev, *psf_next, *psf;
576
577 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800578 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 psf_next = psf->sf_next;
580 if (psf->sf_crcount == 0) {
581 if (psf_prev)
582 psf_prev->sf_next = psf->sf_next;
583 else
584 *ppsf = psf->sf_next;
585 kfree(psf);
586 } else
587 psf_prev = psf;
588 }
589}
590
591static void igmpv3_send_cr(struct in_device *in_dev)
592{
593 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
594 struct sk_buff *skb = NULL;
595 int type, dtype;
596
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000597 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 spin_lock_bh(&in_dev->mc_tomb_lock);
599
600 /* deleted MCA's */
601 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800602 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 pmc_next = pmc->next;
604 if (pmc->sfmode == MCAST_INCLUDE) {
605 type = IGMPV3_BLOCK_OLD_SOURCES;
606 dtype = IGMPV3_BLOCK_OLD_SOURCES;
607 skb = add_grec(skb, pmc, type, 1, 0);
608 skb = add_grec(skb, pmc, dtype, 1, 1);
609 }
610 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (pmc->sfmode == MCAST_EXCLUDE) {
612 type = IGMPV3_CHANGE_TO_INCLUDE;
613 skb = add_grec(skb, pmc, type, 1, 0);
614 }
David L Stevensad125832006-01-18 14:20:56 -0800615 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (pmc->crcount == 0) {
617 igmpv3_clear_zeros(&pmc->tomb);
618 igmpv3_clear_zeros(&pmc->sources);
619 }
620 }
621 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
622 if (pmc_prev)
623 pmc_prev->next = pmc_next;
624 else
625 in_dev->mc_tomb = pmc_next;
626 in_dev_put(pmc->interface);
627 kfree(pmc);
628 } else
629 pmc_prev = pmc;
630 }
631 spin_unlock_bh(&in_dev->mc_tomb_lock);
632
633 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000634 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 spin_lock_bh(&pmc->lock);
636 if (pmc->sfcount[MCAST_EXCLUDE]) {
637 type = IGMPV3_BLOCK_OLD_SOURCES;
638 dtype = IGMPV3_ALLOW_NEW_SOURCES;
639 } else {
640 type = IGMPV3_ALLOW_NEW_SOURCES;
641 dtype = IGMPV3_BLOCK_OLD_SOURCES;
642 }
643 skb = add_grec(skb, pmc, type, 0, 0);
644 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
645
646 /* filter mode changes */
647 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (pmc->sfmode == MCAST_EXCLUDE)
649 type = IGMPV3_CHANGE_TO_EXCLUDE;
650 else
651 type = IGMPV3_CHANGE_TO_INCLUDE;
652 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800653 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 spin_unlock_bh(&pmc->lock);
656 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000657 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (!skb)
660 return;
661 (void) igmpv3_sendpack(skb);
662}
663
664static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
665 int type)
666{
667 struct sk_buff *skb;
668 struct iphdr *iph;
669 struct igmphdr *ih;
670 struct rtable *rt;
671 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700672 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700673 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700674 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700675 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000676 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
679 return igmpv3_send_report(in_dev, pmc);
680 else if (type == IGMP_HOST_LEAVE_MESSAGE)
681 dst = IGMP_ALL_ROUTER;
682 else
683 dst = group;
684
David S. Miller31e45432011-05-03 20:25:42 -0700685 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500686 0, 0,
687 IPPROTO_IGMP, 0, dev->ifindex);
688 if (IS_ERR(rt))
689 return -1;
690
Herbert Xu66088242011-11-18 02:20:04 +0000691 hlen = LL_RESERVED_SPACE(dev);
692 tlen = dev->needed_tailroom;
693 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (skb == NULL) {
695 ip_rt_put(rt);
696 return -1;
697 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200698 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Changli Gaod8d1f302010-06-10 23:31:35 -0700700 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Herbert Xu66088242011-11-18 02:20:04 +0000702 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300704 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700705 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300706 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 iph->version = 4;
709 iph->ihl = (sizeof(struct iphdr)+4)>>2;
710 iph->tos = 0xc0;
711 iph->frag_off = htons(IP_DF);
712 iph->ttl = 1;
713 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700714 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 iph->protocol = IPPROTO_IGMP;
Eric Dumazet73f156a2014-06-02 05:26:03 -0700716 ip_select_ident(skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000717 ((u8 *)&iph[1])[0] = IPOPT_RA;
718 ((u8 *)&iph[1])[1] = 4;
719 ((u8 *)&iph[1])[2] = 0;
720 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800723 ih->type = type;
724 ih->code = 0;
725 ih->csum = 0;
726 ih->group = group;
727 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Herbert Xuc439cb22008-01-11 19:14:00 -0800729 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732static void igmp_gq_timer_expire(unsigned long data)
733{
734 struct in_device *in_dev = (struct in_device *)data;
735
736 in_dev->mr_gq_running = 0;
737 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700738 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741static void igmp_ifc_timer_expire(unsigned long data)
742{
743 struct in_device *in_dev = (struct in_device *)data;
744
745 igmpv3_send_cr(in_dev);
746 if (in_dev->mr_ifc_count) {
747 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100748 igmp_ifc_start_timer(in_dev,
749 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700751 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754static void igmp_ifc_event(struct in_device *in_dev)
755{
756 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
757 return;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +0200758 in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 igmp_ifc_start_timer(in_dev, 1);
760}
761
762
763static void igmp_timer_expire(unsigned long data)
764{
Weilong Chenc71151f2013-12-23 14:37:29 +0800765 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct in_device *in_dev = im->interface;
767
768 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800769 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (im->unsolicit_count) {
772 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100773 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 im->reporter = 1;
776 spin_unlock(&im->lock);
777
778 if (IGMP_V1_SEEN(in_dev))
779 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
780 else if (IGMP_V2_SEEN(in_dev))
781 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
782 else
783 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
784
785 ip_ma_put(im);
786}
787
David L Stevensad125832006-01-18 14:20:56 -0800788/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700789static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct ip_sf_list *psf;
792 int i, scount;
793
794 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800795 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (scount == nsrcs)
797 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800798 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800799 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000800 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800801 pmc->sfcount[MCAST_EXCLUDE] !=
802 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800803 break;
David L Stevensad125832006-01-18 14:20:56 -0800804 if (srcs[i] == psf->sf_inaddr) {
805 scount++;
806 break;
807 }
808 }
809 }
810 pmc->gsquery = 0;
811 if (scount == nsrcs) /* all sources excluded */
812 return 0;
813 return 1;
814}
815
Al Viroea4d9e72006-09-27 18:30:52 -0700816static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800817{
818 struct ip_sf_list *psf;
819 int i, scount;
820
821 if (pmc->sfmode == MCAST_EXCLUDE)
822 return igmp_xmarksources(pmc, nsrcs, srcs);
823
824 /* mark INCLUDE-mode sources */
825 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800826 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800827 if (scount == nsrcs)
828 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800829 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (srcs[i] == psf->sf_inaddr) {
831 psf->sf_gsresp = 1;
832 scount++;
833 break;
834 }
835 }
David L Stevensad125832006-01-18 14:20:56 -0800836 if (!scount) {
837 pmc->gsquery = 0;
838 return 0;
839 }
840 pmc->gsquery = 1;
841 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Eric Dumazetd679c532012-09-06 20:37:06 +0000844/* return true if packet was dropped */
845static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct ip_mc_list *im;
848
849 /* Timers are only set for non-local groups */
850
851 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000852 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000854 rcu_read_lock();
855 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (im->multiaddr == group) {
857 igmp_stop_timer(im);
858 break;
859 }
860 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000861 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000862 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Eric Dumazetd679c532012-09-06 20:37:06 +0000865/* return true if packet was dropped */
866static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int len)
868{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300869 struct igmphdr *ih = igmp_hdr(skb);
870 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700872 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int max_delay;
874 int mark = 0;
875
876
David Stevens5b7c8402010-09-30 14:29:40 +0000877 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (ih->code == 0) {
879 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900880
Fabian Frederick436f7c22014-11-04 20:52:14 +0100881 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 in_dev->mr_v1_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100883 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 group = 0;
885 } else {
886 /* v2 router present */
887 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
888 in_dev->mr_v2_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100889 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 /* cancel the interface change timer */
892 in_dev->mr_ifc_count = 0;
893 if (del_timer(&in_dev->mr_ifc_timer))
894 __in_dev_put(in_dev);
895 /* clear deleted report items */
896 igmpv3_clear_delrec(in_dev);
897 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000898 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000899 } else if (IGMP_V1_SEEN(in_dev)) {
900 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100901 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000902 group = 0;
903 } else if (IGMP_V2_SEEN(in_dev)) {
904 /* this is a v3 query with v2 queriers present;
905 * Interpretation of the max_delay code is problematic here.
906 * A real v2 host would use ih_code directly, while v3 has a
907 * different encoding. We use the v3 encoding as more likely
908 * to be intended in a v3 query.
909 */
910 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800911 if (!max_delay)
912 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else { /* v3 */
914 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000915 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900916
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300917 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900919 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700920 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000921 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300922 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
925 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
926 if (!max_delay)
927 max_delay = 1; /* can't mod w/ 0 */
928 in_dev->mr_maxdelay = max_delay;
929 if (ih3->qrv)
930 in_dev->mr_qrv = ih3->qrv;
931 if (!group) { /* general query */
932 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +0200933 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000935 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 /* mark sources to include, if group & source-specific */
938 mark = ih3->nsrcs != 0;
939 }
940
941 /*
942 * - Start the timers in all of our membership records
943 * that the query applies to for the interface on
944 * which the query arrived excl. those that belong
945 * to a "local" group (224.0.0.X)
946 * - For timers already running check if they need to
947 * be reset.
948 * - Use the igmp->igmp_code field as the maximum
949 * delay possible
950 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000951 rcu_read_lock();
952 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800953 int changed;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (group && group != im->multiaddr)
956 continue;
957 if (im->multiaddr == IGMP_ALL_HOSTS)
958 continue;
959 spin_lock_bh(&im->lock);
960 if (im->tm_running)
961 im->gsquery = im->gsquery && mark;
962 else
963 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800964 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900965 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800967 if (changed)
968 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000970 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000971 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000974/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975int igmp_rcv(struct sk_buff *skb)
976{
977 /* This basically follows the spec line by line -- see RFC1112 */
978 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000979 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +0000981 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800983 if (in_dev == NULL)
984 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Herbert Xufb286bb2005-11-10 13:01:24 -0800986 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000987 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800988
Tom Herbertde08dc12014-05-07 16:52:10 -0700989 if (skb_checksum_simple_validate(skb))
990 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300992 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 switch (ih->type) {
994 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +0000995 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
997 case IGMP_HOST_MEMBERSHIP_REPORT:
998 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001000 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
David Stevens24c69272005-12-02 20:32:59 -08001002 /* don't rely on MC router hearing unicast reports */
1003 if (skb->pkt_type == PACKET_MULTICAST ||
1004 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001005 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
1007 case IGMP_PIM:
1008#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return pim_rcv_v1(skb);
1010#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001011 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 case IGMP_DVMRP:
1013 case IGMP_TRACE:
1014 case IGMP_HOST_LEAVE_MESSAGE:
1015 case IGMP_MTRACE:
1016 case IGMP_MTRACE_RESP:
1017 break;
1018 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001019 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001021
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001022drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001023 if (dropped)
1024 kfree_skb(skb);
1025 else
1026 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 0;
1028}
1029
1030#endif
1031
1032
1033/*
1034 * Add a filter to a device
1035 */
1036
Al Viro63007722006-09-27 18:31:32 -07001037static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 char buf[MAX_ADDR_LEN];
1040 struct net_device *dev = in_dev->dev;
1041
1042 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1043 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001044 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 routine. Something sort of:
1046 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1047 --ANK
1048 */
1049 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001050 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
1054 * Remove a filter from a device
1055 */
1056
Al Viro63007722006-09-27 18:31:32 -07001057static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 char buf[MAX_ADDR_LEN];
1060 struct net_device *dev = in_dev->dev;
1061
1062 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001063 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066#ifdef CONFIG_IP_MULTICAST
1067/*
1068 * deleted ip_mc_list manipulation
1069 */
1070static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1071{
1072 struct ip_mc_list *pmc;
1073
1074 /* this is an "ip_mc_list" for convenience; only the fields below
1075 * are actually used. In particular, the refcnt and users are not
1076 * used for management of the delete list. Using the same structure
1077 * for deleted items allows change reports to use common code with
1078 * non-deleted or query-response MCA's.
1079 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001080 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!pmc)
1082 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 spin_lock_bh(&im->lock);
1084 pmc->interface = im->interface;
1085 in_dev_hold(in_dev);
1086 pmc->multiaddr = im->multiaddr;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001087 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 pmc->sfmode = im->sfmode;
1089 if (pmc->sfmode == MCAST_INCLUDE) {
1090 struct ip_sf_list *psf;
1091
1092 pmc->tomb = im->tomb;
1093 pmc->sources = im->sources;
1094 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001095 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 psf->sf_crcount = pmc->crcount;
1097 }
1098 spin_unlock_bh(&im->lock);
1099
1100 spin_lock_bh(&in_dev->mc_tomb_lock);
1101 pmc->next = in_dev->mc_tomb;
1102 in_dev->mc_tomb = pmc;
1103 spin_unlock_bh(&in_dev->mc_tomb_lock);
1104}
1105
Al Viro63007722006-09-27 18:31:32 -07001106static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 struct ip_mc_list *pmc, *pmc_prev;
1109 struct ip_sf_list *psf, *psf_next;
1110
1111 spin_lock_bh(&in_dev->mc_tomb_lock);
1112 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001113 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (pmc->multiaddr == multiaddr)
1115 break;
1116 pmc_prev = pmc;
1117 }
1118 if (pmc) {
1119 if (pmc_prev)
1120 pmc_prev->next = pmc->next;
1121 else
1122 in_dev->mc_tomb = pmc->next;
1123 }
1124 spin_unlock_bh(&in_dev->mc_tomb_lock);
1125 if (pmc) {
Weilong Chenc71151f2013-12-23 14:37:29 +08001126 for (psf = pmc->tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 psf_next = psf->sf_next;
1128 kfree(psf);
1129 }
1130 in_dev_put(pmc->interface);
1131 kfree(pmc);
1132 }
1133}
1134
1135static void igmpv3_clear_delrec(struct in_device *in_dev)
1136{
1137 struct ip_mc_list *pmc, *nextpmc;
1138
1139 spin_lock_bh(&in_dev->mc_tomb_lock);
1140 pmc = in_dev->mc_tomb;
1141 in_dev->mc_tomb = NULL;
1142 spin_unlock_bh(&in_dev->mc_tomb_lock);
1143
1144 for (; pmc; pmc = nextpmc) {
1145 nextpmc = pmc->next;
1146 ip_mc_clear_src(pmc);
1147 in_dev_put(pmc->interface);
1148 kfree(pmc);
1149 }
1150 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001151 rcu_read_lock();
1152 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct ip_sf_list *psf, *psf_next;
1154
1155 spin_lock_bh(&pmc->lock);
1156 psf = pmc->tomb;
1157 pmc->tomb = NULL;
1158 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001159 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 psf_next = psf->sf_next;
1161 kfree(psf);
1162 }
1163 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001164 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166#endif
1167
1168static void igmp_group_dropped(struct ip_mc_list *im)
1169{
1170 struct in_device *in_dev = im->interface;
1171#ifdef CONFIG_IP_MULTICAST
1172 int reporter;
1173#endif
1174
1175 if (im->loaded) {
1176 im->loaded = 0;
1177 ip_mc_filter_del(in_dev, im->multiaddr);
1178 }
1179
1180#ifdef CONFIG_IP_MULTICAST
1181 if (im->multiaddr == IGMP_ALL_HOSTS)
1182 return;
1183
1184 reporter = im->reporter;
1185 igmp_stop_timer(im);
1186
1187 if (!in_dev->dead) {
1188 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (IGMP_V2_SEEN(in_dev)) {
1191 if (reporter)
1192 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195 /* IGMPv3 */
1196 igmpv3_add_delrec(in_dev, im);
1197
1198 igmp_ifc_event(in_dev);
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203static void igmp_group_added(struct ip_mc_list *im)
1204{
1205 struct in_device *in_dev = im->interface;
1206
1207 if (im->loaded == 0) {
1208 im->loaded = 1;
1209 ip_mc_filter_add(in_dev, im->multiaddr);
1210 }
1211
1212#ifdef CONFIG_IP_MULTICAST
1213 if (im->multiaddr == IGMP_ALL_HOSTS)
1214 return;
1215
1216 if (in_dev->dead)
1217 return;
1218 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1219 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001220 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 spin_unlock_bh(&im->lock);
1222 return;
1223 }
1224 /* else, v3 */
1225
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001226 im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 igmp_ifc_event(in_dev);
1228#endif
1229}
1230
1231
1232/*
1233 * Multicast list managers
1234 */
1235
Eric Dumazete9897072013-06-07 08:48:57 -07001236static u32 ip_mc_hash(const struct ip_mc_list *im)
1237{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001238 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001239}
1240
1241static void ip_mc_hash_add(struct in_device *in_dev,
1242 struct ip_mc_list *im)
1243{
1244 struct ip_mc_list __rcu **mc_hash;
1245 u32 hash;
1246
1247 mc_hash = rtnl_dereference(in_dev->mc_hash);
1248 if (mc_hash) {
1249 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001250 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001251 rcu_assign_pointer(mc_hash[hash], im);
1252 return;
1253 }
1254
1255 /* do not use a hash table for small number of items */
1256 if (in_dev->mc_count < 4)
1257 return;
1258
1259 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1260 GFP_KERNEL);
1261 if (!mc_hash)
1262 return;
1263
1264 for_each_pmc_rtnl(in_dev, im) {
1265 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001266 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001267 RCU_INIT_POINTER(mc_hash[hash], im);
1268 }
1269
1270 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1271}
1272
1273static void ip_mc_hash_remove(struct in_device *in_dev,
1274 struct ip_mc_list *im)
1275{
1276 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1277 struct ip_mc_list *aux;
1278
1279 if (!mc_hash)
1280 return;
1281 mc_hash += ip_mc_hash(im);
1282 while ((aux = rtnl_dereference(*mc_hash)) != im)
1283 mc_hash = &aux->next_hash;
1284 *mc_hash = im->next_hash;
1285}
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288/*
1289 * A socket has joined a multicast group on device dev.
1290 */
1291
Al Viro8f935bb2006-09-27 18:30:07 -07001292void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct ip_mc_list *im;
1295
1296 ASSERT_RTNL();
1297
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001298 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (im->multiaddr == addr) {
1300 im->users++;
1301 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1302 goto out;
1303 }
1304 }
1305
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001306 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (!im)
1308 goto out;
1309
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001310 im->users = 1;
1311 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001313 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* initial mode is (EX, empty) */
1315 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 atomic_set(&im->refcnt, 1);
1318 spin_lock_init(&im->lock);
1319#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301320 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001321 im->unsolicit_count = sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001323
1324 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001325 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001326 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001327
Eric Dumazete9897072013-06-07 08:48:57 -07001328 ip_mc_hash_add(in_dev, im);
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#ifdef CONFIG_IP_MULTICAST
1331 igmpv3_del_delrec(in_dev, im->multiaddr);
1332#endif
1333 igmp_group_added(im);
1334 if (!in_dev->dead)
1335 ip_rt_multicast_event(in_dev);
1336out:
1337 return;
1338}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001339EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001342 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001343 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001344static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001345{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001346#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001347 struct ip_mc_list *im;
1348 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001349
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001350 ASSERT_RTNL();
1351
1352 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001353 if (im->multiaddr == IGMP_ALL_HOSTS)
1354 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001355
Eric Dumazet866f3b22010-11-18 09:33:19 -08001356 /* a failover is happening and switches
1357 * must be notified immediately
1358 */
1359 if (IGMP_V1_SEEN(in_dev))
1360 type = IGMP_HOST_MEMBERSHIP_REPORT;
1361 else if (IGMP_V2_SEEN(in_dev))
1362 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1363 else
1364 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1365 igmp_send_report(in_dev, im, type);
1366 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001367#endif
1368}
1369
1370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * A socket has left a multicast group on device dev
1372 */
1373
Al Viro8f935bb2006-09-27 18:30:07 -07001374void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001376 struct ip_mc_list *i;
1377 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001380
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001381 for (ip = &in_dev->mc_list;
1382 (i = rtnl_dereference(*ip)) != NULL;
1383 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001384 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001386 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001387 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001388 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001390 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (!in_dev->dead)
1393 ip_rt_multicast_event(in_dev);
1394
1395 ip_ma_put(i);
1396 return;
1397 }
1398 break;
1399 }
1400 }
1401}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001402EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Moni Shoua75c78502009-09-15 02:37:40 -07001404/* Device changing type */
1405
1406void ip_mc_unmap(struct in_device *in_dev)
1407{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001408 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001409
1410 ASSERT_RTNL();
1411
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001412 for_each_pmc_rtnl(in_dev, pmc)
1413 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001414}
1415
1416void ip_mc_remap(struct in_device *in_dev)
1417{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001418 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001419
1420 ASSERT_RTNL();
1421
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001422 for_each_pmc_rtnl(in_dev, pmc)
1423 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001424}
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426/* Device going down */
1427
1428void ip_mc_down(struct in_device *in_dev)
1429{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001430 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 ASSERT_RTNL();
1433
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001434 for_each_pmc_rtnl(in_dev, pmc)
1435 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437#ifdef CONFIG_IP_MULTICAST
1438 in_dev->mr_ifc_count = 0;
1439 if (del_timer(&in_dev->mr_ifc_timer))
1440 __in_dev_put(in_dev);
1441 in_dev->mr_gq_running = 0;
1442 if (del_timer(&in_dev->mr_gq_timer))
1443 __in_dev_put(in_dev);
1444 igmpv3_clear_delrec(in_dev);
1445#endif
1446
1447 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1448}
1449
1450void ip_mc_init_dev(struct in_device *in_dev)
1451{
1452 ASSERT_RTNL();
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001455 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1456 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001457 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1458 (unsigned long)in_dev);
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001459 in_dev->mr_qrv = sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460#endif
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 spin_lock_init(&in_dev->mc_tomb_lock);
1463}
1464
1465/* Device going up */
1466
1467void ip_mc_up(struct in_device *in_dev)
1468{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001469 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 ASSERT_RTNL();
1472
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001473#ifdef CONFIG_IP_MULTICAST
1474 in_dev->mr_qrv = sysctl_igmp_qrv;
1475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1477
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001478 for_each_pmc_rtnl(in_dev, pmc)
1479 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/*
1483 * Device is about to be destroyed: clean up.
1484 */
1485
1486void ip_mc_destroy_dev(struct in_device *in_dev)
1487{
1488 struct ip_mc_list *i;
1489
1490 ASSERT_RTNL();
1491
1492 /* Deactivate timers */
1493 ip_mc_down(in_dev);
1494
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001495 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1496 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001497 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001498
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001499 /* We've dropped the groups in ip_mc_down already */
1500 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001505/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001506static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct net_device *dev = NULL;
1509 struct in_device *idev = NULL;
1510
1511 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001512 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return idev;
1514 }
1515 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001516 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (!dev)
1518 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520
David S. Millerb23dd4f2011-03-02 14:31:35 -08001521 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001522 struct rtable *rt = ip_route_output(net,
1523 imr->imr_multiaddr.s_addr,
1524 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001525 if (!IS_ERR(rt)) {
1526 dev = rt->dst.dev;
1527 ip_rt_put(rt);
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530 if (dev) {
1531 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001532 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 return idev;
1535}
1536
1537/*
1538 * Join a socket to a group
1539 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001540int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1541int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001542#ifdef CONFIG_IP_MULTICAST
Fabian Frederick436f7c22014-11-04 20:52:14 +01001543int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001544#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001547 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 struct ip_sf_list *psf, *psf_prev;
1550 int rv = 0;
1551
1552 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001553 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (psf->sf_inaddr == *psfsrc)
1555 break;
1556 psf_prev = psf;
1557 }
1558 if (!psf || psf->sf_count[sfmode] == 0) {
1559 /* source filter not found, or count wrong => bug */
1560 return -ESRCH;
1561 }
1562 psf->sf_count[sfmode]--;
1563 if (psf->sf_count[sfmode] == 0) {
1564 ip_rt_multicast_event(pmc->interface);
1565 }
1566 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1567#ifdef CONFIG_IP_MULTICAST
1568 struct in_device *in_dev = pmc->interface;
1569#endif
1570
1571 /* no more filters for this source */
1572 if (psf_prev)
1573 psf_prev->sf_next = psf->sf_next;
1574 else
1575 pmc->sources = psf->sf_next;
1576#ifdef CONFIG_IP_MULTICAST
1577 if (psf->sf_oldin &&
1578 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001579 psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 psf->sf_next = pmc->tomb;
1581 pmc->tomb = psf;
1582 rv = 1;
1583 } else
1584#endif
1585 kfree(psf);
1586 }
1587 return rv;
1588}
1589
1590#ifndef CONFIG_IP_MULTICAST
1591#define igmp_ifc_event(x) do { } while (0)
1592#endif
1593
Al Viro8f935bb2006-09-27 18:30:07 -07001594static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1595 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 struct ip_mc_list *pmc;
1598 int changerec = 0;
1599 int i, err;
1600
1601 if (!in_dev)
1602 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001603 rcu_read_lock();
1604 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (*pmca == pmc->multiaddr)
1606 break;
1607 }
1608 if (!pmc) {
1609 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001610 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return -ESRCH;
1612 }
1613 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001614 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615#ifdef CONFIG_IP_MULTICAST
1616 sf_markstate(pmc);
1617#endif
1618 if (!delta) {
1619 err = -EINVAL;
1620 if (!pmc->sfcount[sfmode])
1621 goto out_unlock;
1622 pmc->sfcount[sfmode]--;
1623 }
1624 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001625 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1627
1628 changerec |= rv > 0;
1629 if (!err && rv < 0)
1630 err = rv;
1631 }
1632 if (pmc->sfmode == MCAST_EXCLUDE &&
1633 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1634 pmc->sfcount[MCAST_INCLUDE]) {
1635#ifdef CONFIG_IP_MULTICAST
1636 struct ip_sf_list *psf;
1637#endif
1638
1639 /* filter mode change */
1640 pmc->sfmode = MCAST_INCLUDE;
1641#ifdef CONFIG_IP_MULTICAST
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001642 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001644 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 psf->sf_crcount = 0;
1646 igmp_ifc_event(pmc->interface);
1647 } else if (sf_setstate(pmc) || changerec) {
1648 igmp_ifc_event(pmc->interface);
1649#endif
1650 }
1651out_unlock:
1652 spin_unlock_bh(&pmc->lock);
1653 return err;
1654}
1655
1656/*
1657 * Add multicast single-source filter to the interface list
1658 */
1659static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001660 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 struct ip_sf_list *psf, *psf_prev;
1663
1664 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001665 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (psf->sf_inaddr == *psfsrc)
1667 break;
1668 psf_prev = psf;
1669 }
1670 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001671 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (!psf)
1673 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 psf->sf_inaddr = *psfsrc;
1675 if (psf_prev) {
1676 psf_prev->sf_next = psf;
1677 } else
1678 pmc->sources = psf;
1679 }
1680 psf->sf_count[sfmode]++;
1681 if (psf->sf_count[sfmode] == 1) {
1682 ip_rt_multicast_event(pmc->interface);
1683 }
1684 return 0;
1685}
1686
1687#ifdef CONFIG_IP_MULTICAST
1688static void sf_markstate(struct ip_mc_list *pmc)
1689{
1690 struct ip_sf_list *psf;
1691 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1692
Weilong Chenc71151f2013-12-23 14:37:29 +08001693 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (pmc->sfcount[MCAST_EXCLUDE]) {
1695 psf->sf_oldin = mca_xcount ==
1696 psf->sf_count[MCAST_EXCLUDE] &&
1697 !psf->sf_count[MCAST_INCLUDE];
1698 } else
1699 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1700}
1701
1702static int sf_setstate(struct ip_mc_list *pmc)
1703{
David L Stevensad125832006-01-18 14:20:56 -08001704 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1706 int qrv = pmc->interface->mr_qrv;
1707 int new_in, rv;
1708
1709 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001710 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (pmc->sfcount[MCAST_EXCLUDE]) {
1712 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1713 !psf->sf_count[MCAST_INCLUDE];
1714 } else
1715 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001716 if (new_in) {
1717 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001718 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001719
Weilong Chenc71151f2013-12-23 14:37:29 +08001720 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001721 if (dpsf->sf_inaddr == psf->sf_inaddr)
1722 break;
1723 prev = dpsf;
1724 }
1725 if (dpsf) {
1726 if (prev)
1727 prev->sf_next = dpsf->sf_next;
1728 else
1729 pmc->tomb = dpsf->sf_next;
1730 kfree(dpsf);
1731 }
1732 psf->sf_crcount = qrv;
1733 rv++;
1734 }
1735 } else if (psf->sf_oldin) {
1736
1737 psf->sf_crcount = 0;
1738 /*
1739 * add or update "delete" records if an active filter
1740 * is now inactive
1741 */
Weilong Chenc71151f2013-12-23 14:37:29 +08001742 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08001743 if (dpsf->sf_inaddr == psf->sf_inaddr)
1744 break;
1745 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001746 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001747 if (!dpsf)
1748 continue;
1749 *dpsf = *psf;
1750 /* pmc->lock held by callers */
1751 dpsf->sf_next = pmc->tomb;
1752 pmc->tomb = dpsf;
1753 }
1754 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 rv++;
1756 }
1757 }
1758 return rv;
1759}
1760#endif
1761
1762/*
1763 * Add multicast source filter list to the interface list
1764 */
Al Viro8f935bb2006-09-27 18:30:07 -07001765static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1766 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 struct ip_mc_list *pmc;
1769 int isexclude;
1770 int i, err;
1771
1772 if (!in_dev)
1773 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001774 rcu_read_lock();
1775 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (*pmca == pmc->multiaddr)
1777 break;
1778 }
1779 if (!pmc) {
1780 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001781 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return -ESRCH;
1783 }
1784 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001785 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787#ifdef CONFIG_IP_MULTICAST
1788 sf_markstate(pmc);
1789#endif
1790 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1791 if (!delta)
1792 pmc->sfcount[sfmode]++;
1793 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001794 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00001795 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (err)
1797 break;
1798 }
1799 if (err) {
1800 int j;
1801
Jun Zhao685f94e62011-11-22 17:19:03 +00001802 if (!delta)
1803 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08001804 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00001805 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1807#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001809 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810#endif
1811
1812 /* filter mode change */
1813 if (pmc->sfcount[MCAST_EXCLUDE])
1814 pmc->sfmode = MCAST_EXCLUDE;
1815 else if (pmc->sfcount[MCAST_INCLUDE])
1816 pmc->sfmode = MCAST_INCLUDE;
1817#ifdef CONFIG_IP_MULTICAST
1818 /* else no filters; keep old mode for reports */
1819
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001820 pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001822 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 psf->sf_crcount = 0;
1824 igmp_ifc_event(in_dev);
1825 } else if (sf_setstate(pmc)) {
1826 igmp_ifc_event(in_dev);
1827#endif
1828 }
1829 spin_unlock_bh(&pmc->lock);
1830 return err;
1831}
1832
1833static void ip_mc_clear_src(struct ip_mc_list *pmc)
1834{
1835 struct ip_sf_list *psf, *nextpsf;
1836
Weilong Chenc71151f2013-12-23 14:37:29 +08001837 for (psf = pmc->tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 nextpsf = psf->sf_next;
1839 kfree(psf);
1840 }
1841 pmc->tomb = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001842 for (psf = pmc->sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 nextpsf = psf->sf_next;
1844 kfree(psf);
1845 }
1846 pmc->sources = NULL;
1847 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001848 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 pmc->sfcount[MCAST_EXCLUDE] = 1;
1850}
1851
Eric Dumazet959d10f2015-02-17 03:19:24 -08001852int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Al Viro8f935bb2006-09-27 18:30:07 -07001854 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08001855 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct in_device *in_dev;
1857 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001858 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001859 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08001861 int err;
1862
1863 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Joe Perchesf97c1e02007-12-16 13:45:43 -08001865 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return -EINVAL;
1867
Daniel Lezcano877aced2008-08-13 16:15:57 -07001868 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 err = -ENODEV;
1872 goto done;
1873 }
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001876 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001877 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001878 if (i->multi.imr_multiaddr.s_addr == addr &&
1879 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 count++;
1882 }
1883 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001884 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001886 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001887 if (iml == NULL)
1888 goto done;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001891 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 iml->sflist = NULL;
1893 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001894 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return err;
1899}
Eric Dumazet959d10f2015-02-17 03:19:24 -08001900EXPORT_SYMBOL(__ip_mc_join_group);
1901
1902/* Join a multicast group
1903 */
1904int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
1905{
1906 int ret;
1907
1908 rtnl_lock();
1909 ret = __ip_mc_join_group(sk, imr);
1910 rtnl_unlock();
1911
1912 return ret;
1913}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001914EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1917 struct in_device *in_dev)
1918{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001919 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 int err;
1921
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001922 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 /* any-source empty exclude case */
1924 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1925 iml->sfmode, 0, NULL, 0);
1926 }
1927 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001928 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001929 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001930 /* decrease mem now to avoid the memleak warning */
1931 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08001932 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return err;
1934}
1935
Eric Dumazet959d10f2015-02-17 03:19:24 -08001936int __ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001939 struct ip_mc_socklist *iml;
1940 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001941 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001942 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001943 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001944 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001945 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Eric Dumazet959d10f2015-02-17 03:19:24 -08001947 ASSERT_RTNL();
1948
Daniel Lezcano877aced2008-08-13 16:15:57 -07001949 in_dev = ip_mc_find_dev(net, imr);
dingtianhong52ad3532014-07-02 13:50:48 +08001950 if (!in_dev) {
1951 ret = -ENODEV;
1952 goto out;
1953 }
David L Stevens84b42ba2005-07-08 17:48:38 -07001954 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001955 for (imlp = &inet->mc_list;
1956 (iml = rtnl_dereference(*imlp)) != NULL;
1957 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001958 if (iml->multi.imr_multiaddr.s_addr != group)
1959 continue;
1960 if (ifindex) {
1961 if (iml->multi.imr_ifindex != ifindex)
1962 continue;
1963 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1964 iml->multi.imr_address.s_addr)
1965 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
David L Stevensacd6e002006-08-17 16:27:39 -07001967 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001969 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001970
dingtianhong52ad3532014-07-02 13:50:48 +08001971 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08001972
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001973 /* decrease mem now to avoid the memleak warning */
1974 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08001975 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07001976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
dingtianhong52ad3532014-07-02 13:50:48 +08001978out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08001979 return ret;
1980}
1981EXPORT_SYMBOL(__ip_mc_leave_group);
1982
1983int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1984{
1985 int ret;
1986
1987 rtnl_lock();
1988 ret = __ip_mc_leave_group(sk, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 rtnl_unlock();
Eric Dumazet959d10f2015-02-17 03:19:24 -08001990
David L Stevensacd6e002006-08-17 16:27:39 -07001991 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
stephen hemminger193ba922012-10-01 12:32:34 +00001993EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995int ip_mc_source(int add, int omode, struct sock *sk, struct
1996 ip_mreq_source *mreqs, int ifindex)
1997{
1998 int err;
1999 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002000 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 struct ip_mc_socklist *pmc;
2002 struct in_device *in_dev = NULL;
2003 struct inet_sock *inet = inet_sk(sk);
2004 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002005 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002006 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int i, j, rv;
2008
Joe Perchesf97c1e02007-12-16 13:45:43 -08002009 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return -EINVAL;
2011
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002012 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2015 imr.imr_address.s_addr = mreqs->imr_interface;
2016 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002017 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 if (!in_dev) {
2020 err = -ENODEV;
2021 goto done;
2022 }
2023 err = -EADDRNOTAVAIL;
2024
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002025 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002026 if ((pmc->multi.imr_multiaddr.s_addr ==
2027 imr.imr_multiaddr.s_addr) &&
2028 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
2030 }
David L Stevens917f2f12005-07-08 17:45:16 -07002031 if (!pmc) { /* must have a prior join */
2032 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /* if a source filter was set, must be the same mode as before */
2036 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002037 if (pmc->sfmode != omode) {
2038 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 } else if (pmc->sfmode != omode) {
2042 /* allow mode switches for empty-set filters */
2043 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002044 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 NULL, 0);
2046 pmc->sfmode = omode;
2047 }
2048
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002049 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (!add) {
2051 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002052 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002054 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002056 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (rv == 0)
2058 break;
2059 }
2060 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002061 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
David L Stevens8cdaaa12005-07-08 17:39:23 -07002063 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2064 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2065 leavegroup = 1;
2066 goto done;
2067 }
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002070 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 &mreqs->imr_sourceaddr, 1);
2072
Weilong Chenc71151f2013-12-23 14:37:29 +08002073 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 psl->sl_addr[j-1] = psl->sl_addr[j];
2075 psl->sl_count--;
2076 err = 0;
2077 goto done;
2078 }
2079 /* else, add a new source to the filter */
2080
2081 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2082 err = -ENOBUFS;
2083 goto done;
2084 }
2085 if (!psl || psl->sl_count == psl->sl_max) {
2086 struct ip_sf_socklist *newpsl;
2087 int count = IP_SFBLOCK;
2088
2089 if (psl)
2090 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002091 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (!newpsl) {
2093 err = -ENOBUFS;
2094 goto done;
2095 }
2096 newpsl->sl_max = count;
2097 newpsl->sl_count = count - IP_SFBLOCK;
2098 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002099 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002101 /* decrease mem now to avoid the memleak warning */
2102 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002103 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002105 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002106 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002109 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002111 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (rv == 0)
2113 break;
2114 }
2115 if (rv == 0) /* address already there is an error */
2116 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002117 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 psl->sl_addr[j+1] = psl->sl_addr[j];
2119 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2120 psl->sl_count++;
2121 err = 0;
2122 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002123 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 &mreqs->imr_sourceaddr, 1);
2125done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002126 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002127 if (leavegroup)
2128 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return err;
2130}
2131
2132int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2133{
David L Stevens9951f032005-07-08 17:47:28 -07002134 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002136 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct ip_mc_socklist *pmc;
2138 struct in_device *in_dev;
2139 struct inet_sock *inet = inet_sk(sk);
2140 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002141 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002142 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Joe Perchesf97c1e02007-12-16 13:45:43 -08002144 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return -EINVAL;
2146 if (msf->imsf_fmode != MCAST_INCLUDE &&
2147 msf->imsf_fmode != MCAST_EXCLUDE)
2148 return -EINVAL;
2149
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002150 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2153 imr.imr_address.s_addr = msf->imsf_interface;
2154 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002155 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 if (!in_dev) {
2158 err = -ENODEV;
2159 goto done;
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
David L Stevens9951f032005-07-08 17:47:28 -07002162 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2163 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2164 leavegroup = 1;
2165 goto done;
2166 }
2167
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002168 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2170 pmc->multi.imr_ifindex == imr.imr_ifindex)
2171 break;
2172 }
David L Stevens917f2f12005-07-08 17:45:16 -07002173 if (!pmc) { /* must have a prior join */
2174 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002178 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2179 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (!newpsl) {
2181 err = -ENOBUFS;
2182 goto done;
2183 }
2184 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2185 memcpy(newpsl->sl_addr, msf->imsf_slist,
2186 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2187 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2188 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2189 if (err) {
2190 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2191 goto done;
2192 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002193 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002195 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2196 msf->imsf_fmode, 0, NULL, 0);
2197 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002198 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (psl) {
2200 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2201 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002202 /* decrease mem now to avoid the memleak warning */
2203 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002204 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 } else
2206 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2207 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002208 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002210 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002212 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002213 if (leavegroup)
2214 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return err;
2216}
2217
2218int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2219 struct ip_msfilter __user *optval, int __user *optlen)
2220{
2221 int err, len, count, copycount;
2222 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002223 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 struct ip_mc_socklist *pmc;
2225 struct in_device *in_dev;
2226 struct inet_sock *inet = inet_sk(sk);
2227 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002228 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Joe Perchesf97c1e02007-12-16 13:45:43 -08002230 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return -EINVAL;
2232
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002233 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2236 imr.imr_address.s_addr = msf->imsf_interface;
2237 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002238 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 if (!in_dev) {
2241 err = -ENODEV;
2242 goto done;
2243 }
2244 err = -EADDRNOTAVAIL;
2245
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002246 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2248 pmc->multi.imr_ifindex == imr.imr_ifindex)
2249 break;
2250 }
2251 if (!pmc) /* must have a prior join */
2252 goto done;
2253 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002254 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002255 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (!psl) {
2257 len = 0;
2258 count = 0;
2259 } else {
2260 count = psl->sl_count;
2261 }
2262 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2263 len = copycount * sizeof(psl->sl_addr[0]);
2264 msf->imsf_numsrc = count;
2265 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2266 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2267 return -EFAULT;
2268 }
2269 if (len &&
2270 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2271 return -EFAULT;
2272 return 0;
2273done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002274 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return err;
2276}
2277
2278int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2279 struct group_filter __user *optval, int __user *optlen)
2280{
2281 int err, i, count, copycount;
2282 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002283 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct ip_mc_socklist *pmc;
2285 struct inet_sock *inet = inet_sk(sk);
2286 struct ip_sf_socklist *psl;
2287
2288 psin = (struct sockaddr_in *)&gsf->gf_group;
2289 if (psin->sin_family != AF_INET)
2290 return -EINVAL;
2291 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002292 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return -EINVAL;
2294
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002295 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 err = -EADDRNOTAVAIL;
2298
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002299 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2301 pmc->multi.imr_ifindex == gsf->gf_interface)
2302 break;
2303 }
2304 if (!pmc) /* must have a prior join */
2305 goto done;
2306 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002307 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002308 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 count = psl ? psl->sl_count : 0;
2310 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2311 gsf->gf_numsrc = count;
2312 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2313 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2314 return -EFAULT;
2315 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002316 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 struct sockaddr_storage ss;
2318
2319 psin = (struct sockaddr_in *)&ss;
2320 memset(&ss, 0, sizeof(ss));
2321 psin->sin_family = AF_INET;
2322 psin->sin_addr.s_addr = psl->sl_addr[i];
2323 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2324 return -EFAULT;
2325 }
2326 return 0;
2327done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002328 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return err;
2330}
2331
2332/*
2333 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2334 */
Al Viroc0cda062006-09-27 18:31:10 -07002335int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 struct inet_sock *inet = inet_sk(sk);
2338 struct ip_mc_socklist *pmc;
2339 struct ip_sf_socklist *psl;
2340 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002341 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002343 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002344 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002347 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002348 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2350 pmc->multi.imr_ifindex == dif)
2351 break;
2352 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002353 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002355 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002356 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002357 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002359 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Weilong Chenc71151f2013-12-23 14:37:29 +08002361 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (psl->sl_addr[i] == rmt_addr)
2363 break;
2364 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002365 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002367 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002369 goto unlock;
2370 ret = 1;
2371unlock:
2372 rcu_read_unlock();
2373out:
2374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
2377/*
2378 * A socket is closing.
2379 */
2380
2381void ip_mc_drop_socket(struct sock *sk)
2382{
2383 struct inet_sock *inet = inet_sk(sk);
2384 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002385 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 if (inet->mc_list == NULL)
2388 return;
2389
2390 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002391 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002394 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002395 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002396 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002397 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002399 /* decrease mem now to avoid the memleak warning */
2400 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002401 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403 rtnl_unlock();
2404}
2405
David S. Millerdbdd9a52011-03-10 16:34:38 -08002406/* called with rcu_read_lock() */
2407int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002410 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 struct ip_sf_list *psf;
2412 int rv = 0;
2413
Eric Dumazete9897072013-06-07 08:48:57 -07002414 mc_hash = rcu_dereference(in_dev->mc_hash);
2415 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002416 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002417
2418 for (im = rcu_dereference(mc_hash[hash]);
2419 im != NULL;
2420 im = rcu_dereference(im->next_hash)) {
2421 if (im->multiaddr == mc_addr)
2422 break;
2423 }
2424 } else {
2425 for_each_pmc_rcu(in_dev, im) {
2426 if (im->multiaddr == mc_addr)
2427 break;
2428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430 if (im && proto == IPPROTO_IGMP) {
2431 rv = 1;
2432 } else if (im) {
2433 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002434 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (psf->sf_inaddr == src_addr)
2436 break;
2437 }
2438 if (psf)
2439 rv = psf->sf_count[MCAST_INCLUDE] ||
2440 psf->sf_count[MCAST_EXCLUDE] !=
2441 im->sfcount[MCAST_EXCLUDE];
2442 else
2443 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2444 } else
2445 rv = 1; /* unspecified source; tentatively allow */
2446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return rv;
2448}
2449
2450#if defined(CONFIG_PROC_FS)
2451struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002452 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct net_device *dev;
2454 struct in_device *in_dev;
2455};
2456
2457#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2458
2459static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2460{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002461 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 struct ip_mc_list *im = NULL;
2463 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2464
Pavel Emelianov7562f872007-05-03 15:13:45 -07002465 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002466 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002468
2469 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (!in_dev)
2471 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002472 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (im) {
2474 state->in_dev = in_dev;
2475 break;
2476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478 return im;
2479}
2480
2481static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2482{
2483 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002484
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002485 im = rcu_dereference(im->next_rcu);
2486 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002487 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (!state->dev) {
2489 state->in_dev = NULL;
2490 break;
2491 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002492 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (!state->in_dev)
2494 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002495 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
2497 return im;
2498}
2499
2500static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2501{
2502 struct ip_mc_list *im = igmp_mc_get_first(seq);
2503 if (im)
2504 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2505 --pos;
2506 return pos ? NULL : im;
2507}
2508
2509static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002510 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
stephen hemminger61fbab72009-11-10 07:54:55 +00002512 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2514}
2515
2516static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2517{
2518 struct ip_mc_list *im;
2519 if (v == SEQ_START_TOKEN)
2520 im = igmp_mc_get_first(seq);
2521 else
2522 im = igmp_mc_get_next(seq, v);
2523 ++*pos;
2524 return im;
2525}
2526
2527static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002528 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002531
2532 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002534 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
2537static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2538{
2539 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002540 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2542 else {
2543 struct ip_mc_list *im = (struct ip_mc_list *)v;
2544 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2545 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002546 long delta;
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548#ifdef CONFIG_IP_MULTICAST
2549 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2550 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2551 "V3";
2552#else
2553 querier = "NONE";
2554#endif
2555
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002556 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002558 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560
Eric Dumazeta399a802012-08-08 21:13:53 +00002561 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002563 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002565 im->tm_running,
2566 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 im->reporter);
2568 }
2569 return 0;
2570}
2571
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002572static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 .start = igmp_mc_seq_start,
2574 .next = igmp_mc_seq_next,
2575 .stop = igmp_mc_seq_stop,
2576 .show = igmp_mc_seq_show,
2577};
2578
2579static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2580{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002581 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002582 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Arjan van de Ven9a321442007-02-12 00:55:35 -08002585static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 .owner = THIS_MODULE,
2587 .open = igmp_mc_seq_open,
2588 .read = seq_read,
2589 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002590 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591};
2592
2593struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002594 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 struct net_device *dev;
2596 struct in_device *idev;
2597 struct ip_mc_list *im;
2598};
2599
2600#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2601
2602static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2603{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002604 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct ip_sf_list *psf = NULL;
2606 struct ip_mc_list *im = NULL;
2607 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2608
Pavel Emelianov7562f872007-05-03 15:13:45 -07002609 state->idev = NULL;
2610 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002611 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002613 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (unlikely(idev == NULL))
2615 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002616 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (likely(im != NULL)) {
2618 spin_lock_bh(&im->lock);
2619 psf = im->sources;
2620 if (likely(psf != NULL)) {
2621 state->im = im;
2622 state->idev = idev;
2623 break;
2624 }
2625 spin_unlock_bh(&im->lock);
2626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
2628 return psf;
2629}
2630
2631static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2632{
2633 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2634
2635 psf = psf->sf_next;
2636 while (!psf) {
2637 spin_unlock_bh(&state->im->lock);
2638 state->im = state->im->next;
2639 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002640 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (!state->dev) {
2642 state->idev = NULL;
2643 goto out;
2644 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002645 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (!state->idev)
2647 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002648 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
2650 if (!state->im)
2651 break;
2652 spin_lock_bh(&state->im->lock);
2653 psf = state->im->sources;
2654 }
2655out:
2656 return psf;
2657}
2658
2659static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2660{
2661 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2662 if (psf)
2663 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2664 --pos;
2665 return pos ? NULL : psf;
2666}
2667
2668static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002669 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
stephen hemminger61fbab72009-11-10 07:54:55 +00002671 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2673}
2674
2675static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2676{
2677 struct ip_sf_list *psf;
2678 if (v == SEQ_START_TOKEN)
2679 psf = igmp_mcf_get_first(seq);
2680 else
2681 psf = igmp_mcf_get_next(seq, v);
2682 ++*pos;
2683 return psf;
2684}
2685
2686static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002687 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2690 if (likely(state->im != NULL)) {
2691 spin_unlock_bh(&state->im->lock);
2692 state->im = NULL;
2693 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002694 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002696 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2700{
2701 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2702 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2703
2704 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002705 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 } else {
2707 seq_printf(seq,
2708 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002709 "0x%08x %6lu %6lu\n",
2710 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 ntohl(state->im->multiaddr),
2712 ntohl(psf->sf_inaddr),
2713 psf->sf_count[MCAST_INCLUDE],
2714 psf->sf_count[MCAST_EXCLUDE]);
2715 }
2716 return 0;
2717}
2718
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002719static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 .start = igmp_mcf_seq_start,
2721 .next = igmp_mcf_seq_next,
2722 .stop = igmp_mcf_seq_stop,
2723 .show = igmp_mcf_seq_show,
2724};
2725
2726static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2727{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002728 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002729 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Arjan van de Ven9a321442007-02-12 00:55:35 -08002732static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .owner = THIS_MODULE,
2734 .open = igmp_mcf_seq_open,
2735 .read = seq_read,
2736 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002737 .release = seq_release_net,
2738};
2739
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002740static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002741{
2742 struct proc_dir_entry *pde;
2743
Gao fengd4beaa62013-02-18 01:34:54 +00002744 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002745 if (!pde)
2746 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002747 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2748 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002749 if (!pde)
2750 goto out_mcfilter;
2751 return 0;
2752
2753out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002754 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002755out_igmp:
2756 return -ENOMEM;
2757}
2758
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002759static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002760{
Gao fengece31ff2013-02-18 01:34:56 +00002761 remove_proc_entry("mcfilter", net->proc_net);
2762 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002763}
2764
2765static struct pernet_operations igmp_net_ops = {
2766 .init = igmp_net_init,
2767 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768};
WANG Cong72c1d3b2014-01-10 16:09:45 -08002769#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002771static int igmp_netdev_event(struct notifier_block *this,
2772 unsigned long event, void *ptr)
2773{
2774 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2775 struct in_device *in_dev;
2776
2777 switch (event) {
2778 case NETDEV_RESEND_IGMP:
2779 in_dev = __in_dev_get_rtnl(dev);
2780 if (in_dev)
2781 ip_mc_rejoin_groups(in_dev);
2782 break;
2783 default:
2784 break;
2785 }
2786 return NOTIFY_DONE;
2787}
2788
2789static struct notifier_block igmp_notifier = {
2790 .notifier_call = igmp_netdev_event,
2791};
2792
WANG Cong72c1d3b2014-01-10 16:09:45 -08002793int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
WANG Cong72c1d3b2014-01-10 16:09:45 -08002795#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002796 int err;
2797
2798 err = register_pernet_subsys(&igmp_net_ops);
2799 if (err)
2800 return err;
2801 err = register_netdevice_notifier(&igmp_notifier);
2802 if (err)
2803 goto reg_notif_fail;
2804 return 0;
2805
2806reg_notif_fail:
2807 unregister_pernet_subsys(&igmp_net_ops);
2808 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08002809#else
2810 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08002812}