1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73#include <linux/module.h>
74#include <asm/uaccess.h>
75#include <asm/system.h>
76#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>
91
92#include <net/net_namespace.h>
93#include <net/arp.h>
94#include <net/ip.h>
95#include <net/protocol.h>
96#include <net/route.h>
97#include <net/sock.h>
98#include <net/checksum.h>
99#include <linux/netfilter_ipv4.h>
100#ifdef CONFIG_IP_MROUTE
101#include <linux/mroute.h>
102#endif
103#ifdef CONFIG_PROC_FS
104#include <linux/proc_fs.h>
105#include <linux/seq_file.h>
106#endif
107
108#define IP_MAX_MEMBERSHIPS 20
109#define IP_MAX_MSF 10
110
111#ifdef CONFIG_IP_MULTICAST
112
113
114#define IGMP_V1_Router_Present_Timeout (400*HZ)
115#define IGMP_V2_Router_Present_Timeout (400*HZ)
116#define IGMP_Unsolicited_Report_Interval (10*HZ)
117#define IGMP_Query_Response_Interval (10*HZ)
118#define IGMP_Unsolicited_Report_Count 2
119
120
121#define IGMP_Initial_Report_Delay (1)
122
123
124
125
126
127
128
129
130#define IGMP_V1_SEEN(in_dev) \
131 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
132 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
133 ((in_dev)->mr_v1_seen && \
134 time_before(jiffies, (in_dev)->mr_v1_seen)))
135#define IGMP_V2_SEEN(in_dev) \
136 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
137 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
140
141static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
142static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
143static void igmpv3_clear_delrec(struct in_device *in_dev);
144static int sf_setstate(struct ip_mc_list *pmc);
145static void sf_markstate(struct ip_mc_list *pmc);
146#endif
147static void ip_mc_clear_src(struct ip_mc_list *pmc);
148static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
149 int sfcount, __be32 *psfsrc, int delta);
150
151static void ip_ma_put(struct ip_mc_list *im)
152{
153 if (atomic_dec_and_test(&im->refcnt)) {
154 in_dev_put(im->interface);
155 kfree(im);
156 }
157}
158
159#ifdef CONFIG_IP_MULTICAST
160
161
162
163
164
165static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
166{
167 spin_lock_bh(&im->lock);
168 if (del_timer(&im->timer))
169 atomic_dec(&im->refcnt);
170 im->tm_running=0;
171 im->reporter = 0;
172 im->unsolicit_count = 0;
173 spin_unlock_bh(&im->lock);
174}
175
176
177static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
178{
179 int tv=net_random() % max_delay;
180
181 im->tm_running=1;
182 if (!mod_timer(&im->timer, jiffies+tv+2))
183 atomic_inc(&im->refcnt);
184}
185
186static void igmp_gq_start_timer(struct in_device *in_dev)
187{
188 int tv = net_random() % in_dev->mr_maxdelay;
189
190 in_dev->mr_gq_running = 1;
191 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
192 in_dev_hold(in_dev);
193}
194
195static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
196{
197 int tv = net_random() % delay;
198
199 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
200 in_dev_hold(in_dev);
201}
202
203static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
204{
205 spin_lock_bh(&im->lock);
206 im->unsolicit_count = 0;
207 if (del_timer(&im->timer)) {
208 if ((long)(im->timer.expires-jiffies) < max_delay) {
209 add_timer(&im->timer);
210 im->tm_running=1;
211 spin_unlock_bh(&im->lock);
212 return;
213 }
214 atomic_dec(&im->refcnt);
215 }
216 igmp_start_timer(im, max_delay);
217 spin_unlock_bh(&im->lock);
218}
219
220
221
222
223
224
225#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
226
227
228static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
229 int gdeleted, int sdeleted)
230{
231 switch (type) {
232 case IGMPV3_MODE_IS_INCLUDE:
233 case IGMPV3_MODE_IS_EXCLUDE:
234 if (gdeleted || sdeleted)
235 return 0;
236 if (!(pmc->gsquery && !psf->sf_gsresp)) {
237 if (pmc->sfmode == MCAST_INCLUDE)
238 return 1;
239
240
241
242 if (psf->sf_count[MCAST_INCLUDE])
243 return type == IGMPV3_MODE_IS_INCLUDE;
244 return pmc->sfcount[MCAST_EXCLUDE] ==
245 psf->sf_count[MCAST_EXCLUDE];
246 }
247 return 0;
248 case IGMPV3_CHANGE_TO_INCLUDE:
249 if (gdeleted || sdeleted)
250 return 0;
251 return psf->sf_count[MCAST_INCLUDE] != 0;
252 case IGMPV3_CHANGE_TO_EXCLUDE:
253 if (gdeleted || sdeleted)
254 return 0;
255 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
256 psf->sf_count[MCAST_INCLUDE])
257 return 0;
258 return pmc->sfcount[MCAST_EXCLUDE] ==
259 psf->sf_count[MCAST_EXCLUDE];
260 case IGMPV3_ALLOW_NEW_SOURCES:
261 if (gdeleted || !psf->sf_crcount)
262 return 0;
263 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
264 case IGMPV3_BLOCK_OLD_SOURCES:
265 if (pmc->sfmode == MCAST_INCLUDE)
266 return gdeleted || (psf->sf_crcount && sdeleted);
267 return psf->sf_crcount && !gdeleted && !sdeleted;
268 }
269 return 0;
270}
271
272static int
273igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
274{
275 struct ip_sf_list *psf;
276 int scount = 0;
277
278 for (psf=pmc->sources; psf; psf=psf->sf_next) {
279 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
280 continue;
281 scount++;
282 }
283 return scount;
284}
285
286static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
287{
288 struct sk_buff *skb;
289 struct rtable *rt;
290 struct iphdr *pip;
291 struct igmpv3_report *pig;
292 struct net *net = dev_net(dev);
293
294 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
295 if (skb == NULL)
296 return NULL;
297
298 {
299 struct flowi fl = { .oif = dev->ifindex,
300 .nl_u = { .ip4_u = {
301 .daddr = IGMPV3_ALL_MCR } },
302 .proto = IPPROTO_IGMP };
303 if (ip_route_output_key(net, &rt, &fl)) {
304 kfree_skb(skb);
305 return NULL;
306 }
307 }
308 if (rt->rt_src == 0) {
309 kfree_skb(skb);
310 ip_rt_put(rt);
311 return NULL;
312 }
313
314 skb->dst = &rt->u.dst;
315 skb->dev = dev;
316
317 skb_reserve(skb, LL_RESERVED_SPACE(dev));
318
319 skb_reset_network_header(skb);
320 pip = ip_hdr(skb);
321 skb_put(skb, sizeof(struct iphdr) + 4);
322
323 pip->version = 4;
324 pip->ihl = (sizeof(struct iphdr)+4)>>2;
325 pip->tos = 0xc0;
326 pip->frag_off = htons(IP_DF);
327 pip->ttl = 1;
328 pip->daddr = rt->rt_dst;
329 pip->saddr = rt->rt_src;
330 pip->protocol = IPPROTO_IGMP;
331 pip->tot_len = 0;
332 ip_select_ident(pip, &rt->u.dst, NULL);
333 ((u8*)&pip[1])[0] = IPOPT_RA;
334 ((u8*)&pip[1])[1] = 4;
335 ((u8*)&pip[1])[2] = 0;
336 ((u8*)&pip[1])[3] = 0;
337
338 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
339 skb_put(skb, sizeof(*pig));
340 pig = igmpv3_report_hdr(skb);
341 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
342 pig->resv1 = 0;
343 pig->csum = 0;
344 pig->resv2 = 0;
345 pig->ngrec = 0;
346 return skb;
347}
348
349static int igmpv3_sendpack(struct sk_buff *skb)
350{
351 struct igmphdr *pig = igmp_hdr(skb);
352 const int igmplen = skb->tail - skb->transport_header;
353
354 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
355
356 return ip_local_out(skb);
357}
358
359static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
360{
361 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
362}
363
364static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
365 int type, struct igmpv3_grec **ppgr)
366{
367 struct net_device *dev = pmc->interface->dev;
368 struct igmpv3_report *pih;
369 struct igmpv3_grec *pgr;
370
371 if (!skb)
372 skb = igmpv3_newpack(dev, dev->mtu);
373 if (!skb)
374 return NULL;
375 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
376 pgr->grec_type = type;
377 pgr->grec_auxwords = 0;
378 pgr->grec_nsrcs = 0;
379 pgr->grec_mca = pmc->multiaddr;
380 pih = igmpv3_report_hdr(skb);
381 pih->ngrec = htons(ntohs(pih->ngrec)+1);
382 *ppgr = pgr;
383 return skb;
384}
385
386#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
387 skb_tailroom(skb)) : 0)
388
389static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
390 int type, int gdeleted, int sdeleted)
391{
392 struct net_device *dev = pmc->interface->dev;
393 struct igmpv3_report *pih;
394 struct igmpv3_grec *pgr = NULL;
395 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
396 int scount, stotal, first, isquery, truncate;
397
398 if (pmc->multiaddr == IGMP_ALL_HOSTS)
399 return skb;
400
401 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
402 type == IGMPV3_MODE_IS_EXCLUDE;
403 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
404 type == IGMPV3_CHANGE_TO_EXCLUDE;
405
406 stotal = scount = 0;
407
408 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
409
410 if (!*psf_list)
411 goto empty_source;
412
413 pih = skb ? igmpv3_report_hdr(skb) : NULL;
414
415
416 if (truncate) {
417 if (pih && pih->ngrec &&
418 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
419 if (skb)
420 igmpv3_sendpack(skb);
421 skb = igmpv3_newpack(dev, dev->mtu);
422 }
423 }
424 first = 1;
425 psf_prev = NULL;
426 for (psf=*psf_list; psf; psf=psf_next) {
427 __be32 *psrc;
428
429 psf_next = psf->sf_next;
430
431 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
432 psf_prev = psf;
433 continue;
434 }
435
436
437 if (isquery)
438 psf->sf_gsresp = 0;
439
440 if (AVAILABLE(skb) < sizeof(__be32) +
441 first*sizeof(struct igmpv3_grec)) {
442 if (truncate && !first)
443 break;
444 if (pgr)
445 pgr->grec_nsrcs = htons(scount);
446 if (skb)
447 igmpv3_sendpack(skb);
448 skb = igmpv3_newpack(dev, dev->mtu);
449 first = 1;
450 scount = 0;
451 }
452 if (first) {
453 skb = add_grhead(skb, pmc, type, &pgr);
454 first = 0;
455 }
456 if (!skb)
457 return NULL;
458 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
459 *psrc = psf->sf_inaddr;
460 scount++; stotal++;
461 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
462 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
463 psf->sf_crcount--;
464 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
465 if (psf_prev)
466 psf_prev->sf_next = psf->sf_next;
467 else
468 *psf_list = psf->sf_next;
469 kfree(psf);
470 continue;
471 }
472 }
473 psf_prev = psf;
474 }
475
476empty_source:
477 if (!stotal) {
478 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
479 type == IGMPV3_BLOCK_OLD_SOURCES)
480 return skb;
481 if (pmc->crcount || isquery) {
482
483 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
484 igmpv3_sendpack(skb);
485 skb = NULL;
486 }
487 skb = add_grhead(skb, pmc, type, &pgr);
488 }
489 }
490 if (pgr)
491 pgr->grec_nsrcs = htons(scount);
492
493 if (isquery)
494 pmc->gsquery = 0;
495 return skb;
496}
497
498static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
499{
500 struct sk_buff *skb = NULL;
501 int type;
502
503 if (!pmc) {
504 read_lock(&in_dev->mc_list_lock);
505 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
506 if (pmc->multiaddr == IGMP_ALL_HOSTS)
507 continue;
508 spin_lock_bh(&pmc->lock);
509 if (pmc->sfcount[MCAST_EXCLUDE])
510 type = IGMPV3_MODE_IS_EXCLUDE;
511 else
512 type = IGMPV3_MODE_IS_INCLUDE;
513 skb = add_grec(skb, pmc, type, 0, 0);
514 spin_unlock_bh(&pmc->lock);
515 }
516 read_unlock(&in_dev->mc_list_lock);
517 } else {
518 spin_lock_bh(&pmc->lock);
519 if (pmc->sfcount[MCAST_EXCLUDE])
520 type = IGMPV3_MODE_IS_EXCLUDE;
521 else
522 type = IGMPV3_MODE_IS_INCLUDE;
523 skb = add_grec(skb, pmc, type, 0, 0);
524 spin_unlock_bh(&pmc->lock);
525 }
526 if (!skb)
527 return 0;
528 return igmpv3_sendpack(skb);
529}
530
531
532
533
534static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
535{
536 struct ip_sf_list *psf_prev, *psf_next, *psf;
537
538 psf_prev = NULL;
539 for (psf=*ppsf; psf; psf = psf_next) {
540 psf_next = psf->sf_next;
541 if (psf->sf_crcount == 0) {
542 if (psf_prev)
543 psf_prev->sf_next = psf->sf_next;
544 else
545 *ppsf = psf->sf_next;
546 kfree(psf);
547 } else
548 psf_prev = psf;
549 }
550}
551
552static void igmpv3_send_cr(struct in_device *in_dev)
553{
554 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
555 struct sk_buff *skb = NULL;
556 int type, dtype;
557
558 read_lock(&in_dev->mc_list_lock);
559 spin_lock_bh(&in_dev->mc_tomb_lock);
560
561
562 pmc_prev = NULL;
563 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
564 pmc_next = pmc->next;
565 if (pmc->sfmode == MCAST_INCLUDE) {
566 type = IGMPV3_BLOCK_OLD_SOURCES;
567 dtype = IGMPV3_BLOCK_OLD_SOURCES;
568 skb = add_grec(skb, pmc, type, 1, 0);
569 skb = add_grec(skb, pmc, dtype, 1, 1);
570 }
571 if (pmc->crcount) {
572 if (pmc->sfmode == MCAST_EXCLUDE) {
573 type = IGMPV3_CHANGE_TO_INCLUDE;
574 skb = add_grec(skb, pmc, type, 1, 0);
575 }
576 pmc->crcount--;
577 if (pmc->crcount == 0) {
578 igmpv3_clear_zeros(&pmc->tomb);
579 igmpv3_clear_zeros(&pmc->sources);
580 }
581 }
582 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
583 if (pmc_prev)
584 pmc_prev->next = pmc_next;
585 else
586 in_dev->mc_tomb = pmc_next;
587 in_dev_put(pmc->interface);
588 kfree(pmc);
589 } else
590 pmc_prev = pmc;
591 }
592 spin_unlock_bh(&in_dev->mc_tomb_lock);
593
594
595 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
596 spin_lock_bh(&pmc->lock);
597 if (pmc->sfcount[MCAST_EXCLUDE]) {
598 type = IGMPV3_BLOCK_OLD_SOURCES;
599 dtype = IGMPV3_ALLOW_NEW_SOURCES;
600 } else {
601 type = IGMPV3_ALLOW_NEW_SOURCES;
602 dtype = IGMPV3_BLOCK_OLD_SOURCES;
603 }
604 skb = add_grec(skb, pmc, type, 0, 0);
605 skb = add_grec(skb, pmc, dtype, 0, 1);
606
607
608 if (pmc->crcount) {
609 if (pmc->sfmode == MCAST_EXCLUDE)
610 type = IGMPV3_CHANGE_TO_EXCLUDE;
611 else
612 type = IGMPV3_CHANGE_TO_INCLUDE;
613 skb = add_grec(skb, pmc, type, 0, 0);
614 pmc->crcount--;
615 }
616 spin_unlock_bh(&pmc->lock);
617 }
618 read_unlock(&in_dev->mc_list_lock);
619
620 if (!skb)
621 return;
622 (void) igmpv3_sendpack(skb);
623}
624
625static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
626 int type)
627{
628 struct sk_buff *skb;
629 struct iphdr *iph;
630 struct igmphdr *ih;
631 struct rtable *rt;
632 struct net_device *dev = in_dev->dev;
633 struct net *net = dev_net(dev);
634 __be32 group = pmc ? pmc->multiaddr : 0;
635 __be32 dst;
636
637 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
638 return igmpv3_send_report(in_dev, pmc);
639 else if (type == IGMP_HOST_LEAVE_MESSAGE)
640 dst = IGMP_ALL_ROUTER;
641 else
642 dst = group;
643
644 {
645 struct flowi fl = { .oif = dev->ifindex,
646 .nl_u = { .ip4_u = { .daddr = dst } },
647 .proto = IPPROTO_IGMP };
648 if (ip_route_output_key(net, &rt, &fl))
649 return -1;
650 }
651 if (rt->rt_src == 0) {
652 ip_rt_put(rt);
653 return -1;
654 }
655
656 skb=alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
657 if (skb == NULL) {
658 ip_rt_put(rt);
659 return -1;
660 }
661
662 skb->dst = &rt->u.dst;
663
664 skb_reserve(skb, LL_RESERVED_SPACE(dev));
665
666 skb_reset_network_header(skb);
667 iph = ip_hdr(skb);
668 skb_put(skb, sizeof(struct iphdr) + 4);
669
670 iph->version = 4;
671 iph->ihl = (sizeof(struct iphdr)+4)>>2;
672 iph->tos = 0xc0;
673 iph->frag_off = htons(IP_DF);
674 iph->ttl = 1;
675 iph->daddr = dst;
676 iph->saddr = rt->rt_src;
677 iph->protocol = IPPROTO_IGMP;
678 ip_select_ident(iph, &rt->u.dst, NULL);
679 ((u8*)&iph[1])[0] = IPOPT_RA;
680 ((u8*)&iph[1])[1] = 4;
681 ((u8*)&iph[1])[2] = 0;
682 ((u8*)&iph[1])[3] = 0;
683
684 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
685 ih->type=type;
686 ih->code=0;
687 ih->csum=0;
688 ih->group=group;
689 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
690
691 return ip_local_out(skb);
692}
693
694static void igmp_gq_timer_expire(unsigned long data)
695{
696 struct in_device *in_dev = (struct in_device *)data;
697
698 in_dev->mr_gq_running = 0;
699 igmpv3_send_report(in_dev, NULL);
700 __in_dev_put(in_dev);
701}
702
703static void igmp_ifc_timer_expire(unsigned long data)
704{
705 struct in_device *in_dev = (struct in_device *)data;
706
707 igmpv3_send_cr(in_dev);
708 if (in_dev->mr_ifc_count) {
709 in_dev->mr_ifc_count--;
710 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
711 }
712 __in_dev_put(in_dev);
713}
714
715static void igmp_ifc_event(struct in_device *in_dev)
716{
717 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
718 return;
719 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
720 IGMP_Unsolicited_Report_Count;
721 igmp_ifc_start_timer(in_dev, 1);
722}
723
724
725static void igmp_timer_expire(unsigned long data)
726{
727 struct ip_mc_list *im=(struct ip_mc_list *)data;
728 struct in_device *in_dev = im->interface;
729
730 spin_lock(&im->lock);
731 im->tm_running=0;
732
733 if (im->unsolicit_count) {
734 im->unsolicit_count--;
735 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
736 }
737 im->reporter = 1;
738 spin_unlock(&im->lock);
739
740 if (IGMP_V1_SEEN(in_dev))
741 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
742 else if (IGMP_V2_SEEN(in_dev))
743 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
744 else
745 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
746
747 ip_ma_put(im);
748}
749
750
751static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
752{
753 struct ip_sf_list *psf;
754 int i, scount;
755
756 scount = 0;
757 for (psf=pmc->sources; psf; psf=psf->sf_next) {
758 if (scount == nsrcs)
759 break;
760 for (i=0; i<nsrcs; i++) {
761
762 if (pmc->sfcount[MCAST_INCLUDE] ||
763 pmc->sfcount[MCAST_EXCLUDE] !=
764 psf->sf_count[MCAST_EXCLUDE])
765 continue;
766 if (srcs[i] == psf->sf_inaddr) {
767 scount++;
768 break;
769 }
770 }
771 }
772 pmc->gsquery = 0;
773 if (scount == nsrcs)
774 return 0;
775 return 1;
776}
777
778static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
779{
780 struct ip_sf_list *psf;
781 int i, scount;
782
783 if (pmc->sfmode == MCAST_EXCLUDE)
784 return igmp_xmarksources(pmc, nsrcs, srcs);
785
786
787 scount = 0;
788 for (psf=pmc->sources; psf; psf=psf->sf_next) {
789 if (scount == nsrcs)
790 break;
791 for (i=0; i<nsrcs; i++)
792 if (srcs[i] == psf->sf_inaddr) {
793 psf->sf_gsresp = 1;
794 scount++;
795 break;
796 }
797 }
798 if (!scount) {
799 pmc->gsquery = 0;
800 return 0;
801 }
802 pmc->gsquery = 1;
803 return 1;
804}
805
806static void igmp_heard_report(struct in_device *in_dev, __be32 group)
807{
808 struct ip_mc_list *im;
809
810
811
812 if (group == IGMP_ALL_HOSTS)
813 return;
814
815 read_lock(&in_dev->mc_list_lock);
816 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
817 if (im->multiaddr == group) {
818 igmp_stop_timer(im);
819 break;
820 }
821 }
822 read_unlock(&in_dev->mc_list_lock);
823}
824
825static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
826 int len)
827{
828 struct igmphdr *ih = igmp_hdr(skb);
829 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
830 struct ip_mc_list *im;
831 __be32 group = ih->group;
832 int max_delay;
833 int mark = 0;
834
835
836 if (len == 8) {
837 if (ih->code == 0) {
838
839
840 max_delay = IGMP_Query_Response_Interval;
841 in_dev->mr_v1_seen = jiffies +
842 IGMP_V1_Router_Present_Timeout;
843 group = 0;
844 } else {
845
846 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
847 in_dev->mr_v2_seen = jiffies +
848 IGMP_V2_Router_Present_Timeout;
849 }
850
851 in_dev->mr_ifc_count = 0;
852 if (del_timer(&in_dev->mr_ifc_timer))
853 __in_dev_put(in_dev);
854
855 igmpv3_clear_delrec(in_dev);
856 } else if (len < 12) {
857 return;
858 } else {
859 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
860 return;
861
862 ih3 = igmpv3_query_hdr(skb);
863 if (ih3->nsrcs) {
864 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
865 + ntohs(ih3->nsrcs)*sizeof(__be32)))
866 return;
867 ih3 = igmpv3_query_hdr(skb);
868 }
869
870 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
871 if (!max_delay)
872 max_delay = 1;
873 in_dev->mr_maxdelay = max_delay;
874 if (ih3->qrv)
875 in_dev->mr_qrv = ih3->qrv;
876 if (!group) {
877 if (ih3->nsrcs)
878 return;
879 igmp_gq_start_timer(in_dev);
880 return;
881 }
882
883 mark = ih3->nsrcs != 0;
884 }
885
886
887
888
889
890
891
892
893
894
895
896 read_lock(&in_dev->mc_list_lock);
897 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
898 int changed;
899
900 if (group && group != im->multiaddr)
901 continue;
902 if (im->multiaddr == IGMP_ALL_HOSTS)
903 continue;
904 spin_lock_bh(&im->lock);
905 if (im->tm_running)
906 im->gsquery = im->gsquery && mark;
907 else
908 im->gsquery = mark;
909 changed = !im->gsquery ||
910 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
911 spin_unlock_bh(&im->lock);
912 if (changed)
913 igmp_mod_timer(im, max_delay);
914 }
915 read_unlock(&in_dev->mc_list_lock);
916}
917
918int igmp_rcv(struct sk_buff *skb)
919{
920
921 struct igmphdr *ih;
922 struct in_device *in_dev = in_dev_get(skb->dev);
923 int len = skb->len;
924
925 if (in_dev == NULL)
926 goto drop;
927
928 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
929 goto drop_ref;
930
931 switch (skb->ip_summed) {
932 case CHECKSUM_COMPLETE:
933 if (!csum_fold(skb->csum))
934 break;
935
936 case CHECKSUM_NONE:
937 skb->csum = 0;
938 if (__skb_checksum_complete(skb))
939 goto drop_ref;
940 }
941
942 ih = igmp_hdr(skb);
943 switch (ih->type) {
944 case IGMP_HOST_MEMBERSHIP_QUERY:
945 igmp_heard_query(in_dev, skb, len);
946 break;
947 case IGMP_HOST_MEMBERSHIP_REPORT:
948 case IGMPV2_HOST_MEMBERSHIP_REPORT:
949 case IGMPV3_HOST_MEMBERSHIP_REPORT:
950
951 if (skb->rtable->fl.iif == 0)
952 break;
953
954 if (skb->pkt_type == PACKET_MULTICAST ||
955 skb->pkt_type == PACKET_BROADCAST)
956 igmp_heard_report(in_dev, ih->group);
957 break;
958 case IGMP_PIM:
959#ifdef CONFIG_IP_PIMSM_V1
960 in_dev_put(in_dev);
961 return pim_rcv_v1(skb);
962#endif
963 case IGMP_DVMRP:
964 case IGMP_TRACE:
965 case IGMP_HOST_LEAVE_MESSAGE:
966 case IGMP_MTRACE:
967 case IGMP_MTRACE_RESP:
968 break;
969 default:
970 break;
971 }
972
973drop_ref:
974 in_dev_put(in_dev);
975drop:
976 kfree_skb(skb);
977 return 0;
978}
979
980#endif
981
982
983
984
985
986
987static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
988{
989 char buf[MAX_ADDR_LEN];
990 struct net_device *dev = in_dev->dev;
991
992
993
994
995
996
997
998
999 if (arp_mc_map(addr, buf, dev, 0) == 0)
1000 dev_mc_add(dev,buf,dev->addr_len,0);
1001}
1002
1003
1004
1005
1006
1007static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1008{
1009 char buf[MAX_ADDR_LEN];
1010 struct net_device *dev = in_dev->dev;
1011
1012 if (arp_mc_map(addr, buf, dev, 0) == 0)
1013 dev_mc_delete(dev,buf,dev->addr_len,0);
1014}
1015
1016#ifdef CONFIG_IP_MULTICAST
1017
1018
1019
1020static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1021{
1022 struct ip_mc_list *pmc;
1023
1024
1025
1026
1027
1028
1029
1030 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1031 if (!pmc)
1032 return;
1033 spin_lock_bh(&im->lock);
1034 pmc->interface = im->interface;
1035 in_dev_hold(in_dev);
1036 pmc->multiaddr = im->multiaddr;
1037 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1038 IGMP_Unsolicited_Report_Count;
1039 pmc->sfmode = im->sfmode;
1040 if (pmc->sfmode == MCAST_INCLUDE) {
1041 struct ip_sf_list *psf;
1042
1043 pmc->tomb = im->tomb;
1044 pmc->sources = im->sources;
1045 im->tomb = im->sources = NULL;
1046 for (psf=pmc->sources; psf; psf=psf->sf_next)
1047 psf->sf_crcount = pmc->crcount;
1048 }
1049 spin_unlock_bh(&im->lock);
1050
1051 spin_lock_bh(&in_dev->mc_tomb_lock);
1052 pmc->next = in_dev->mc_tomb;
1053 in_dev->mc_tomb = pmc;
1054 spin_unlock_bh(&in_dev->mc_tomb_lock);
1055}
1056
1057static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1058{
1059 struct ip_mc_list *pmc, *pmc_prev;
1060 struct ip_sf_list *psf, *psf_next;
1061
1062 spin_lock_bh(&in_dev->mc_tomb_lock);
1063 pmc_prev = NULL;
1064 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1065 if (pmc->multiaddr == multiaddr)
1066 break;
1067 pmc_prev = pmc;
1068 }
1069 if (pmc) {
1070 if (pmc_prev)
1071 pmc_prev->next = pmc->next;
1072 else
1073 in_dev->mc_tomb = pmc->next;
1074 }
1075 spin_unlock_bh(&in_dev->mc_tomb_lock);
1076 if (pmc) {
1077 for (psf=pmc->tomb; psf; psf=psf_next) {
1078 psf_next = psf->sf_next;
1079 kfree(psf);
1080 }
1081 in_dev_put(pmc->interface);
1082 kfree(pmc);
1083 }
1084}
1085
1086static void igmpv3_clear_delrec(struct in_device *in_dev)
1087{
1088 struct ip_mc_list *pmc, *nextpmc;
1089
1090 spin_lock_bh(&in_dev->mc_tomb_lock);
1091 pmc = in_dev->mc_tomb;
1092 in_dev->mc_tomb = NULL;
1093 spin_unlock_bh(&in_dev->mc_tomb_lock);
1094
1095 for (; pmc; pmc = nextpmc) {
1096 nextpmc = pmc->next;
1097 ip_mc_clear_src(pmc);
1098 in_dev_put(pmc->interface);
1099 kfree(pmc);
1100 }
1101
1102 read_lock(&in_dev->mc_list_lock);
1103 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1104 struct ip_sf_list *psf, *psf_next;
1105
1106 spin_lock_bh(&pmc->lock);
1107 psf = pmc->tomb;
1108 pmc->tomb = NULL;
1109 spin_unlock_bh(&pmc->lock);
1110 for (; psf; psf=psf_next) {
1111 psf_next = psf->sf_next;
1112 kfree(psf);
1113 }
1114 }
1115 read_unlock(&in_dev->mc_list_lock);
1116}
1117#endif
1118
1119static void igmp_group_dropped(struct ip_mc_list *im)
1120{
1121 struct in_device *in_dev = im->interface;
1122#ifdef CONFIG_IP_MULTICAST
1123 int reporter;
1124#endif
1125
1126 if (im->loaded) {
1127 im->loaded = 0;
1128 ip_mc_filter_del(in_dev, im->multiaddr);
1129 }
1130
1131#ifdef CONFIG_IP_MULTICAST
1132 if (im->multiaddr == IGMP_ALL_HOSTS)
1133 return;
1134
1135 reporter = im->reporter;
1136 igmp_stop_timer(im);
1137
1138 if (!in_dev->dead) {
1139 if (IGMP_V1_SEEN(in_dev))
1140 goto done;
1141 if (IGMP_V2_SEEN(in_dev)) {
1142 if (reporter)
1143 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1144 goto done;
1145 }
1146
1147 igmpv3_add_delrec(in_dev, im);
1148
1149 igmp_ifc_event(in_dev);
1150 }
1151done:
1152#endif
1153 ip_mc_clear_src(im);
1154}
1155
1156static void igmp_group_added(struct ip_mc_list *im)
1157{
1158 struct in_device *in_dev = im->interface;
1159
1160 if (im->loaded == 0) {
1161 im->loaded = 1;
1162 ip_mc_filter_add(in_dev, im->multiaddr);
1163 }
1164
1165#ifdef CONFIG_IP_MULTICAST
1166 if (im->multiaddr == IGMP_ALL_HOSTS)
1167 return;
1168
1169 if (in_dev->dead)
1170 return;
1171 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1172 spin_lock_bh(&im->lock);
1173 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1174 spin_unlock_bh(&im->lock);
1175 return;
1176 }
1177
1178
1179 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1180 IGMP_Unsolicited_Report_Count;
1181 igmp_ifc_event(in_dev);
1182#endif
1183}
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1196{
1197 struct ip_mc_list *im;
1198
1199 ASSERT_RTNL();
1200
1201 for (im=in_dev->mc_list; im; im=im->next) {
1202 if (im->multiaddr == addr) {
1203 im->users++;
1204 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1205 goto out;
1206 }
1207 }
1208
1209 im = kmalloc(sizeof(*im), GFP_KERNEL);
1210 if (!im)
1211 goto out;
1212
1213 im->users=1;
1214 im->interface=in_dev;
1215 in_dev_hold(in_dev);
1216 im->multiaddr=addr;
1217
1218 im->sfmode = MCAST_EXCLUDE;
1219 im->sfcount[MCAST_INCLUDE] = 0;
1220 im->sfcount[MCAST_EXCLUDE] = 1;
1221 im->sources = NULL;
1222 im->tomb = NULL;
1223 im->crcount = 0;
1224 atomic_set(&im->refcnt, 1);
1225 spin_lock_init(&im->lock);
1226#ifdef CONFIG_IP_MULTICAST
1227 im->tm_running=0;
1228 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1229 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1230 im->reporter = 0;
1231 im->gsquery = 0;
1232#endif
1233 im->loaded = 0;
1234 write_lock_bh(&in_dev->mc_list_lock);
1235 im->next=in_dev->mc_list;
1236 in_dev->mc_list=im;
1237 in_dev->mc_count++;
1238 write_unlock_bh(&in_dev->mc_list_lock);
1239#ifdef CONFIG_IP_MULTICAST
1240 igmpv3_del_delrec(in_dev, im->multiaddr);
1241#endif
1242 igmp_group_added(im);
1243 if (!in_dev->dead)
1244 ip_rt_multicast_event(in_dev);
1245out:
1246 return;
1247}
1248
1249
1250
1251
1252void ip_mc_rejoin_group(struct ip_mc_list *im)
1253{
1254#ifdef CONFIG_IP_MULTICAST
1255 struct in_device *in_dev = im->interface;
1256
1257 if (im->multiaddr == IGMP_ALL_HOSTS)
1258 return;
1259
1260 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1261 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1262 return;
1263 }
1264
1265 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1266 IGMP_Unsolicited_Report_Count;
1267 igmp_ifc_event(in_dev);
1268#endif
1269}
1270
1271
1272
1273
1274
1275void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1276{
1277 struct ip_mc_list *i, **ip;
1278
1279 ASSERT_RTNL();
1280
1281 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1282 if (i->multiaddr==addr) {
1283 if (--i->users == 0) {
1284 write_lock_bh(&in_dev->mc_list_lock);
1285 *ip = i->next;
1286 in_dev->mc_count--;
1287 write_unlock_bh(&in_dev->mc_list_lock);
1288 igmp_group_dropped(i);
1289
1290 if (!in_dev->dead)
1291 ip_rt_multicast_event(in_dev);
1292
1293 ip_ma_put(i);
1294 return;
1295 }
1296 break;
1297 }
1298 }
1299}
1300
1301
1302
1303void ip_mc_down(struct in_device *in_dev)
1304{
1305 struct ip_mc_list *i;
1306
1307 ASSERT_RTNL();
1308
1309 for (i=in_dev->mc_list; i; i=i->next)
1310 igmp_group_dropped(i);
1311
1312#ifdef CONFIG_IP_MULTICAST
1313 in_dev->mr_ifc_count = 0;
1314 if (del_timer(&in_dev->mr_ifc_timer))
1315 __in_dev_put(in_dev);
1316 in_dev->mr_gq_running = 0;
1317 if (del_timer(&in_dev->mr_gq_timer))
1318 __in_dev_put(in_dev);
1319 igmpv3_clear_delrec(in_dev);
1320#endif
1321
1322 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1323}
1324
1325void ip_mc_init_dev(struct in_device *in_dev)
1326{
1327 ASSERT_RTNL();
1328
1329 in_dev->mc_tomb = NULL;
1330#ifdef CONFIG_IP_MULTICAST
1331 in_dev->mr_gq_running = 0;
1332 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1333 (unsigned long)in_dev);
1334 in_dev->mr_ifc_count = 0;
1335 in_dev->mc_count = 0;
1336 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1337 (unsigned long)in_dev);
1338 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1339#endif
1340
1341 rwlock_init(&in_dev->mc_list_lock);
1342 spin_lock_init(&in_dev->mc_tomb_lock);
1343}
1344
1345
1346
1347void ip_mc_up(struct in_device *in_dev)
1348{
1349 struct ip_mc_list *i;
1350
1351 ASSERT_RTNL();
1352
1353 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1354
1355 for (i=in_dev->mc_list; i; i=i->next)
1356 igmp_group_added(i);
1357}
1358
1359
1360
1361
1362
1363void ip_mc_destroy_dev(struct in_device *in_dev)
1364{
1365 struct ip_mc_list *i;
1366
1367 ASSERT_RTNL();
1368
1369
1370 ip_mc_down(in_dev);
1371
1372 write_lock_bh(&in_dev->mc_list_lock);
1373 while ((i = in_dev->mc_list) != NULL) {
1374 in_dev->mc_list = i->next;
1375 in_dev->mc_count--;
1376 write_unlock_bh(&in_dev->mc_list_lock);
1377 igmp_group_dropped(i);
1378 ip_ma_put(i);
1379
1380 write_lock_bh(&in_dev->mc_list_lock);
1381 }
1382 write_unlock_bh(&in_dev->mc_list_lock);
1383}
1384
1385static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1386{
1387 struct flowi fl = { .nl_u = { .ip4_u =
1388 { .daddr = imr->imr_multiaddr.s_addr } } };
1389 struct rtable *rt;
1390 struct net_device *dev = NULL;
1391 struct in_device *idev = NULL;
1392
1393 if (imr->imr_ifindex) {
1394 idev = inetdev_by_index(net, imr->imr_ifindex);
1395 if (idev)
1396 __in_dev_put(idev);
1397 return idev;
1398 }
1399 if (imr->imr_address.s_addr) {
1400 dev = ip_dev_find(net, imr->imr_address.s_addr);
1401 if (!dev)
1402 return NULL;
1403 dev_put(dev);
1404 }
1405
1406 if (!dev && !ip_route_output_key(net, &rt, &fl)) {
1407 dev = rt->u.dst.dev;
1408 ip_rt_put(rt);
1409 }
1410 if (dev) {
1411 imr->imr_ifindex = dev->ifindex;
1412 idev = __in_dev_get_rtnl(dev);
1413 }
1414 return idev;
1415}
1416
1417
1418
1419
1420int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1421int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1422
1423
1424static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1425 __be32 *psfsrc)
1426{
1427 struct ip_sf_list *psf, *psf_prev;
1428 int rv = 0;
1429
1430 psf_prev = NULL;
1431 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1432 if (psf->sf_inaddr == *psfsrc)
1433 break;
1434 psf_prev = psf;
1435 }
1436 if (!psf || psf->sf_count[sfmode] == 0) {
1437
1438 return -ESRCH;
1439 }
1440 psf->sf_count[sfmode]--;
1441 if (psf->sf_count[sfmode] == 0) {
1442 ip_rt_multicast_event(pmc->interface);
1443 }
1444 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1445#ifdef CONFIG_IP_MULTICAST
1446 struct in_device *in_dev = pmc->interface;
1447#endif
1448
1449
1450 if (psf_prev)
1451 psf_prev->sf_next = psf->sf_next;
1452 else
1453 pmc->sources = psf->sf_next;
1454#ifdef CONFIG_IP_MULTICAST
1455 if (psf->sf_oldin &&
1456 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1457 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1458 IGMP_Unsolicited_Report_Count;
1459 psf->sf_next = pmc->tomb;
1460 pmc->tomb = psf;
1461 rv = 1;
1462 } else
1463#endif
1464 kfree(psf);
1465 }
1466 return rv;
1467}
1468
1469#ifndef CONFIG_IP_MULTICAST
1470#define igmp_ifc_event(x) do { } while (0)
1471#endif
1472
1473static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1474 int sfcount, __be32 *psfsrc, int delta)
1475{
1476 struct ip_mc_list *pmc;
1477 int changerec = 0;
1478 int i, err;
1479
1480 if (!in_dev)
1481 return -ENODEV;
1482 read_lock(&in_dev->mc_list_lock);
1483 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1484 if (*pmca == pmc->multiaddr)
1485 break;
1486 }
1487 if (!pmc) {
1488
1489 read_unlock(&in_dev->mc_list_lock);
1490 return -ESRCH;
1491 }
1492 spin_lock_bh(&pmc->lock);
1493 read_unlock(&in_dev->mc_list_lock);
1494#ifdef CONFIG_IP_MULTICAST
1495 sf_markstate(pmc);
1496#endif
1497 if (!delta) {
1498 err = -EINVAL;
1499 if (!pmc->sfcount[sfmode])
1500 goto out_unlock;
1501 pmc->sfcount[sfmode]--;
1502 }
1503 err = 0;
1504 for (i=0; i<sfcount; i++) {
1505 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1506
1507 changerec |= rv > 0;
1508 if (!err && rv < 0)
1509 err = rv;
1510 }
1511 if (pmc->sfmode == MCAST_EXCLUDE &&
1512 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1513 pmc->sfcount[MCAST_INCLUDE]) {
1514#ifdef CONFIG_IP_MULTICAST
1515 struct ip_sf_list *psf;
1516#endif
1517
1518
1519 pmc->sfmode = MCAST_INCLUDE;
1520#ifdef CONFIG_IP_MULTICAST
1521 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1522 IGMP_Unsolicited_Report_Count;
1523 in_dev->mr_ifc_count = pmc->crcount;
1524 for (psf=pmc->sources; psf; psf = psf->sf_next)
1525 psf->sf_crcount = 0;
1526 igmp_ifc_event(pmc->interface);
1527 } else if (sf_setstate(pmc) || changerec) {
1528 igmp_ifc_event(pmc->interface);
1529#endif
1530 }
1531out_unlock:
1532 spin_unlock_bh(&pmc->lock);
1533 return err;
1534}
1535
1536
1537
1538
1539static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1540 __be32 *psfsrc, int delta)
1541{
1542 struct ip_sf_list *psf, *psf_prev;
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) {
1551 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1552 if (!psf)
1553 return -ENOBUFS;
1554 psf->sf_inaddr = *psfsrc;
1555 if (psf_prev) {
1556 psf_prev->sf_next = psf;
1557 } else
1558 pmc->sources = psf;
1559 }
1560 psf->sf_count[sfmode]++;
1561 if (psf->sf_count[sfmode] == 1) {
1562 ip_rt_multicast_event(pmc->interface);
1563 }
1564 return 0;
1565}
1566
1567#ifdef CONFIG_IP_MULTICAST
1568static void sf_markstate(struct ip_mc_list *pmc)
1569{
1570 struct ip_sf_list *psf;
1571 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1572
1573 for (psf=pmc->sources; psf; psf=psf->sf_next)
1574 if (pmc->sfcount[MCAST_EXCLUDE]) {
1575 psf->sf_oldin = mca_xcount ==
1576 psf->sf_count[MCAST_EXCLUDE] &&
1577 !psf->sf_count[MCAST_INCLUDE];
1578 } else
1579 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1580}
1581
1582static int sf_setstate(struct ip_mc_list *pmc)
1583{
1584 struct ip_sf_list *psf, *dpsf;
1585 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1586 int qrv = pmc->interface->mr_qrv;
1587 int new_in, rv;
1588
1589 rv = 0;
1590 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1591 if (pmc->sfcount[MCAST_EXCLUDE]) {
1592 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1593 !psf->sf_count[MCAST_INCLUDE];
1594 } else
1595 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1596 if (new_in) {
1597 if (!psf->sf_oldin) {
1598 struct ip_sf_list *prev = NULL;
1599
1600 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1601 if (dpsf->sf_inaddr == psf->sf_inaddr)
1602 break;
1603 prev = dpsf;
1604 }
1605 if (dpsf) {
1606 if (prev)
1607 prev->sf_next = dpsf->sf_next;
1608 else
1609 pmc->tomb = dpsf->sf_next;
1610 kfree(dpsf);
1611 }
1612 psf->sf_crcount = qrv;
1613 rv++;
1614 }
1615 } else if (psf->sf_oldin) {
1616
1617 psf->sf_crcount = 0;
1618
1619
1620
1621
1622 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1623 if (dpsf->sf_inaddr == psf->sf_inaddr)
1624 break;
1625 if (!dpsf) {
1626 dpsf = (struct ip_sf_list *)
1627 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1628 if (!dpsf)
1629 continue;
1630 *dpsf = *psf;
1631
1632 dpsf->sf_next = pmc->tomb;
1633 pmc->tomb = dpsf;
1634 }
1635 dpsf->sf_crcount = qrv;
1636 rv++;
1637 }
1638 }
1639 return rv;
1640}
1641#endif
1642
1643
1644
1645
1646static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1647 int sfcount, __be32 *psfsrc, int delta)
1648{
1649 struct ip_mc_list *pmc;
1650 int isexclude;
1651 int i, err;
1652
1653 if (!in_dev)
1654 return -ENODEV;
1655 read_lock(&in_dev->mc_list_lock);
1656 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1657 if (*pmca == pmc->multiaddr)
1658 break;
1659 }
1660 if (!pmc) {
1661
1662 read_unlock(&in_dev->mc_list_lock);
1663 return -ESRCH;
1664 }
1665 spin_lock_bh(&pmc->lock);
1666 read_unlock(&in_dev->mc_list_lock);
1667
1668#ifdef CONFIG_IP_MULTICAST
1669 sf_markstate(pmc);
1670#endif
1671 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1672 if (!delta)
1673 pmc->sfcount[sfmode]++;
1674 err = 0;
1675 for (i=0; i<sfcount; i++) {
1676 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1677 if (err)
1678 break;
1679 }
1680 if (err) {
1681 int j;
1682
1683 pmc->sfcount[sfmode]--;
1684 for (j=0; j<i; j++)
1685 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1686 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1687#ifdef CONFIG_IP_MULTICAST
1688 struct ip_sf_list *psf;
1689 in_dev = pmc->interface;
1690#endif
1691
1692
1693 if (pmc->sfcount[MCAST_EXCLUDE])
1694 pmc->sfmode = MCAST_EXCLUDE;
1695 else if (pmc->sfcount[MCAST_INCLUDE])
1696 pmc->sfmode = MCAST_INCLUDE;
1697#ifdef CONFIG_IP_MULTICAST
1698
1699
1700 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1701 IGMP_Unsolicited_Report_Count;
1702 in_dev->mr_ifc_count = pmc->crcount;
1703 for (psf=pmc->sources; psf; psf = psf->sf_next)
1704 psf->sf_crcount = 0;
1705 igmp_ifc_event(in_dev);
1706 } else if (sf_setstate(pmc)) {
1707 igmp_ifc_event(in_dev);
1708#endif
1709 }
1710 spin_unlock_bh(&pmc->lock);
1711 return err;
1712}
1713
1714static void ip_mc_clear_src(struct ip_mc_list *pmc)
1715{
1716 struct ip_sf_list *psf, *nextpsf;
1717
1718 for (psf=pmc->tomb; psf; psf=nextpsf) {
1719 nextpsf = psf->sf_next;
1720 kfree(psf);
1721 }
1722 pmc->tomb = NULL;
1723 for (psf=pmc->sources; psf; psf=nextpsf) {
1724 nextpsf = psf->sf_next;
1725 kfree(psf);
1726 }
1727 pmc->sources = NULL;
1728 pmc->sfmode = MCAST_EXCLUDE;
1729 pmc->sfcount[MCAST_INCLUDE] = 0;
1730 pmc->sfcount[MCAST_EXCLUDE] = 1;
1731}
1732
1733
1734
1735
1736
1737int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1738{
1739 int err;
1740 __be32 addr = imr->imr_multiaddr.s_addr;
1741 struct ip_mc_socklist *iml=NULL, *i;
1742 struct in_device *in_dev;
1743 struct inet_sock *inet = inet_sk(sk);
1744 struct net *net = sock_net(sk);
1745 int ifindex;
1746 int count = 0;
1747
1748 if (!ipv4_is_multicast(addr))
1749 return -EINVAL;
1750
1751 rtnl_lock();
1752
1753 in_dev = ip_mc_find_dev(net, imr);
1754
1755 if (!in_dev) {
1756 iml = NULL;
1757 err = -ENODEV;
1758 goto done;
1759 }
1760
1761 err = -EADDRINUSE;
1762 ifindex = imr->imr_ifindex;
1763 for (i = inet->mc_list; i; i = i->next) {
1764 if (i->multi.imr_multiaddr.s_addr == addr &&
1765 i->multi.imr_ifindex == ifindex)
1766 goto done;
1767 count++;
1768 }
1769 err = -ENOBUFS;
1770 if (count >= sysctl_igmp_max_memberships)
1771 goto done;
1772 iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1773 if (iml == NULL)
1774 goto done;
1775
1776 memcpy(&iml->multi, imr, sizeof(*imr));
1777 iml->next = inet->mc_list;
1778 iml->sflist = NULL;
1779 iml->sfmode = MCAST_EXCLUDE;
1780 inet->mc_list = iml;
1781 ip_mc_inc_group(in_dev, addr);
1782 err = 0;
1783done:
1784 rtnl_unlock();
1785 return err;
1786}
1787
1788static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1789 struct in_device *in_dev)
1790{
1791 int err;
1792
1793 if (iml->sflist == NULL) {
1794
1795 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1796 iml->sfmode, 0, NULL, 0);
1797 }
1798 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1799 iml->sfmode, iml->sflist->sl_count,
1800 iml->sflist->sl_addr, 0);
1801 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1802 iml->sflist = NULL;
1803 return err;
1804}
1805
1806
1807
1808
1809
1810int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1811{
1812 struct inet_sock *inet = inet_sk(sk);
1813 struct ip_mc_socklist *iml, **imlp;
1814 struct in_device *in_dev;
1815 struct net *net = sock_net(sk);
1816 __be32 group = imr->imr_multiaddr.s_addr;
1817 u32 ifindex;
1818 int ret = -EADDRNOTAVAIL;
1819
1820 rtnl_lock();
1821 in_dev = ip_mc_find_dev(net, imr);
1822 ifindex = imr->imr_ifindex;
1823 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1824 if (iml->multi.imr_multiaddr.s_addr != group)
1825 continue;
1826 if (ifindex) {
1827 if (iml->multi.imr_ifindex != ifindex)
1828 continue;
1829 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1830 iml->multi.imr_address.s_addr)
1831 continue;
1832
1833 (void) ip_mc_leave_src(sk, iml, in_dev);
1834
1835 *imlp = iml->next;
1836
1837 if (in_dev)
1838 ip_mc_dec_group(in_dev, group);
1839 rtnl_unlock();
1840 sock_kfree_s(sk, iml, sizeof(*iml));
1841 return 0;
1842 }
1843 if (!in_dev)
1844 ret = -ENODEV;
1845 rtnl_unlock();
1846 return ret;
1847}
1848
1849int ip_mc_source(int add, int omode, struct sock *sk, struct
1850 ip_mreq_source *mreqs, int ifindex)
1851{
1852 int err;
1853 struct ip_mreqn imr;
1854 __be32 addr = mreqs->imr_multiaddr;
1855 struct ip_mc_socklist *pmc;
1856 struct in_device *in_dev = NULL;
1857 struct inet_sock *inet = inet_sk(sk);
1858 struct ip_sf_socklist *psl;
1859 struct net *net = sock_net(sk);
1860 int leavegroup = 0;
1861 int i, j, rv;
1862
1863 if (!ipv4_is_multicast(addr))
1864 return -EINVAL;
1865
1866 rtnl_lock();
1867
1868 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1869 imr.imr_address.s_addr = mreqs->imr_interface;
1870 imr.imr_ifindex = ifindex;
1871 in_dev = ip_mc_find_dev(net, &imr);
1872
1873 if (!in_dev) {
1874 err = -ENODEV;
1875 goto done;
1876 }
1877 err = -EADDRNOTAVAIL;
1878
1879 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1880 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1881 && pmc->multi.imr_ifindex == imr.imr_ifindex)
1882 break;
1883 }
1884 if (!pmc) {
1885 err = -EINVAL;
1886 goto done;
1887 }
1888
1889 if (pmc->sflist) {
1890 if (pmc->sfmode != omode) {
1891 err = -EINVAL;
1892 goto done;
1893 }
1894 } else if (pmc->sfmode != omode) {
1895
1896 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1897 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1898 NULL, 0);
1899 pmc->sfmode = omode;
1900 }
1901
1902 psl = pmc->sflist;
1903 if (!add) {
1904 if (!psl)
1905 goto done;
1906 rv = !0;
1907 for (i=0; i<psl->sl_count; i++) {
1908 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1909 sizeof(__be32));
1910 if (rv == 0)
1911 break;
1912 }
1913 if (rv)
1914 goto done;
1915
1916
1917 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1918 leavegroup = 1;
1919 goto done;
1920 }
1921
1922
1923 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1924 &mreqs->imr_sourceaddr, 1);
1925
1926 for (j=i+1; j<psl->sl_count; j++)
1927 psl->sl_addr[j-1] = psl->sl_addr[j];
1928 psl->sl_count--;
1929 err = 0;
1930 goto done;
1931 }
1932
1933
1934 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1935 err = -ENOBUFS;
1936 goto done;
1937 }
1938 if (!psl || psl->sl_count == psl->sl_max) {
1939 struct ip_sf_socklist *newpsl;
1940 int count = IP_SFBLOCK;
1941
1942 if (psl)
1943 count += psl->sl_max;
1944 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1945 if (!newpsl) {
1946 err = -ENOBUFS;
1947 goto done;
1948 }
1949 newpsl->sl_max = count;
1950 newpsl->sl_count = count - IP_SFBLOCK;
1951 if (psl) {
1952 for (i=0; i<psl->sl_count; i++)
1953 newpsl->sl_addr[i] = psl->sl_addr[i];
1954 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1955 }
1956 pmc->sflist = psl = newpsl;
1957 }
1958 rv = 1;
1959 for (i=0; i<psl->sl_count; i++) {
1960 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1961 sizeof(__be32));
1962 if (rv == 0)
1963 break;
1964 }
1965 if (rv == 0)
1966 goto done;
1967 for (j=psl->sl_count-1; j>=i; j--)
1968 psl->sl_addr[j+1] = psl->sl_addr[j];
1969 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1970 psl->sl_count++;
1971 err = 0;
1972
1973 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1974 &mreqs->imr_sourceaddr, 1);
1975done:
1976 rtnl_unlock();
1977 if (leavegroup)
1978 return ip_mc_leave_group(sk, &imr);
1979 return err;
1980}
1981
1982int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1983{
1984 int err = 0;
1985 struct ip_mreqn imr;
1986 __be32 addr = msf->imsf_multiaddr;
1987 struct ip_mc_socklist *pmc;
1988 struct in_device *in_dev;
1989 struct inet_sock *inet = inet_sk(sk);
1990 struct ip_sf_socklist *newpsl, *psl;
1991 struct net *net = sock_net(sk);
1992 int leavegroup = 0;
1993
1994 if (!ipv4_is_multicast(addr))
1995 return -EINVAL;
1996 if (msf->imsf_fmode != MCAST_INCLUDE &&
1997 msf->imsf_fmode != MCAST_EXCLUDE)
1998 return -EINVAL;
1999
2000 rtnl_lock();
2001
2002 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2003 imr.imr_address.s_addr = msf->imsf_interface;
2004 imr.imr_ifindex = ifindex;
2005 in_dev = ip_mc_find_dev(net, &imr);
2006
2007 if (!in_dev) {
2008 err = -ENODEV;
2009 goto done;
2010 }
2011
2012
2013 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2014 leavegroup = 1;
2015 goto done;
2016 }
2017
2018 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2019 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2020 pmc->multi.imr_ifindex == imr.imr_ifindex)
2021 break;
2022 }
2023 if (!pmc) {
2024 err = -EINVAL;
2025 goto done;
2026 }
2027 if (msf->imsf_numsrc) {
2028 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2029 GFP_KERNEL);
2030 if (!newpsl) {
2031 err = -ENOBUFS;
2032 goto done;
2033 }
2034 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2035 memcpy(newpsl->sl_addr, msf->imsf_slist,
2036 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2037 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2038 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2039 if (err) {
2040 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2041 goto done;
2042 }
2043 } else {
2044 newpsl = NULL;
2045 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2046 msf->imsf_fmode, 0, NULL, 0);
2047 }
2048 psl = pmc->sflist;
2049 if (psl) {
2050 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2051 psl->sl_count, psl->sl_addr, 0);
2052 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2053 } else
2054 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2055 0, NULL, 0);
2056 pmc->sflist = newpsl;
2057 pmc->sfmode = msf->imsf_fmode;
2058 err = 0;
2059done:
2060 rtnl_unlock();
2061 if (leavegroup)
2062 err = ip_mc_leave_group(sk, &imr);
2063 return err;
2064}
2065
2066int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2067 struct ip_msfilter __user *optval, int __user *optlen)
2068{
2069 int err, len, count, copycount;
2070 struct ip_mreqn imr;
2071 __be32 addr = msf->imsf_multiaddr;
2072 struct ip_mc_socklist *pmc;
2073 struct in_device *in_dev;
2074 struct inet_sock *inet = inet_sk(sk);
2075 struct ip_sf_socklist *psl;
2076 struct net *net = sock_net(sk);
2077
2078 if (!ipv4_is_multicast(addr))
2079 return -EINVAL;
2080
2081 rtnl_lock();
2082
2083 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2084 imr.imr_address.s_addr = msf->imsf_interface;
2085 imr.imr_ifindex = 0;
2086 in_dev = ip_mc_find_dev(net, &imr);
2087
2088 if (!in_dev) {
2089 err = -ENODEV;
2090 goto done;
2091 }
2092 err = -EADDRNOTAVAIL;
2093
2094 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2095 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2096 pmc->multi.imr_ifindex == imr.imr_ifindex)
2097 break;
2098 }
2099 if (!pmc)
2100 goto done;
2101 msf->imsf_fmode = pmc->sfmode;
2102 psl = pmc->sflist;
2103 rtnl_unlock();
2104 if (!psl) {
2105 len = 0;
2106 count = 0;
2107 } else {
2108 count = psl->sl_count;
2109 }
2110 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2111 len = copycount * sizeof(psl->sl_addr[0]);
2112 msf->imsf_numsrc = count;
2113 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2114 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2115 return -EFAULT;
2116 }
2117 if (len &&
2118 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2119 return -EFAULT;
2120 return 0;
2121done:
2122 rtnl_unlock();
2123 return err;
2124}
2125
2126int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2127 struct group_filter __user *optval, int __user *optlen)
2128{
2129 int err, i, count, copycount;
2130 struct sockaddr_in *psin;
2131 __be32 addr;
2132 struct ip_mc_socklist *pmc;
2133 struct inet_sock *inet = inet_sk(sk);
2134 struct ip_sf_socklist *psl;
2135
2136 psin = (struct sockaddr_in *)&gsf->gf_group;
2137 if (psin->sin_family != AF_INET)
2138 return -EINVAL;
2139 addr = psin->sin_addr.s_addr;
2140 if (!ipv4_is_multicast(addr))
2141 return -EINVAL;
2142
2143 rtnl_lock();
2144
2145 err = -EADDRNOTAVAIL;
2146
2147 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2148 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2149 pmc->multi.imr_ifindex == gsf->gf_interface)
2150 break;
2151 }
2152 if (!pmc)
2153 goto done;
2154 gsf->gf_fmode = pmc->sfmode;
2155 psl = pmc->sflist;
2156 rtnl_unlock();
2157 count = psl ? psl->sl_count : 0;
2158 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2159 gsf->gf_numsrc = count;
2160 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2161 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2162 return -EFAULT;
2163 }
2164 for (i=0; i<copycount; i++) {
2165 struct sockaddr_storage ss;
2166
2167 psin = (struct sockaddr_in *)&ss;
2168 memset(&ss, 0, sizeof(ss));
2169 psin->sin_family = AF_INET;
2170 psin->sin_addr.s_addr = psl->sl_addr[i];
2171 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2172 return -EFAULT;
2173 }
2174 return 0;
2175done:
2176 rtnl_unlock();
2177 return err;
2178}
2179
2180
2181
2182
2183int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2184{
2185 struct inet_sock *inet = inet_sk(sk);
2186 struct ip_mc_socklist *pmc;
2187 struct ip_sf_socklist *psl;
2188 int i;
2189
2190 if (!ipv4_is_multicast(loc_addr))
2191 return 1;
2192
2193 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2194 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2195 pmc->multi.imr_ifindex == dif)
2196 break;
2197 }
2198 if (!pmc)
2199 return 1;
2200 psl = pmc->sflist;
2201 if (!psl)
2202 return pmc->sfmode == MCAST_EXCLUDE;
2203
2204 for (i=0; i<psl->sl_count; i++) {
2205 if (psl->sl_addr[i] == rmt_addr)
2206 break;
2207 }
2208 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2209 return 0;
2210 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2211 return 0;
2212 return 1;
2213}
2214
2215
2216
2217
2218
2219void ip_mc_drop_socket(struct sock *sk)
2220{
2221 struct inet_sock *inet = inet_sk(sk);
2222 struct ip_mc_socklist *iml;
2223 struct net *net = sock_net(sk);
2224
2225 if (inet->mc_list == NULL)
2226 return;
2227
2228 rtnl_lock();
2229 while ((iml = inet->mc_list) != NULL) {
2230 struct in_device *in_dev;
2231 inet->mc_list = iml->next;
2232
2233 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2234 (void) ip_mc_leave_src(sk, iml, in_dev);
2235 if (in_dev != NULL) {
2236 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2237 in_dev_put(in_dev);
2238 }
2239 sock_kfree_s(sk, iml, sizeof(*iml));
2240 }
2241 rtnl_unlock();
2242}
2243
2244int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2245{
2246 struct ip_mc_list *im;
2247 struct ip_sf_list *psf;
2248 int rv = 0;
2249
2250 read_lock(&in_dev->mc_list_lock);
2251 for (im=in_dev->mc_list; im; im=im->next) {
2252 if (im->multiaddr == mc_addr)
2253 break;
2254 }
2255 if (im && proto == IPPROTO_IGMP) {
2256 rv = 1;
2257 } else if (im) {
2258 if (src_addr) {
2259 for (psf=im->sources; psf; psf=psf->sf_next) {
2260 if (psf->sf_inaddr == src_addr)
2261 break;
2262 }
2263 if (psf)
2264 rv = psf->sf_count[MCAST_INCLUDE] ||
2265 psf->sf_count[MCAST_EXCLUDE] !=
2266 im->sfcount[MCAST_EXCLUDE];
2267 else
2268 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2269 } else
2270 rv = 1;
2271 }
2272 read_unlock(&in_dev->mc_list_lock);
2273 return rv;
2274}
2275
2276#if defined(CONFIG_PROC_FS)
2277struct igmp_mc_iter_state {
2278 struct net_device *dev;
2279 struct in_device *in_dev;
2280};
2281
2282#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2283
2284static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2285{
2286 struct ip_mc_list *im = NULL;
2287 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2288
2289 state->in_dev = NULL;
2290 for_each_netdev(&init_net, state->dev) {
2291 struct in_device *in_dev;
2292 in_dev = in_dev_get(state->dev);
2293 if (!in_dev)
2294 continue;
2295 read_lock(&in_dev->mc_list_lock);
2296 im = in_dev->mc_list;
2297 if (im) {
2298 state->in_dev = in_dev;
2299 break;
2300 }
2301 read_unlock(&in_dev->mc_list_lock);
2302 in_dev_put(in_dev);
2303 }
2304 return im;
2305}
2306
2307static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2308{
2309 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2310 im = im->next;
2311 while (!im) {
2312 if (likely(state->in_dev != NULL)) {
2313 read_unlock(&state->in_dev->mc_list_lock);
2314 in_dev_put(state->in_dev);
2315 }
2316 state->dev = next_net_device(state->dev);
2317 if (!state->dev) {
2318 state->in_dev = NULL;
2319 break;
2320 }
2321 state->in_dev = in_dev_get(state->dev);
2322 if (!state->in_dev)
2323 continue;
2324 read_lock(&state->in_dev->mc_list_lock);
2325 im = state->in_dev->mc_list;
2326 }
2327 return im;
2328}
2329
2330static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2331{
2332 struct ip_mc_list *im = igmp_mc_get_first(seq);
2333 if (im)
2334 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2335 --pos;
2336 return pos ? NULL : im;
2337}
2338
2339static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2340 __acquires(dev_base_lock)
2341{
2342 read_lock(&dev_base_lock);
2343 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2344}
2345
2346static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2347{
2348 struct ip_mc_list *im;
2349 if (v == SEQ_START_TOKEN)
2350 im = igmp_mc_get_first(seq);
2351 else
2352 im = igmp_mc_get_next(seq, v);
2353 ++*pos;
2354 return im;
2355}
2356
2357static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2358 __releases(dev_base_lock)
2359{
2360 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2361 if (likely(state->in_dev != NULL)) {
2362 read_unlock(&state->in_dev->mc_list_lock);
2363 in_dev_put(state->in_dev);
2364 state->in_dev = NULL;
2365 }
2366 state->dev = NULL;
2367 read_unlock(&dev_base_lock);
2368}
2369
2370static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2371{
2372 if (v == SEQ_START_TOKEN)
2373 seq_puts(seq,
2374 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2375 else {
2376 struct ip_mc_list *im = (struct ip_mc_list *)v;
2377 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2378 char *querier;
2379#ifdef CONFIG_IP_MULTICAST
2380 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2381 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2382 "V3";
2383#else
2384 querier = "NONE";
2385#endif
2386
2387 if (state->in_dev->mc_list == im) {
2388 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2389 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2390 }
2391
2392 seq_printf(seq,
2393 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2394 im->multiaddr, im->users,
2395 im->tm_running, im->tm_running ?
2396 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2397 im->reporter);
2398 }
2399 return 0;
2400}
2401
2402static const struct seq_operations igmp_mc_seq_ops = {
2403 .start = igmp_mc_seq_start,
2404 .next = igmp_mc_seq_next,
2405 .stop = igmp_mc_seq_stop,
2406 .show = igmp_mc_seq_show,
2407};
2408
2409static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2410{
2411 return seq_open_private(file, &igmp_mc_seq_ops,
2412 sizeof(struct igmp_mc_iter_state));
2413}
2414
2415static const struct file_operations igmp_mc_seq_fops = {
2416 .owner = THIS_MODULE,
2417 .open = igmp_mc_seq_open,
2418 .read = seq_read,
2419 .llseek = seq_lseek,
2420 .release = seq_release_private,
2421};
2422
2423struct igmp_mcf_iter_state {
2424 struct net_device *dev;
2425 struct in_device *idev;
2426 struct ip_mc_list *im;
2427};
2428
2429#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2430
2431static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2432{
2433 struct ip_sf_list *psf = NULL;
2434 struct ip_mc_list *im = NULL;
2435 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2436
2437 state->idev = NULL;
2438 state->im = NULL;
2439 for_each_netdev(&init_net, state->dev) {
2440 struct in_device *idev;
2441 idev = in_dev_get(state->dev);
2442 if (unlikely(idev == NULL))
2443 continue;
2444 read_lock(&idev->mc_list_lock);
2445 im = idev->mc_list;
2446 if (likely(im != NULL)) {
2447 spin_lock_bh(&im->lock);
2448 psf = im->sources;
2449 if (likely(psf != NULL)) {
2450 state->im = im;
2451 state->idev = idev;
2452 break;
2453 }
2454 spin_unlock_bh(&im->lock);
2455 }
2456 read_unlock(&idev->mc_list_lock);
2457 in_dev_put(idev);
2458 }
2459 return psf;
2460}
2461
2462static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2463{
2464 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2465
2466 psf = psf->sf_next;
2467 while (!psf) {
2468 spin_unlock_bh(&state->im->lock);
2469 state->im = state->im->next;
2470 while (!state->im) {
2471 if (likely(state->idev != NULL)) {
2472 read_unlock(&state->idev->mc_list_lock);
2473 in_dev_put(state->idev);
2474 }
2475 state->dev = next_net_device(state->dev);
2476 if (!state->dev) {
2477 state->idev = NULL;
2478 goto out;
2479 }
2480 state->idev = in_dev_get(state->dev);
2481 if (!state->idev)
2482 continue;
2483 read_lock(&state->idev->mc_list_lock);
2484 state->im = state->idev->mc_list;
2485 }
2486 if (!state->im)
2487 break;
2488 spin_lock_bh(&state->im->lock);
2489 psf = state->im->sources;
2490 }
2491out:
2492 return psf;
2493}
2494
2495static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2496{
2497 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2498 if (psf)
2499 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2500 --pos;
2501 return pos ? NULL : psf;
2502}
2503
2504static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2505{
2506 read_lock(&dev_base_lock);
2507 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2508}
2509
2510static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2511{
2512 struct ip_sf_list *psf;
2513 if (v == SEQ_START_TOKEN)
2514 psf = igmp_mcf_get_first(seq);
2515 else
2516 psf = igmp_mcf_get_next(seq, v);
2517 ++*pos;
2518 return psf;
2519}
2520
2521static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2522{
2523 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2524 if (likely(state->im != NULL)) {
2525 spin_unlock_bh(&state->im->lock);
2526 state->im = NULL;
2527 }
2528 if (likely(state->idev != NULL)) {
2529 read_unlock(&state->idev->mc_list_lock);
2530 in_dev_put(state->idev);
2531 state->idev = NULL;
2532 }
2533 state->dev = NULL;
2534 read_unlock(&dev_base_lock);
2535}
2536
2537static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2538{
2539 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2540 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2541
2542 if (v == SEQ_START_TOKEN) {
2543 seq_printf(seq,
2544 "%3s %6s "
2545 "%10s %10s %6s %6s\n", "Idx",
2546 "Device", "MCA",
2547 "SRC", "INC", "EXC");
2548 } else {
2549 seq_printf(seq,
2550 "%3d %6.6s 0x%08x "
2551 "0x%08x %6lu %6lu\n",
2552 state->dev->ifindex, state->dev->name,
2553 ntohl(state->im->multiaddr),
2554 ntohl(psf->sf_inaddr),
2555 psf->sf_count[MCAST_INCLUDE],
2556 psf->sf_count[MCAST_EXCLUDE]);
2557 }
2558 return 0;
2559}
2560
2561static const struct seq_operations igmp_mcf_seq_ops = {
2562 .start = igmp_mcf_seq_start,
2563 .next = igmp_mcf_seq_next,
2564 .stop = igmp_mcf_seq_stop,
2565 .show = igmp_mcf_seq_show,
2566};
2567
2568static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2569{
2570 return seq_open_private(file, &igmp_mcf_seq_ops,
2571 sizeof(struct igmp_mcf_iter_state));
2572}
2573
2574static const struct file_operations igmp_mcf_seq_fops = {
2575 .owner = THIS_MODULE,
2576 .open = igmp_mcf_seq_open,
2577 .read = seq_read,
2578 .llseek = seq_lseek,
2579 .release = seq_release_private,
2580};
2581
2582int __init igmp_mc_proc_init(void)
2583{
2584 proc_net_fops_create(&init_net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2585 proc_net_fops_create(&init_net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2586 return 0;
2587}
2588#endif
2589
2590EXPORT_SYMBOL(ip_mc_dec_group);
2591EXPORT_SYMBOL(ip_mc_inc_group);
2592EXPORT_SYMBOL(ip_mc_join_group);
2593EXPORT_SYMBOL(ip_mc_rejoin_group);