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 <linux/slab.h>
75#include <linux/uaccess.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#include <linux/pkt_sched.h>
92#include <linux/byteorder/generic.h>
93
94#include <net/net_namespace.h>
95#include <net/arp.h>
96#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 <net/inet_common.h>
102#include <linux/netfilter_ipv4.h>
103#ifdef CONFIG_IP_MROUTE
104#include <linux/mroute.h>
105#endif
106#ifdef CONFIG_PROC_FS
107#include <linux/proc_fs.h>
108#include <linux/seq_file.h>
109#endif
110
111#ifdef CONFIG_IP_MULTICAST
112
113
114#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
115#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
116#define IGMP_QUERY_INTERVAL (125*HZ)
117#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
118
119#define IGMP_INITIAL_REPORT_DELAY (1)
120
121
122
123
124
125
126
127
128#define IGMP_V1_SEEN(in_dev) \
129 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
130 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
131 ((in_dev)->mr_v1_seen && \
132 time_before(jiffies, (in_dev)->mr_v1_seen)))
133#define IGMP_V2_SEEN(in_dev) \
134 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
135 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
136 ((in_dev)->mr_v2_seen && \
137 time_before(jiffies, (in_dev)->mr_v2_seen)))
138
139static int unsolicited_report_interval(struct in_device *in_dev)
140{
141 int interval_ms, interval_jiffies;
142
143 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
144 interval_ms = IN_DEV_CONF_GET(
145 in_dev,
146 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
147 else
148 interval_ms = IN_DEV_CONF_GET(
149 in_dev,
150 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
151
152 interval_jiffies = msecs_to_jiffies(interval_ms);
153
154
155
156
157 if (interval_jiffies <= 0)
158 interval_jiffies = 1;
159 return interval_jiffies;
160}
161
162static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
163static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
164static void igmpv3_clear_delrec(struct in_device *in_dev);
165static int sf_setstate(struct ip_mc_list *pmc);
166static void sf_markstate(struct ip_mc_list *pmc);
167#endif
168static void ip_mc_clear_src(struct ip_mc_list *pmc);
169static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
170 int sfcount, __be32 *psfsrc, int delta);
171
172static void ip_ma_put(struct ip_mc_list *im)
173{
174 if (refcount_dec_and_test(&im->refcnt)) {
175 in_dev_put(im->interface);
176 kfree_rcu(im, rcu);
177 }
178}
179
180#define for_each_pmc_rcu(in_dev, pmc) \
181 for (pmc = rcu_dereference(in_dev->mc_list); \
182 pmc != NULL; \
183 pmc = rcu_dereference(pmc->next_rcu))
184
185#define for_each_pmc_rtnl(in_dev, pmc) \
186 for (pmc = rtnl_dereference(in_dev->mc_list); \
187 pmc != NULL; \
188 pmc = rtnl_dereference(pmc->next_rcu))
189
190static void ip_sf_list_clear_all(struct ip_sf_list *psf)
191{
192 struct ip_sf_list *next;
193
194 while (psf) {
195 next = psf->sf_next;
196 kfree(psf);
197 psf = next;
198 }
199}
200
201#ifdef CONFIG_IP_MULTICAST
202
203
204
205
206
207static void igmp_stop_timer(struct ip_mc_list *im)
208{
209 spin_lock_bh(&im->lock);
210 if (del_timer(&im->timer))
211 refcount_dec(&im->refcnt);
212 im->tm_running = 0;
213 im->reporter = 0;
214 im->unsolicit_count = 0;
215 spin_unlock_bh(&im->lock);
216}
217
218
219static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
220{
221 int tv = prandom_u32() % max_delay;
222
223 im->tm_running = 1;
224 if (!mod_timer(&im->timer, jiffies+tv+2))
225 refcount_inc(&im->refcnt);
226}
227
228static void igmp_gq_start_timer(struct in_device *in_dev)
229{
230 int tv = prandom_u32() % in_dev->mr_maxdelay;
231 unsigned long exp = jiffies + tv + 2;
232
233 if (in_dev->mr_gq_running &&
234 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
235 return;
236
237 in_dev->mr_gq_running = 1;
238 if (!mod_timer(&in_dev->mr_gq_timer, exp))
239 in_dev_hold(in_dev);
240}
241
242static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
243{
244 int tv = prandom_u32() % delay;
245
246 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
247 in_dev_hold(in_dev);
248}
249
250static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
251{
252 spin_lock_bh(&im->lock);
253 im->unsolicit_count = 0;
254 if (del_timer(&im->timer)) {
255 if ((long)(im->timer.expires-jiffies) < max_delay) {
256 add_timer(&im->timer);
257 im->tm_running = 1;
258 spin_unlock_bh(&im->lock);
259 return;
260 }
261 refcount_dec(&im->refcnt);
262 }
263 igmp_start_timer(im, max_delay);
264 spin_unlock_bh(&im->lock);
265}
266
267
268
269
270
271
272#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
273
274
275static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
276 int gdeleted, int sdeleted)
277{
278 switch (type) {
279 case IGMPV3_MODE_IS_INCLUDE:
280 case IGMPV3_MODE_IS_EXCLUDE:
281 if (gdeleted || sdeleted)
282 return 0;
283 if (!(pmc->gsquery && !psf->sf_gsresp)) {
284 if (pmc->sfmode == MCAST_INCLUDE)
285 return 1;
286
287
288
289 if (psf->sf_count[MCAST_INCLUDE])
290 return type == IGMPV3_MODE_IS_INCLUDE;
291 return pmc->sfcount[MCAST_EXCLUDE] ==
292 psf->sf_count[MCAST_EXCLUDE];
293 }
294 return 0;
295 case IGMPV3_CHANGE_TO_INCLUDE:
296 if (gdeleted || sdeleted)
297 return 0;
298 return psf->sf_count[MCAST_INCLUDE] != 0;
299 case IGMPV3_CHANGE_TO_EXCLUDE:
300 if (gdeleted || sdeleted)
301 return 0;
302 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
303 psf->sf_count[MCAST_INCLUDE])
304 return 0;
305 return pmc->sfcount[MCAST_EXCLUDE] ==
306 psf->sf_count[MCAST_EXCLUDE];
307 case IGMPV3_ALLOW_NEW_SOURCES:
308 if (gdeleted || !psf->sf_crcount)
309 return 0;
310 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
311 case IGMPV3_BLOCK_OLD_SOURCES:
312 if (pmc->sfmode == MCAST_INCLUDE)
313 return gdeleted || (psf->sf_crcount && sdeleted);
314 return psf->sf_crcount && !gdeleted && !sdeleted;
315 }
316 return 0;
317}
318
319static int
320igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
321{
322 struct ip_sf_list *psf;
323 int scount = 0;
324
325 for (psf = pmc->sources; psf; psf = psf->sf_next) {
326 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
327 continue;
328 scount++;
329 }
330 return scount;
331}
332
333
334static __be32 igmpv3_get_srcaddr(struct net_device *dev,
335 const struct flowi4 *fl4)
336{
337 struct in_device *in_dev = __in_dev_get_rcu(dev);
338 const struct in_ifaddr *ifa;
339
340 if (!in_dev)
341 return htonl(INADDR_ANY);
342
343 in_dev_for_each_ifa_rcu(ifa, in_dev) {
344 if (fl4->saddr == ifa->ifa_local)
345 return fl4->saddr;
346 }
347
348 return htonl(INADDR_ANY);
349}
350
351static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
352{
353 struct sk_buff *skb;
354 struct rtable *rt;
355 struct iphdr *pip;
356 struct igmpv3_report *pig;
357 struct net *net = dev_net(dev);
358 struct flowi4 fl4;
359 int hlen = LL_RESERVED_SPACE(dev);
360 int tlen = dev->needed_tailroom;
361 unsigned int size = mtu;
362
363 while (1) {
364 skb = alloc_skb(size + hlen + tlen,
365 GFP_ATOMIC | __GFP_NOWARN);
366 if (skb)
367 break;
368 size >>= 1;
369 if (size < 256)
370 return NULL;
371 }
372 skb->priority = TC_PRIO_CONTROL;
373
374 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
375 0, 0,
376 IPPROTO_IGMP, 0, dev->ifindex);
377 if (IS_ERR(rt)) {
378 kfree_skb(skb);
379 return NULL;
380 }
381
382 skb_dst_set(skb, &rt->dst);
383 skb->dev = dev;
384
385 skb_reserve(skb, hlen);
386 skb_tailroom_reserve(skb, mtu, tlen);
387
388 skb_reset_network_header(skb);
389 pip = ip_hdr(skb);
390 skb_put(skb, sizeof(struct iphdr) + 4);
391
392 pip->version = 4;
393 pip->ihl = (sizeof(struct iphdr)+4)>>2;
394 pip->tos = 0xc0;
395 pip->frag_off = htons(IP_DF);
396 pip->ttl = 1;
397 pip->daddr = fl4.daddr;
398
399 rcu_read_lock();
400 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
401 rcu_read_unlock();
402
403 pip->protocol = IPPROTO_IGMP;
404 pip->tot_len = 0;
405 ip_select_ident(net, skb, NULL);
406 ((u8 *)&pip[1])[0] = IPOPT_RA;
407 ((u8 *)&pip[1])[1] = 4;
408 ((u8 *)&pip[1])[2] = 0;
409 ((u8 *)&pip[1])[3] = 0;
410
411 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
412 skb_put(skb, sizeof(*pig));
413 pig = igmpv3_report_hdr(skb);
414 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
415 pig->resv1 = 0;
416 pig->csum = 0;
417 pig->resv2 = 0;
418 pig->ngrec = 0;
419 return skb;
420}
421
422static int igmpv3_sendpack(struct sk_buff *skb)
423{
424 struct igmphdr *pig = igmp_hdr(skb);
425 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
426
427 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
428
429 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
430}
431
432static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
433{
434 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
435}
436
437static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
438 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
439{
440 struct net_device *dev = pmc->interface->dev;
441 struct igmpv3_report *pih;
442 struct igmpv3_grec *pgr;
443
444 if (!skb) {
445 skb = igmpv3_newpack(dev, mtu);
446 if (!skb)
447 return NULL;
448 }
449 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
450 pgr->grec_type = type;
451 pgr->grec_auxwords = 0;
452 pgr->grec_nsrcs = 0;
453 pgr->grec_mca = pmc->multiaddr;
454 pih = igmpv3_report_hdr(skb);
455 pih->ngrec = htons(ntohs(pih->ngrec)+1);
456 *ppgr = pgr;
457 return skb;
458}
459
460#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
461
462static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
463 int type, int gdeleted, int sdeleted)
464{
465 struct net_device *dev = pmc->interface->dev;
466 struct net *net = dev_net(dev);
467 struct igmpv3_report *pih;
468 struct igmpv3_grec *pgr = NULL;
469 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
470 int scount, stotal, first, isquery, truncate;
471 unsigned int mtu;
472
473 if (pmc->multiaddr == IGMP_ALL_HOSTS)
474 return skb;
475 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
476 return skb;
477
478 mtu = READ_ONCE(dev->mtu);
479 if (mtu < IPV4_MIN_MTU)
480 return skb;
481
482 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
483 type == IGMPV3_MODE_IS_EXCLUDE;
484 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
485 type == IGMPV3_CHANGE_TO_EXCLUDE;
486
487 stotal = scount = 0;
488
489 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
490
491 if (!*psf_list)
492 goto empty_source;
493
494 pih = skb ? igmpv3_report_hdr(skb) : NULL;
495
496
497 if (truncate) {
498 if (pih && pih->ngrec &&
499 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
500 if (skb)
501 igmpv3_sendpack(skb);
502 skb = igmpv3_newpack(dev, mtu);
503 }
504 }
505 first = 1;
506 psf_prev = NULL;
507 for (psf = *psf_list; psf; psf = psf_next) {
508 __be32 *psrc;
509
510 psf_next = psf->sf_next;
511
512 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
513 psf_prev = psf;
514 continue;
515 }
516
517
518
519
520 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
521 (!gdeleted && pmc->crcount)) &&
522 (type == IGMPV3_ALLOW_NEW_SOURCES ||
523 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
524 goto decrease_sf_crcount;
525
526
527 if (isquery)
528 psf->sf_gsresp = 0;
529
530 if (AVAILABLE(skb) < sizeof(__be32) +
531 first*sizeof(struct igmpv3_grec)) {
532 if (truncate && !first)
533 break;
534 if (pgr)
535 pgr->grec_nsrcs = htons(scount);
536 if (skb)
537 igmpv3_sendpack(skb);
538 skb = igmpv3_newpack(dev, mtu);
539 first = 1;
540 scount = 0;
541 }
542 if (first) {
543 skb = add_grhead(skb, pmc, type, &pgr, mtu);
544 first = 0;
545 }
546 if (!skb)
547 return NULL;
548 psrc = skb_put(skb, sizeof(__be32));
549 *psrc = psf->sf_inaddr;
550 scount++; stotal++;
551 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
552 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
553decrease_sf_crcount:
554 psf->sf_crcount--;
555 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
556 if (psf_prev)
557 psf_prev->sf_next = psf->sf_next;
558 else
559 *psf_list = psf->sf_next;
560 kfree(psf);
561 continue;
562 }
563 }
564 psf_prev = psf;
565 }
566
567empty_source:
568 if (!stotal) {
569 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
570 type == IGMPV3_BLOCK_OLD_SOURCES)
571 return skb;
572 if (pmc->crcount || isquery) {
573
574 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
575 igmpv3_sendpack(skb);
576 skb = NULL;
577 }
578 skb = add_grhead(skb, pmc, type, &pgr, mtu);
579 }
580 }
581 if (pgr)
582 pgr->grec_nsrcs = htons(scount);
583
584 if (isquery)
585 pmc->gsquery = 0;
586 return skb;
587}
588
589static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
590{
591 struct sk_buff *skb = NULL;
592 struct net *net = dev_net(in_dev->dev);
593 int type;
594
595 if (!pmc) {
596 rcu_read_lock();
597 for_each_pmc_rcu(in_dev, pmc) {
598 if (pmc->multiaddr == IGMP_ALL_HOSTS)
599 continue;
600 if (ipv4_is_local_multicast(pmc->multiaddr) &&
601 !net->ipv4.sysctl_igmp_llm_reports)
602 continue;
603 spin_lock_bh(&pmc->lock);
604 if (pmc->sfcount[MCAST_EXCLUDE])
605 type = IGMPV3_MODE_IS_EXCLUDE;
606 else
607 type = IGMPV3_MODE_IS_INCLUDE;
608 skb = add_grec(skb, pmc, type, 0, 0);
609 spin_unlock_bh(&pmc->lock);
610 }
611 rcu_read_unlock();
612 } else {
613 spin_lock_bh(&pmc->lock);
614 if (pmc->sfcount[MCAST_EXCLUDE])
615 type = IGMPV3_MODE_IS_EXCLUDE;
616 else
617 type = IGMPV3_MODE_IS_INCLUDE;
618 skb = add_grec(skb, pmc, type, 0, 0);
619 spin_unlock_bh(&pmc->lock);
620 }
621 if (!skb)
622 return 0;
623 return igmpv3_sendpack(skb);
624}
625
626
627
628
629static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
630{
631 struct ip_sf_list *psf_prev, *psf_next, *psf;
632
633 psf_prev = NULL;
634 for (psf = *ppsf; psf; psf = psf_next) {
635 psf_next = psf->sf_next;
636 if (psf->sf_crcount == 0) {
637 if (psf_prev)
638 psf_prev->sf_next = psf->sf_next;
639 else
640 *ppsf = psf->sf_next;
641 kfree(psf);
642 } else
643 psf_prev = psf;
644 }
645}
646
647static void kfree_pmc(struct ip_mc_list *pmc)
648{
649 ip_sf_list_clear_all(pmc->sources);
650 ip_sf_list_clear_all(pmc->tomb);
651 kfree(pmc);
652}
653
654static void igmpv3_send_cr(struct in_device *in_dev)
655{
656 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
657 struct sk_buff *skb = NULL;
658 int type, dtype;
659
660 rcu_read_lock();
661 spin_lock_bh(&in_dev->mc_tomb_lock);
662
663
664 pmc_prev = NULL;
665 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
666 pmc_next = pmc->next;
667 if (pmc->sfmode == MCAST_INCLUDE) {
668 type = IGMPV3_BLOCK_OLD_SOURCES;
669 dtype = IGMPV3_BLOCK_OLD_SOURCES;
670 skb = add_grec(skb, pmc, type, 1, 0);
671 skb = add_grec(skb, pmc, dtype, 1, 1);
672 }
673 if (pmc->crcount) {
674 if (pmc->sfmode == MCAST_EXCLUDE) {
675 type = IGMPV3_CHANGE_TO_INCLUDE;
676 skb = add_grec(skb, pmc, type, 1, 0);
677 }
678 pmc->crcount--;
679 if (pmc->crcount == 0) {
680 igmpv3_clear_zeros(&pmc->tomb);
681 igmpv3_clear_zeros(&pmc->sources);
682 }
683 }
684 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
685 if (pmc_prev)
686 pmc_prev->next = pmc_next;
687 else
688 in_dev->mc_tomb = pmc_next;
689 in_dev_put(pmc->interface);
690 kfree_pmc(pmc);
691 } else
692 pmc_prev = pmc;
693 }
694 spin_unlock_bh(&in_dev->mc_tomb_lock);
695
696
697 for_each_pmc_rcu(in_dev, pmc) {
698 spin_lock_bh(&pmc->lock);
699 if (pmc->sfcount[MCAST_EXCLUDE]) {
700 type = IGMPV3_BLOCK_OLD_SOURCES;
701 dtype = IGMPV3_ALLOW_NEW_SOURCES;
702 } else {
703 type = IGMPV3_ALLOW_NEW_SOURCES;
704 dtype = IGMPV3_BLOCK_OLD_SOURCES;
705 }
706 skb = add_grec(skb, pmc, type, 0, 0);
707 skb = add_grec(skb, pmc, dtype, 0, 1);
708
709
710 if (pmc->crcount) {
711 if (pmc->sfmode == MCAST_EXCLUDE)
712 type = IGMPV3_CHANGE_TO_EXCLUDE;
713 else
714 type = IGMPV3_CHANGE_TO_INCLUDE;
715 skb = add_grec(skb, pmc, type, 0, 0);
716 pmc->crcount--;
717 }
718 spin_unlock_bh(&pmc->lock);
719 }
720 rcu_read_unlock();
721
722 if (!skb)
723 return;
724 (void) igmpv3_sendpack(skb);
725}
726
727static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
728 int type)
729{
730 struct sk_buff *skb;
731 struct iphdr *iph;
732 struct igmphdr *ih;
733 struct rtable *rt;
734 struct net_device *dev = in_dev->dev;
735 struct net *net = dev_net(dev);
736 __be32 group = pmc ? pmc->multiaddr : 0;
737 struct flowi4 fl4;
738 __be32 dst;
739 int hlen, tlen;
740
741 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
742 return igmpv3_send_report(in_dev, pmc);
743
744 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
745 return 0;
746
747 if (type == IGMP_HOST_LEAVE_MESSAGE)
748 dst = IGMP_ALL_ROUTER;
749 else
750 dst = group;
751
752 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
753 0, 0,
754 IPPROTO_IGMP, 0, dev->ifindex);
755 if (IS_ERR(rt))
756 return -1;
757
758 hlen = LL_RESERVED_SPACE(dev);
759 tlen = dev->needed_tailroom;
760 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
761 if (!skb) {
762 ip_rt_put(rt);
763 return -1;
764 }
765 skb->priority = TC_PRIO_CONTROL;
766
767 skb_dst_set(skb, &rt->dst);
768
769 skb_reserve(skb, hlen);
770
771 skb_reset_network_header(skb);
772 iph = ip_hdr(skb);
773 skb_put(skb, sizeof(struct iphdr) + 4);
774
775 iph->version = 4;
776 iph->ihl = (sizeof(struct iphdr)+4)>>2;
777 iph->tos = 0xc0;
778 iph->frag_off = htons(IP_DF);
779 iph->ttl = 1;
780 iph->daddr = dst;
781 iph->saddr = fl4.saddr;
782 iph->protocol = IPPROTO_IGMP;
783 ip_select_ident(net, skb, NULL);
784 ((u8 *)&iph[1])[0] = IPOPT_RA;
785 ((u8 *)&iph[1])[1] = 4;
786 ((u8 *)&iph[1])[2] = 0;
787 ((u8 *)&iph[1])[3] = 0;
788
789 ih = skb_put(skb, sizeof(struct igmphdr));
790 ih->type = type;
791 ih->code = 0;
792 ih->csum = 0;
793 ih->group = group;
794 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
795
796 return ip_local_out(net, skb->sk, skb);
797}
798
799static void igmp_gq_timer_expire(struct timer_list *t)
800{
801 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
802
803 in_dev->mr_gq_running = 0;
804 igmpv3_send_report(in_dev, NULL);
805 in_dev_put(in_dev);
806}
807
808static void igmp_ifc_timer_expire(struct timer_list *t)
809{
810 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
811
812 igmpv3_send_cr(in_dev);
813 if (in_dev->mr_ifc_count) {
814 in_dev->mr_ifc_count--;
815 igmp_ifc_start_timer(in_dev,
816 unsolicited_report_interval(in_dev));
817 }
818 in_dev_put(in_dev);
819}
820
821static void igmp_ifc_event(struct in_device *in_dev)
822{
823 struct net *net = dev_net(in_dev->dev);
824 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
825 return;
826 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
827 igmp_ifc_start_timer(in_dev, 1);
828}
829
830
831static void igmp_timer_expire(struct timer_list *t)
832{
833 struct ip_mc_list *im = from_timer(im, t, timer);
834 struct in_device *in_dev = im->interface;
835
836 spin_lock(&im->lock);
837 im->tm_running = 0;
838
839 if (im->unsolicit_count && --im->unsolicit_count)
840 igmp_start_timer(im, unsolicited_report_interval(in_dev));
841
842 im->reporter = 1;
843 spin_unlock(&im->lock);
844
845 if (IGMP_V1_SEEN(in_dev))
846 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
847 else if (IGMP_V2_SEEN(in_dev))
848 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
849 else
850 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
851
852 ip_ma_put(im);
853}
854
855
856static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
857{
858 struct ip_sf_list *psf;
859 int i, scount;
860
861 scount = 0;
862 for (psf = pmc->sources; psf; psf = psf->sf_next) {
863 if (scount == nsrcs)
864 break;
865 for (i = 0; i < nsrcs; i++) {
866
867 if (psf->sf_count[MCAST_INCLUDE] ||
868 pmc->sfcount[MCAST_EXCLUDE] !=
869 psf->sf_count[MCAST_EXCLUDE])
870 break;
871 if (srcs[i] == psf->sf_inaddr) {
872 scount++;
873 break;
874 }
875 }
876 }
877 pmc->gsquery = 0;
878 if (scount == nsrcs)
879 return 0;
880 return 1;
881}
882
883static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
884{
885 struct ip_sf_list *psf;
886 int i, scount;
887
888 if (pmc->sfmode == MCAST_EXCLUDE)
889 return igmp_xmarksources(pmc, nsrcs, srcs);
890
891
892 scount = 0;
893 for (psf = pmc->sources; psf; psf = psf->sf_next) {
894 if (scount == nsrcs)
895 break;
896 for (i = 0; i < nsrcs; i++)
897 if (srcs[i] == psf->sf_inaddr) {
898 psf->sf_gsresp = 1;
899 scount++;
900 break;
901 }
902 }
903 if (!scount) {
904 pmc->gsquery = 0;
905 return 0;
906 }
907 pmc->gsquery = 1;
908 return 1;
909}
910
911
912static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
913{
914 struct ip_mc_list *im;
915 struct net *net = dev_net(in_dev->dev);
916
917
918
919 if (group == IGMP_ALL_HOSTS)
920 return false;
921 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
922 return false;
923
924 rcu_read_lock();
925 for_each_pmc_rcu(in_dev, im) {
926 if (im->multiaddr == group) {
927 igmp_stop_timer(im);
928 break;
929 }
930 }
931 rcu_read_unlock();
932 return false;
933}
934
935
936static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
937 int len)
938{
939 struct igmphdr *ih = igmp_hdr(skb);
940 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
941 struct ip_mc_list *im;
942 __be32 group = ih->group;
943 int max_delay;
944 int mark = 0;
945 struct net *net = dev_net(in_dev->dev);
946
947
948 if (len == 8) {
949 if (ih->code == 0) {
950
951
952 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
953 in_dev->mr_v1_seen = jiffies +
954 (in_dev->mr_qrv * in_dev->mr_qi) +
955 in_dev->mr_qri;
956 group = 0;
957 } else {
958
959 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
960 in_dev->mr_v2_seen = jiffies +
961 (in_dev->mr_qrv * in_dev->mr_qi) +
962 in_dev->mr_qri;
963 }
964
965 in_dev->mr_ifc_count = 0;
966 if (del_timer(&in_dev->mr_ifc_timer))
967 __in_dev_put(in_dev);
968
969 igmpv3_clear_delrec(in_dev);
970 } else if (len < 12) {
971 return true;
972 } else if (IGMP_V1_SEEN(in_dev)) {
973
974 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
975 group = 0;
976 } else if (IGMP_V2_SEEN(in_dev)) {
977
978
979
980
981
982
983 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
984 if (!max_delay)
985 max_delay = 1;
986 } else {
987 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
988 return true;
989
990 ih3 = igmpv3_query_hdr(skb);
991 if (ih3->nsrcs) {
992 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
993 + ntohs(ih3->nsrcs)*sizeof(__be32)))
994 return true;
995 ih3 = igmpv3_query_hdr(skb);
996 }
997
998 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
999 if (!max_delay)
1000 max_delay = 1;
1001 in_dev->mr_maxdelay = max_delay;
1002
1003
1004
1005
1006
1007 in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
1008 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
1009
1010
1011
1012
1013
1014 if (in_dev->mr_qri >= in_dev->mr_qi)
1015 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
1016
1017 if (!group) {
1018 if (ih3->nsrcs)
1019 return true;
1020 igmp_gq_start_timer(in_dev);
1021 return false;
1022 }
1023
1024 mark = ih3->nsrcs != 0;
1025 }
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 rcu_read_lock();
1038 for_each_pmc_rcu(in_dev, im) {
1039 int changed;
1040
1041 if (group && group != im->multiaddr)
1042 continue;
1043 if (im->multiaddr == IGMP_ALL_HOSTS)
1044 continue;
1045 if (ipv4_is_local_multicast(im->multiaddr) &&
1046 !net->ipv4.sysctl_igmp_llm_reports)
1047 continue;
1048 spin_lock_bh(&im->lock);
1049 if (im->tm_running)
1050 im->gsquery = im->gsquery && mark;
1051 else
1052 im->gsquery = mark;
1053 changed = !im->gsquery ||
1054 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
1055 spin_unlock_bh(&im->lock);
1056 if (changed)
1057 igmp_mod_timer(im, max_delay);
1058 }
1059 rcu_read_unlock();
1060 return false;
1061}
1062
1063
1064int igmp_rcv(struct sk_buff *skb)
1065{
1066
1067 struct igmphdr *ih;
1068 struct net_device *dev = skb->dev;
1069 struct in_device *in_dev;
1070 int len = skb->len;
1071 bool dropped = true;
1072
1073 if (netif_is_l3_master(dev)) {
1074 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1075 if (!dev)
1076 goto drop;
1077 }
1078
1079 in_dev = __in_dev_get_rcu(dev);
1080 if (!in_dev)
1081 goto drop;
1082
1083 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1084 goto drop;
1085
1086 if (skb_checksum_simple_validate(skb))
1087 goto drop;
1088
1089 ih = igmp_hdr(skb);
1090 switch (ih->type) {
1091 case IGMP_HOST_MEMBERSHIP_QUERY:
1092 dropped = igmp_heard_query(in_dev, skb, len);
1093 break;
1094 case IGMP_HOST_MEMBERSHIP_REPORT:
1095 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1096
1097 if (rt_is_output_route(skb_rtable(skb)))
1098 break;
1099
1100 if (skb->pkt_type == PACKET_MULTICAST ||
1101 skb->pkt_type == PACKET_BROADCAST)
1102 dropped = igmp_heard_report(in_dev, ih->group);
1103 break;
1104 case IGMP_PIM:
1105#ifdef CONFIG_IP_PIMSM_V1
1106 return pim_rcv_v1(skb);
1107#endif
1108 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1109 case IGMP_DVMRP:
1110 case IGMP_TRACE:
1111 case IGMP_HOST_LEAVE_MESSAGE:
1112 case IGMP_MTRACE:
1113 case IGMP_MTRACE_RESP:
1114 break;
1115 default:
1116 break;
1117 }
1118
1119drop:
1120 if (dropped)
1121 kfree_skb(skb);
1122 else
1123 consume_skb(skb);
1124 return 0;
1125}
1126
1127#endif
1128
1129
1130
1131
1132
1133
1134static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1135{
1136 char buf[MAX_ADDR_LEN];
1137 struct net_device *dev = in_dev->dev;
1138
1139
1140
1141
1142
1143
1144
1145
1146 if (arp_mc_map(addr, buf, dev, 0) == 0)
1147 dev_mc_add(dev, buf);
1148}
1149
1150
1151
1152
1153
1154static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1155{
1156 char buf[MAX_ADDR_LEN];
1157 struct net_device *dev = in_dev->dev;
1158
1159 if (arp_mc_map(addr, buf, dev, 0) == 0)
1160 dev_mc_del(dev, buf);
1161}
1162
1163#ifdef CONFIG_IP_MULTICAST
1164
1165
1166
1167static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1168{
1169 struct ip_mc_list *pmc;
1170 struct net *net = dev_net(in_dev->dev);
1171
1172
1173
1174
1175
1176
1177
1178 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1179 if (!pmc)
1180 return;
1181 spin_lock_init(&pmc->lock);
1182 spin_lock_bh(&im->lock);
1183 pmc->interface = im->interface;
1184 in_dev_hold(in_dev);
1185 pmc->multiaddr = im->multiaddr;
1186 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1187 pmc->sfmode = im->sfmode;
1188 if (pmc->sfmode == MCAST_INCLUDE) {
1189 struct ip_sf_list *psf;
1190
1191 pmc->tomb = im->tomb;
1192 pmc->sources = im->sources;
1193 im->tomb = im->sources = NULL;
1194 for (psf = pmc->sources; psf; psf = psf->sf_next)
1195 psf->sf_crcount = pmc->crcount;
1196 }
1197 spin_unlock_bh(&im->lock);
1198
1199 spin_lock_bh(&in_dev->mc_tomb_lock);
1200 pmc->next = in_dev->mc_tomb;
1201 in_dev->mc_tomb = pmc;
1202 spin_unlock_bh(&in_dev->mc_tomb_lock);
1203}
1204
1205
1206
1207
1208static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1209{
1210 struct ip_mc_list *pmc, *pmc_prev;
1211 struct ip_sf_list *psf;
1212 struct net *net = dev_net(in_dev->dev);
1213 __be32 multiaddr = im->multiaddr;
1214
1215 spin_lock_bh(&in_dev->mc_tomb_lock);
1216 pmc_prev = NULL;
1217 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1218 if (pmc->multiaddr == multiaddr)
1219 break;
1220 pmc_prev = pmc;
1221 }
1222 if (pmc) {
1223 if (pmc_prev)
1224 pmc_prev->next = pmc->next;
1225 else
1226 in_dev->mc_tomb = pmc->next;
1227 }
1228 spin_unlock_bh(&in_dev->mc_tomb_lock);
1229
1230 spin_lock_bh(&im->lock);
1231 if (pmc) {
1232 im->interface = pmc->interface;
1233 if (im->sfmode == MCAST_INCLUDE) {
1234 swap(im->tomb, pmc->tomb);
1235 swap(im->sources, pmc->sources);
1236 for (psf = im->sources; psf; psf = psf->sf_next)
1237 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1238 } else {
1239 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1240 }
1241 in_dev_put(pmc->interface);
1242 kfree_pmc(pmc);
1243 }
1244 spin_unlock_bh(&im->lock);
1245}
1246
1247
1248
1249
1250static void igmpv3_clear_delrec(struct in_device *in_dev)
1251{
1252 struct ip_mc_list *pmc, *nextpmc;
1253
1254 spin_lock_bh(&in_dev->mc_tomb_lock);
1255 pmc = in_dev->mc_tomb;
1256 in_dev->mc_tomb = NULL;
1257 spin_unlock_bh(&in_dev->mc_tomb_lock);
1258
1259 for (; pmc; pmc = nextpmc) {
1260 nextpmc = pmc->next;
1261 ip_mc_clear_src(pmc);
1262 in_dev_put(pmc->interface);
1263 kfree_pmc(pmc);
1264 }
1265
1266 rcu_read_lock();
1267 for_each_pmc_rcu(in_dev, pmc) {
1268 struct ip_sf_list *psf;
1269
1270 spin_lock_bh(&pmc->lock);
1271 psf = pmc->tomb;
1272 pmc->tomb = NULL;
1273 spin_unlock_bh(&pmc->lock);
1274 ip_sf_list_clear_all(psf);
1275 }
1276 rcu_read_unlock();
1277}
1278#endif
1279
1280static void igmp_group_dropped(struct ip_mc_list *im)
1281{
1282 struct in_device *in_dev = im->interface;
1283#ifdef CONFIG_IP_MULTICAST
1284 struct net *net = dev_net(in_dev->dev);
1285 int reporter;
1286#endif
1287
1288 if (im->loaded) {
1289 im->loaded = 0;
1290 ip_mc_filter_del(in_dev, im->multiaddr);
1291 }
1292
1293#ifdef CONFIG_IP_MULTICAST
1294 if (im->multiaddr == IGMP_ALL_HOSTS)
1295 return;
1296 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1297 return;
1298
1299 reporter = im->reporter;
1300 igmp_stop_timer(im);
1301
1302 if (!in_dev->dead) {
1303 if (IGMP_V1_SEEN(in_dev))
1304 return;
1305 if (IGMP_V2_SEEN(in_dev)) {
1306 if (reporter)
1307 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1308 return;
1309 }
1310
1311 igmpv3_add_delrec(in_dev, im);
1312
1313 igmp_ifc_event(in_dev);
1314 }
1315#endif
1316}
1317
1318static void igmp_group_added(struct ip_mc_list *im, unsigned int mode)
1319{
1320 struct in_device *in_dev = im->interface;
1321#ifdef CONFIG_IP_MULTICAST
1322 struct net *net = dev_net(in_dev->dev);
1323#endif
1324
1325 if (im->loaded == 0) {
1326 im->loaded = 1;
1327 ip_mc_filter_add(in_dev, im->multiaddr);
1328 }
1329
1330#ifdef CONFIG_IP_MULTICAST
1331 if (im->multiaddr == IGMP_ALL_HOSTS)
1332 return;
1333 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1334 return;
1335
1336 if (in_dev->dead)
1337 return;
1338
1339 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1340 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1341 spin_lock_bh(&im->lock);
1342 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1343 spin_unlock_bh(&im->lock);
1344 return;
1345 }
1346
1347
1348
1349
1350
1351
1352 if (mode == MCAST_EXCLUDE)
1353 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1354
1355 igmp_ifc_event(in_dev);
1356#endif
1357}
1358
1359
1360
1361
1362
1363
1364static u32 ip_mc_hash(const struct ip_mc_list *im)
1365{
1366 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1367}
1368
1369static void ip_mc_hash_add(struct in_device *in_dev,
1370 struct ip_mc_list *im)
1371{
1372 struct ip_mc_list __rcu **mc_hash;
1373 u32 hash;
1374
1375 mc_hash = rtnl_dereference(in_dev->mc_hash);
1376 if (mc_hash) {
1377 hash = ip_mc_hash(im);
1378 im->next_hash = mc_hash[hash];
1379 rcu_assign_pointer(mc_hash[hash], im);
1380 return;
1381 }
1382
1383
1384 if (in_dev->mc_count < 4)
1385 return;
1386
1387 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1388 GFP_KERNEL);
1389 if (!mc_hash)
1390 return;
1391
1392 for_each_pmc_rtnl(in_dev, im) {
1393 hash = ip_mc_hash(im);
1394 im->next_hash = mc_hash[hash];
1395 RCU_INIT_POINTER(mc_hash[hash], im);
1396 }
1397
1398 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1399}
1400
1401static void ip_mc_hash_remove(struct in_device *in_dev,
1402 struct ip_mc_list *im)
1403{
1404 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1405 struct ip_mc_list *aux;
1406
1407 if (!mc_hash)
1408 return;
1409 mc_hash += ip_mc_hash(im);
1410 while ((aux = rtnl_dereference(*mc_hash)) != im)
1411 mc_hash = &aux->next_hash;
1412 *mc_hash = im->next_hash;
1413}
1414
1415
1416
1417
1418
1419static void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1420 unsigned int mode)
1421{
1422 struct ip_mc_list *im;
1423
1424 ASSERT_RTNL();
1425
1426 for_each_pmc_rtnl(in_dev, im) {
1427 if (im->multiaddr == addr) {
1428 im->users++;
1429 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
1430 goto out;
1431 }
1432 }
1433
1434 im = kzalloc(sizeof(*im), GFP_KERNEL);
1435 if (!im)
1436 goto out;
1437
1438 im->users = 1;
1439 im->interface = in_dev;
1440 in_dev_hold(in_dev);
1441 im->multiaddr = addr;
1442
1443 im->sfmode = mode;
1444 im->sfcount[mode] = 1;
1445 refcount_set(&im->refcnt, 1);
1446 spin_lock_init(&im->lock);
1447#ifdef CONFIG_IP_MULTICAST
1448 timer_setup(&im->timer, igmp_timer_expire, 0);
1449#endif
1450
1451 im->next_rcu = in_dev->mc_list;
1452 in_dev->mc_count++;
1453 rcu_assign_pointer(in_dev->mc_list, im);
1454
1455 ip_mc_hash_add(in_dev, im);
1456
1457#ifdef CONFIG_IP_MULTICAST
1458 igmpv3_del_delrec(in_dev, im);
1459#endif
1460 igmp_group_added(im, mode);
1461 if (!in_dev->dead)
1462 ip_rt_multicast_event(in_dev);
1463out:
1464 return;
1465}
1466
1467void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1468{
1469 __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE);
1470}
1471EXPORT_SYMBOL(ip_mc_inc_group);
1472
1473static int ip_mc_check_iphdr(struct sk_buff *skb)
1474{
1475 const struct iphdr *iph;
1476 unsigned int len;
1477 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1478
1479 if (!pskb_may_pull(skb, offset))
1480 return -EINVAL;
1481
1482 iph = ip_hdr(skb);
1483
1484 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1485 return -EINVAL;
1486
1487 offset += ip_hdrlen(skb) - sizeof(*iph);
1488
1489 if (!pskb_may_pull(skb, offset))
1490 return -EINVAL;
1491
1492 iph = ip_hdr(skb);
1493
1494 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1495 return -EINVAL;
1496
1497 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1498 if (skb->len < len || len < offset)
1499 return -EINVAL;
1500
1501 skb_set_transport_header(skb, offset);
1502
1503 return 0;
1504}
1505
1506static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1507{
1508 unsigned int len = skb_transport_offset(skb);
1509
1510 len += sizeof(struct igmpv3_report);
1511
1512 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1513}
1514
1515static int ip_mc_check_igmp_query(struct sk_buff *skb)
1516{
1517 unsigned int len = skb_transport_offset(skb);
1518
1519 len += sizeof(struct igmphdr);
1520 if (skb->len < len)
1521 return -EINVAL;
1522
1523
1524 if (skb->len != len) {
1525
1526 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1527 if (skb->len < len || !pskb_may_pull(skb, len))
1528 return -EINVAL;
1529 }
1530
1531
1532
1533
1534 if (!igmp_hdr(skb)->group &&
1535 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1536 return -EINVAL;
1537
1538 return 0;
1539}
1540
1541static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1542{
1543 switch (igmp_hdr(skb)->type) {
1544 case IGMP_HOST_LEAVE_MESSAGE:
1545 case IGMP_HOST_MEMBERSHIP_REPORT:
1546 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1547
1548 return 0;
1549 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1550 return ip_mc_check_igmp_reportv3(skb);
1551 case IGMP_HOST_MEMBERSHIP_QUERY:
1552 return ip_mc_check_igmp_query(skb);
1553 default:
1554 return -ENOMSG;
1555 }
1556}
1557
1558static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1559{
1560 return skb_checksum_simple_validate(skb);
1561}
1562
1563static int __ip_mc_check_igmp(struct sk_buff *skb)
1564
1565{
1566 struct sk_buff *skb_chk;
1567 unsigned int transport_len;
1568 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1569 int ret = -EINVAL;
1570
1571 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1572
1573 skb_chk = skb_checksum_trimmed(skb, transport_len,
1574 ip_mc_validate_checksum);
1575 if (!skb_chk)
1576 goto err;
1577
1578 if (!pskb_may_pull(skb_chk, len))
1579 goto err;
1580
1581 ret = ip_mc_check_igmp_msg(skb_chk);
1582 if (ret)
1583 goto err;
1584
1585 ret = 0;
1586
1587err:
1588 if (skb_chk && skb_chk != skb)
1589 kfree_skb(skb_chk);
1590
1591 return ret;
1592}
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609int ip_mc_check_igmp(struct sk_buff *skb)
1610{
1611 int ret = ip_mc_check_iphdr(skb);
1612
1613 if (ret < 0)
1614 return ret;
1615
1616 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1617 return -ENOMSG;
1618
1619 return __ip_mc_check_igmp(skb);
1620}
1621EXPORT_SYMBOL(ip_mc_check_igmp);
1622
1623
1624
1625
1626static void ip_mc_rejoin_groups(struct in_device *in_dev)
1627{
1628#ifdef CONFIG_IP_MULTICAST
1629 struct ip_mc_list *im;
1630 int type;
1631 struct net *net = dev_net(in_dev->dev);
1632
1633 ASSERT_RTNL();
1634
1635 for_each_pmc_rtnl(in_dev, im) {
1636 if (im->multiaddr == IGMP_ALL_HOSTS)
1637 continue;
1638 if (ipv4_is_local_multicast(im->multiaddr) &&
1639 !net->ipv4.sysctl_igmp_llm_reports)
1640 continue;
1641
1642
1643
1644
1645 if (IGMP_V1_SEEN(in_dev))
1646 type = IGMP_HOST_MEMBERSHIP_REPORT;
1647 else if (IGMP_V2_SEEN(in_dev))
1648 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1649 else
1650 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1651 igmp_send_report(in_dev, im, type);
1652 }
1653#endif
1654}
1655
1656
1657
1658
1659
1660void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1661{
1662 struct ip_mc_list *i;
1663 struct ip_mc_list __rcu **ip;
1664
1665 ASSERT_RTNL();
1666
1667 for (ip = &in_dev->mc_list;
1668 (i = rtnl_dereference(*ip)) != NULL;
1669 ip = &i->next_rcu) {
1670 if (i->multiaddr == addr) {
1671 if (--i->users == 0) {
1672 ip_mc_hash_remove(in_dev, i);
1673 *ip = i->next_rcu;
1674 in_dev->mc_count--;
1675 igmp_group_dropped(i);
1676 ip_mc_clear_src(i);
1677
1678 if (!in_dev->dead)
1679 ip_rt_multicast_event(in_dev);
1680
1681 ip_ma_put(i);
1682 return;
1683 }
1684 break;
1685 }
1686 }
1687}
1688EXPORT_SYMBOL(ip_mc_dec_group);
1689
1690
1691
1692void ip_mc_unmap(struct in_device *in_dev)
1693{
1694 struct ip_mc_list *pmc;
1695
1696 ASSERT_RTNL();
1697
1698 for_each_pmc_rtnl(in_dev, pmc)
1699 igmp_group_dropped(pmc);
1700}
1701
1702void ip_mc_remap(struct in_device *in_dev)
1703{
1704 struct ip_mc_list *pmc;
1705
1706 ASSERT_RTNL();
1707
1708 for_each_pmc_rtnl(in_dev, pmc) {
1709#ifdef CONFIG_IP_MULTICAST
1710 igmpv3_del_delrec(in_dev, pmc);
1711#endif
1712 igmp_group_added(pmc, pmc->sfmode);
1713 }
1714}
1715
1716
1717
1718void ip_mc_down(struct in_device *in_dev)
1719{
1720 struct ip_mc_list *pmc;
1721
1722 ASSERT_RTNL();
1723
1724 for_each_pmc_rtnl(in_dev, pmc)
1725 igmp_group_dropped(pmc);
1726
1727#ifdef CONFIG_IP_MULTICAST
1728 in_dev->mr_ifc_count = 0;
1729 if (del_timer(&in_dev->mr_ifc_timer))
1730 __in_dev_put(in_dev);
1731 in_dev->mr_gq_running = 0;
1732 if (del_timer(&in_dev->mr_gq_timer))
1733 __in_dev_put(in_dev);
1734#endif
1735
1736 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1737}
1738
1739#ifdef CONFIG_IP_MULTICAST
1740static void ip_mc_reset(struct in_device *in_dev)
1741{
1742 struct net *net = dev_net(in_dev->dev);
1743
1744 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1745 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1746 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1747}
1748#else
1749static void ip_mc_reset(struct in_device *in_dev)
1750{
1751}
1752#endif
1753
1754void ip_mc_init_dev(struct in_device *in_dev)
1755{
1756 ASSERT_RTNL();
1757
1758#ifdef CONFIG_IP_MULTICAST
1759 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1760 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
1761#endif
1762 ip_mc_reset(in_dev);
1763
1764 spin_lock_init(&in_dev->mc_tomb_lock);
1765}
1766
1767
1768
1769void ip_mc_up(struct in_device *in_dev)
1770{
1771 struct ip_mc_list *pmc;
1772
1773 ASSERT_RTNL();
1774
1775 ip_mc_reset(in_dev);
1776 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1777
1778 for_each_pmc_rtnl(in_dev, pmc) {
1779#ifdef CONFIG_IP_MULTICAST
1780 igmpv3_del_delrec(in_dev, pmc);
1781#endif
1782 igmp_group_added(pmc, pmc->sfmode);
1783 }
1784}
1785
1786
1787
1788
1789
1790void ip_mc_destroy_dev(struct in_device *in_dev)
1791{
1792 struct ip_mc_list *i;
1793
1794 ASSERT_RTNL();
1795
1796
1797 ip_mc_down(in_dev);
1798#ifdef CONFIG_IP_MULTICAST
1799 igmpv3_clear_delrec(in_dev);
1800#endif
1801
1802 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1803 in_dev->mc_list = i->next_rcu;
1804 in_dev->mc_count--;
1805 ip_ma_put(i);
1806 }
1807}
1808
1809
1810static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1811{
1812 struct net_device *dev = NULL;
1813 struct in_device *idev = NULL;
1814
1815 if (imr->imr_ifindex) {
1816 idev = inetdev_by_index(net, imr->imr_ifindex);
1817 return idev;
1818 }
1819 if (imr->imr_address.s_addr) {
1820 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1821 if (!dev)
1822 return NULL;
1823 }
1824
1825 if (!dev) {
1826 struct rtable *rt = ip_route_output(net,
1827 imr->imr_multiaddr.s_addr,
1828 0, 0, 0);
1829 if (!IS_ERR(rt)) {
1830 dev = rt->dst.dev;
1831 ip_rt_put(rt);
1832 }
1833 }
1834 if (dev) {
1835 imr->imr_ifindex = dev->ifindex;
1836 idev = __in_dev_get_rtnl(dev);
1837 }
1838 return idev;
1839}
1840
1841
1842
1843
1844
1845static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1846 __be32 *psfsrc)
1847{
1848 struct ip_sf_list *psf, *psf_prev;
1849 int rv = 0;
1850
1851 psf_prev = NULL;
1852 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1853 if (psf->sf_inaddr == *psfsrc)
1854 break;
1855 psf_prev = psf;
1856 }
1857 if (!psf || psf->sf_count[sfmode] == 0) {
1858
1859 return -ESRCH;
1860 }
1861 psf->sf_count[sfmode]--;
1862 if (psf->sf_count[sfmode] == 0) {
1863 ip_rt_multicast_event(pmc->interface);
1864 }
1865 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1866#ifdef CONFIG_IP_MULTICAST
1867 struct in_device *in_dev = pmc->interface;
1868 struct net *net = dev_net(in_dev->dev);
1869#endif
1870
1871
1872 if (psf_prev)
1873 psf_prev->sf_next = psf->sf_next;
1874 else
1875 pmc->sources = psf->sf_next;
1876#ifdef CONFIG_IP_MULTICAST
1877 if (psf->sf_oldin &&
1878 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1879 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1880 psf->sf_next = pmc->tomb;
1881 pmc->tomb = psf;
1882 rv = 1;
1883 } else
1884#endif
1885 kfree(psf);
1886 }
1887 return rv;
1888}
1889
1890#ifndef CONFIG_IP_MULTICAST
1891#define igmp_ifc_event(x) do { } while (0)
1892#endif
1893
1894static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1895 int sfcount, __be32 *psfsrc, int delta)
1896{
1897 struct ip_mc_list *pmc;
1898 int changerec = 0;
1899 int i, err;
1900
1901 if (!in_dev)
1902 return -ENODEV;
1903 rcu_read_lock();
1904 for_each_pmc_rcu(in_dev, pmc) {
1905 if (*pmca == pmc->multiaddr)
1906 break;
1907 }
1908 if (!pmc) {
1909
1910 rcu_read_unlock();
1911 return -ESRCH;
1912 }
1913 spin_lock_bh(&pmc->lock);
1914 rcu_read_unlock();
1915#ifdef CONFIG_IP_MULTICAST
1916 sf_markstate(pmc);
1917#endif
1918 if (!delta) {
1919 err = -EINVAL;
1920 if (!pmc->sfcount[sfmode])
1921 goto out_unlock;
1922 pmc->sfcount[sfmode]--;
1923 }
1924 err = 0;
1925 for (i = 0; i < sfcount; i++) {
1926 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1927
1928 changerec |= rv > 0;
1929 if (!err && rv < 0)
1930 err = rv;
1931 }
1932 if (pmc->sfmode == MCAST_EXCLUDE &&
1933 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1934 pmc->sfcount[MCAST_INCLUDE]) {
1935#ifdef CONFIG_IP_MULTICAST
1936 struct ip_sf_list *psf;
1937 struct net *net = dev_net(in_dev->dev);
1938#endif
1939
1940
1941 pmc->sfmode = MCAST_INCLUDE;
1942#ifdef CONFIG_IP_MULTICAST
1943 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1944 in_dev->mr_ifc_count = pmc->crcount;
1945 for (psf = pmc->sources; psf; psf = psf->sf_next)
1946 psf->sf_crcount = 0;
1947 igmp_ifc_event(pmc->interface);
1948 } else if (sf_setstate(pmc) || changerec) {
1949 igmp_ifc_event(pmc->interface);
1950#endif
1951 }
1952out_unlock:
1953 spin_unlock_bh(&pmc->lock);
1954 return err;
1955}
1956
1957
1958
1959
1960static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1961 __be32 *psfsrc)
1962{
1963 struct ip_sf_list *psf, *psf_prev;
1964
1965 psf_prev = NULL;
1966 for (psf = pmc->sources; psf; psf = psf->sf_next) {
1967 if (psf->sf_inaddr == *psfsrc)
1968 break;
1969 psf_prev = psf;
1970 }
1971 if (!psf) {
1972 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1973 if (!psf)
1974 return -ENOBUFS;
1975 psf->sf_inaddr = *psfsrc;
1976 if (psf_prev) {
1977 psf_prev->sf_next = psf;
1978 } else
1979 pmc->sources = psf;
1980 }
1981 psf->sf_count[sfmode]++;
1982 if (psf->sf_count[sfmode] == 1) {
1983 ip_rt_multicast_event(pmc->interface);
1984 }
1985 return 0;
1986}
1987
1988#ifdef CONFIG_IP_MULTICAST
1989static void sf_markstate(struct ip_mc_list *pmc)
1990{
1991 struct ip_sf_list *psf;
1992 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1993
1994 for (psf = pmc->sources; psf; psf = psf->sf_next)
1995 if (pmc->sfcount[MCAST_EXCLUDE]) {
1996 psf->sf_oldin = mca_xcount ==
1997 psf->sf_count[MCAST_EXCLUDE] &&
1998 !psf->sf_count[MCAST_INCLUDE];
1999 } else
2000 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2001}
2002
2003static int sf_setstate(struct ip_mc_list *pmc)
2004{
2005 struct ip_sf_list *psf, *dpsf;
2006 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2007 int qrv = pmc->interface->mr_qrv;
2008 int new_in, rv;
2009
2010 rv = 0;
2011 for (psf = pmc->sources; psf; psf = psf->sf_next) {
2012 if (pmc->sfcount[MCAST_EXCLUDE]) {
2013 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2014 !psf->sf_count[MCAST_INCLUDE];
2015 } else
2016 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2017 if (new_in) {
2018 if (!psf->sf_oldin) {
2019 struct ip_sf_list *prev = NULL;
2020
2021 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2022 if (dpsf->sf_inaddr == psf->sf_inaddr)
2023 break;
2024 prev = dpsf;
2025 }
2026 if (dpsf) {
2027 if (prev)
2028 prev->sf_next = dpsf->sf_next;
2029 else
2030 pmc->tomb = dpsf->sf_next;
2031 kfree(dpsf);
2032 }
2033 psf->sf_crcount = qrv;
2034 rv++;
2035 }
2036 } else if (psf->sf_oldin) {
2037
2038 psf->sf_crcount = 0;
2039
2040
2041
2042
2043 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2044 if (dpsf->sf_inaddr == psf->sf_inaddr)
2045 break;
2046 if (!dpsf) {
2047 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2048 if (!dpsf)
2049 continue;
2050 *dpsf = *psf;
2051
2052 dpsf->sf_next = pmc->tomb;
2053 pmc->tomb = dpsf;
2054 }
2055 dpsf->sf_crcount = qrv;
2056 rv++;
2057 }
2058 }
2059 return rv;
2060}
2061#endif
2062
2063
2064
2065
2066static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2067 int sfcount, __be32 *psfsrc, int delta)
2068{
2069 struct ip_mc_list *pmc;
2070 int isexclude;
2071 int i, err;
2072
2073 if (!in_dev)
2074 return -ENODEV;
2075 rcu_read_lock();
2076 for_each_pmc_rcu(in_dev, pmc) {
2077 if (*pmca == pmc->multiaddr)
2078 break;
2079 }
2080 if (!pmc) {
2081
2082 rcu_read_unlock();
2083 return -ESRCH;
2084 }
2085 spin_lock_bh(&pmc->lock);
2086 rcu_read_unlock();
2087
2088#ifdef CONFIG_IP_MULTICAST
2089 sf_markstate(pmc);
2090#endif
2091 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2092 if (!delta)
2093 pmc->sfcount[sfmode]++;
2094 err = 0;
2095 for (i = 0; i < sfcount; i++) {
2096 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2097 if (err)
2098 break;
2099 }
2100 if (err) {
2101 int j;
2102
2103 if (!delta)
2104 pmc->sfcount[sfmode]--;
2105 for (j = 0; j < i; j++)
2106 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2107 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2108#ifdef CONFIG_IP_MULTICAST
2109 struct ip_sf_list *psf;
2110 struct net *net = dev_net(pmc->interface->dev);
2111 in_dev = pmc->interface;
2112#endif
2113
2114
2115 if (pmc->sfcount[MCAST_EXCLUDE])
2116 pmc->sfmode = MCAST_EXCLUDE;
2117 else if (pmc->sfcount[MCAST_INCLUDE])
2118 pmc->sfmode = MCAST_INCLUDE;
2119#ifdef CONFIG_IP_MULTICAST
2120
2121
2122 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2123 in_dev->mr_ifc_count = pmc->crcount;
2124 for (psf = pmc->sources; psf; psf = psf->sf_next)
2125 psf->sf_crcount = 0;
2126 igmp_ifc_event(in_dev);
2127 } else if (sf_setstate(pmc)) {
2128 igmp_ifc_event(in_dev);
2129#endif
2130 }
2131 spin_unlock_bh(&pmc->lock);
2132 return err;
2133}
2134
2135static void ip_mc_clear_src(struct ip_mc_list *pmc)
2136{
2137 struct ip_sf_list *tomb, *sources;
2138
2139 spin_lock_bh(&pmc->lock);
2140 tomb = pmc->tomb;
2141 pmc->tomb = NULL;
2142 sources = pmc->sources;
2143 pmc->sources = NULL;
2144 pmc->sfmode = MCAST_EXCLUDE;
2145 pmc->sfcount[MCAST_INCLUDE] = 0;
2146 pmc->sfcount[MCAST_EXCLUDE] = 1;
2147 spin_unlock_bh(&pmc->lock);
2148
2149 ip_sf_list_clear_all(tomb);
2150 ip_sf_list_clear_all(sources);
2151}
2152
2153
2154
2155static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2156 unsigned int mode)
2157{
2158 __be32 addr = imr->imr_multiaddr.s_addr;
2159 struct ip_mc_socklist *iml, *i;
2160 struct in_device *in_dev;
2161 struct inet_sock *inet = inet_sk(sk);
2162 struct net *net = sock_net(sk);
2163 int ifindex;
2164 int count = 0;
2165 int err;
2166
2167 ASSERT_RTNL();
2168
2169 if (!ipv4_is_multicast(addr))
2170 return -EINVAL;
2171
2172 in_dev = ip_mc_find_dev(net, imr);
2173
2174 if (!in_dev) {
2175 err = -ENODEV;
2176 goto done;
2177 }
2178
2179 err = -EADDRINUSE;
2180 ifindex = imr->imr_ifindex;
2181 for_each_pmc_rtnl(inet, i) {
2182 if (i->multi.imr_multiaddr.s_addr == addr &&
2183 i->multi.imr_ifindex == ifindex)
2184 goto done;
2185 count++;
2186 }
2187 err = -ENOBUFS;
2188 if (count >= net->ipv4.sysctl_igmp_max_memberships)
2189 goto done;
2190 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2191 if (!iml)
2192 goto done;
2193
2194 memcpy(&iml->multi, imr, sizeof(*imr));
2195 iml->next_rcu = inet->mc_list;
2196 iml->sflist = NULL;
2197 iml->sfmode = mode;
2198 rcu_assign_pointer(inet->mc_list, iml);
2199 __ip_mc_inc_group(in_dev, addr, mode);
2200 err = 0;
2201done:
2202 return err;
2203}
2204
2205
2206
2207int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2208{
2209 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2210}
2211EXPORT_SYMBOL(ip_mc_join_group);
2212
2213
2214
2215int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2216 unsigned int mode)
2217{
2218 return __ip_mc_join_group(sk, imr, mode);
2219}
2220
2221static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2222 struct in_device *in_dev)
2223{
2224 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2225 int err;
2226
2227 if (!psf) {
2228
2229 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2230 iml->sfmode, 0, NULL, 0);
2231 }
2232 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2233 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2234 RCU_INIT_POINTER(iml->sflist, NULL);
2235
2236 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2237 kfree_rcu(psf, rcu);
2238 return err;
2239}
2240
2241int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2242{
2243 struct inet_sock *inet = inet_sk(sk);
2244 struct ip_mc_socklist *iml;
2245 struct ip_mc_socklist __rcu **imlp;
2246 struct in_device *in_dev;
2247 struct net *net = sock_net(sk);
2248 __be32 group = imr->imr_multiaddr.s_addr;
2249 u32 ifindex;
2250 int ret = -EADDRNOTAVAIL;
2251
2252 ASSERT_RTNL();
2253
2254 in_dev = ip_mc_find_dev(net, imr);
2255 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2256 ret = -ENODEV;
2257 goto out;
2258 }
2259 ifindex = imr->imr_ifindex;
2260 for (imlp = &inet->mc_list;
2261 (iml = rtnl_dereference(*imlp)) != NULL;
2262 imlp = &iml->next_rcu) {
2263 if (iml->multi.imr_multiaddr.s_addr != group)
2264 continue;
2265 if (ifindex) {
2266 if (iml->multi.imr_ifindex != ifindex)
2267 continue;
2268 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2269 iml->multi.imr_address.s_addr)
2270 continue;
2271
2272 (void) ip_mc_leave_src(sk, iml, in_dev);
2273
2274 *imlp = iml->next_rcu;
2275
2276 if (in_dev)
2277 ip_mc_dec_group(in_dev, group);
2278
2279
2280 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2281 kfree_rcu(iml, rcu);
2282 return 0;
2283 }
2284out:
2285 return ret;
2286}
2287EXPORT_SYMBOL(ip_mc_leave_group);
2288
2289int ip_mc_source(int add, int omode, struct sock *sk, struct
2290 ip_mreq_source *mreqs, int ifindex)
2291{
2292 int err;
2293 struct ip_mreqn imr;
2294 __be32 addr = mreqs->imr_multiaddr;
2295 struct ip_mc_socklist *pmc;
2296 struct in_device *in_dev = NULL;
2297 struct inet_sock *inet = inet_sk(sk);
2298 struct ip_sf_socklist *psl;
2299 struct net *net = sock_net(sk);
2300 int leavegroup = 0;
2301 int i, j, rv;
2302
2303 if (!ipv4_is_multicast(addr))
2304 return -EINVAL;
2305
2306 ASSERT_RTNL();
2307
2308 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2309 imr.imr_address.s_addr = mreqs->imr_interface;
2310 imr.imr_ifindex = ifindex;
2311 in_dev = ip_mc_find_dev(net, &imr);
2312
2313 if (!in_dev) {
2314 err = -ENODEV;
2315 goto done;
2316 }
2317 err = -EADDRNOTAVAIL;
2318
2319 for_each_pmc_rtnl(inet, pmc) {
2320 if ((pmc->multi.imr_multiaddr.s_addr ==
2321 imr.imr_multiaddr.s_addr) &&
2322 (pmc->multi.imr_ifindex == imr.imr_ifindex))
2323 break;
2324 }
2325 if (!pmc) {
2326 err = -EINVAL;
2327 goto done;
2328 }
2329
2330 if (pmc->sflist) {
2331 if (pmc->sfmode != omode) {
2332 err = -EINVAL;
2333 goto done;
2334 }
2335 } else if (pmc->sfmode != omode) {
2336
2337 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2338 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2339 NULL, 0);
2340 pmc->sfmode = omode;
2341 }
2342
2343 psl = rtnl_dereference(pmc->sflist);
2344 if (!add) {
2345 if (!psl)
2346 goto done;
2347 rv = !0;
2348 for (i = 0; i < psl->sl_count; i++) {
2349 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2350 sizeof(__be32));
2351 if (rv == 0)
2352 break;
2353 }
2354 if (rv)
2355 goto done;
2356
2357
2358 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2359 leavegroup = 1;
2360 goto done;
2361 }
2362
2363
2364 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2365 &mreqs->imr_sourceaddr, 1);
2366
2367 for (j = i+1; j < psl->sl_count; j++)
2368 psl->sl_addr[j-1] = psl->sl_addr[j];
2369 psl->sl_count--;
2370 err = 0;
2371 goto done;
2372 }
2373
2374
2375 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2376 err = -ENOBUFS;
2377 goto done;
2378 }
2379 if (!psl || psl->sl_count == psl->sl_max) {
2380 struct ip_sf_socklist *newpsl;
2381 int count = IP_SFBLOCK;
2382
2383 if (psl)
2384 count += psl->sl_max;
2385 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2386 if (!newpsl) {
2387 err = -ENOBUFS;
2388 goto done;
2389 }
2390 newpsl->sl_max = count;
2391 newpsl->sl_count = count - IP_SFBLOCK;
2392 if (psl) {
2393 for (i = 0; i < psl->sl_count; i++)
2394 newpsl->sl_addr[i] = psl->sl_addr[i];
2395
2396 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2397 kfree_rcu(psl, rcu);
2398 }
2399 rcu_assign_pointer(pmc->sflist, newpsl);
2400 psl = newpsl;
2401 }
2402 rv = 1;
2403 for (i = 0; i < psl->sl_count; i++) {
2404 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2405 sizeof(__be32));
2406 if (rv == 0)
2407 break;
2408 }
2409 if (rv == 0)
2410 goto done;
2411 for (j = psl->sl_count-1; j >= i; j--)
2412 psl->sl_addr[j+1] = psl->sl_addr[j];
2413 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2414 psl->sl_count++;
2415 err = 0;
2416
2417 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2418 &mreqs->imr_sourceaddr, 1);
2419done:
2420 if (leavegroup)
2421 err = ip_mc_leave_group(sk, &imr);
2422 return err;
2423}
2424
2425int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2426{
2427 int err = 0;
2428 struct ip_mreqn imr;
2429 __be32 addr = msf->imsf_multiaddr;
2430 struct ip_mc_socklist *pmc;
2431 struct in_device *in_dev;
2432 struct inet_sock *inet = inet_sk(sk);
2433 struct ip_sf_socklist *newpsl, *psl;
2434 struct net *net = sock_net(sk);
2435 int leavegroup = 0;
2436
2437 if (!ipv4_is_multicast(addr))
2438 return -EINVAL;
2439 if (msf->imsf_fmode != MCAST_INCLUDE &&
2440 msf->imsf_fmode != MCAST_EXCLUDE)
2441 return -EINVAL;
2442
2443 ASSERT_RTNL();
2444
2445 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2446 imr.imr_address.s_addr = msf->imsf_interface;
2447 imr.imr_ifindex = ifindex;
2448 in_dev = ip_mc_find_dev(net, &imr);
2449
2450 if (!in_dev) {
2451 err = -ENODEV;
2452 goto done;
2453 }
2454
2455
2456 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2457 leavegroup = 1;
2458 goto done;
2459 }
2460
2461 for_each_pmc_rtnl(inet, pmc) {
2462 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2463 pmc->multi.imr_ifindex == imr.imr_ifindex)
2464 break;
2465 }
2466 if (!pmc) {
2467 err = -EINVAL;
2468 goto done;
2469 }
2470 if (msf->imsf_numsrc) {
2471 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2472 GFP_KERNEL);
2473 if (!newpsl) {
2474 err = -ENOBUFS;
2475 goto done;
2476 }
2477 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2478 memcpy(newpsl->sl_addr, msf->imsf_slist,
2479 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2480 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2481 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2482 if (err) {
2483 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2484 goto done;
2485 }
2486 } else {
2487 newpsl = NULL;
2488 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2489 msf->imsf_fmode, 0, NULL, 0);
2490 }
2491 psl = rtnl_dereference(pmc->sflist);
2492 if (psl) {
2493 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2494 psl->sl_count, psl->sl_addr, 0);
2495
2496 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2497 kfree_rcu(psl, rcu);
2498 } else
2499 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2500 0, NULL, 0);
2501 rcu_assign_pointer(pmc->sflist, newpsl);
2502 pmc->sfmode = msf->imsf_fmode;
2503 err = 0;
2504done:
2505 if (leavegroup)
2506 err = ip_mc_leave_group(sk, &imr);
2507 return err;
2508}
2509
2510int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2511 struct ip_msfilter __user *optval, int __user *optlen)
2512{
2513 int err, len, count, copycount;
2514 struct ip_mreqn imr;
2515 __be32 addr = msf->imsf_multiaddr;
2516 struct ip_mc_socklist *pmc;
2517 struct in_device *in_dev;
2518 struct inet_sock *inet = inet_sk(sk);
2519 struct ip_sf_socklist *psl;
2520 struct net *net = sock_net(sk);
2521
2522 ASSERT_RTNL();
2523
2524 if (!ipv4_is_multicast(addr))
2525 return -EINVAL;
2526
2527 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2528 imr.imr_address.s_addr = msf->imsf_interface;
2529 imr.imr_ifindex = 0;
2530 in_dev = ip_mc_find_dev(net, &imr);
2531
2532 if (!in_dev) {
2533 err = -ENODEV;
2534 goto done;
2535 }
2536 err = -EADDRNOTAVAIL;
2537
2538 for_each_pmc_rtnl(inet, pmc) {
2539 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2540 pmc->multi.imr_ifindex == imr.imr_ifindex)
2541 break;
2542 }
2543 if (!pmc)
2544 goto done;
2545 msf->imsf_fmode = pmc->sfmode;
2546 psl = rtnl_dereference(pmc->sflist);
2547 if (!psl) {
2548 len = 0;
2549 count = 0;
2550 } else {
2551 count = psl->sl_count;
2552 }
2553 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2554 len = copycount * sizeof(psl->sl_addr[0]);
2555 msf->imsf_numsrc = count;
2556 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2557 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2558 return -EFAULT;
2559 }
2560 if (len &&
2561 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2562 return -EFAULT;
2563 return 0;
2564done:
2565 return err;
2566}
2567
2568int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2569 struct group_filter __user *optval, int __user *optlen)
2570{
2571 int err, i, count, copycount;
2572 struct sockaddr_in *psin;
2573 __be32 addr;
2574 struct ip_mc_socklist *pmc;
2575 struct inet_sock *inet = inet_sk(sk);
2576 struct ip_sf_socklist *psl;
2577
2578 ASSERT_RTNL();
2579
2580 psin = (struct sockaddr_in *)&gsf->gf_group;
2581 if (psin->sin_family != AF_INET)
2582 return -EINVAL;
2583 addr = psin->sin_addr.s_addr;
2584 if (!ipv4_is_multicast(addr))
2585 return -EINVAL;
2586
2587 err = -EADDRNOTAVAIL;
2588
2589 for_each_pmc_rtnl(inet, pmc) {
2590 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2591 pmc->multi.imr_ifindex == gsf->gf_interface)
2592 break;
2593 }
2594 if (!pmc)
2595 goto done;
2596 gsf->gf_fmode = pmc->sfmode;
2597 psl = rtnl_dereference(pmc->sflist);
2598 count = psl ? psl->sl_count : 0;
2599 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2600 gsf->gf_numsrc = count;
2601 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2602 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2603 return -EFAULT;
2604 }
2605 for (i = 0; i < copycount; i++) {
2606 struct sockaddr_storage ss;
2607
2608 psin = (struct sockaddr_in *)&ss;
2609 memset(&ss, 0, sizeof(ss));
2610 psin->sin_family = AF_INET;
2611 psin->sin_addr.s_addr = psl->sl_addr[i];
2612 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2613 return -EFAULT;
2614 }
2615 return 0;
2616done:
2617 return err;
2618}
2619
2620
2621
2622
2623int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2624 int dif, int sdif)
2625{
2626 struct inet_sock *inet = inet_sk(sk);
2627 struct ip_mc_socklist *pmc;
2628 struct ip_sf_socklist *psl;
2629 int i;
2630 int ret;
2631
2632 ret = 1;
2633 if (!ipv4_is_multicast(loc_addr))
2634 goto out;
2635
2636 rcu_read_lock();
2637 for_each_pmc_rcu(inet, pmc) {
2638 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2639 (pmc->multi.imr_ifindex == dif ||
2640 (sdif && pmc->multi.imr_ifindex == sdif)))
2641 break;
2642 }
2643 ret = inet->mc_all;
2644 if (!pmc)
2645 goto unlock;
2646 psl = rcu_dereference(pmc->sflist);
2647 ret = (pmc->sfmode == MCAST_EXCLUDE);
2648 if (!psl)
2649 goto unlock;
2650
2651 for (i = 0; i < psl->sl_count; i++) {
2652 if (psl->sl_addr[i] == rmt_addr)
2653 break;
2654 }
2655 ret = 0;
2656 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2657 goto unlock;
2658 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2659 goto unlock;
2660 ret = 1;
2661unlock:
2662 rcu_read_unlock();
2663out:
2664 return ret;
2665}
2666
2667
2668
2669
2670
2671void ip_mc_drop_socket(struct sock *sk)
2672{
2673 struct inet_sock *inet = inet_sk(sk);
2674 struct ip_mc_socklist *iml;
2675 struct net *net = sock_net(sk);
2676
2677 if (!inet->mc_list)
2678 return;
2679
2680 rtnl_lock();
2681 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2682 struct in_device *in_dev;
2683
2684 inet->mc_list = iml->next_rcu;
2685 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2686 (void) ip_mc_leave_src(sk, iml, in_dev);
2687 if (in_dev)
2688 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2689
2690 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2691 kfree_rcu(iml, rcu);
2692 }
2693 rtnl_unlock();
2694}
2695
2696
2697int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2698{
2699 struct ip_mc_list *im;
2700 struct ip_mc_list __rcu **mc_hash;
2701 struct ip_sf_list *psf;
2702 int rv = 0;
2703
2704 mc_hash = rcu_dereference(in_dev->mc_hash);
2705 if (mc_hash) {
2706 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2707
2708 for (im = rcu_dereference(mc_hash[hash]);
2709 im != NULL;
2710 im = rcu_dereference(im->next_hash)) {
2711 if (im->multiaddr == mc_addr)
2712 break;
2713 }
2714 } else {
2715 for_each_pmc_rcu(in_dev, im) {
2716 if (im->multiaddr == mc_addr)
2717 break;
2718 }
2719 }
2720 if (im && proto == IPPROTO_IGMP) {
2721 rv = 1;
2722 } else if (im) {
2723 if (src_addr) {
2724 for (psf = im->sources; psf; psf = psf->sf_next) {
2725 if (psf->sf_inaddr == src_addr)
2726 break;
2727 }
2728 if (psf)
2729 rv = psf->sf_count[MCAST_INCLUDE] ||
2730 psf->sf_count[MCAST_EXCLUDE] !=
2731 im->sfcount[MCAST_EXCLUDE];
2732 else
2733 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2734 } else
2735 rv = 1;
2736 }
2737 return rv;
2738}
2739
2740#if defined(CONFIG_PROC_FS)
2741struct igmp_mc_iter_state {
2742 struct seq_net_private p;
2743 struct net_device *dev;
2744 struct in_device *in_dev;
2745};
2746
2747#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2748
2749static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2750{
2751 struct net *net = seq_file_net(seq);
2752 struct ip_mc_list *im = NULL;
2753 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2754
2755 state->in_dev = NULL;
2756 for_each_netdev_rcu(net, state->dev) {
2757 struct in_device *in_dev;
2758
2759 in_dev = __in_dev_get_rcu(state->dev);
2760 if (!in_dev)
2761 continue;
2762 im = rcu_dereference(in_dev->mc_list);
2763 if (im) {
2764 state->in_dev = in_dev;
2765 break;
2766 }
2767 }
2768 return im;
2769}
2770
2771static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2772{
2773 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2774
2775 im = rcu_dereference(im->next_rcu);
2776 while (!im) {
2777 state->dev = next_net_device_rcu(state->dev);
2778 if (!state->dev) {
2779 state->in_dev = NULL;
2780 break;
2781 }
2782 state->in_dev = __in_dev_get_rcu(state->dev);
2783 if (!state->in_dev)
2784 continue;
2785 im = rcu_dereference(state->in_dev->mc_list);
2786 }
2787 return im;
2788}
2789
2790static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2791{
2792 struct ip_mc_list *im = igmp_mc_get_first(seq);
2793 if (im)
2794 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2795 --pos;
2796 return pos ? NULL : im;
2797}
2798
2799static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2800 __acquires(rcu)
2801{
2802 rcu_read_lock();
2803 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2804}
2805
2806static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2807{
2808 struct ip_mc_list *im;
2809 if (v == SEQ_START_TOKEN)
2810 im = igmp_mc_get_first(seq);
2811 else
2812 im = igmp_mc_get_next(seq, v);
2813 ++*pos;
2814 return im;
2815}
2816
2817static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2818 __releases(rcu)
2819{
2820 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2821
2822 state->in_dev = NULL;
2823 state->dev = NULL;
2824 rcu_read_unlock();
2825}
2826
2827static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2828{
2829 if (v == SEQ_START_TOKEN)
2830 seq_puts(seq,
2831 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2832 else {
2833 struct ip_mc_list *im = (struct ip_mc_list *)v;
2834 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2835 char *querier;
2836 long delta;
2837
2838#ifdef CONFIG_IP_MULTICAST
2839 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2840 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2841 "V3";
2842#else
2843 querier = "NONE";
2844#endif
2845
2846 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2847 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2848 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2849 }
2850
2851 delta = im->timer.expires - jiffies;
2852 seq_printf(seq,
2853 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2854 im->multiaddr, im->users,
2855 im->tm_running,
2856 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2857 im->reporter);
2858 }
2859 return 0;
2860}
2861
2862static const struct seq_operations igmp_mc_seq_ops = {
2863 .start = igmp_mc_seq_start,
2864 .next = igmp_mc_seq_next,
2865 .stop = igmp_mc_seq_stop,
2866 .show = igmp_mc_seq_show,
2867};
2868
2869struct igmp_mcf_iter_state {
2870 struct seq_net_private p;
2871 struct net_device *dev;
2872 struct in_device *idev;
2873 struct ip_mc_list *im;
2874};
2875
2876#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2877
2878static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2879{
2880 struct net *net = seq_file_net(seq);
2881 struct ip_sf_list *psf = NULL;
2882 struct ip_mc_list *im = NULL;
2883 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2884
2885 state->idev = NULL;
2886 state->im = NULL;
2887 for_each_netdev_rcu(net, state->dev) {
2888 struct in_device *idev;
2889 idev = __in_dev_get_rcu(state->dev);
2890 if (unlikely(!idev))
2891 continue;
2892 im = rcu_dereference(idev->mc_list);
2893 if (likely(im)) {
2894 spin_lock_bh(&im->lock);
2895 psf = im->sources;
2896 if (likely(psf)) {
2897 state->im = im;
2898 state->idev = idev;
2899 break;
2900 }
2901 spin_unlock_bh(&im->lock);
2902 }
2903 }
2904 return psf;
2905}
2906
2907static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2908{
2909 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2910
2911 psf = psf->sf_next;
2912 while (!psf) {
2913 spin_unlock_bh(&state->im->lock);
2914 state->im = state->im->next;
2915 while (!state->im) {
2916 state->dev = next_net_device_rcu(state->dev);
2917 if (!state->dev) {
2918 state->idev = NULL;
2919 goto out;
2920 }
2921 state->idev = __in_dev_get_rcu(state->dev);
2922 if (!state->idev)
2923 continue;
2924 state->im = rcu_dereference(state->idev->mc_list);
2925 }
2926 if (!state->im)
2927 break;
2928 spin_lock_bh(&state->im->lock);
2929 psf = state->im->sources;
2930 }
2931out:
2932 return psf;
2933}
2934
2935static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2936{
2937 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2938 if (psf)
2939 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2940 --pos;
2941 return pos ? NULL : psf;
2942}
2943
2944static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2945 __acquires(rcu)
2946{
2947 rcu_read_lock();
2948 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2949}
2950
2951static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2952{
2953 struct ip_sf_list *psf;
2954 if (v == SEQ_START_TOKEN)
2955 psf = igmp_mcf_get_first(seq);
2956 else
2957 psf = igmp_mcf_get_next(seq, v);
2958 ++*pos;
2959 return psf;
2960}
2961
2962static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2963 __releases(rcu)
2964{
2965 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2966 if (likely(state->im)) {
2967 spin_unlock_bh(&state->im->lock);
2968 state->im = NULL;
2969 }
2970 state->idev = NULL;
2971 state->dev = NULL;
2972 rcu_read_unlock();
2973}
2974
2975static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2976{
2977 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2978 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2979
2980 if (v == SEQ_START_TOKEN) {
2981 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
2982 } else {
2983 seq_printf(seq,
2984 "%3d %6.6s 0x%08x "
2985 "0x%08x %6lu %6lu\n",
2986 state->dev->ifindex, state->dev->name,
2987 ntohl(state->im->multiaddr),
2988 ntohl(psf->sf_inaddr),
2989 psf->sf_count[MCAST_INCLUDE],
2990 psf->sf_count[MCAST_EXCLUDE]);
2991 }
2992 return 0;
2993}
2994
2995static const struct seq_operations igmp_mcf_seq_ops = {
2996 .start = igmp_mcf_seq_start,
2997 .next = igmp_mcf_seq_next,
2998 .stop = igmp_mcf_seq_stop,
2999 .show = igmp_mcf_seq_show,
3000};
3001
3002static int __net_init igmp_net_init(struct net *net)
3003{
3004 struct proc_dir_entry *pde;
3005 int err;
3006
3007 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3008 sizeof(struct igmp_mc_iter_state));
3009 if (!pde)
3010 goto out_igmp;
3011 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3012 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
3013 if (!pde)
3014 goto out_mcfilter;
3015 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3016 SOCK_DGRAM, 0, net);
3017 if (err < 0) {
3018 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3019 err);
3020 goto out_sock;
3021 }
3022
3023 return 0;
3024
3025out_sock:
3026 remove_proc_entry("mcfilter", net->proc_net);
3027out_mcfilter:
3028 remove_proc_entry("igmp", net->proc_net);
3029out_igmp:
3030 return -ENOMEM;
3031}
3032
3033static void __net_exit igmp_net_exit(struct net *net)
3034{
3035 remove_proc_entry("mcfilter", net->proc_net);
3036 remove_proc_entry("igmp", net->proc_net);
3037 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3038}
3039
3040static struct pernet_operations igmp_net_ops = {
3041 .init = igmp_net_init,
3042 .exit = igmp_net_exit,
3043};
3044#endif
3045
3046static int igmp_netdev_event(struct notifier_block *this,
3047 unsigned long event, void *ptr)
3048{
3049 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3050 struct in_device *in_dev;
3051
3052 switch (event) {
3053 case NETDEV_RESEND_IGMP:
3054 in_dev = __in_dev_get_rtnl(dev);
3055 if (in_dev)
3056 ip_mc_rejoin_groups(in_dev);
3057 break;
3058 default:
3059 break;
3060 }
3061 return NOTIFY_DONE;
3062}
3063
3064static struct notifier_block igmp_notifier = {
3065 .notifier_call = igmp_netdev_event,
3066};
3067
3068int __init igmp_mc_init(void)
3069{
3070#if defined(CONFIG_PROC_FS)
3071 int err;
3072
3073 err = register_pernet_subsys(&igmp_net_ops);
3074 if (err)
3075 return err;
3076 err = register_netdevice_notifier(&igmp_notifier);
3077 if (err)
3078 goto reg_notif_fail;
3079 return 0;
3080
3081reg_notif_fail:
3082 unregister_pernet_subsys(&igmp_net_ops);
3083 return err;
3084#else
3085 return register_netdevice_notifier(&igmp_notifier);
3086#endif
3087}
3088