blob: d78599a9dbd5a2ebdeee1a2429cd2b243d341680 [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 *
11 * Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
12 *
13 * Authors:
14 * Alan Cox <Alan.Cox@linux.org>
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 *
21 * Fixes:
22 *
23 * Alan Cox : Added lots of __inline__ to optimise
24 * the memory usage of all the tiny little
25 * functions.
26 * Alan Cox : Dumped the header building experiment.
27 * Alan Cox : Minor tweaks ready for multicast routing
28 * and extended IGMP protocol.
29 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
30 * writes utterly bogus code otherwise (sigh)
31 * fixed IGMP loopback to behave in the manner
32 * desired by mrouted, fixed the fact it has been
33 * broken since 1.3.6 and cleaned up a few minor
34 * points.
35 *
36 * Chih-Jen Chang : Tried to revise IGMP to Version 2
37 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090038 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * ipmulti-3.5 source code.
40 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
41 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
42 * the mrouted version on that device.
43 * Chih-Jen Chang : Added the max_resp_time parameter to
44 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
45 * to identify the multicast router version
46 * and do what the IGMP version 2 specified.
47 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
48 * Tsu-Sheng Tsao if the specified time expired.
49 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
50 * Alan Cox : Use GFP_ATOMIC in the right places.
51 * Christian Daudt : igmp timer wasn't set for local group
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090052 * memberships but was being deleted,
53 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * from %p with timer not initialized\n"
55 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090056 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * igmp_timer_expire function (960205).
58 * Christian Daudt : igmp_heard_report now only calls
59 * igmp_timer_expire if tm->running is
60 * true (960216).
61 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
62 * igmp_heard_query never trigger. Expiry
63 * miscalculation fixed in igmp_heard_query
64 * and random() made to return unsigned to
65 * prevent negative expiry times.
66 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
67 * fix from pending 2.1.x patches.
68 * Alan Cox: Forget to enable FDDI support earlier.
69 * Alexey Kuznetsov: Fixed leaving groups on device down.
70 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
71 * David L Stevens: IGMPv3 support, with help from
72 * Vinay Kulkarni
73 */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/module.h>
76#include <asm/uaccess.h>
77#include <asm/system.h>
78#include <linux/types.h>
79#include <linux/kernel.h>
80#include <linux/jiffies.h>
81#include <linux/string.h>
82#include <linux/socket.h>
83#include <linux/sockios.h>
84#include <linux/in.h>
85#include <linux/inet.h>
86#include <linux/netdevice.h>
87#include <linux/skbuff.h>
88#include <linux/inetdevice.h>
89#include <linux/igmp.h>
90#include <linux/if_arp.h>
91#include <linux/rtnetlink.h>
92#include <linux/times.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020093
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020094#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020095#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <net/ip.h>
97#include <net/protocol.h>
98#include <net/route.h>
99#include <net/sock.h>
100#include <net/checksum.h>
101#include <linux/netfilter_ipv4.h>
102#ifdef CONFIG_IP_MROUTE
103#include <linux/mroute.h>
104#endif
105#ifdef CONFIG_PROC_FS
106#include <linux/proc_fs.h>
107#include <linux/seq_file.h>
108#endif
109
110#define IP_MAX_MEMBERSHIPS 20
111#define IP_MAX_MSF 10
112
113#ifdef CONFIG_IP_MULTICAST
114/* Parameter names and values are taken from igmp-v2-06 draft */
115
116#define IGMP_V1_Router_Present_Timeout (400*HZ)
117#define IGMP_V2_Router_Present_Timeout (400*HZ)
118#define IGMP_Unsolicited_Report_Interval (10*HZ)
119#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) \
133 (IPV4_DEVCONF_ALL(FORCE_IGMP_VERSION) == 1 || \
134 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) \
138 (IPV4_DEVCONF_ALL(FORCE_IGMP_VERSION) == 2 || \
139 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
143static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700144static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static void igmpv3_clear_delrec(struct in_device *in_dev);
146static int sf_setstate(struct ip_mc_list *pmc);
147static void sf_markstate(struct ip_mc_list *pmc);
148#endif
149static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700150static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
151 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153static void ip_ma_put(struct ip_mc_list *im)
154{
155 if (atomic_dec_and_test(&im->refcnt)) {
156 in_dev_put(im->interface);
157 kfree(im);
158 }
159}
160
161#ifdef CONFIG_IP_MULTICAST
162
163/*
164 * Timer management
165 */
166
167static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
168{
169 spin_lock_bh(&im->lock);
170 if (del_timer(&im->timer))
171 atomic_dec(&im->refcnt);
172 im->tm_running=0;
173 im->reporter = 0;
174 im->unsolicit_count = 0;
175 spin_unlock_bh(&im->lock);
176}
177
178/* It must be called with locked im->lock */
179static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
180{
181 int tv=net_random() % max_delay;
182
183 im->tm_running=1;
184 if (!mod_timer(&im->timer, jiffies+tv+2))
185 atomic_inc(&im->refcnt);
186}
187
188static void igmp_gq_start_timer(struct in_device *in_dev)
189{
190 int tv = net_random() % in_dev->mr_maxdelay;
191
192 in_dev->mr_gq_running = 1;
193 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
194 in_dev_hold(in_dev);
195}
196
197static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
198{
199 int tv = net_random() % delay;
200
201 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
202 in_dev_hold(in_dev);
203}
204
205static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
206{
207 spin_lock_bh(&im->lock);
208 im->unsolicit_count = 0;
209 if (del_timer(&im->timer)) {
210 if ((long)(im->timer.expires-jiffies) < max_delay) {
211 add_timer(&im->timer);
212 im->tm_running=1;
213 spin_unlock_bh(&im->lock);
214 return;
215 }
216 atomic_dec(&im->refcnt);
217 }
218 igmp_start_timer(im, max_delay);
219 spin_unlock_bh(&im->lock);
220}
221
222
223/*
224 * Send an IGMP report.
225 */
226
227#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
228
229
230static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
231 int gdeleted, int sdeleted)
232{
233 switch (type) {
234 case IGMPV3_MODE_IS_INCLUDE:
235 case IGMPV3_MODE_IS_EXCLUDE:
236 if (gdeleted || sdeleted)
237 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800238 if (!(pmc->gsquery && !psf->sf_gsresp)) {
239 if (pmc->sfmode == MCAST_INCLUDE)
240 return 1;
241 /* don't include if this source is excluded
242 * in all filters
243 */
244 if (psf->sf_count[MCAST_INCLUDE])
245 return type == IGMPV3_MODE_IS_INCLUDE;
246 return pmc->sfcount[MCAST_EXCLUDE] ==
247 psf->sf_count[MCAST_EXCLUDE];
248 }
249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 case IGMPV3_CHANGE_TO_INCLUDE:
251 if (gdeleted || sdeleted)
252 return 0;
253 return psf->sf_count[MCAST_INCLUDE] != 0;
254 case IGMPV3_CHANGE_TO_EXCLUDE:
255 if (gdeleted || sdeleted)
256 return 0;
257 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
258 psf->sf_count[MCAST_INCLUDE])
259 return 0;
260 return pmc->sfcount[MCAST_EXCLUDE] ==
261 psf->sf_count[MCAST_EXCLUDE];
262 case IGMPV3_ALLOW_NEW_SOURCES:
263 if (gdeleted || !psf->sf_crcount)
264 return 0;
265 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
266 case IGMPV3_BLOCK_OLD_SOURCES:
267 if (pmc->sfmode == MCAST_INCLUDE)
268 return gdeleted || (psf->sf_crcount && sdeleted);
269 return psf->sf_crcount && !gdeleted && !sdeleted;
270 }
271 return 0;
272}
273
274static int
275igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
276{
277 struct ip_sf_list *psf;
278 int scount = 0;
279
280 for (psf=pmc->sources; psf; psf=psf->sf_next) {
281 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
282 continue;
283 scount++;
284 }
285 return scount;
286}
287
288static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
289{
290 struct sk_buff *skb;
291 struct rtable *rt;
292 struct iphdr *pip;
293 struct igmpv3_report *pig;
294
295 skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
296 if (skb == NULL)
297 return NULL;
298
299 {
300 struct flowi fl = { .oif = dev->ifindex,
301 .nl_u = { .ip4_u = {
302 .daddr = IGMPV3_ALL_MCR } },
303 .proto = IPPROTO_IGMP };
304 if (ip_route_output_key(&rt, &fl)) {
305 kfree_skb(skb);
306 return NULL;
307 }
308 }
309 if (rt->rt_src == 0) {
310 kfree_skb(skb);
311 ip_rt_put(rt);
312 return NULL;
313 }
314
315 skb->dst = &rt->u.dst;
316 skb->dev = dev;
317
318 skb_reserve(skb, LL_RESERVED_SPACE(dev));
319
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300320 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700321 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300322 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 pip->version = 4;
325 pip->ihl = (sizeof(struct iphdr)+4)>>2;
326 pip->tos = 0xc0;
327 pip->frag_off = htons(IP_DF);
328 pip->ttl = 1;
329 pip->daddr = rt->rt_dst;
330 pip->saddr = rt->rt_src;
331 pip->protocol = IPPROTO_IGMP;
332 pip->tot_len = 0; /* filled in later */
333 ip_select_ident(pip, &rt->u.dst, NULL);
334 ((u8*)&pip[1])[0] = IPOPT_RA;
335 ((u8*)&pip[1])[1] = 4;
336 ((u8*)&pip[1])[2] = 0;
337 ((u8*)&pip[1])[3] = 0;
338
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700339 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300340 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300341 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
343 pig->resv1 = 0;
344 pig->csum = 0;
345 pig->resv2 = 0;
346 pig->ngrec = 0;
347 return skb;
348}
349
350static int igmpv3_sendpack(struct sk_buff *skb)
351{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700352 struct iphdr *pip = ip_hdr(skb);
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300353 struct igmphdr *pig = igmp_hdr(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700354 const int iplen = skb->tail - skb->network_header;
355 const int igmplen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 pip->tot_len = htons(iplen);
358 ip_send_check(pip);
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300359 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
362 dst_output);
363}
364
365static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
366{
367 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
368}
369
370static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
371 int type, struct igmpv3_grec **ppgr)
372{
373 struct net_device *dev = pmc->interface->dev;
374 struct igmpv3_report *pih;
375 struct igmpv3_grec *pgr;
376
377 if (!skb)
378 skb = igmpv3_newpack(dev, dev->mtu);
379 if (!skb)
380 return NULL;
381 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
382 pgr->grec_type = type;
383 pgr->grec_auxwords = 0;
384 pgr->grec_nsrcs = 0;
385 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300386 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 pih->ngrec = htons(ntohs(pih->ngrec)+1);
388 *ppgr = pgr;
389 return skb;
390}
391
392#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
393 skb_tailroom(skb)) : 0)
394
395static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
396 int type, int gdeleted, int sdeleted)
397{
398 struct net_device *dev = pmc->interface->dev;
399 struct igmpv3_report *pih;
400 struct igmpv3_grec *pgr = NULL;
401 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800402 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 if (pmc->multiaddr == IGMP_ALL_HOSTS)
405 return skb;
406
407 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
408 type == IGMPV3_MODE_IS_EXCLUDE;
409 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
410 type == IGMPV3_CHANGE_TO_EXCLUDE;
411
David L Stevensad125832006-01-18 14:20:56 -0800412 stotal = scount = 0;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
415
David L Stevensad125832006-01-18 14:20:56 -0800416 if (!*psf_list)
417 goto empty_source;
418
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300419 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* EX and TO_EX get a fresh packet, if needed */
422 if (truncate) {
423 if (pih && pih->ngrec &&
424 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
425 if (skb)
426 igmpv3_sendpack(skb);
427 skb = igmpv3_newpack(dev, dev->mtu);
428 }
429 }
430 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 psf_prev = NULL;
432 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700433 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 psf_next = psf->sf_next;
436
437 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
438 psf_prev = psf;
439 continue;
440 }
441
442 /* clear marks on query responses */
443 if (isquery)
444 psf->sf_gsresp = 0;
445
Al Viro63007722006-09-27 18:31:32 -0700446 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 first*sizeof(struct igmpv3_grec)) {
448 if (truncate && !first)
449 break; /* truncate these */
450 if (pgr)
451 pgr->grec_nsrcs = htons(scount);
452 if (skb)
453 igmpv3_sendpack(skb);
454 skb = igmpv3_newpack(dev, dev->mtu);
455 first = 1;
456 scount = 0;
457 }
458 if (first) {
459 skb = add_grhead(skb, pmc, type, &pgr);
460 first = 0;
461 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800462 if (!skb)
463 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700464 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800466 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
468 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
469 psf->sf_crcount--;
470 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
471 if (psf_prev)
472 psf_prev->sf_next = psf->sf_next;
473 else
474 *psf_list = psf->sf_next;
475 kfree(psf);
476 continue;
477 }
478 }
479 psf_prev = psf;
480 }
David L Stevensad125832006-01-18 14:20:56 -0800481
482empty_source:
483 if (!stotal) {
484 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
485 type == IGMPV3_BLOCK_OLD_SOURCES)
486 return skb;
487 if (pmc->crcount || isquery) {
488 /* make sure we have room for group header */
489 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
490 igmpv3_sendpack(skb);
491 skb = NULL; /* add_grhead will get a new one */
492 }
493 skb = add_grhead(skb, pmc, type, &pgr);
494 }
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (pgr)
497 pgr->grec_nsrcs = htons(scount);
498
499 if (isquery)
500 pmc->gsquery = 0; /* clear query state on report */
501 return skb;
502}
503
504static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
505{
506 struct sk_buff *skb = NULL;
507 int type;
508
509 if (!pmc) {
510 read_lock(&in_dev->mc_list_lock);
511 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
512 if (pmc->multiaddr == IGMP_ALL_HOSTS)
513 continue;
514 spin_lock_bh(&pmc->lock);
515 if (pmc->sfcount[MCAST_EXCLUDE])
516 type = IGMPV3_MODE_IS_EXCLUDE;
517 else
518 type = IGMPV3_MODE_IS_INCLUDE;
519 skb = add_grec(skb, pmc, type, 0, 0);
520 spin_unlock_bh(&pmc->lock);
521 }
522 read_unlock(&in_dev->mc_list_lock);
523 } else {
524 spin_lock_bh(&pmc->lock);
525 if (pmc->sfcount[MCAST_EXCLUDE])
526 type = IGMPV3_MODE_IS_EXCLUDE;
527 else
528 type = IGMPV3_MODE_IS_INCLUDE;
529 skb = add_grec(skb, pmc, type, 0, 0);
530 spin_unlock_bh(&pmc->lock);
531 }
532 if (!skb)
533 return 0;
534 return igmpv3_sendpack(skb);
535}
536
537/*
538 * remove zero-count source records from a source filter list
539 */
540static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
541{
542 struct ip_sf_list *psf_prev, *psf_next, *psf;
543
544 psf_prev = NULL;
545 for (psf=*ppsf; psf; psf = psf_next) {
546 psf_next = psf->sf_next;
547 if (psf->sf_crcount == 0) {
548 if (psf_prev)
549 psf_prev->sf_next = psf->sf_next;
550 else
551 *ppsf = psf->sf_next;
552 kfree(psf);
553 } else
554 psf_prev = psf;
555 }
556}
557
558static void igmpv3_send_cr(struct in_device *in_dev)
559{
560 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
561 struct sk_buff *skb = NULL;
562 int type, dtype;
563
564 read_lock(&in_dev->mc_list_lock);
565 spin_lock_bh(&in_dev->mc_tomb_lock);
566
567 /* deleted MCA's */
568 pmc_prev = NULL;
569 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
570 pmc_next = pmc->next;
571 if (pmc->sfmode == MCAST_INCLUDE) {
572 type = IGMPV3_BLOCK_OLD_SOURCES;
573 dtype = IGMPV3_BLOCK_OLD_SOURCES;
574 skb = add_grec(skb, pmc, type, 1, 0);
575 skb = add_grec(skb, pmc, dtype, 1, 1);
576 }
577 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (pmc->sfmode == MCAST_EXCLUDE) {
579 type = IGMPV3_CHANGE_TO_INCLUDE;
580 skb = add_grec(skb, pmc, type, 1, 0);
581 }
David L Stevensad125832006-01-18 14:20:56 -0800582 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (pmc->crcount == 0) {
584 igmpv3_clear_zeros(&pmc->tomb);
585 igmpv3_clear_zeros(&pmc->sources);
586 }
587 }
588 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
589 if (pmc_prev)
590 pmc_prev->next = pmc_next;
591 else
592 in_dev->mc_tomb = pmc_next;
593 in_dev_put(pmc->interface);
594 kfree(pmc);
595 } else
596 pmc_prev = pmc;
597 }
598 spin_unlock_bh(&in_dev->mc_tomb_lock);
599
600 /* change recs */
601 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
602 spin_lock_bh(&pmc->lock);
603 if (pmc->sfcount[MCAST_EXCLUDE]) {
604 type = IGMPV3_BLOCK_OLD_SOURCES;
605 dtype = IGMPV3_ALLOW_NEW_SOURCES;
606 } else {
607 type = IGMPV3_ALLOW_NEW_SOURCES;
608 dtype = IGMPV3_BLOCK_OLD_SOURCES;
609 }
610 skb = add_grec(skb, pmc, type, 0, 0);
611 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
612
613 /* filter mode changes */
614 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (pmc->sfmode == MCAST_EXCLUDE)
616 type = IGMPV3_CHANGE_TO_EXCLUDE;
617 else
618 type = IGMPV3_CHANGE_TO_INCLUDE;
619 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800620 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 spin_unlock_bh(&pmc->lock);
623 }
624 read_unlock(&in_dev->mc_list_lock);
625
626 if (!skb)
627 return;
628 (void) igmpv3_sendpack(skb);
629}
630
631static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
632 int type)
633{
634 struct sk_buff *skb;
635 struct iphdr *iph;
636 struct igmphdr *ih;
637 struct rtable *rt;
638 struct net_device *dev = in_dev->dev;
Al Viro63007722006-09-27 18:31:32 -0700639 __be32 group = pmc ? pmc->multiaddr : 0;
640 __be32 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
643 return igmpv3_send_report(in_dev, pmc);
644 else if (type == IGMP_HOST_LEAVE_MESSAGE)
645 dst = IGMP_ALL_ROUTER;
646 else
647 dst = group;
648
649 {
650 struct flowi fl = { .oif = dev->ifindex,
651 .nl_u = { .ip4_u = { .daddr = dst } },
652 .proto = IPPROTO_IGMP };
653 if (ip_route_output_key(&rt, &fl))
654 return -1;
655 }
656 if (rt->rt_src == 0) {
657 ip_rt_put(rt);
658 return -1;
659 }
660
661 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
662 if (skb == NULL) {
663 ip_rt_put(rt);
664 return -1;
665 }
666
667 skb->dst = &rt->u.dst;
668
669 skb_reserve(skb, LL_RESERVED_SPACE(dev));
670
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300671 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700672 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300673 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 iph->version = 4;
676 iph->ihl = (sizeof(struct iphdr)+4)>>2;
677 iph->tos = 0xc0;
678 iph->frag_off = htons(IP_DF);
679 iph->ttl = 1;
680 iph->daddr = dst;
681 iph->saddr = rt->rt_src;
682 iph->protocol = IPPROTO_IGMP;
683 iph->tot_len = htons(IGMP_SIZE);
684 ip_select_ident(iph, &rt->u.dst, NULL);
685 ((u8*)&iph[1])[0] = IPOPT_RA;
686 ((u8*)&iph[1])[1] = 4;
687 ((u8*)&iph[1])[2] = 0;
688 ((u8*)&iph[1])[3] = 0;
689 ip_send_check(iph);
690
691 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
692 ih->type=type;
693 ih->code=0;
694 ih->csum=0;
695 ih->group=group;
696 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
697
698 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
699 dst_output);
700}
701
702static void igmp_gq_timer_expire(unsigned long data)
703{
704 struct in_device *in_dev = (struct in_device *)data;
705
706 in_dev->mr_gq_running = 0;
707 igmpv3_send_report(in_dev, NULL);
708 __in_dev_put(in_dev);
709}
710
711static void igmp_ifc_timer_expire(unsigned long data)
712{
713 struct in_device *in_dev = (struct in_device *)data;
714
715 igmpv3_send_cr(in_dev);
716 if (in_dev->mr_ifc_count) {
717 in_dev->mr_ifc_count--;
718 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
719 }
720 __in_dev_put(in_dev);
721}
722
723static void igmp_ifc_event(struct in_device *in_dev)
724{
725 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
726 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900727 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 IGMP_Unsolicited_Report_Count;
729 igmp_ifc_start_timer(in_dev, 1);
730}
731
732
733static void igmp_timer_expire(unsigned long data)
734{
735 struct ip_mc_list *im=(struct ip_mc_list *)data;
736 struct in_device *in_dev = im->interface;
737
738 spin_lock(&im->lock);
739 im->tm_running=0;
740
741 if (im->unsolicit_count) {
742 im->unsolicit_count--;
743 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
744 }
745 im->reporter = 1;
746 spin_unlock(&im->lock);
747
748 if (IGMP_V1_SEEN(in_dev))
749 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
750 else if (IGMP_V2_SEEN(in_dev))
751 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
752 else
753 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
754
755 ip_ma_put(im);
756}
757
David L Stevensad125832006-01-18 14:20:56 -0800758/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700759static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 struct ip_sf_list *psf;
762 int i, scount;
763
764 scount = 0;
765 for (psf=pmc->sources; psf; psf=psf->sf_next) {
766 if (scount == nsrcs)
767 break;
David L Stevensad125832006-01-18 14:20:56 -0800768 for (i=0; i<nsrcs; i++) {
769 /* skip inactive filters */
770 if (pmc->sfcount[MCAST_INCLUDE] ||
771 pmc->sfcount[MCAST_EXCLUDE] !=
772 psf->sf_count[MCAST_EXCLUDE])
773 continue;
774 if (srcs[i] == psf->sf_inaddr) {
775 scount++;
776 break;
777 }
778 }
779 }
780 pmc->gsquery = 0;
781 if (scount == nsrcs) /* all sources excluded */
782 return 0;
783 return 1;
784}
785
Al Viroea4d9e72006-09-27 18:30:52 -0700786static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800787{
788 struct ip_sf_list *psf;
789 int i, scount;
790
791 if (pmc->sfmode == MCAST_EXCLUDE)
792 return igmp_xmarksources(pmc, nsrcs, srcs);
793
794 /* mark INCLUDE-mode sources */
795 scount = 0;
796 for (psf=pmc->sources; psf; psf=psf->sf_next) {
797 if (scount == nsrcs)
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 for (i=0; i<nsrcs; i++)
800 if (srcs[i] == psf->sf_inaddr) {
801 psf->sf_gsresp = 1;
802 scount++;
803 break;
804 }
805 }
David L Stevensad125832006-01-18 14:20:56 -0800806 if (!scount) {
807 pmc->gsquery = 0;
808 return 0;
809 }
810 pmc->gsquery = 1;
811 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Al Viro63007722006-09-27 18:31:32 -0700814static void igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct ip_mc_list *im;
817
818 /* Timers are only set for non-local groups */
819
820 if (group == IGMP_ALL_HOSTS)
821 return;
822
823 read_lock(&in_dev->mc_list_lock);
824 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
825 if (im->multiaddr == group) {
826 igmp_stop_timer(im);
827 break;
828 }
829 }
830 read_unlock(&in_dev->mc_list_lock);
831}
832
833static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
834 int len)
835{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300836 struct igmphdr *ih = igmp_hdr(skb);
837 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700839 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int max_delay;
841 int mark = 0;
842
843
844 if (len == 8) {
845 if (ih->code == 0) {
846 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 max_delay = IGMP_Query_Response_Interval;
849 in_dev->mr_v1_seen = jiffies +
850 IGMP_V1_Router_Present_Timeout;
851 group = 0;
852 } else {
853 /* v2 router present */
854 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
855 in_dev->mr_v2_seen = jiffies +
856 IGMP_V2_Router_Present_Timeout;
857 }
858 /* cancel the interface change timer */
859 in_dev->mr_ifc_count = 0;
860 if (del_timer(&in_dev->mr_ifc_timer))
861 __in_dev_put(in_dev);
862 /* clear deleted report items */
863 igmpv3_clear_delrec(in_dev);
864 } else if (len < 12) {
865 return; /* ignore bogus packet; freed by caller */
866 } else { /* v3 */
867 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
868 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900869
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300870 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900872 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700873 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300875 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
878 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
879 if (!max_delay)
880 max_delay = 1; /* can't mod w/ 0 */
881 in_dev->mr_maxdelay = max_delay;
882 if (ih3->qrv)
883 in_dev->mr_qrv = ih3->qrv;
884 if (!group) { /* general query */
885 if (ih3->nsrcs)
886 return; /* no sources allowed */
887 igmp_gq_start_timer(in_dev);
888 return;
889 }
890 /* mark sources to include, if group & source-specific */
891 mark = ih3->nsrcs != 0;
892 }
893
894 /*
895 * - Start the timers in all of our membership records
896 * that the query applies to for the interface on
897 * which the query arrived excl. those that belong
898 * to a "local" group (224.0.0.X)
899 * - For timers already running check if they need to
900 * be reset.
901 * - Use the igmp->igmp_code field as the maximum
902 * delay possible
903 */
904 read_lock(&in_dev->mc_list_lock);
905 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
David L Stevensad125832006-01-18 14:20:56 -0800906 int changed;
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (group && group != im->multiaddr)
909 continue;
910 if (im->multiaddr == IGMP_ALL_HOSTS)
911 continue;
912 spin_lock_bh(&im->lock);
913 if (im->tm_running)
914 im->gsquery = im->gsquery && mark;
915 else
916 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800917 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900918 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800920 if (changed)
921 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 read_unlock(&in_dev->mc_list_lock);
924}
925
926int igmp_rcv(struct sk_buff *skb)
927{
928 /* This basically follows the spec line by line -- see RFC1112 */
929 struct igmphdr *ih;
930 struct in_device *in_dev = in_dev_get(skb->dev);
931 int len = skb->len;
932
933 if (in_dev==NULL) {
934 kfree_skb(skb);
935 return 0;
936 }
937
Herbert Xufb286bb2005-11-10 13:01:24 -0800938 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
939 goto drop;
940
941 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700942 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800943 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800944 break;
945 /* fall through */
946 case CHECKSUM_NONE:
947 skb->csum = 0;
948 if (__skb_checksum_complete(skb))
949 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300952 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 switch (ih->type) {
954 case IGMP_HOST_MEMBERSHIP_QUERY:
955 igmp_heard_query(in_dev, skb, len);
956 break;
957 case IGMP_HOST_MEMBERSHIP_REPORT:
958 case IGMPV2_HOST_MEMBERSHIP_REPORT:
959 case IGMPV3_HOST_MEMBERSHIP_REPORT:
960 /* Is it our report looped back? */
961 if (((struct rtable*)skb->dst)->fl.iif == 0)
962 break;
David Stevens24c69272005-12-02 20:32:59 -0800963 /* don't rely on MC router hearing unicast reports */
964 if (skb->pkt_type == PACKET_MULTICAST ||
965 skb->pkt_type == PACKET_BROADCAST)
966 igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 case IGMP_PIM:
969#ifdef CONFIG_IP_PIMSM_V1
970 in_dev_put(in_dev);
971 return pim_rcv_v1(skb);
972#endif
973 case IGMP_DVMRP:
974 case IGMP_TRACE:
975 case IGMP_HOST_LEAVE_MESSAGE:
976 case IGMP_MTRACE:
977 case IGMP_MTRACE_RESP:
978 break;
979 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -0800980 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Herbert Xufb286bb2005-11-10 13:01:24 -0800982
983drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 in_dev_put(in_dev);
985 kfree_skb(skb);
986 return 0;
987}
988
989#endif
990
991
992/*
993 * Add a filter to a device
994 */
995
Al Viro63007722006-09-27 18:31:32 -0700996static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 char buf[MAX_ADDR_LEN];
999 struct net_device *dev = in_dev->dev;
1000
1001 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1002 We will get multicast token leakage, when IFF_MULTICAST
1003 is changed. This check should be done in dev->set_multicast_list
1004 routine. Something sort of:
1005 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1006 --ANK
1007 */
1008 if (arp_mc_map(addr, buf, dev, 0) == 0)
1009 dev_mc_add(dev,buf,dev->addr_len,0);
1010}
1011
1012/*
1013 * Remove a filter from a device
1014 */
1015
Al Viro63007722006-09-27 18:31:32 -07001016static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 char buf[MAX_ADDR_LEN];
1019 struct net_device *dev = in_dev->dev;
1020
1021 if (arp_mc_map(addr, buf, dev, 0) == 0)
1022 dev_mc_delete(dev,buf,dev->addr_len,0);
1023}
1024
1025#ifdef CONFIG_IP_MULTICAST
1026/*
1027 * deleted ip_mc_list manipulation
1028 */
1029static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1030{
1031 struct ip_mc_list *pmc;
1032
1033 /* this is an "ip_mc_list" for convenience; only the fields below
1034 * are actually used. In particular, the refcnt and users are not
1035 * used for management of the delete list. Using the same structure
1036 * for deleted items allows change reports to use common code with
1037 * non-deleted or query-response MCA's.
1038 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001039 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!pmc)
1041 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 spin_lock_bh(&im->lock);
1043 pmc->interface = im->interface;
1044 in_dev_hold(in_dev);
1045 pmc->multiaddr = im->multiaddr;
1046 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1047 IGMP_Unsolicited_Report_Count;
1048 pmc->sfmode = im->sfmode;
1049 if (pmc->sfmode == MCAST_INCLUDE) {
1050 struct ip_sf_list *psf;
1051
1052 pmc->tomb = im->tomb;
1053 pmc->sources = im->sources;
1054 im->tomb = im->sources = NULL;
1055 for (psf=pmc->sources; psf; psf=psf->sf_next)
1056 psf->sf_crcount = pmc->crcount;
1057 }
1058 spin_unlock_bh(&im->lock);
1059
1060 spin_lock_bh(&in_dev->mc_tomb_lock);
1061 pmc->next = in_dev->mc_tomb;
1062 in_dev->mc_tomb = pmc;
1063 spin_unlock_bh(&in_dev->mc_tomb_lock);
1064}
1065
Al Viro63007722006-09-27 18:31:32 -07001066static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct ip_mc_list *pmc, *pmc_prev;
1069 struct ip_sf_list *psf, *psf_next;
1070
1071 spin_lock_bh(&in_dev->mc_tomb_lock);
1072 pmc_prev = NULL;
1073 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1074 if (pmc->multiaddr == multiaddr)
1075 break;
1076 pmc_prev = pmc;
1077 }
1078 if (pmc) {
1079 if (pmc_prev)
1080 pmc_prev->next = pmc->next;
1081 else
1082 in_dev->mc_tomb = pmc->next;
1083 }
1084 spin_unlock_bh(&in_dev->mc_tomb_lock);
1085 if (pmc) {
1086 for (psf=pmc->tomb; psf; psf=psf_next) {
1087 psf_next = psf->sf_next;
1088 kfree(psf);
1089 }
1090 in_dev_put(pmc->interface);
1091 kfree(pmc);
1092 }
1093}
1094
1095static void igmpv3_clear_delrec(struct in_device *in_dev)
1096{
1097 struct ip_mc_list *pmc, *nextpmc;
1098
1099 spin_lock_bh(&in_dev->mc_tomb_lock);
1100 pmc = in_dev->mc_tomb;
1101 in_dev->mc_tomb = NULL;
1102 spin_unlock_bh(&in_dev->mc_tomb_lock);
1103
1104 for (; pmc; pmc = nextpmc) {
1105 nextpmc = pmc->next;
1106 ip_mc_clear_src(pmc);
1107 in_dev_put(pmc->interface);
1108 kfree(pmc);
1109 }
1110 /* clear dead sources, too */
1111 read_lock(&in_dev->mc_list_lock);
1112 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1113 struct ip_sf_list *psf, *psf_next;
1114
1115 spin_lock_bh(&pmc->lock);
1116 psf = pmc->tomb;
1117 pmc->tomb = NULL;
1118 spin_unlock_bh(&pmc->lock);
1119 for (; psf; psf=psf_next) {
1120 psf_next = psf->sf_next;
1121 kfree(psf);
1122 }
1123 }
1124 read_unlock(&in_dev->mc_list_lock);
1125}
1126#endif
1127
1128static void igmp_group_dropped(struct ip_mc_list *im)
1129{
1130 struct in_device *in_dev = im->interface;
1131#ifdef CONFIG_IP_MULTICAST
1132 int reporter;
1133#endif
1134
1135 if (im->loaded) {
1136 im->loaded = 0;
1137 ip_mc_filter_del(in_dev, im->multiaddr);
1138 }
1139
1140#ifdef CONFIG_IP_MULTICAST
1141 if (im->multiaddr == IGMP_ALL_HOSTS)
1142 return;
1143
1144 reporter = im->reporter;
1145 igmp_stop_timer(im);
1146
1147 if (!in_dev->dead) {
1148 if (IGMP_V1_SEEN(in_dev))
1149 goto done;
1150 if (IGMP_V2_SEEN(in_dev)) {
1151 if (reporter)
1152 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1153 goto done;
1154 }
1155 /* IGMPv3 */
1156 igmpv3_add_delrec(in_dev, im);
1157
1158 igmp_ifc_event(in_dev);
1159 }
1160done:
1161#endif
1162 ip_mc_clear_src(im);
1163}
1164
1165static void igmp_group_added(struct ip_mc_list *im)
1166{
1167 struct in_device *in_dev = im->interface;
1168
1169 if (im->loaded == 0) {
1170 im->loaded = 1;
1171 ip_mc_filter_add(in_dev, im->multiaddr);
1172 }
1173
1174#ifdef CONFIG_IP_MULTICAST
1175 if (im->multiaddr == IGMP_ALL_HOSTS)
1176 return;
1177
1178 if (in_dev->dead)
1179 return;
1180 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1181 spin_lock_bh(&im->lock);
1182 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1183 spin_unlock_bh(&im->lock);
1184 return;
1185 }
1186 /* else, v3 */
1187
1188 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1189 IGMP_Unsolicited_Report_Count;
1190 igmp_ifc_event(in_dev);
1191#endif
1192}
1193
1194
1195/*
1196 * Multicast list managers
1197 */
1198
1199
1200/*
1201 * A socket has joined a multicast group on device dev.
1202 */
1203
Al Viro8f935bb2006-09-27 18:30:07 -07001204void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct ip_mc_list *im;
1207
1208 ASSERT_RTNL();
1209
1210 for (im=in_dev->mc_list; im; im=im->next) {
1211 if (im->multiaddr == addr) {
1212 im->users++;
1213 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1214 goto out;
1215 }
1216 }
1217
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001218 im = kmalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (!im)
1220 goto out;
1221
1222 im->users=1;
1223 im->interface=in_dev;
1224 in_dev_hold(in_dev);
1225 im->multiaddr=addr;
1226 /* initial mode is (EX, empty) */
1227 im->sfmode = MCAST_EXCLUDE;
1228 im->sfcount[MCAST_INCLUDE] = 0;
1229 im->sfcount[MCAST_EXCLUDE] = 1;
1230 im->sources = NULL;
1231 im->tomb = NULL;
1232 im->crcount = 0;
1233 atomic_set(&im->refcnt, 1);
1234 spin_lock_init(&im->lock);
1235#ifdef CONFIG_IP_MULTICAST
1236 im->tm_running=0;
1237 init_timer(&im->timer);
1238 im->timer.data=(unsigned long)im;
1239 im->timer.function=&igmp_timer_expire;
1240 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1241 im->reporter = 0;
1242 im->gsquery = 0;
1243#endif
1244 im->loaded = 0;
1245 write_lock_bh(&in_dev->mc_list_lock);
1246 im->next=in_dev->mc_list;
1247 in_dev->mc_list=im;
1248 write_unlock_bh(&in_dev->mc_list_lock);
1249#ifdef CONFIG_IP_MULTICAST
1250 igmpv3_del_delrec(in_dev, im->multiaddr);
1251#endif
1252 igmp_group_added(im);
1253 if (!in_dev->dead)
1254 ip_rt_multicast_event(in_dev);
1255out:
1256 return;
1257}
1258
1259/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001260 * Resend IGMP JOIN report; used for bonding.
1261 */
1262void ip_mc_rejoin_group(struct ip_mc_list *im)
1263{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001264#ifdef CONFIG_IP_MULTICAST
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001265 struct in_device *in_dev = im->interface;
1266
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001267 if (im->multiaddr == IGMP_ALL_HOSTS)
1268 return;
1269
1270 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1271 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1272 return;
1273 }
1274 /* else, v3 */
1275 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1276 IGMP_Unsolicited_Report_Count;
1277 igmp_ifc_event(in_dev);
1278#endif
1279}
1280
1281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * A socket has left a multicast group on device dev
1283 */
1284
Al Viro8f935bb2006-09-27 18:30:07 -07001285void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct ip_mc_list *i, **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1292 if (i->multiaddr==addr) {
1293 if (--i->users == 0) {
1294 write_lock_bh(&in_dev->mc_list_lock);
1295 *ip = i->next;
1296 write_unlock_bh(&in_dev->mc_list_lock);
1297 igmp_group_dropped(i);
1298
1299 if (!in_dev->dead)
1300 ip_rt_multicast_event(in_dev);
1301
1302 ip_ma_put(i);
1303 return;
1304 }
1305 break;
1306 }
1307 }
1308}
1309
1310/* Device going down */
1311
1312void ip_mc_down(struct in_device *in_dev)
1313{
1314 struct ip_mc_list *i;
1315
1316 ASSERT_RTNL();
1317
1318 for (i=in_dev->mc_list; i; i=i->next)
1319 igmp_group_dropped(i);
1320
1321#ifdef CONFIG_IP_MULTICAST
1322 in_dev->mr_ifc_count = 0;
1323 if (del_timer(&in_dev->mr_ifc_timer))
1324 __in_dev_put(in_dev);
1325 in_dev->mr_gq_running = 0;
1326 if (del_timer(&in_dev->mr_gq_timer))
1327 __in_dev_put(in_dev);
1328 igmpv3_clear_delrec(in_dev);
1329#endif
1330
1331 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1332}
1333
1334void ip_mc_init_dev(struct in_device *in_dev)
1335{
1336 ASSERT_RTNL();
1337
1338 in_dev->mc_tomb = NULL;
1339#ifdef CONFIG_IP_MULTICAST
1340 in_dev->mr_gq_running = 0;
1341 init_timer(&in_dev->mr_gq_timer);
1342 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1343 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1344 in_dev->mr_ifc_count = 0;
1345 init_timer(&in_dev->mr_ifc_timer);
1346 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1347 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1348 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1349#endif
1350
1351 rwlock_init(&in_dev->mc_list_lock);
1352 spin_lock_init(&in_dev->mc_tomb_lock);
1353}
1354
1355/* Device going up */
1356
1357void ip_mc_up(struct in_device *in_dev)
1358{
1359 struct ip_mc_list *i;
1360
1361 ASSERT_RTNL();
1362
1363 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1364
1365 for (i=in_dev->mc_list; i; i=i->next)
1366 igmp_group_added(i);
1367}
1368
1369/*
1370 * Device is about to be destroyed: clean up.
1371 */
1372
1373void ip_mc_destroy_dev(struct in_device *in_dev)
1374{
1375 struct ip_mc_list *i;
1376
1377 ASSERT_RTNL();
1378
1379 /* Deactivate timers */
1380 ip_mc_down(in_dev);
1381
1382 write_lock_bh(&in_dev->mc_list_lock);
1383 while ((i = in_dev->mc_list) != NULL) {
1384 in_dev->mc_list = i->next;
1385 write_unlock_bh(&in_dev->mc_list_lock);
1386
1387 igmp_group_dropped(i);
1388 ip_ma_put(i);
1389
1390 write_lock_bh(&in_dev->mc_list_lock);
1391 }
1392 write_unlock_bh(&in_dev->mc_list_lock);
1393}
1394
1395static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1396{
1397 struct flowi fl = { .nl_u = { .ip4_u =
1398 { .daddr = imr->imr_multiaddr.s_addr } } };
1399 struct rtable *rt;
1400 struct net_device *dev = NULL;
1401 struct in_device *idev = NULL;
1402
1403 if (imr->imr_ifindex) {
1404 idev = inetdev_by_index(imr->imr_ifindex);
1405 if (idev)
1406 __in_dev_put(idev);
1407 return idev;
1408 }
1409 if (imr->imr_address.s_addr) {
1410 dev = ip_dev_find(imr->imr_address.s_addr);
1411 if (!dev)
1412 return NULL;
Stephen Hemminger15333062006-03-20 22:32:28 -08001413 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
1416 if (!dev && !ip_route_output_key(&rt, &fl)) {
1417 dev = rt->u.dst.dev;
1418 ip_rt_put(rt);
1419 }
1420 if (dev) {
1421 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001422 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 return idev;
1425}
1426
1427/*
1428 * Join a socket to a group
1429 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001430int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1431int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433
1434static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001435 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct ip_sf_list *psf, *psf_prev;
1438 int rv = 0;
1439
1440 psf_prev = NULL;
1441 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1442 if (psf->sf_inaddr == *psfsrc)
1443 break;
1444 psf_prev = psf;
1445 }
1446 if (!psf || psf->sf_count[sfmode] == 0) {
1447 /* source filter not found, or count wrong => bug */
1448 return -ESRCH;
1449 }
1450 psf->sf_count[sfmode]--;
1451 if (psf->sf_count[sfmode] == 0) {
1452 ip_rt_multicast_event(pmc->interface);
1453 }
1454 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1455#ifdef CONFIG_IP_MULTICAST
1456 struct in_device *in_dev = pmc->interface;
1457#endif
1458
1459 /* no more filters for this source */
1460 if (psf_prev)
1461 psf_prev->sf_next = psf->sf_next;
1462 else
1463 pmc->sources = psf->sf_next;
1464#ifdef CONFIG_IP_MULTICAST
1465 if (psf->sf_oldin &&
1466 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001467 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 IGMP_Unsolicited_Report_Count;
1469 psf->sf_next = pmc->tomb;
1470 pmc->tomb = psf;
1471 rv = 1;
1472 } else
1473#endif
1474 kfree(psf);
1475 }
1476 return rv;
1477}
1478
1479#ifndef CONFIG_IP_MULTICAST
1480#define igmp_ifc_event(x) do { } while (0)
1481#endif
1482
Al Viro8f935bb2006-09-27 18:30:07 -07001483static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1484 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 struct ip_mc_list *pmc;
1487 int changerec = 0;
1488 int i, err;
1489
1490 if (!in_dev)
1491 return -ENODEV;
1492 read_lock(&in_dev->mc_list_lock);
1493 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1494 if (*pmca == pmc->multiaddr)
1495 break;
1496 }
1497 if (!pmc) {
1498 /* MCA not found?? bug */
1499 read_unlock(&in_dev->mc_list_lock);
1500 return -ESRCH;
1501 }
1502 spin_lock_bh(&pmc->lock);
1503 read_unlock(&in_dev->mc_list_lock);
1504#ifdef CONFIG_IP_MULTICAST
1505 sf_markstate(pmc);
1506#endif
1507 if (!delta) {
1508 err = -EINVAL;
1509 if (!pmc->sfcount[sfmode])
1510 goto out_unlock;
1511 pmc->sfcount[sfmode]--;
1512 }
1513 err = 0;
1514 for (i=0; i<sfcount; i++) {
1515 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1516
1517 changerec |= rv > 0;
1518 if (!err && rv < 0)
1519 err = rv;
1520 }
1521 if (pmc->sfmode == MCAST_EXCLUDE &&
1522 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1523 pmc->sfcount[MCAST_INCLUDE]) {
1524#ifdef CONFIG_IP_MULTICAST
1525 struct ip_sf_list *psf;
1526#endif
1527
1528 /* filter mode change */
1529 pmc->sfmode = MCAST_INCLUDE;
1530#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001531 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 IGMP_Unsolicited_Report_Count;
1533 in_dev->mr_ifc_count = pmc->crcount;
1534 for (psf=pmc->sources; psf; psf = psf->sf_next)
1535 psf->sf_crcount = 0;
1536 igmp_ifc_event(pmc->interface);
1537 } else if (sf_setstate(pmc) || changerec) {
1538 igmp_ifc_event(pmc->interface);
1539#endif
1540 }
1541out_unlock:
1542 spin_unlock_bh(&pmc->lock);
1543 return err;
1544}
1545
1546/*
1547 * Add multicast single-source filter to the interface list
1548 */
1549static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001550 __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 struct ip_sf_list *psf, *psf_prev;
1553
1554 psf_prev = NULL;
1555 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1556 if (psf->sf_inaddr == *psfsrc)
1557 break;
1558 psf_prev = psf;
1559 }
1560 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001561 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (!psf)
1563 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 psf->sf_inaddr = *psfsrc;
1565 if (psf_prev) {
1566 psf_prev->sf_next = psf;
1567 } else
1568 pmc->sources = psf;
1569 }
1570 psf->sf_count[sfmode]++;
1571 if (psf->sf_count[sfmode] == 1) {
1572 ip_rt_multicast_event(pmc->interface);
1573 }
1574 return 0;
1575}
1576
1577#ifdef CONFIG_IP_MULTICAST
1578static void sf_markstate(struct ip_mc_list *pmc)
1579{
1580 struct ip_sf_list *psf;
1581 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1582
1583 for (psf=pmc->sources; psf; psf=psf->sf_next)
1584 if (pmc->sfcount[MCAST_EXCLUDE]) {
1585 psf->sf_oldin = mca_xcount ==
1586 psf->sf_count[MCAST_EXCLUDE] &&
1587 !psf->sf_count[MCAST_INCLUDE];
1588 } else
1589 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1590}
1591
1592static int sf_setstate(struct ip_mc_list *pmc)
1593{
David L Stevensad125832006-01-18 14:20:56 -08001594 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1596 int qrv = pmc->interface->mr_qrv;
1597 int new_in, rv;
1598
1599 rv = 0;
1600 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1601 if (pmc->sfcount[MCAST_EXCLUDE]) {
1602 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1603 !psf->sf_count[MCAST_INCLUDE];
1604 } else
1605 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001606 if (new_in) {
1607 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001608 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001609
1610 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1611 if (dpsf->sf_inaddr == psf->sf_inaddr)
1612 break;
1613 prev = dpsf;
1614 }
1615 if (dpsf) {
1616 if (prev)
1617 prev->sf_next = dpsf->sf_next;
1618 else
1619 pmc->tomb = dpsf->sf_next;
1620 kfree(dpsf);
1621 }
1622 psf->sf_crcount = qrv;
1623 rv++;
1624 }
1625 } else if (psf->sf_oldin) {
1626
1627 psf->sf_crcount = 0;
1628 /*
1629 * add or update "delete" records if an active filter
1630 * is now inactive
1631 */
1632 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1633 if (dpsf->sf_inaddr == psf->sf_inaddr)
1634 break;
1635 if (!dpsf) {
1636 dpsf = (struct ip_sf_list *)
1637 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1638 if (!dpsf)
1639 continue;
1640 *dpsf = *psf;
1641 /* pmc->lock held by callers */
1642 dpsf->sf_next = pmc->tomb;
1643 pmc->tomb = dpsf;
1644 }
1645 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 rv++;
1647 }
1648 }
1649 return rv;
1650}
1651#endif
1652
1653/*
1654 * Add multicast source filter list to the interface list
1655 */
Al Viro8f935bb2006-09-27 18:30:07 -07001656static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1657 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct ip_mc_list *pmc;
1660 int isexclude;
1661 int i, err;
1662
1663 if (!in_dev)
1664 return -ENODEV;
1665 read_lock(&in_dev->mc_list_lock);
1666 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1667 if (*pmca == pmc->multiaddr)
1668 break;
1669 }
1670 if (!pmc) {
1671 /* MCA not found?? bug */
1672 read_unlock(&in_dev->mc_list_lock);
1673 return -ESRCH;
1674 }
1675 spin_lock_bh(&pmc->lock);
1676 read_unlock(&in_dev->mc_list_lock);
1677
1678#ifdef CONFIG_IP_MULTICAST
1679 sf_markstate(pmc);
1680#endif
1681 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1682 if (!delta)
1683 pmc->sfcount[sfmode]++;
1684 err = 0;
1685 for (i=0; i<sfcount; i++) {
1686 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1687 if (err)
1688 break;
1689 }
1690 if (err) {
1691 int j;
1692
1693 pmc->sfcount[sfmode]--;
1694 for (j=0; j<i; j++)
1695 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1696 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1697#ifdef CONFIG_IP_MULTICAST
1698 struct in_device *in_dev = pmc->interface;
1699 struct ip_sf_list *psf;
1700#endif
1701
1702 /* filter mode change */
1703 if (pmc->sfcount[MCAST_EXCLUDE])
1704 pmc->sfmode = MCAST_EXCLUDE;
1705 else if (pmc->sfcount[MCAST_INCLUDE])
1706 pmc->sfmode = MCAST_INCLUDE;
1707#ifdef CONFIG_IP_MULTICAST
1708 /* else no filters; keep old mode for reports */
1709
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001710 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 IGMP_Unsolicited_Report_Count;
1712 in_dev->mr_ifc_count = pmc->crcount;
1713 for (psf=pmc->sources; psf; psf = psf->sf_next)
1714 psf->sf_crcount = 0;
1715 igmp_ifc_event(in_dev);
1716 } else if (sf_setstate(pmc)) {
1717 igmp_ifc_event(in_dev);
1718#endif
1719 }
1720 spin_unlock_bh(&pmc->lock);
1721 return err;
1722}
1723
1724static void ip_mc_clear_src(struct ip_mc_list *pmc)
1725{
1726 struct ip_sf_list *psf, *nextpsf;
1727
1728 for (psf=pmc->tomb; psf; psf=nextpsf) {
1729 nextpsf = psf->sf_next;
1730 kfree(psf);
1731 }
1732 pmc->tomb = NULL;
1733 for (psf=pmc->sources; psf; psf=nextpsf) {
1734 nextpsf = psf->sf_next;
1735 kfree(psf);
1736 }
1737 pmc->sources = NULL;
1738 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001739 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 pmc->sfcount[MCAST_EXCLUDE] = 1;
1741}
1742
1743
1744/*
1745 * Join a multicast group
1746 */
1747int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1748{
1749 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001750 __be32 addr = imr->imr_multiaddr.s_addr;
David L Stevensca9b9072005-07-08 17:38:07 -07001751 struct ip_mc_socklist *iml=NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct in_device *in_dev;
1753 struct inet_sock *inet = inet_sk(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001754 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int count = 0;
1756
1757 if (!MULTICAST(addr))
1758 return -EINVAL;
1759
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001760 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 in_dev = ip_mc_find_dev(imr);
1763
1764 if (!in_dev) {
1765 iml = NULL;
1766 err = -ENODEV;
1767 goto done;
1768 }
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001771 ifindex = imr->imr_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 for (i = inet->mc_list; i; i = i->next) {
David L Stevensca9b9072005-07-08 17:38:07 -07001773 if (i->multi.imr_multiaddr.s_addr == addr &&
1774 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 count++;
1777 }
1778 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001779 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto done;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001781 iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001782 if (iml == NULL)
1783 goto done;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 memcpy(&iml->multi, imr, sizeof(*imr));
1786 iml->next = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 iml->sflist = NULL;
1788 iml->sfmode = MCAST_EXCLUDE;
1789 inet->mc_list = iml;
1790 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001793 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return err;
1795}
1796
1797static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1798 struct in_device *in_dev)
1799{
1800 int err;
1801
1802 if (iml->sflist == 0) {
1803 /* any-source empty exclude case */
1804 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1805 iml->sfmode, 0, NULL, 0);
1806 }
1807 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1808 iml->sfmode, iml->sflist->sl_count,
1809 iml->sflist->sl_addr, 0);
1810 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1811 iml->sflist = NULL;
1812 return err;
1813}
1814
1815/*
1816 * Ask a socket to leave a group.
1817 */
1818
1819int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1820{
1821 struct inet_sock *inet = inet_sk(sk);
1822 struct ip_mc_socklist *iml, **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001823 struct in_device *in_dev;
Al Viro8f935bb2006-09-27 18:30:07 -07001824 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001825 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001826 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 rtnl_lock();
David L Stevens84b42ba2005-07-08 17:48:38 -07001829 in_dev = ip_mc_find_dev(imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001830 ifindex = imr->imr_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
David L Stevensacd6e002006-08-17 16:27:39 -07001832 if (iml->multi.imr_multiaddr.s_addr != group)
1833 continue;
1834 if (ifindex) {
1835 if (iml->multi.imr_ifindex != ifindex)
1836 continue;
1837 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1838 iml->multi.imr_address.s_addr)
1839 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
David L Stevensacd6e002006-08-17 16:27:39 -07001841 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
David L Stevensacd6e002006-08-17 16:27:39 -07001843 *imlp = iml->next;
1844
1845 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001846 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001847 rtnl_unlock();
1848 sock_kfree_s(sk, iml, sizeof(*iml));
1849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
David L Stevensacd6e002006-08-17 16:27:39 -07001851 if (!in_dev)
1852 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001854 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857int ip_mc_source(int add, int omode, struct sock *sk, struct
1858 ip_mreq_source *mreqs, int ifindex)
1859{
1860 int err;
1861 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001862 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct ip_mc_socklist *pmc;
1864 struct in_device *in_dev = NULL;
1865 struct inet_sock *inet = inet_sk(sk);
1866 struct ip_sf_socklist *psl;
David L Stevens8cdaaa12005-07-08 17:39:23 -07001867 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 int i, j, rv;
1869
1870 if (!MULTICAST(addr))
1871 return -EINVAL;
1872
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001873 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1876 imr.imr_address.s_addr = mreqs->imr_interface;
1877 imr.imr_ifindex = ifindex;
1878 in_dev = ip_mc_find_dev(&imr);
1879
1880 if (!in_dev) {
1881 err = -ENODEV;
1882 goto done;
1883 }
1884 err = -EADDRNOTAVAIL;
1885
1886 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
David L Stevensca9b9072005-07-08 17:38:07 -07001887 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1888 && pmc->multi.imr_ifindex == imr.imr_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 break;
1890 }
David L Stevens917f2f12005-07-08 17:45:16 -07001891 if (!pmc) { /* must have a prior join */
1892 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /* if a source filter was set, must be the same mode as before */
1896 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07001897 if (pmc->sfmode != omode) {
1898 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 } else if (pmc->sfmode != omode) {
1902 /* allow mode switches for empty-set filters */
1903 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001904 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 NULL, 0);
1906 pmc->sfmode = omode;
1907 }
1908
1909 psl = pmc->sflist;
1910 if (!add) {
1911 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07001912 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 rv = !0;
1914 for (i=0; i<psl->sl_count; i++) {
1915 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001916 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (rv == 0)
1918 break;
1919 }
1920 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07001921 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
David L Stevens8cdaaa12005-07-08 17:39:23 -07001923 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1924 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1925 leavegroup = 1;
1926 goto done;
1927 }
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001930 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 &mreqs->imr_sourceaddr, 1);
1932
1933 for (j=i+1; j<psl->sl_count; j++)
1934 psl->sl_addr[j-1] = psl->sl_addr[j];
1935 psl->sl_count--;
1936 err = 0;
1937 goto done;
1938 }
1939 /* else, add a new source to the filter */
1940
1941 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1942 err = -ENOBUFS;
1943 goto done;
1944 }
1945 if (!psl || psl->sl_count == psl->sl_max) {
1946 struct ip_sf_socklist *newpsl;
1947 int count = IP_SFBLOCK;
1948
1949 if (psl)
1950 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001951 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (!newpsl) {
1953 err = -ENOBUFS;
1954 goto done;
1955 }
1956 newpsl->sl_max = count;
1957 newpsl->sl_count = count - IP_SFBLOCK;
1958 if (psl) {
1959 for (i=0; i<psl->sl_count; i++)
1960 newpsl->sl_addr[i] = psl->sl_addr[i];
1961 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1962 }
1963 pmc->sflist = psl = newpsl;
1964 }
1965 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1966 for (i=0; i<psl->sl_count; i++) {
1967 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001968 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (rv == 0)
1970 break;
1971 }
1972 if (rv == 0) /* address already there is an error */
1973 goto done;
1974 for (j=psl->sl_count-1; j>=i; j--)
1975 psl->sl_addr[j+1] = psl->sl_addr[j];
1976 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1977 psl->sl_count++;
1978 err = 0;
1979 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001980 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 &mreqs->imr_sourceaddr, 1);
1982done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001983 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07001984 if (leavegroup)
1985 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return err;
1987}
1988
1989int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1990{
David L Stevens9951f032005-07-08 17:47:28 -07001991 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001993 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct ip_mc_socklist *pmc;
1995 struct in_device *in_dev;
1996 struct inet_sock *inet = inet_sk(sk);
1997 struct ip_sf_socklist *newpsl, *psl;
David L Stevens9951f032005-07-08 17:47:28 -07001998 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 if (!MULTICAST(addr))
2001 return -EINVAL;
2002 if (msf->imsf_fmode != MCAST_INCLUDE &&
2003 msf->imsf_fmode != MCAST_EXCLUDE)
2004 return -EINVAL;
2005
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002006 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2009 imr.imr_address.s_addr = msf->imsf_interface;
2010 imr.imr_ifindex = ifindex;
2011 in_dev = ip_mc_find_dev(&imr);
2012
2013 if (!in_dev) {
2014 err = -ENODEV;
2015 goto done;
2016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
David L Stevens9951f032005-07-08 17:47:28 -07002018 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2019 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2020 leavegroup = 1;
2021 goto done;
2022 }
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2025 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2026 pmc->multi.imr_ifindex == imr.imr_ifindex)
2027 break;
2028 }
David L Stevens917f2f12005-07-08 17:45:16 -07002029 if (!pmc) { /* must have a prior join */
2030 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002034 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2035 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (!newpsl) {
2037 err = -ENOBUFS;
2038 goto done;
2039 }
2040 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2041 memcpy(newpsl->sl_addr, msf->imsf_slist,
2042 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2043 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2044 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2045 if (err) {
2046 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2047 goto done;
2048 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002049 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002051 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2052 msf->imsf_fmode, 0, NULL, 0);
2053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 psl = pmc->sflist;
2055 if (psl) {
2056 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2057 psl->sl_count, psl->sl_addr, 0);
2058 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2059 } else
2060 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2061 0, NULL, 0);
2062 pmc->sflist = newpsl;
2063 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002064 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002066 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002067 if (leavegroup)
2068 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return err;
2070}
2071
2072int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2073 struct ip_msfilter __user *optval, int __user *optlen)
2074{
2075 int err, len, count, copycount;
2076 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002077 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct ip_mc_socklist *pmc;
2079 struct in_device *in_dev;
2080 struct inet_sock *inet = inet_sk(sk);
2081 struct ip_sf_socklist *psl;
2082
2083 if (!MULTICAST(addr))
2084 return -EINVAL;
2085
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002086 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2089 imr.imr_address.s_addr = msf->imsf_interface;
2090 imr.imr_ifindex = 0;
2091 in_dev = ip_mc_find_dev(&imr);
2092
2093 if (!in_dev) {
2094 err = -ENODEV;
2095 goto done;
2096 }
2097 err = -EADDRNOTAVAIL;
2098
2099 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2100 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2101 pmc->multi.imr_ifindex == imr.imr_ifindex)
2102 break;
2103 }
2104 if (!pmc) /* must have a prior join */
2105 goto done;
2106 msf->imsf_fmode = pmc->sfmode;
2107 psl = pmc->sflist;
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002108 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (!psl) {
2110 len = 0;
2111 count = 0;
2112 } else {
2113 count = psl->sl_count;
2114 }
2115 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2116 len = copycount * sizeof(psl->sl_addr[0]);
2117 msf->imsf_numsrc = count;
2118 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2119 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2120 return -EFAULT;
2121 }
2122 if (len &&
2123 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2124 return -EFAULT;
2125 return 0;
2126done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002127 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return err;
2129}
2130
2131int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2132 struct group_filter __user *optval, int __user *optlen)
2133{
2134 int err, i, count, copycount;
2135 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002136 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct ip_mc_socklist *pmc;
2138 struct inet_sock *inet = inet_sk(sk);
2139 struct ip_sf_socklist *psl;
2140
2141 psin = (struct sockaddr_in *)&gsf->gf_group;
2142 if (psin->sin_family != AF_INET)
2143 return -EINVAL;
2144 addr = psin->sin_addr.s_addr;
2145 if (!MULTICAST(addr))
2146 return -EINVAL;
2147
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002148 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 err = -EADDRNOTAVAIL;
2151
2152 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2153 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2154 pmc->multi.imr_ifindex == gsf->gf_interface)
2155 break;
2156 }
2157 if (!pmc) /* must have a prior join */
2158 goto done;
2159 gsf->gf_fmode = pmc->sfmode;
2160 psl = pmc->sflist;
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002161 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 count = psl ? psl->sl_count : 0;
2163 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2164 gsf->gf_numsrc = count;
2165 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2166 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2167 return -EFAULT;
2168 }
2169 for (i=0; i<copycount; i++) {
2170 struct sockaddr_in *psin;
2171 struct sockaddr_storage ss;
2172
2173 psin = (struct sockaddr_in *)&ss;
2174 memset(&ss, 0, sizeof(ss));
2175 psin->sin_family = AF_INET;
2176 psin->sin_addr.s_addr = psl->sl_addr[i];
2177 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2178 return -EFAULT;
2179 }
2180 return 0;
2181done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002182 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return err;
2184}
2185
2186/*
2187 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2188 */
Al Viroc0cda062006-09-27 18:31:10 -07002189int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct inet_sock *inet = inet_sk(sk);
2192 struct ip_mc_socklist *pmc;
2193 struct ip_sf_socklist *psl;
2194 int i;
2195
2196 if (!MULTICAST(loc_addr))
2197 return 1;
2198
2199 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2200 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2201 pmc->multi.imr_ifindex == dif)
2202 break;
2203 }
2204 if (!pmc)
2205 return 1;
2206 psl = pmc->sflist;
2207 if (!psl)
2208 return pmc->sfmode == MCAST_EXCLUDE;
2209
2210 for (i=0; i<psl->sl_count; i++) {
2211 if (psl->sl_addr[i] == rmt_addr)
2212 break;
2213 }
2214 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2215 return 0;
2216 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2217 return 0;
2218 return 1;
2219}
2220
2221/*
2222 * A socket is closing.
2223 */
2224
2225void ip_mc_drop_socket(struct sock *sk)
2226{
2227 struct inet_sock *inet = inet_sk(sk);
2228 struct ip_mc_socklist *iml;
2229
2230 if (inet->mc_list == NULL)
2231 return;
2232
2233 rtnl_lock();
2234 while ((iml = inet->mc_list) != NULL) {
2235 struct in_device *in_dev;
2236 inet->mc_list = iml->next;
2237
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002238 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
2239 (void) ip_mc_leave_src(sk, iml, in_dev);
2240 if (in_dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2242 in_dev_put(in_dev);
2243 }
2244 sock_kfree_s(sk, iml, sizeof(*iml));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246 rtnl_unlock();
2247}
2248
Al Viroa60c4922006-09-26 21:28:34 -07002249int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 struct ip_mc_list *im;
2252 struct ip_sf_list *psf;
2253 int rv = 0;
2254
2255 read_lock(&in_dev->mc_list_lock);
2256 for (im=in_dev->mc_list; im; im=im->next) {
2257 if (im->multiaddr == mc_addr)
2258 break;
2259 }
2260 if (im && proto == IPPROTO_IGMP) {
2261 rv = 1;
2262 } else if (im) {
2263 if (src_addr) {
2264 for (psf=im->sources; psf; psf=psf->sf_next) {
2265 if (psf->sf_inaddr == src_addr)
2266 break;
2267 }
2268 if (psf)
2269 rv = psf->sf_count[MCAST_INCLUDE] ||
2270 psf->sf_count[MCAST_EXCLUDE] !=
2271 im->sfcount[MCAST_EXCLUDE];
2272 else
2273 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2274 } else
2275 rv = 1; /* unspecified source; tentatively allow */
2276 }
2277 read_unlock(&in_dev->mc_list_lock);
2278 return rv;
2279}
2280
2281#if defined(CONFIG_PROC_FS)
2282struct igmp_mc_iter_state {
2283 struct net_device *dev;
2284 struct in_device *in_dev;
2285};
2286
2287#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2288
2289static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2290{
2291 struct ip_mc_list *im = NULL;
2292 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2293
Pavel Emelianov7562f872007-05-03 15:13:45 -07002294 state->in_dev = NULL;
2295 for_each_netdev(state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 struct in_device *in_dev;
2297 in_dev = in_dev_get(state->dev);
2298 if (!in_dev)
2299 continue;
2300 read_lock(&in_dev->mc_list_lock);
2301 im = in_dev->mc_list;
2302 if (im) {
2303 state->in_dev = in_dev;
2304 break;
2305 }
2306 read_unlock(&in_dev->mc_list_lock);
2307 in_dev_put(in_dev);
2308 }
2309 return im;
2310}
2311
2312static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2313{
2314 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2315 im = im->next;
2316 while (!im) {
2317 if (likely(state->in_dev != NULL)) {
2318 read_unlock(&state->in_dev->mc_list_lock);
2319 in_dev_put(state->in_dev);
2320 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002321 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (!state->dev) {
2323 state->in_dev = NULL;
2324 break;
2325 }
2326 state->in_dev = in_dev_get(state->dev);
2327 if (!state->in_dev)
2328 continue;
2329 read_lock(&state->in_dev->mc_list_lock);
2330 im = state->in_dev->mc_list;
2331 }
2332 return im;
2333}
2334
2335static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2336{
2337 struct ip_mc_list *im = igmp_mc_get_first(seq);
2338 if (im)
2339 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2340 --pos;
2341 return pos ? NULL : im;
2342}
2343
2344static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2345{
2346 read_lock(&dev_base_lock);
2347 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2348}
2349
2350static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2351{
2352 struct ip_mc_list *im;
2353 if (v == SEQ_START_TOKEN)
2354 im = igmp_mc_get_first(seq);
2355 else
2356 im = igmp_mc_get_next(seq, v);
2357 ++*pos;
2358 return im;
2359}
2360
2361static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2362{
2363 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2364 if (likely(state->in_dev != NULL)) {
2365 read_unlock(&state->in_dev->mc_list_lock);
2366 in_dev_put(state->in_dev);
2367 state->in_dev = NULL;
2368 }
2369 state->dev = NULL;
2370 read_unlock(&dev_base_lock);
2371}
2372
2373static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2374{
2375 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002376 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2378 else {
2379 struct ip_mc_list *im = (struct ip_mc_list *)v;
2380 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2381 char *querier;
2382#ifdef CONFIG_IP_MULTICAST
2383 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2384 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2385 "V3";
2386#else
2387 querier = "NONE";
2388#endif
2389
2390 if (state->in_dev->mc_list == im) {
2391 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2392 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2393 }
2394
2395 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002396 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 im->multiaddr, im->users,
2398 im->tm_running, im->tm_running ?
2399 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2400 im->reporter);
2401 }
2402 return 0;
2403}
2404
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002405static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 .start = igmp_mc_seq_start,
2407 .next = igmp_mc_seq_next,
2408 .stop = igmp_mc_seq_stop,
2409 .show = igmp_mc_seq_show,
2410};
2411
2412static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2413{
2414 struct seq_file *seq;
2415 int rc = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002416 struct igmp_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 if (!s)
2419 goto out;
2420 rc = seq_open(file, &igmp_mc_seq_ops);
2421 if (rc)
2422 goto out_kfree;
2423
2424 seq = file->private_data;
2425 seq->private = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426out:
2427 return rc;
2428out_kfree:
2429 kfree(s);
2430 goto out;
2431}
2432
Arjan van de Ven9a321442007-02-12 00:55:35 -08002433static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 .owner = THIS_MODULE,
2435 .open = igmp_mc_seq_open,
2436 .read = seq_read,
2437 .llseek = seq_lseek,
2438 .release = seq_release_private,
2439};
2440
2441struct igmp_mcf_iter_state {
2442 struct net_device *dev;
2443 struct in_device *idev;
2444 struct ip_mc_list *im;
2445};
2446
2447#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2448
2449static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2450{
2451 struct ip_sf_list *psf = NULL;
2452 struct ip_mc_list *im = NULL;
2453 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2454
Pavel Emelianov7562f872007-05-03 15:13:45 -07002455 state->idev = NULL;
2456 state->im = NULL;
2457 for_each_netdev(state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 struct in_device *idev;
2459 idev = in_dev_get(state->dev);
2460 if (unlikely(idev == NULL))
2461 continue;
2462 read_lock(&idev->mc_list_lock);
2463 im = idev->mc_list;
2464 if (likely(im != NULL)) {
2465 spin_lock_bh(&im->lock);
2466 psf = im->sources;
2467 if (likely(psf != NULL)) {
2468 state->im = im;
2469 state->idev = idev;
2470 break;
2471 }
2472 spin_unlock_bh(&im->lock);
2473 }
2474 read_unlock(&idev->mc_list_lock);
2475 in_dev_put(idev);
2476 }
2477 return psf;
2478}
2479
2480static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2481{
2482 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2483
2484 psf = psf->sf_next;
2485 while (!psf) {
2486 spin_unlock_bh(&state->im->lock);
2487 state->im = state->im->next;
2488 while (!state->im) {
2489 if (likely(state->idev != NULL)) {
2490 read_unlock(&state->idev->mc_list_lock);
2491 in_dev_put(state->idev);
2492 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002493 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (!state->dev) {
2495 state->idev = NULL;
2496 goto out;
2497 }
2498 state->idev = in_dev_get(state->dev);
2499 if (!state->idev)
2500 continue;
2501 read_lock(&state->idev->mc_list_lock);
2502 state->im = state->idev->mc_list;
2503 }
2504 if (!state->im)
2505 break;
2506 spin_lock_bh(&state->im->lock);
2507 psf = state->im->sources;
2508 }
2509out:
2510 return psf;
2511}
2512
2513static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2514{
2515 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2516 if (psf)
2517 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2518 --pos;
2519 return pos ? NULL : psf;
2520}
2521
2522static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2523{
2524 read_lock(&dev_base_lock);
2525 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2526}
2527
2528static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2529{
2530 struct ip_sf_list *psf;
2531 if (v == SEQ_START_TOKEN)
2532 psf = igmp_mcf_get_first(seq);
2533 else
2534 psf = igmp_mcf_get_next(seq, v);
2535 ++*pos;
2536 return psf;
2537}
2538
2539static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2540{
2541 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2542 if (likely(state->im != NULL)) {
2543 spin_unlock_bh(&state->im->lock);
2544 state->im = NULL;
2545 }
2546 if (likely(state->idev != NULL)) {
2547 read_unlock(&state->idev->mc_list_lock);
2548 in_dev_put(state->idev);
2549 state->idev = NULL;
2550 }
2551 state->dev = NULL;
2552 read_unlock(&dev_base_lock);
2553}
2554
2555static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2556{
2557 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2558 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2559
2560 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002561 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 "%3s %6s "
2563 "%10s %10s %6s %6s\n", "Idx",
2564 "Device", "MCA",
2565 "SRC", "INC", "EXC");
2566 } else {
2567 seq_printf(seq,
2568 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002569 "0x%08x %6lu %6lu\n",
2570 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 ntohl(state->im->multiaddr),
2572 ntohl(psf->sf_inaddr),
2573 psf->sf_count[MCAST_INCLUDE],
2574 psf->sf_count[MCAST_EXCLUDE]);
2575 }
2576 return 0;
2577}
2578
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002579static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 .start = igmp_mcf_seq_start,
2581 .next = igmp_mcf_seq_next,
2582 .stop = igmp_mcf_seq_stop,
2583 .show = igmp_mcf_seq_show,
2584};
2585
2586static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2587{
2588 struct seq_file *seq;
2589 int rc = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002590 struct igmp_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 if (!s)
2593 goto out;
2594 rc = seq_open(file, &igmp_mcf_seq_ops);
2595 if (rc)
2596 goto out_kfree;
2597
2598 seq = file->private_data;
2599 seq->private = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600out:
2601 return rc;
2602out_kfree:
2603 kfree(s);
2604 goto out;
2605}
2606
Arjan van de Ven9a321442007-02-12 00:55:35 -08002607static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 .owner = THIS_MODULE,
2609 .open = igmp_mcf_seq_open,
2610 .read = seq_read,
2611 .llseek = seq_lseek,
2612 .release = seq_release_private,
2613};
2614
2615int __init igmp_mc_proc_init(void)
2616{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002617 proc_net_fops_create(&init_net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2618 proc_net_fops_create(&init_net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return 0;
2620}
2621#endif
2622
2623EXPORT_SYMBOL(ip_mc_dec_group);
2624EXPORT_SYMBOL(ip_mc_inc_group);
2625EXPORT_SYMBOL(ip_mc_join_group);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08002626EXPORT_SYMBOL(ip_mc_rejoin_group);