blob: ceb19ab9cf9d7d35217d2b00dd58c9f3febf8c32 [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
115#define IGMP_V1_Router_Present_Timeout (400*HZ)
116#define IGMP_V2_Router_Present_Timeout (400*HZ)
William Manleycab70042013-08-06 19:03:13 +0100117#define IGMP_V2_Unsolicited_Report_Interval (10*HZ)
118#define IGMP_V3_Unsolicited_Report_Interval (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define IGMP_Query_Response_Interval (10*HZ)
120#define IGMP_Unsolicited_Report_Count 2
121
122
123#define IGMP_Initial_Report_Delay (1)
124
125/* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * 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{
145 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
146 return IGMP_V2_Unsolicited_Report_Interval;
147 else /* v3 */
148 return IGMP_V3_Unsolicited_Report_Interval;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700152static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static void igmpv3_clear_delrec(struct in_device *in_dev);
154static int sf_setstate(struct ip_mc_list *pmc);
155static void sf_markstate(struct ip_mc_list *pmc);
156#endif
157static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700158static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
159 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161static void ip_ma_put(struct ip_mc_list *im)
162{
163 if (atomic_dec_and_test(&im->refcnt)) {
164 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800165 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167}
168
David S. Millerd9aa9382010-11-15 08:52:02 -0800169#define for_each_pmc_rcu(in_dev, pmc) \
170 for (pmc = rcu_dereference(in_dev->mc_list); \
171 pmc != NULL; \
172 pmc = rcu_dereference(pmc->next_rcu))
173
174#define for_each_pmc_rtnl(in_dev, pmc) \
175 for (pmc = rtnl_dereference(in_dev->mc_list); \
176 pmc != NULL; \
177 pmc = rtnl_dereference(pmc->next_rcu))
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#ifdef CONFIG_IP_MULTICAST
180
181/*
182 * Timer management
183 */
184
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000185static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 spin_lock_bh(&im->lock);
188 if (del_timer(&im->timer))
189 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800190 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 im->reporter = 0;
192 im->unsolicit_count = 0;
193 spin_unlock_bh(&im->lock);
194}
195
196/* It must be called with locked im->lock */
197static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
198{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800199 int tv = net_random() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800201 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!mod_timer(&im->timer, jiffies+tv+2))
203 atomic_inc(&im->refcnt);
204}
205
206static void igmp_gq_start_timer(struct in_device *in_dev)
207{
208 int tv = net_random() % in_dev->mr_maxdelay;
209
210 in_dev->mr_gq_running = 1;
211 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
212 in_dev_hold(in_dev);
213}
214
215static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
216{
217 int tv = net_random() % delay;
218
219 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
220 in_dev_hold(in_dev);
221}
222
223static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
224{
225 spin_lock_bh(&im->lock);
226 im->unsolicit_count = 0;
227 if (del_timer(&im->timer)) {
228 if ((long)(im->timer.expires-jiffies) < max_delay) {
229 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800230 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 spin_unlock_bh(&im->lock);
232 return;
233 }
234 atomic_dec(&im->refcnt);
235 }
236 igmp_start_timer(im, max_delay);
237 spin_unlock_bh(&im->lock);
238}
239
240
241/*
242 * Send an IGMP report.
243 */
244
245#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
246
247
248static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
249 int gdeleted, int sdeleted)
250{
251 switch (type) {
252 case IGMPV3_MODE_IS_INCLUDE:
253 case IGMPV3_MODE_IS_EXCLUDE:
254 if (gdeleted || sdeleted)
255 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800256 if (!(pmc->gsquery && !psf->sf_gsresp)) {
257 if (pmc->sfmode == MCAST_INCLUDE)
258 return 1;
259 /* don't include if this source is excluded
260 * in all filters
261 */
262 if (psf->sf_count[MCAST_INCLUDE])
263 return type == IGMPV3_MODE_IS_INCLUDE;
264 return pmc->sfcount[MCAST_EXCLUDE] ==
265 psf->sf_count[MCAST_EXCLUDE];
266 }
267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 case IGMPV3_CHANGE_TO_INCLUDE:
269 if (gdeleted || sdeleted)
270 return 0;
271 return psf->sf_count[MCAST_INCLUDE] != 0;
272 case IGMPV3_CHANGE_TO_EXCLUDE:
273 if (gdeleted || sdeleted)
274 return 0;
275 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
276 psf->sf_count[MCAST_INCLUDE])
277 return 0;
278 return pmc->sfcount[MCAST_EXCLUDE] ==
279 psf->sf_count[MCAST_EXCLUDE];
280 case IGMPV3_ALLOW_NEW_SOURCES:
281 if (gdeleted || !psf->sf_crcount)
282 return 0;
283 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
284 case IGMPV3_BLOCK_OLD_SOURCES:
285 if (pmc->sfmode == MCAST_INCLUDE)
286 return gdeleted || (psf->sf_crcount && sdeleted);
287 return psf->sf_crcount && !gdeleted && !sdeleted;
288 }
289 return 0;
290}
291
292static int
293igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
294{
295 struct ip_sf_list *psf;
296 int scount = 0;
297
298 for (psf=pmc->sources; psf; psf=psf->sf_next) {
299 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
300 continue;
301 scount++;
302 }
303 return scount;
304}
305
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000306#define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
309{
310 struct sk_buff *skb;
311 struct rtable *rt;
312 struct iphdr *pip;
313 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700314 struct net *net = dev_net(dev);
David S. Miller31e4543d2011-05-03 20:25:42 -0700315 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000316 int hlen = LL_RESERVED_SPACE(dev);
317 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000319 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000320 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000321 GFP_ATOMIC | __GFP_NOWARN);
322 if (skb)
323 break;
324 size >>= 1;
325 if (size < 256)
326 return NULL;
327 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200328 skb->priority = TC_PRIO_CONTROL;
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000329 igmp_skb_size(skb) = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
David S. Miller31e4543d2011-05-03 20:25:42 -0700331 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500332 0, 0,
333 IPPROTO_IGMP, 0, dev->ifindex);
334 if (IS_ERR(rt)) {
335 kfree_skb(skb);
336 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Changli Gaod8d1f302010-06-10 23:31:35 -0700339 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 skb->dev = dev;
341
Herbert Xu66088242011-11-18 02:20:04 +0000342 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300344 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700345 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300346 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 pip->version = 4;
349 pip->ihl = (sizeof(struct iphdr)+4)>>2;
350 pip->tos = 0xc0;
351 pip->frag_off = htons(IP_DF);
352 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700353 pip->daddr = fl4.daddr;
354 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 pip->protocol = IPPROTO_IGMP;
356 pip->tot_len = 0; /* filled in later */
Changli Gaod8d1f302010-06-10 23:31:35 -0700357 ip_select_ident(pip, &rt->dst, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000358 ((u8 *)&pip[1])[0] = IPOPT_RA;
359 ((u8 *)&pip[1])[1] = 4;
360 ((u8 *)&pip[1])[2] = 0;
361 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700363 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300364 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300365 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
367 pig->resv1 = 0;
368 pig->csum = 0;
369 pig->resv2 = 0;
370 pig->ngrec = 0;
371 return skb;
372}
373
374static int igmpv3_sendpack(struct sk_buff *skb)
375{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300376 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000377 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300379 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Herbert Xuc439cb22008-01-11 19:14:00 -0800381 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
385{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800386 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
390 int type, struct igmpv3_grec **ppgr)
391{
392 struct net_device *dev = pmc->interface->dev;
393 struct igmpv3_report *pih;
394 struct igmpv3_grec *pgr;
395
396 if (!skb)
397 skb = igmpv3_newpack(dev, dev->mtu);
398 if (!skb)
399 return NULL;
400 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
401 pgr->grec_type = type;
402 pgr->grec_auxwords = 0;
403 pgr->grec_nsrcs = 0;
404 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300405 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 pih->ngrec = htons(ntohs(pih->ngrec)+1);
407 *ppgr = pgr;
408 return skb;
409}
410
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000411#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 skb_tailroom(skb)) : 0)
413
414static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
415 int type, int gdeleted, int sdeleted)
416{
417 struct net_device *dev = pmc->interface->dev;
418 struct igmpv3_report *pih;
419 struct igmpv3_grec *pgr = NULL;
420 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800421 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (pmc->multiaddr == IGMP_ALL_HOSTS)
424 return skb;
425
426 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
427 type == IGMPV3_MODE_IS_EXCLUDE;
428 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
429 type == IGMPV3_CHANGE_TO_EXCLUDE;
430
David L Stevensad125832006-01-18 14:20:56 -0800431 stotal = scount = 0;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
434
David L Stevensad125832006-01-18 14:20:56 -0800435 if (!*psf_list)
436 goto empty_source;
437
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300438 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* EX and TO_EX get a fresh packet, if needed */
441 if (truncate) {
442 if (pih && pih->ngrec &&
443 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
444 if (skb)
445 igmpv3_sendpack(skb);
446 skb = igmpv3_newpack(dev, dev->mtu);
447 }
448 }
449 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 psf_prev = NULL;
451 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700452 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 psf_next = psf->sf_next;
455
456 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
457 psf_prev = psf;
458 continue;
459 }
460
461 /* clear marks on query responses */
462 if (isquery)
463 psf->sf_gsresp = 0;
464
Al Viro63007722006-09-27 18:31:32 -0700465 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 first*sizeof(struct igmpv3_grec)) {
467 if (truncate && !first)
468 break; /* truncate these */
469 if (pgr)
470 pgr->grec_nsrcs = htons(scount);
471 if (skb)
472 igmpv3_sendpack(skb);
473 skb = igmpv3_newpack(dev, dev->mtu);
474 first = 1;
475 scount = 0;
476 }
477 if (first) {
478 skb = add_grhead(skb, pmc, type, &pgr);
479 first = 0;
480 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800481 if (!skb)
482 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700483 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800485 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
487 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
488 psf->sf_crcount--;
489 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
490 if (psf_prev)
491 psf_prev->sf_next = psf->sf_next;
492 else
493 *psf_list = psf->sf_next;
494 kfree(psf);
495 continue;
496 }
497 }
498 psf_prev = psf;
499 }
David L Stevensad125832006-01-18 14:20:56 -0800500
501empty_source:
502 if (!stotal) {
503 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
504 type == IGMPV3_BLOCK_OLD_SOURCES)
505 return skb;
506 if (pmc->crcount || isquery) {
507 /* make sure we have room for group header */
508 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
509 igmpv3_sendpack(skb);
510 skb = NULL; /* add_grhead will get a new one */
511 }
512 skb = add_grhead(skb, pmc, type, &pgr);
513 }
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (pgr)
516 pgr->grec_nsrcs = htons(scount);
517
518 if (isquery)
519 pmc->gsquery = 0; /* clear query state on report */
520 return skb;
521}
522
523static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
524{
525 struct sk_buff *skb = NULL;
526 int type;
527
528 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000529 rcu_read_lock();
530 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (pmc->multiaddr == IGMP_ALL_HOSTS)
532 continue;
533 spin_lock_bh(&pmc->lock);
534 if (pmc->sfcount[MCAST_EXCLUDE])
535 type = IGMPV3_MODE_IS_EXCLUDE;
536 else
537 type = IGMPV3_MODE_IS_INCLUDE;
538 skb = add_grec(skb, pmc, type, 0, 0);
539 spin_unlock_bh(&pmc->lock);
540 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000541 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
543 spin_lock_bh(&pmc->lock);
544 if (pmc->sfcount[MCAST_EXCLUDE])
545 type = IGMPV3_MODE_IS_EXCLUDE;
546 else
547 type = IGMPV3_MODE_IS_INCLUDE;
548 skb = add_grec(skb, pmc, type, 0, 0);
549 spin_unlock_bh(&pmc->lock);
550 }
551 if (!skb)
552 return 0;
553 return igmpv3_sendpack(skb);
554}
555
556/*
557 * remove zero-count source records from a source filter list
558 */
559static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
560{
561 struct ip_sf_list *psf_prev, *psf_next, *psf;
562
563 psf_prev = NULL;
564 for (psf=*ppsf; psf; psf = psf_next) {
565 psf_next = psf->sf_next;
566 if (psf->sf_crcount == 0) {
567 if (psf_prev)
568 psf_prev->sf_next = psf->sf_next;
569 else
570 *ppsf = psf->sf_next;
571 kfree(psf);
572 } else
573 psf_prev = psf;
574 }
575}
576
577static void igmpv3_send_cr(struct in_device *in_dev)
578{
579 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
580 struct sk_buff *skb = NULL;
581 int type, dtype;
582
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000583 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 spin_lock_bh(&in_dev->mc_tomb_lock);
585
586 /* deleted MCA's */
587 pmc_prev = NULL;
588 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
589 pmc_next = pmc->next;
590 if (pmc->sfmode == MCAST_INCLUDE) {
591 type = IGMPV3_BLOCK_OLD_SOURCES;
592 dtype = IGMPV3_BLOCK_OLD_SOURCES;
593 skb = add_grec(skb, pmc, type, 1, 0);
594 skb = add_grec(skb, pmc, dtype, 1, 1);
595 }
596 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (pmc->sfmode == MCAST_EXCLUDE) {
598 type = IGMPV3_CHANGE_TO_INCLUDE;
599 skb = add_grec(skb, pmc, type, 1, 0);
600 }
David L Stevensad125832006-01-18 14:20:56 -0800601 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (pmc->crcount == 0) {
603 igmpv3_clear_zeros(&pmc->tomb);
604 igmpv3_clear_zeros(&pmc->sources);
605 }
606 }
607 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
608 if (pmc_prev)
609 pmc_prev->next = pmc_next;
610 else
611 in_dev->mc_tomb = pmc_next;
612 in_dev_put(pmc->interface);
613 kfree(pmc);
614 } else
615 pmc_prev = pmc;
616 }
617 spin_unlock_bh(&in_dev->mc_tomb_lock);
618
619 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000620 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 spin_lock_bh(&pmc->lock);
622 if (pmc->sfcount[MCAST_EXCLUDE]) {
623 type = IGMPV3_BLOCK_OLD_SOURCES;
624 dtype = IGMPV3_ALLOW_NEW_SOURCES;
625 } else {
626 type = IGMPV3_ALLOW_NEW_SOURCES;
627 dtype = IGMPV3_BLOCK_OLD_SOURCES;
628 }
629 skb = add_grec(skb, pmc, type, 0, 0);
630 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
631
632 /* filter mode changes */
633 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (pmc->sfmode == MCAST_EXCLUDE)
635 type = IGMPV3_CHANGE_TO_EXCLUDE;
636 else
637 type = IGMPV3_CHANGE_TO_INCLUDE;
638 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800639 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 spin_unlock_bh(&pmc->lock);
642 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000643 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (!skb)
646 return;
647 (void) igmpv3_sendpack(skb);
648}
649
650static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
651 int type)
652{
653 struct sk_buff *skb;
654 struct iphdr *iph;
655 struct igmphdr *ih;
656 struct rtable *rt;
657 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700658 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700659 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e4543d2011-05-03 20:25:42 -0700660 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700661 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000662 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
665 return igmpv3_send_report(in_dev, pmc);
666 else if (type == IGMP_HOST_LEAVE_MESSAGE)
667 dst = IGMP_ALL_ROUTER;
668 else
669 dst = group;
670
David S. Miller31e4543d2011-05-03 20:25:42 -0700671 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500672 0, 0,
673 IPPROTO_IGMP, 0, dev->ifindex);
674 if (IS_ERR(rt))
675 return -1;
676
Herbert Xu66088242011-11-18 02:20:04 +0000677 hlen = LL_RESERVED_SPACE(dev);
678 tlen = dev->needed_tailroom;
679 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (skb == NULL) {
681 ip_rt_put(rt);
682 return -1;
683 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200684 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Changli Gaod8d1f302010-06-10 23:31:35 -0700686 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Herbert Xu66088242011-11-18 02:20:04 +0000688 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300690 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700691 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300692 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 iph->version = 4;
695 iph->ihl = (sizeof(struct iphdr)+4)>>2;
696 iph->tos = 0xc0;
697 iph->frag_off = htons(IP_DF);
698 iph->ttl = 1;
699 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700700 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 iph->protocol = IPPROTO_IGMP;
Changli Gaod8d1f302010-06-10 23:31:35 -0700702 ip_select_ident(iph, &rt->dst, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000703 ((u8 *)&iph[1])[0] = IPOPT_RA;
704 ((u8 *)&iph[1])[1] = 4;
705 ((u8 *)&iph[1])[2] = 0;
706 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800709 ih->type = type;
710 ih->code = 0;
711 ih->csum = 0;
712 ih->group = group;
713 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Herbert Xuc439cb22008-01-11 19:14:00 -0800715 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718static void igmp_gq_timer_expire(unsigned long data)
719{
720 struct in_device *in_dev = (struct in_device *)data;
721
722 in_dev->mr_gq_running = 0;
723 igmpv3_send_report(in_dev, NULL);
724 __in_dev_put(in_dev);
725}
726
727static void igmp_ifc_timer_expire(unsigned long data)
728{
729 struct in_device *in_dev = (struct in_device *)data;
730
731 igmpv3_send_cr(in_dev);
732 if (in_dev->mr_ifc_count) {
733 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100734 igmp_ifc_start_timer(in_dev,
735 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 __in_dev_put(in_dev);
738}
739
740static void igmp_ifc_event(struct in_device *in_dev)
741{
742 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
743 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900744 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 IGMP_Unsolicited_Report_Count;
746 igmp_ifc_start_timer(in_dev, 1);
747}
748
749
750static void igmp_timer_expire(unsigned long data)
751{
752 struct ip_mc_list *im=(struct ip_mc_list *)data;
753 struct in_device *in_dev = im->interface;
754
755 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800756 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (im->unsolicit_count) {
759 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100760 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762 im->reporter = 1;
763 spin_unlock(&im->lock);
764
765 if (IGMP_V1_SEEN(in_dev))
766 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
767 else if (IGMP_V2_SEEN(in_dev))
768 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
769 else
770 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
771
772 ip_ma_put(im);
773}
774
David L Stevensad125832006-01-18 14:20:56 -0800775/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700776static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct ip_sf_list *psf;
779 int i, scount;
780
781 scount = 0;
782 for (psf=pmc->sources; psf; psf=psf->sf_next) {
783 if (scount == nsrcs)
784 break;
David L Stevensad125832006-01-18 14:20:56 -0800785 for (i=0; i<nsrcs; i++) {
786 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000787 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800788 pmc->sfcount[MCAST_EXCLUDE] !=
789 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800790 break;
David L Stevensad125832006-01-18 14:20:56 -0800791 if (srcs[i] == psf->sf_inaddr) {
792 scount++;
793 break;
794 }
795 }
796 }
797 pmc->gsquery = 0;
798 if (scount == nsrcs) /* all sources excluded */
799 return 0;
800 return 1;
801}
802
Al Viroea4d9e72006-09-27 18:30:52 -0700803static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800804{
805 struct ip_sf_list *psf;
806 int i, scount;
807
808 if (pmc->sfmode == MCAST_EXCLUDE)
809 return igmp_xmarksources(pmc, nsrcs, srcs);
810
811 /* mark INCLUDE-mode sources */
812 scount = 0;
813 for (psf=pmc->sources; psf; psf=psf->sf_next) {
814 if (scount == nsrcs)
815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 for (i=0; i<nsrcs; i++)
817 if (srcs[i] == psf->sf_inaddr) {
818 psf->sf_gsresp = 1;
819 scount++;
820 break;
821 }
822 }
David L Stevensad125832006-01-18 14:20:56 -0800823 if (!scount) {
824 pmc->gsquery = 0;
825 return 0;
826 }
827 pmc->gsquery = 1;
828 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Eric Dumazetd679c532012-09-06 20:37:06 +0000831/* return true if packet was dropped */
832static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct ip_mc_list *im;
835
836 /* Timers are only set for non-local groups */
837
838 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000839 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000841 rcu_read_lock();
842 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (im->multiaddr == group) {
844 igmp_stop_timer(im);
845 break;
846 }
847 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000848 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000849 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Eric Dumazetd679c532012-09-06 20:37:06 +0000852/* return true if packet was dropped */
853static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int len)
855{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300856 struct igmphdr *ih = igmp_hdr(skb);
857 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700859 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int max_delay;
861 int mark = 0;
862
863
David Stevens5b7c8402010-09-30 14:29:40 +0000864 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (ih->code == 0) {
866 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 max_delay = IGMP_Query_Response_Interval;
869 in_dev->mr_v1_seen = jiffies +
870 IGMP_V1_Router_Present_Timeout;
871 group = 0;
872 } else {
873 /* v2 router present */
874 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
875 in_dev->mr_v2_seen = jiffies +
876 IGMP_V2_Router_Present_Timeout;
877 }
878 /* cancel the interface change timer */
879 in_dev->mr_ifc_count = 0;
880 if (del_timer(&in_dev->mr_ifc_timer))
881 __in_dev_put(in_dev);
882 /* clear deleted report items */
883 igmpv3_clear_delrec(in_dev);
884 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000885 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000886 } else if (IGMP_V1_SEEN(in_dev)) {
887 /* This is a v3 query with v1 queriers present */
888 max_delay = IGMP_Query_Response_Interval;
889 group = 0;
890 } else if (IGMP_V2_SEEN(in_dev)) {
891 /* this is a v3 query with v2 queriers present;
892 * Interpretation of the max_delay code is problematic here.
893 * A real v2 host would use ih_code directly, while v3 has a
894 * different encoding. We use the v3 encoding as more likely
895 * to be intended in a v3 query.
896 */
897 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800898 if (!max_delay)
899 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 } else { /* v3 */
901 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000902 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900903
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300904 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900906 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700907 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000908 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300909 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
912 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
913 if (!max_delay)
914 max_delay = 1; /* can't mod w/ 0 */
915 in_dev->mr_maxdelay = max_delay;
916 if (ih3->qrv)
917 in_dev->mr_qrv = ih3->qrv;
918 if (!group) { /* general query */
919 if (ih3->nsrcs)
Eric Dumazetd679c532012-09-06 20:37:06 +0000920 return false; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000922 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 /* mark sources to include, if group & source-specific */
925 mark = ih3->nsrcs != 0;
926 }
927
928 /*
929 * - Start the timers in all of our membership records
930 * that the query applies to for the interface on
931 * which the query arrived excl. those that belong
932 * to a "local" group (224.0.0.X)
933 * - For timers already running check if they need to
934 * be reset.
935 * - Use the igmp->igmp_code field as the maximum
936 * delay possible
937 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000938 rcu_read_lock();
939 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800940 int changed;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (group && group != im->multiaddr)
943 continue;
944 if (im->multiaddr == IGMP_ALL_HOSTS)
945 continue;
946 spin_lock_bh(&im->lock);
947 if (im->tm_running)
948 im->gsquery = im->gsquery && mark;
949 else
950 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800951 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900952 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800954 if (changed)
955 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000957 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000958 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000961/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962int igmp_rcv(struct sk_buff *skb)
963{
964 /* This basically follows the spec line by line -- see RFC1112 */
965 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000966 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +0000968 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800970 if (in_dev == NULL)
971 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Herbert Xufb286bb2005-11-10 13:01:24 -0800973 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000974 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800975
976 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700977 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800978 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800979 break;
980 /* fall through */
981 case CHECKSUM_NONE:
982 skb->csum = 0;
983 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000984 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300987 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 switch (ih->type) {
989 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +0000990 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992 case IGMP_HOST_MEMBERSHIP_REPORT:
993 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -0800995 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
David Stevens24c69272005-12-02 20:32:59 -0800997 /* don't rely on MC router hearing unicast reports */
998 if (skb->pkt_type == PACKET_MULTICAST ||
999 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001000 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
1002 case IGMP_PIM:
1003#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return pim_rcv_v1(skb);
1005#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001006 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 case IGMP_DVMRP:
1008 case IGMP_TRACE:
1009 case IGMP_HOST_LEAVE_MESSAGE:
1010 case IGMP_MTRACE:
1011 case IGMP_MTRACE_RESP:
1012 break;
1013 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001014 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001016
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001017drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001018 if (dropped)
1019 kfree_skb(skb);
1020 else
1021 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023}
1024
1025#endif
1026
1027
1028/*
1029 * Add a filter to a device
1030 */
1031
Al Viro63007722006-09-27 18:31:32 -07001032static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 char buf[MAX_ADDR_LEN];
1035 struct net_device *dev = in_dev->dev;
1036
1037 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1038 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001039 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 routine. Something sort of:
1041 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1042 --ANK
1043 */
1044 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001045 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048/*
1049 * Remove a filter from a device
1050 */
1051
Al Viro63007722006-09-27 18:31:32 -07001052static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 char buf[MAX_ADDR_LEN];
1055 struct net_device *dev = in_dev->dev;
1056
1057 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001058 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061#ifdef CONFIG_IP_MULTICAST
1062/*
1063 * deleted ip_mc_list manipulation
1064 */
1065static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1066{
1067 struct ip_mc_list *pmc;
1068
1069 /* this is an "ip_mc_list" for convenience; only the fields below
1070 * are actually used. In particular, the refcnt and users are not
1071 * used for management of the delete list. Using the same structure
1072 * for deleted items allows change reports to use common code with
1073 * non-deleted or query-response MCA's.
1074 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001075 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (!pmc)
1077 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 spin_lock_bh(&im->lock);
1079 pmc->interface = im->interface;
1080 in_dev_hold(in_dev);
1081 pmc->multiaddr = im->multiaddr;
1082 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1083 IGMP_Unsolicited_Report_Count;
1084 pmc->sfmode = im->sfmode;
1085 if (pmc->sfmode == MCAST_INCLUDE) {
1086 struct ip_sf_list *psf;
1087
1088 pmc->tomb = im->tomb;
1089 pmc->sources = im->sources;
1090 im->tomb = im->sources = NULL;
1091 for (psf=pmc->sources; psf; psf=psf->sf_next)
1092 psf->sf_crcount = pmc->crcount;
1093 }
1094 spin_unlock_bh(&im->lock);
1095
1096 spin_lock_bh(&in_dev->mc_tomb_lock);
1097 pmc->next = in_dev->mc_tomb;
1098 in_dev->mc_tomb = pmc;
1099 spin_unlock_bh(&in_dev->mc_tomb_lock);
1100}
1101
Al Viro63007722006-09-27 18:31:32 -07001102static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct ip_mc_list *pmc, *pmc_prev;
1105 struct ip_sf_list *psf, *psf_next;
1106
1107 spin_lock_bh(&in_dev->mc_tomb_lock);
1108 pmc_prev = NULL;
1109 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1110 if (pmc->multiaddr == multiaddr)
1111 break;
1112 pmc_prev = pmc;
1113 }
1114 if (pmc) {
1115 if (pmc_prev)
1116 pmc_prev->next = pmc->next;
1117 else
1118 in_dev->mc_tomb = pmc->next;
1119 }
1120 spin_unlock_bh(&in_dev->mc_tomb_lock);
1121 if (pmc) {
1122 for (psf=pmc->tomb; psf; psf=psf_next) {
1123 psf_next = psf->sf_next;
1124 kfree(psf);
1125 }
1126 in_dev_put(pmc->interface);
1127 kfree(pmc);
1128 }
1129}
1130
1131static void igmpv3_clear_delrec(struct in_device *in_dev)
1132{
1133 struct ip_mc_list *pmc, *nextpmc;
1134
1135 spin_lock_bh(&in_dev->mc_tomb_lock);
1136 pmc = in_dev->mc_tomb;
1137 in_dev->mc_tomb = NULL;
1138 spin_unlock_bh(&in_dev->mc_tomb_lock);
1139
1140 for (; pmc; pmc = nextpmc) {
1141 nextpmc = pmc->next;
1142 ip_mc_clear_src(pmc);
1143 in_dev_put(pmc->interface);
1144 kfree(pmc);
1145 }
1146 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001147 rcu_read_lock();
1148 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 struct ip_sf_list *psf, *psf_next;
1150
1151 spin_lock_bh(&pmc->lock);
1152 psf = pmc->tomb;
1153 pmc->tomb = NULL;
1154 spin_unlock_bh(&pmc->lock);
1155 for (; psf; psf=psf_next) {
1156 psf_next = psf->sf_next;
1157 kfree(psf);
1158 }
1159 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001160 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162#endif
1163
1164static void igmp_group_dropped(struct ip_mc_list *im)
1165{
1166 struct in_device *in_dev = im->interface;
1167#ifdef CONFIG_IP_MULTICAST
1168 int reporter;
1169#endif
1170
1171 if (im->loaded) {
1172 im->loaded = 0;
1173 ip_mc_filter_del(in_dev, im->multiaddr);
1174 }
1175
1176#ifdef CONFIG_IP_MULTICAST
1177 if (im->multiaddr == IGMP_ALL_HOSTS)
1178 return;
1179
1180 reporter = im->reporter;
1181 igmp_stop_timer(im);
1182
1183 if (!in_dev->dead) {
1184 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001185 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (IGMP_V2_SEEN(in_dev)) {
1187 if (reporter)
1188 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191 /* IGMPv3 */
1192 igmpv3_add_delrec(in_dev, im);
1193
1194 igmp_ifc_event(in_dev);
1195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static void igmp_group_added(struct ip_mc_list *im)
1200{
1201 struct in_device *in_dev = im->interface;
1202
1203 if (im->loaded == 0) {
1204 im->loaded = 1;
1205 ip_mc_filter_add(in_dev, im->multiaddr);
1206 }
1207
1208#ifdef CONFIG_IP_MULTICAST
1209 if (im->multiaddr == IGMP_ALL_HOSTS)
1210 return;
1211
1212 if (in_dev->dead)
1213 return;
1214 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1215 spin_lock_bh(&im->lock);
1216 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1217 spin_unlock_bh(&im->lock);
1218 return;
1219 }
1220 /* else, v3 */
1221
1222 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1223 IGMP_Unsolicited_Report_Count;
1224 igmp_ifc_event(in_dev);
1225#endif
1226}
1227
1228
1229/*
1230 * Multicast list managers
1231 */
1232
Eric Dumazete9897072013-06-07 08:48:57 -07001233static u32 ip_mc_hash(const struct ip_mc_list *im)
1234{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001235 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001236}
1237
1238static void ip_mc_hash_add(struct in_device *in_dev,
1239 struct ip_mc_list *im)
1240{
1241 struct ip_mc_list __rcu **mc_hash;
1242 u32 hash;
1243
1244 mc_hash = rtnl_dereference(in_dev->mc_hash);
1245 if (mc_hash) {
1246 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001247 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001248 rcu_assign_pointer(mc_hash[hash], im);
1249 return;
1250 }
1251
1252 /* do not use a hash table for small number of items */
1253 if (in_dev->mc_count < 4)
1254 return;
1255
1256 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1257 GFP_KERNEL);
1258 if (!mc_hash)
1259 return;
1260
1261 for_each_pmc_rtnl(in_dev, im) {
1262 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001263 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001264 RCU_INIT_POINTER(mc_hash[hash], im);
1265 }
1266
1267 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1268}
1269
1270static void ip_mc_hash_remove(struct in_device *in_dev,
1271 struct ip_mc_list *im)
1272{
1273 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1274 struct ip_mc_list *aux;
1275
1276 if (!mc_hash)
1277 return;
1278 mc_hash += ip_mc_hash(im);
1279 while ((aux = rtnl_dereference(*mc_hash)) != im)
1280 mc_hash = &aux->next_hash;
1281 *mc_hash = im->next_hash;
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285/*
1286 * A socket has joined a multicast group on device dev.
1287 */
1288
Al Viro8f935bb2006-09-27 18:30:07 -07001289void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct ip_mc_list *im;
1292
1293 ASSERT_RTNL();
1294
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001295 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (im->multiaddr == addr) {
1297 im->users++;
1298 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1299 goto out;
1300 }
1301 }
1302
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001303 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!im)
1305 goto out;
1306
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001307 im->users = 1;
1308 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001310 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* initial mode is (EX, empty) */
1312 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 atomic_set(&im->refcnt, 1);
1315 spin_lock_init(&im->lock);
1316#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001317 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001320
1321 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001322 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001323 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001324
Eric Dumazete9897072013-06-07 08:48:57 -07001325 ip_mc_hash_add(in_dev, im);
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327#ifdef CONFIG_IP_MULTICAST
1328 igmpv3_del_delrec(in_dev, im->multiaddr);
1329#endif
1330 igmp_group_added(im);
1331 if (!in_dev->dead)
1332 ip_rt_multicast_event(in_dev);
1333out:
1334 return;
1335}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001336EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001339 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001340 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001341static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001342{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001343#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001344 struct ip_mc_list *im;
1345 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001346
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001347 ASSERT_RTNL();
1348
1349 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001350 if (im->multiaddr == IGMP_ALL_HOSTS)
1351 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001352
Eric Dumazet866f3b22010-11-18 09:33:19 -08001353 /* a failover is happening and switches
1354 * must be notified immediately
1355 */
1356 if (IGMP_V1_SEEN(in_dev))
1357 type = IGMP_HOST_MEMBERSHIP_REPORT;
1358 else if (IGMP_V2_SEEN(in_dev))
1359 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1360 else
1361 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1362 igmp_send_report(in_dev, im, type);
1363 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001364#endif
1365}
1366
1367/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 * A socket has left a multicast group on device dev
1369 */
1370
Al Viro8f935bb2006-09-27 18:30:07 -07001371void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001373 struct ip_mc_list *i;
1374 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001377
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001378 for (ip = &in_dev->mc_list;
1379 (i = rtnl_dereference(*ip)) != NULL;
1380 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001381 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001383 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001384 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001385 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001387 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 if (!in_dev->dead)
1390 ip_rt_multicast_event(in_dev);
1391
1392 ip_ma_put(i);
1393 return;
1394 }
1395 break;
1396 }
1397 }
1398}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001399EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Moni Shoua75c78502009-09-15 02:37:40 -07001401/* Device changing type */
1402
1403void ip_mc_unmap(struct in_device *in_dev)
1404{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001405 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001406
1407 ASSERT_RTNL();
1408
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001409 for_each_pmc_rtnl(in_dev, pmc)
1410 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001411}
1412
1413void ip_mc_remap(struct in_device *in_dev)
1414{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001415 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001416
1417 ASSERT_RTNL();
1418
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001419 for_each_pmc_rtnl(in_dev, pmc)
1420 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423/* Device going down */
1424
1425void ip_mc_down(struct in_device *in_dev)
1426{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001427 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 ASSERT_RTNL();
1430
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001431 for_each_pmc_rtnl(in_dev, pmc)
1432 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434#ifdef CONFIG_IP_MULTICAST
1435 in_dev->mr_ifc_count = 0;
1436 if (del_timer(&in_dev->mr_ifc_timer))
1437 __in_dev_put(in_dev);
1438 in_dev->mr_gq_running = 0;
1439 if (del_timer(&in_dev->mr_gq_timer))
1440 __in_dev_put(in_dev);
1441 igmpv3_clear_delrec(in_dev);
1442#endif
1443
1444 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1445}
1446
1447void ip_mc_init_dev(struct in_device *in_dev)
1448{
1449 ASSERT_RTNL();
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001452 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1453 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001454 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1455 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1457#endif
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 spin_lock_init(&in_dev->mc_tomb_lock);
1460}
1461
1462/* Device going up */
1463
1464void ip_mc_up(struct in_device *in_dev)
1465{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001466 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 ASSERT_RTNL();
1469
1470 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1471
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001472 for_each_pmc_rtnl(in_dev, pmc)
1473 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476/*
1477 * Device is about to be destroyed: clean up.
1478 */
1479
1480void ip_mc_destroy_dev(struct in_device *in_dev)
1481{
1482 struct ip_mc_list *i;
1483
1484 ASSERT_RTNL();
1485
1486 /* Deactivate timers */
1487 ip_mc_down(in_dev);
1488
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001489 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1490 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001491 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001492
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001493 /* We've dropped the groups in ip_mc_down already */
1494 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001499/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001500static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 struct net_device *dev = NULL;
1503 struct in_device *idev = NULL;
1504
1505 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001506 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return idev;
1508 }
1509 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001510 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (!dev)
1512 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514
David S. Millerb23dd4f2011-03-02 14:31:35 -08001515 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001516 struct rtable *rt = ip_route_output(net,
1517 imr->imr_multiaddr.s_addr,
1518 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001519 if (!IS_ERR(rt)) {
1520 dev = rt->dst.dev;
1521 ip_rt_put(rt);
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 if (dev) {
1525 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001526 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 return idev;
1529}
1530
1531/*
1532 * Join a socket to a group
1533 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001534int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1535int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537
1538static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001539 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct ip_sf_list *psf, *psf_prev;
1542 int rv = 0;
1543
1544 psf_prev = NULL;
1545 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1546 if (psf->sf_inaddr == *psfsrc)
1547 break;
1548 psf_prev = psf;
1549 }
1550 if (!psf || psf->sf_count[sfmode] == 0) {
1551 /* source filter not found, or count wrong => bug */
1552 return -ESRCH;
1553 }
1554 psf->sf_count[sfmode]--;
1555 if (psf->sf_count[sfmode] == 0) {
1556 ip_rt_multicast_event(pmc->interface);
1557 }
1558 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1559#ifdef CONFIG_IP_MULTICAST
1560 struct in_device *in_dev = pmc->interface;
1561#endif
1562
1563 /* no more filters for this source */
1564 if (psf_prev)
1565 psf_prev->sf_next = psf->sf_next;
1566 else
1567 pmc->sources = psf->sf_next;
1568#ifdef CONFIG_IP_MULTICAST
1569 if (psf->sf_oldin &&
1570 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001571 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 IGMP_Unsolicited_Report_Count;
1573 psf->sf_next = pmc->tomb;
1574 pmc->tomb = psf;
1575 rv = 1;
1576 } else
1577#endif
1578 kfree(psf);
1579 }
1580 return rv;
1581}
1582
1583#ifndef CONFIG_IP_MULTICAST
1584#define igmp_ifc_event(x) do { } while (0)
1585#endif
1586
Al Viro8f935bb2006-09-27 18:30:07 -07001587static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1588 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 struct ip_mc_list *pmc;
1591 int changerec = 0;
1592 int i, err;
1593
1594 if (!in_dev)
1595 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001596 rcu_read_lock();
1597 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (*pmca == pmc->multiaddr)
1599 break;
1600 }
1601 if (!pmc) {
1602 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001603 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return -ESRCH;
1605 }
1606 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001607 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608#ifdef CONFIG_IP_MULTICAST
1609 sf_markstate(pmc);
1610#endif
1611 if (!delta) {
1612 err = -EINVAL;
1613 if (!pmc->sfcount[sfmode])
1614 goto out_unlock;
1615 pmc->sfcount[sfmode]--;
1616 }
1617 err = 0;
1618 for (i=0; i<sfcount; i++) {
1619 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1620
1621 changerec |= rv > 0;
1622 if (!err && rv < 0)
1623 err = rv;
1624 }
1625 if (pmc->sfmode == MCAST_EXCLUDE &&
1626 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1627 pmc->sfcount[MCAST_INCLUDE]) {
1628#ifdef CONFIG_IP_MULTICAST
1629 struct ip_sf_list *psf;
1630#endif
1631
1632 /* filter mode change */
1633 pmc->sfmode = MCAST_INCLUDE;
1634#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001635 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 IGMP_Unsolicited_Report_Count;
1637 in_dev->mr_ifc_count = pmc->crcount;
1638 for (psf=pmc->sources; psf; psf = psf->sf_next)
1639 psf->sf_crcount = 0;
1640 igmp_ifc_event(pmc->interface);
1641 } else if (sf_setstate(pmc) || changerec) {
1642 igmp_ifc_event(pmc->interface);
1643#endif
1644 }
1645out_unlock:
1646 spin_unlock_bh(&pmc->lock);
1647 return err;
1648}
1649
1650/*
1651 * Add multicast single-source filter to the interface list
1652 */
1653static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001654 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 struct ip_sf_list *psf, *psf_prev;
1657
1658 psf_prev = NULL;
1659 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1660 if (psf->sf_inaddr == *psfsrc)
1661 break;
1662 psf_prev = psf;
1663 }
1664 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001665 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (!psf)
1667 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 psf->sf_inaddr = *psfsrc;
1669 if (psf_prev) {
1670 psf_prev->sf_next = psf;
1671 } else
1672 pmc->sources = psf;
1673 }
1674 psf->sf_count[sfmode]++;
1675 if (psf->sf_count[sfmode] == 1) {
1676 ip_rt_multicast_event(pmc->interface);
1677 }
1678 return 0;
1679}
1680
1681#ifdef CONFIG_IP_MULTICAST
1682static void sf_markstate(struct ip_mc_list *pmc)
1683{
1684 struct ip_sf_list *psf;
1685 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1686
1687 for (psf=pmc->sources; psf; psf=psf->sf_next)
1688 if (pmc->sfcount[MCAST_EXCLUDE]) {
1689 psf->sf_oldin = mca_xcount ==
1690 psf->sf_count[MCAST_EXCLUDE] &&
1691 !psf->sf_count[MCAST_INCLUDE];
1692 } else
1693 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1694}
1695
1696static int sf_setstate(struct ip_mc_list *pmc)
1697{
David L Stevensad125832006-01-18 14:20:56 -08001698 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1700 int qrv = pmc->interface->mr_qrv;
1701 int new_in, rv;
1702
1703 rv = 0;
1704 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1705 if (pmc->sfcount[MCAST_EXCLUDE]) {
1706 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1707 !psf->sf_count[MCAST_INCLUDE];
1708 } else
1709 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001710 if (new_in) {
1711 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001712 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001713
1714 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1715 if (dpsf->sf_inaddr == psf->sf_inaddr)
1716 break;
1717 prev = dpsf;
1718 }
1719 if (dpsf) {
1720 if (prev)
1721 prev->sf_next = dpsf->sf_next;
1722 else
1723 pmc->tomb = dpsf->sf_next;
1724 kfree(dpsf);
1725 }
1726 psf->sf_crcount = qrv;
1727 rv++;
1728 }
1729 } else if (psf->sf_oldin) {
1730
1731 psf->sf_crcount = 0;
1732 /*
1733 * add or update "delete" records if an active filter
1734 * is now inactive
1735 */
1736 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1737 if (dpsf->sf_inaddr == psf->sf_inaddr)
1738 break;
1739 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001740 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001741 if (!dpsf)
1742 continue;
1743 *dpsf = *psf;
1744 /* pmc->lock held by callers */
1745 dpsf->sf_next = pmc->tomb;
1746 pmc->tomb = dpsf;
1747 }
1748 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 rv++;
1750 }
1751 }
1752 return rv;
1753}
1754#endif
1755
1756/*
1757 * Add multicast source filter list to the interface list
1758 */
Al Viro8f935bb2006-09-27 18:30:07 -07001759static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1760 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 struct ip_mc_list *pmc;
1763 int isexclude;
1764 int i, err;
1765
1766 if (!in_dev)
1767 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001768 rcu_read_lock();
1769 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (*pmca == pmc->multiaddr)
1771 break;
1772 }
1773 if (!pmc) {
1774 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001775 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return -ESRCH;
1777 }
1778 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001779 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781#ifdef CONFIG_IP_MULTICAST
1782 sf_markstate(pmc);
1783#endif
1784 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1785 if (!delta)
1786 pmc->sfcount[sfmode]++;
1787 err = 0;
1788 for (i=0; i<sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00001789 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (err)
1791 break;
1792 }
1793 if (err) {
1794 int j;
1795
Jun Zhao685f94e62011-11-22 17:19:03 +00001796 if (!delta)
1797 pmc->sfcount[sfmode]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 for (j=0; j<i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00001799 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1801#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001803 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804#endif
1805
1806 /* filter mode change */
1807 if (pmc->sfcount[MCAST_EXCLUDE])
1808 pmc->sfmode = MCAST_EXCLUDE;
1809 else if (pmc->sfcount[MCAST_INCLUDE])
1810 pmc->sfmode = MCAST_INCLUDE;
1811#ifdef CONFIG_IP_MULTICAST
1812 /* else no filters; keep old mode for reports */
1813
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001814 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 IGMP_Unsolicited_Report_Count;
1816 in_dev->mr_ifc_count = pmc->crcount;
1817 for (psf=pmc->sources; psf; psf = psf->sf_next)
1818 psf->sf_crcount = 0;
1819 igmp_ifc_event(in_dev);
1820 } else if (sf_setstate(pmc)) {
1821 igmp_ifc_event(in_dev);
1822#endif
1823 }
1824 spin_unlock_bh(&pmc->lock);
1825 return err;
1826}
1827
1828static void ip_mc_clear_src(struct ip_mc_list *pmc)
1829{
1830 struct ip_sf_list *psf, *nextpsf;
1831
1832 for (psf=pmc->tomb; psf; psf=nextpsf) {
1833 nextpsf = psf->sf_next;
1834 kfree(psf);
1835 }
1836 pmc->tomb = NULL;
1837 for (psf=pmc->sources; psf; psf=nextpsf) {
1838 nextpsf = psf->sf_next;
1839 kfree(psf);
1840 }
1841 pmc->sources = NULL;
1842 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001843 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 pmc->sfcount[MCAST_EXCLUDE] = 1;
1845}
1846
1847
1848/*
1849 * Join a multicast group
1850 */
1851int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1852{
1853 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001854 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001855 struct ip_mc_socklist *iml = NULL, *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;
1861
Joe Perchesf97c1e02007-12-16 13:45:43 -08001862 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return -EINVAL;
1864
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001865 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Daniel Lezcano877aced2008-08-13 16:15:57 -07001867 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 if (!in_dev) {
1870 iml = NULL;
1871 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:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001898 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return err;
1900}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001901EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1904 struct in_device *in_dev)
1905{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001906 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 int err;
1908
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001909 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /* any-source empty exclude case */
1911 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1912 iml->sfmode, 0, NULL, 0);
1913 }
1914 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001915 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001916 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001917 /* decrease mem now to avoid the memleak warning */
1918 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08001919 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return err;
1921}
1922
1923/*
1924 * Ask a socket to leave a group.
1925 */
1926
1927int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1928{
1929 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001930 struct ip_mc_socklist *iml;
1931 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001932 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001933 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001934 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001935 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001936 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001939 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001940 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001941 for (imlp = &inet->mc_list;
1942 (iml = rtnl_dereference(*imlp)) != NULL;
1943 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001944 if (iml->multi.imr_multiaddr.s_addr != group)
1945 continue;
1946 if (ifindex) {
1947 if (iml->multi.imr_ifindex != ifindex)
1948 continue;
1949 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1950 iml->multi.imr_address.s_addr)
1951 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
David L Stevensacd6e002006-08-17 16:27:39 -07001953 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001955 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001956
1957 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001958 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001959 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001960 /* decrease mem now to avoid the memleak warning */
1961 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08001962 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07001963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
David L Stevensacd6e002006-08-17 16:27:39 -07001965 if (!in_dev)
1966 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001968 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
stephen hemminger193ba922012-10-01 12:32:34 +00001970EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972int ip_mc_source(int add, int omode, struct sock *sk, struct
1973 ip_mreq_source *mreqs, int ifindex)
1974{
1975 int err;
1976 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001977 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct ip_mc_socklist *pmc;
1979 struct in_device *in_dev = NULL;
1980 struct inet_sock *inet = inet_sk(sk);
1981 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001982 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001983 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 int i, j, rv;
1985
Joe Perchesf97c1e02007-12-16 13:45:43 -08001986 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return -EINVAL;
1988
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001989 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1992 imr.imr_address.s_addr = mreqs->imr_interface;
1993 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001994 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 if (!in_dev) {
1997 err = -ENODEV;
1998 goto done;
1999 }
2000 err = -EADDRNOTAVAIL;
2001
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002002 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002003 if ((pmc->multi.imr_multiaddr.s_addr ==
2004 imr.imr_multiaddr.s_addr) &&
2005 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 break;
2007 }
David L Stevens917f2f12005-07-08 17:45:16 -07002008 if (!pmc) { /* must have a prior join */
2009 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* if a source filter was set, must be the same mode as before */
2013 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002014 if (pmc->sfmode != omode) {
2015 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 } else if (pmc->sfmode != omode) {
2019 /* allow mode switches for empty-set filters */
2020 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002021 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 NULL, 0);
2023 pmc->sfmode = omode;
2024 }
2025
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002026 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (!add) {
2028 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002029 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 rv = !0;
2031 for (i=0; i<psl->sl_count; i++) {
2032 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002033 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (rv == 0)
2035 break;
2036 }
2037 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002038 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
David L Stevens8cdaaa12005-07-08 17:39:23 -07002040 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2041 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2042 leavegroup = 1;
2043 goto done;
2044 }
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002047 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 &mreqs->imr_sourceaddr, 1);
2049
2050 for (j=i+1; j<psl->sl_count; j++)
2051 psl->sl_addr[j-1] = psl->sl_addr[j];
2052 psl->sl_count--;
2053 err = 0;
2054 goto done;
2055 }
2056 /* else, add a new source to the filter */
2057
2058 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2059 err = -ENOBUFS;
2060 goto done;
2061 }
2062 if (!psl || psl->sl_count == psl->sl_max) {
2063 struct ip_sf_socklist *newpsl;
2064 int count = IP_SFBLOCK;
2065
2066 if (psl)
2067 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002068 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (!newpsl) {
2070 err = -ENOBUFS;
2071 goto done;
2072 }
2073 newpsl->sl_max = count;
2074 newpsl->sl_count = count - IP_SFBLOCK;
2075 if (psl) {
2076 for (i=0; i<psl->sl_count; i++)
2077 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002078 /* decrease mem now to avoid the memleak warning */
2079 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002080 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002082 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002083 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2086 for (i=0; i<psl->sl_count; i++) {
2087 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002088 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (rv == 0)
2090 break;
2091 }
2092 if (rv == 0) /* address already there is an error */
2093 goto done;
2094 for (j=psl->sl_count-1; j>=i; j--)
2095 psl->sl_addr[j+1] = psl->sl_addr[j];
2096 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2097 psl->sl_count++;
2098 err = 0;
2099 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002100 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 &mreqs->imr_sourceaddr, 1);
2102done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002103 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002104 if (leavegroup)
2105 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return err;
2107}
2108
2109int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2110{
David L Stevens9951f032005-07-08 17:47:28 -07002111 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002113 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct ip_mc_socklist *pmc;
2115 struct in_device *in_dev;
2116 struct inet_sock *inet = inet_sk(sk);
2117 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002118 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002119 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Joe Perchesf97c1e02007-12-16 13:45:43 -08002121 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return -EINVAL;
2123 if (msf->imsf_fmode != MCAST_INCLUDE &&
2124 msf->imsf_fmode != MCAST_EXCLUDE)
2125 return -EINVAL;
2126
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002127 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2130 imr.imr_address.s_addr = msf->imsf_interface;
2131 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002132 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 if (!in_dev) {
2135 err = -ENODEV;
2136 goto done;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
David L Stevens9951f032005-07-08 17:47:28 -07002139 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2140 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2141 leavegroup = 1;
2142 goto done;
2143 }
2144
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002145 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2147 pmc->multi.imr_ifindex == imr.imr_ifindex)
2148 break;
2149 }
David L Stevens917f2f12005-07-08 17:45:16 -07002150 if (!pmc) { /* must have a prior join */
2151 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002155 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2156 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (!newpsl) {
2158 err = -ENOBUFS;
2159 goto done;
2160 }
2161 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2162 memcpy(newpsl->sl_addr, msf->imsf_slist,
2163 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2164 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2165 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2166 if (err) {
2167 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2168 goto done;
2169 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002170 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002172 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2173 msf->imsf_fmode, 0, NULL, 0);
2174 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002175 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (psl) {
2177 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2178 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002179 /* decrease mem now to avoid the memleak warning */
2180 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002181 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 } else
2183 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2184 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002185 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002187 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002189 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002190 if (leavegroup)
2191 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return err;
2193}
2194
2195int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2196 struct ip_msfilter __user *optval, int __user *optlen)
2197{
2198 int err, len, count, copycount;
2199 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002200 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 struct ip_mc_socklist *pmc;
2202 struct in_device *in_dev;
2203 struct inet_sock *inet = inet_sk(sk);
2204 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002205 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Joe Perchesf97c1e02007-12-16 13:45:43 -08002207 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return -EINVAL;
2209
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002210 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2213 imr.imr_address.s_addr = msf->imsf_interface;
2214 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002215 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 if (!in_dev) {
2218 err = -ENODEV;
2219 goto done;
2220 }
2221 err = -EADDRNOTAVAIL;
2222
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002223 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2225 pmc->multi.imr_ifindex == imr.imr_ifindex)
2226 break;
2227 }
2228 if (!pmc) /* must have a prior join */
2229 goto done;
2230 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002231 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002232 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (!psl) {
2234 len = 0;
2235 count = 0;
2236 } else {
2237 count = psl->sl_count;
2238 }
2239 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2240 len = copycount * sizeof(psl->sl_addr[0]);
2241 msf->imsf_numsrc = count;
2242 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2243 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2244 return -EFAULT;
2245 }
2246 if (len &&
2247 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2248 return -EFAULT;
2249 return 0;
2250done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002251 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return err;
2253}
2254
2255int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2256 struct group_filter __user *optval, int __user *optlen)
2257{
2258 int err, i, count, copycount;
2259 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002260 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 struct ip_mc_socklist *pmc;
2262 struct inet_sock *inet = inet_sk(sk);
2263 struct ip_sf_socklist *psl;
2264
2265 psin = (struct sockaddr_in *)&gsf->gf_group;
2266 if (psin->sin_family != AF_INET)
2267 return -EINVAL;
2268 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002269 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return -EINVAL;
2271
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002272 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 err = -EADDRNOTAVAIL;
2275
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002276 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2278 pmc->multi.imr_ifindex == gsf->gf_interface)
2279 break;
2280 }
2281 if (!pmc) /* must have a prior join */
2282 goto done;
2283 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002284 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002285 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 count = psl ? psl->sl_count : 0;
2287 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2288 gsf->gf_numsrc = count;
2289 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2290 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2291 return -EFAULT;
2292 }
2293 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 struct sockaddr_storage ss;
2295
2296 psin = (struct sockaddr_in *)&ss;
2297 memset(&ss, 0, sizeof(ss));
2298 psin->sin_family = AF_INET;
2299 psin->sin_addr.s_addr = psl->sl_addr[i];
2300 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2301 return -EFAULT;
2302 }
2303 return 0;
2304done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002305 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return err;
2307}
2308
2309/*
2310 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2311 */
Al Viroc0cda062006-09-27 18:31:10 -07002312int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 struct inet_sock *inet = inet_sk(sk);
2315 struct ip_mc_socklist *pmc;
2316 struct ip_sf_socklist *psl;
2317 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002318 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002320 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002321 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002324 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002325 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2327 pmc->multi.imr_ifindex == dif)
2328 break;
2329 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002330 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002332 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002333 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002334 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002336 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 for (i=0; i<psl->sl_count; i++) {
2339 if (psl->sl_addr[i] == rmt_addr)
2340 break;
2341 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002342 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002344 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002346 goto unlock;
2347 ret = 1;
2348unlock:
2349 rcu_read_unlock();
2350out:
2351 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
2354/*
2355 * A socket is closing.
2356 */
2357
2358void ip_mc_drop_socket(struct sock *sk)
2359{
2360 struct inet_sock *inet = inet_sk(sk);
2361 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002362 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 if (inet->mc_list == NULL)
2365 return;
2366
2367 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002368 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002371 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002372 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002373 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002374 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002376 /* decrease mem now to avoid the memleak warning */
2377 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002378 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 rtnl_unlock();
2381}
2382
David S. Millerdbdd9a52011-03-10 16:34:38 -08002383/* called with rcu_read_lock() */
2384int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002387 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 struct ip_sf_list *psf;
2389 int rv = 0;
2390
Eric Dumazete9897072013-06-07 08:48:57 -07002391 mc_hash = rcu_dereference(in_dev->mc_hash);
2392 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002393 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002394
2395 for (im = rcu_dereference(mc_hash[hash]);
2396 im != NULL;
2397 im = rcu_dereference(im->next_hash)) {
2398 if (im->multiaddr == mc_addr)
2399 break;
2400 }
2401 } else {
2402 for_each_pmc_rcu(in_dev, im) {
2403 if (im->multiaddr == mc_addr)
2404 break;
2405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407 if (im && proto == IPPROTO_IGMP) {
2408 rv = 1;
2409 } else if (im) {
2410 if (src_addr) {
2411 for (psf=im->sources; psf; psf=psf->sf_next) {
2412 if (psf->sf_inaddr == src_addr)
2413 break;
2414 }
2415 if (psf)
2416 rv = psf->sf_count[MCAST_INCLUDE] ||
2417 psf->sf_count[MCAST_EXCLUDE] !=
2418 im->sfcount[MCAST_EXCLUDE];
2419 else
2420 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2421 } else
2422 rv = 1; /* unspecified source; tentatively allow */
2423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return rv;
2425}
2426
2427#if defined(CONFIG_PROC_FS)
2428struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002429 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 struct net_device *dev;
2431 struct in_device *in_dev;
2432};
2433
2434#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2435
2436static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2437{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002438 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct ip_mc_list *im = NULL;
2440 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2441
Pavel Emelianov7562f872007-05-03 15:13:45 -07002442 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002443 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002445
2446 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (!in_dev)
2448 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002449 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (im) {
2451 state->in_dev = in_dev;
2452 break;
2453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455 return im;
2456}
2457
2458static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2459{
2460 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002461
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002462 im = rcu_dereference(im->next_rcu);
2463 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002464 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (!state->dev) {
2466 state->in_dev = NULL;
2467 break;
2468 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002469 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (!state->in_dev)
2471 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002472 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
2474 return im;
2475}
2476
2477static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2478{
2479 struct ip_mc_list *im = igmp_mc_get_first(seq);
2480 if (im)
2481 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2482 --pos;
2483 return pos ? NULL : im;
2484}
2485
2486static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002487 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
stephen hemminger61fbab72009-11-10 07:54:55 +00002489 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2491}
2492
2493static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2494{
2495 struct ip_mc_list *im;
2496 if (v == SEQ_START_TOKEN)
2497 im = igmp_mc_get_first(seq);
2498 else
2499 im = igmp_mc_get_next(seq, v);
2500 ++*pos;
2501 return im;
2502}
2503
2504static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002505 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002508
2509 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002511 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2515{
2516 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002517 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2519 else {
2520 struct ip_mc_list *im = (struct ip_mc_list *)v;
2521 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2522 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002523 long delta;
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525#ifdef CONFIG_IP_MULTICAST
2526 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2527 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2528 "V3";
2529#else
2530 querier = "NONE";
2531#endif
2532
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002533 if (rcu_dereference(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002535 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
2537
Eric Dumazeta399a802012-08-08 21:13:53 +00002538 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002540 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002542 im->tm_running,
2543 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 im->reporter);
2545 }
2546 return 0;
2547}
2548
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002549static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 .start = igmp_mc_seq_start,
2551 .next = igmp_mc_seq_next,
2552 .stop = igmp_mc_seq_stop,
2553 .show = igmp_mc_seq_show,
2554};
2555
2556static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2557{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002558 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002559 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Arjan van de Ven9a321442007-02-12 00:55:35 -08002562static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 .owner = THIS_MODULE,
2564 .open = igmp_mc_seq_open,
2565 .read = seq_read,
2566 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002567 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568};
2569
2570struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002571 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct net_device *dev;
2573 struct in_device *idev;
2574 struct ip_mc_list *im;
2575};
2576
2577#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2578
2579static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2580{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002581 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 struct ip_sf_list *psf = NULL;
2583 struct ip_mc_list *im = NULL;
2584 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2585
Pavel Emelianov7562f872007-05-03 15:13:45 -07002586 state->idev = NULL;
2587 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002588 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002590 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (unlikely(idev == NULL))
2592 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002593 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (likely(im != NULL)) {
2595 spin_lock_bh(&im->lock);
2596 psf = im->sources;
2597 if (likely(psf != NULL)) {
2598 state->im = im;
2599 state->idev = idev;
2600 break;
2601 }
2602 spin_unlock_bh(&im->lock);
2603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605 return psf;
2606}
2607
2608static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2609{
2610 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2611
2612 psf = psf->sf_next;
2613 while (!psf) {
2614 spin_unlock_bh(&state->im->lock);
2615 state->im = state->im->next;
2616 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002617 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 if (!state->dev) {
2619 state->idev = NULL;
2620 goto out;
2621 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002622 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (!state->idev)
2624 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002625 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627 if (!state->im)
2628 break;
2629 spin_lock_bh(&state->im->lock);
2630 psf = state->im->sources;
2631 }
2632out:
2633 return psf;
2634}
2635
2636static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2637{
2638 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2639 if (psf)
2640 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2641 --pos;
2642 return pos ? NULL : psf;
2643}
2644
2645static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002646 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
stephen hemminger61fbab72009-11-10 07:54:55 +00002648 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2650}
2651
2652static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2653{
2654 struct ip_sf_list *psf;
2655 if (v == SEQ_START_TOKEN)
2656 psf = igmp_mcf_get_first(seq);
2657 else
2658 psf = igmp_mcf_get_next(seq, v);
2659 ++*pos;
2660 return psf;
2661}
2662
2663static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002664 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2667 if (likely(state->im != NULL)) {
2668 spin_unlock_bh(&state->im->lock);
2669 state->im = NULL;
2670 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002671 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002673 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
2676static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2677{
2678 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2679 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2680
2681 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002682 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 "%3s %6s "
2684 "%10s %10s %6s %6s\n", "Idx",
2685 "Device", "MCA",
2686 "SRC", "INC", "EXC");
2687 } else {
2688 seq_printf(seq,
2689 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002690 "0x%08x %6lu %6lu\n",
2691 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 ntohl(state->im->multiaddr),
2693 ntohl(psf->sf_inaddr),
2694 psf->sf_count[MCAST_INCLUDE],
2695 psf->sf_count[MCAST_EXCLUDE]);
2696 }
2697 return 0;
2698}
2699
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002700static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 .start = igmp_mcf_seq_start,
2702 .next = igmp_mcf_seq_next,
2703 .stop = igmp_mcf_seq_stop,
2704 .show = igmp_mcf_seq_show,
2705};
2706
2707static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2708{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002709 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002710 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
Arjan van de Ven9a321442007-02-12 00:55:35 -08002713static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 .owner = THIS_MODULE,
2715 .open = igmp_mcf_seq_open,
2716 .read = seq_read,
2717 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002718 .release = seq_release_net,
2719};
2720
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002721static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002722{
2723 struct proc_dir_entry *pde;
2724
Gao fengd4beaa62013-02-18 01:34:54 +00002725 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002726 if (!pde)
2727 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002728 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2729 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002730 if (!pde)
2731 goto out_mcfilter;
2732 return 0;
2733
2734out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002735 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002736out_igmp:
2737 return -ENOMEM;
2738}
2739
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002740static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002741{
Gao fengece31ff2013-02-18 01:34:56 +00002742 remove_proc_entry("mcfilter", net->proc_net);
2743 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002744}
2745
2746static struct pernet_operations igmp_net_ops = {
2747 .init = igmp_net_init,
2748 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749};
2750
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002751static int igmp_netdev_event(struct notifier_block *this,
2752 unsigned long event, void *ptr)
2753{
2754 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2755 struct in_device *in_dev;
2756
2757 switch (event) {
2758 case NETDEV_RESEND_IGMP:
2759 in_dev = __in_dev_get_rtnl(dev);
2760 if (in_dev)
2761 ip_mc_rejoin_groups(in_dev);
2762 break;
2763 default:
2764 break;
2765 }
2766 return NOTIFY_DONE;
2767}
2768
2769static struct notifier_block igmp_notifier = {
2770 .notifier_call = igmp_netdev_event,
2771};
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773int __init igmp_mc_proc_init(void)
2774{
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002775 int err;
2776
2777 err = register_pernet_subsys(&igmp_net_ops);
2778 if (err)
2779 return err;
2780 err = register_netdevice_notifier(&igmp_notifier);
2781 if (err)
2782 goto reg_notif_fail;
2783 return 0;
2784
2785reg_notif_fail:
2786 unregister_pernet_subsys(&igmp_net_ops);
2787 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789#endif