2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
11 * Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
14 * Alan Cox <Alan.Cox@linux.org>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
23 * Alan Cox : Added lots of __inline__ to optimise
24 * the memory usage of all the tiny little
26 * Alan Cox : Dumped the header building experiment.
27 * Alan Cox : Minor tweaks ready for multicast routing
28 * and extended IGMP protocol.
29 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
30 * writes utterly bogus code otherwise (sigh)
31 * fixed IGMP loopback to behave in the manner
32 * desired by mrouted, fixed the fact it has been
33 * broken since 1.3.6 and cleaned up a few minor
36 * Chih-Jen Chang : Tried to revise IGMP to Version 2
37 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
38 * The enhancements are mainly based on Steve Deering's
39 * ipmulti-3.5 source code.
40 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
41 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
42 * the mrouted version on that device.
43 * Chih-Jen Chang : Added the max_resp_time parameter to
44 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
45 * to identify the multicast router version
46 * and do what the IGMP version 2 specified.
47 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
48 * Tsu-Sheng Tsao if the specified time expired.
49 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
50 * Alan Cox : Use GFP_ATOMIC in the right places.
51 * Christian Daudt : igmp timer wasn't set for local group
52 * memberships but was being deleted,
53 * which caused a "del_timer() called
54 * from %p with timer not initialized\n"
56 * Christian Daudt : removed del_timer from
57 * igmp_timer_expire function (960205).
58 * Christian Daudt : igmp_heard_report now only calls
59 * igmp_timer_expire if tm->running is
61 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
62 * igmp_heard_query never trigger. Expiry
63 * miscalculation fixed in igmp_heard_query
64 * and random() made to return unsigned to
65 * prevent negative expiry times.
66 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
67 * fix from pending 2.1.x patches.
68 * Alan Cox: Forget to enable FDDI support earlier.
69 * Alexey Kuznetsov: Fixed leaving groups on device down.
70 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
71 * David L Stevens: IGMPv3 support, with help from
75 #include <linux/config.h>
76 #include <linux/module.h>
77 #include <asm/uaccess.h>
78 #include <asm/system.h>
79 #include <linux/types.h>
80 #include <linux/kernel.h>
81 #include <linux/jiffies.h>
82 #include <linux/string.h>
83 #include <linux/socket.h>
84 #include <linux/sockios.h>
86 #include <linux/inet.h>
87 #include <linux/netdevice.h>
88 #include <linux/skbuff.h>
89 #include <linux/inetdevice.h>
90 #include <linux/igmp.h>
91 #include <linux/if_arp.h>
92 #include <linux/rtnetlink.h>
93 #include <linux/times.h>
95 #include <net/protocol.h>
96 #include <net/route.h>
98 #include <net/checksum.h>
99 #include <linux/netfilter_ipv4.h>
100 #ifdef CONFIG_IP_MROUTE
101 #include <linux/mroute.h>
103 #ifdef CONFIG_PROC_FS
104 #include <linux/proc_fs.h>
105 #include <linux/seq_file.h>
108 #define IP_MAX_MEMBERSHIPS 20
109 #define IP_MAX_MSF 10
111 #ifdef CONFIG_IP_MULTICAST
112 /* Parameter names and values are taken from igmp-v2-06 draft */
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
121 #define IGMP_Initial_Report_Delay (1)
123 /* IGMP_Initial_Report_Delay is not from IGMP specs!
124 * IGMP specs require to report membership immediately after
125 * joining a group, but we delay the first report by a
126 * small interval. It seems more natural and still does not
127 * contradict to specs provided this delay is small enough.
130 #define IGMP_V1_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 1 || \
131 (in_dev)->cnf.force_igmp_version == 1 || \
132 ((in_dev)->mr_v1_seen && \
133 time_before(jiffies, (in_dev)->mr_v1_seen)))
134 #define IGMP_V2_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 2 || \
135 (in_dev)->cnf.force_igmp_version == 2 || \
136 ((in_dev)->mr_v2_seen && \
137 time_before(jiffies, (in_dev)->mr_v2_seen)))
139 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
140 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr);
141 static void igmpv3_clear_delrec(struct in_device *in_dev);
142 static int sf_setstate(struct ip_mc_list *pmc);
143 static void sf_markstate(struct ip_mc_list *pmc);
145 static void ip_mc_clear_src(struct ip_mc_list *pmc);
146 int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
147 int sfcount, __u32 *psfsrc, int delta);
149 static void ip_ma_put(struct ip_mc_list *im)
151 if (atomic_dec_and_test(&im->refcnt)) {
152 in_dev_put(im->interface);
157 #ifdef CONFIG_IP_MULTICAST
163 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
165 spin_lock_bh(&im->lock);
166 if (del_timer(&im->timer))
167 atomic_dec(&im->refcnt);
170 im->unsolicit_count = 0;
171 spin_unlock_bh(&im->lock);
174 /* It must be called with locked im->lock */
175 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
177 int tv=net_random() % max_delay;
180 if (!mod_timer(&im->timer, jiffies+tv+2))
181 atomic_inc(&im->refcnt);
184 static void igmp_gq_start_timer(struct in_device *in_dev)
186 int tv = net_random() % in_dev->mr_maxdelay;
188 in_dev->mr_gq_running = 1;
189 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
193 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
195 int tv = net_random() % delay;
197 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
201 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
203 spin_lock_bh(&im->lock);
204 im->unsolicit_count = 0;
205 if (del_timer(&im->timer)) {
206 if ((long)(im->timer.expires-jiffies) < max_delay) {
207 add_timer(&im->timer);
209 spin_unlock_bh(&im->lock);
212 atomic_dec(&im->refcnt);
214 igmp_start_timer(im, max_delay);
215 spin_unlock_bh(&im->lock);
220 * Send an IGMP report.
223 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
226 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
227 int gdeleted, int sdeleted)
230 case IGMPV3_MODE_IS_INCLUDE:
231 case IGMPV3_MODE_IS_EXCLUDE:
232 if (gdeleted || sdeleted)
234 return !(pmc->gsquery && !psf->sf_gsresp);
235 case IGMPV3_CHANGE_TO_INCLUDE:
236 if (gdeleted || sdeleted)
238 return psf->sf_count[MCAST_INCLUDE] != 0;
239 case IGMPV3_CHANGE_TO_EXCLUDE:
240 if (gdeleted || sdeleted)
242 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
243 psf->sf_count[MCAST_INCLUDE])
245 return pmc->sfcount[MCAST_EXCLUDE] ==
246 psf->sf_count[MCAST_EXCLUDE];
247 case IGMPV3_ALLOW_NEW_SOURCES:
248 if (gdeleted || !psf->sf_crcount)
250 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
251 case IGMPV3_BLOCK_OLD_SOURCES:
252 if (pmc->sfmode == MCAST_INCLUDE)
253 return gdeleted || (psf->sf_crcount && sdeleted);
254 return psf->sf_crcount && !gdeleted && !sdeleted;
260 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
262 struct ip_sf_list *psf;
265 for (psf=pmc->sources; psf; psf=psf->sf_next) {
266 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
273 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
278 struct igmpv3_report *pig;
280 skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
285 struct flowi fl = { .oif = dev->ifindex,
287 .daddr = IGMPV3_ALL_MCR } },
288 .proto = IPPROTO_IGMP };
289 if (ip_route_output_key(&rt, &fl)) {
294 if (rt->rt_src == 0) {
300 skb->dst = &rt->u.dst;
303 skb_reserve(skb, LL_RESERVED_SPACE(dev));
305 skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
308 pip->ihl = (sizeof(struct iphdr)+4)>>2;
310 pip->frag_off = htons(IP_DF);
312 pip->daddr = rt->rt_dst;
313 pip->saddr = rt->rt_src;
314 pip->protocol = IPPROTO_IGMP;
315 pip->tot_len = 0; /* filled in later */
316 ip_select_ident(pip, &rt->u.dst, NULL);
317 ((u8*)&pip[1])[0] = IPOPT_RA;
318 ((u8*)&pip[1])[1] = 4;
319 ((u8*)&pip[1])[2] = 0;
320 ((u8*)&pip[1])[3] = 0;
322 pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
323 skb->h.igmph = (struct igmphdr *)pig;
324 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
332 static int igmpv3_sendpack(struct sk_buff *skb)
334 struct iphdr *pip = skb->nh.iph;
335 struct igmphdr *pig = skb->h.igmph;
338 iplen = skb->tail - (unsigned char *)skb->nh.iph;
339 pip->tot_len = htons(iplen);
342 igmplen = skb->tail - (unsigned char *)skb->h.igmph;
343 pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
345 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
349 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
351 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
354 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
355 int type, struct igmpv3_grec **ppgr)
357 struct net_device *dev = pmc->interface->dev;
358 struct igmpv3_report *pih;
359 struct igmpv3_grec *pgr;
362 skb = igmpv3_newpack(dev, dev->mtu);
365 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
366 pgr->grec_type = type;
367 pgr->grec_auxwords = 0;
369 pgr->grec_mca = pmc->multiaddr;
370 pih = (struct igmpv3_report *)skb->h.igmph;
371 pih->ngrec = htons(ntohs(pih->ngrec)+1);
376 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
377 skb_tailroom(skb)) : 0)
379 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
380 int type, int gdeleted, int sdeleted)
382 struct net_device *dev = pmc->interface->dev;
383 struct igmpv3_report *pih;
384 struct igmpv3_grec *pgr = 0;
385 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
386 int scount, first, isquery, truncate;
388 if (pmc->multiaddr == IGMP_ALL_HOSTS)
391 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
392 type == IGMPV3_MODE_IS_EXCLUDE;
393 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
394 type == IGMPV3_CHANGE_TO_EXCLUDE;
396 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
399 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
400 type == IGMPV3_BLOCK_OLD_SOURCES)
402 if (pmc->crcount || isquery) {
403 /* make sure we have room for group header and at
406 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)+
408 igmpv3_sendpack(skb);
409 skb = 0; /* add_grhead will get a new one */
411 skb = add_grhead(skb, pmc, type, &pgr);
415 pih = skb ? (struct igmpv3_report *)skb->h.igmph : 0;
417 /* EX and TO_EX get a fresh packet, if needed */
419 if (pih && pih->ngrec &&
420 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
422 igmpv3_sendpack(skb);
423 skb = igmpv3_newpack(dev, dev->mtu);
429 for (psf=*psf_list; psf; psf=psf_next) {
432 psf_next = psf->sf_next;
434 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
439 /* clear marks on query responses */
443 if (AVAILABLE(skb) < sizeof(u32) +
444 first*sizeof(struct igmpv3_grec)) {
445 if (truncate && !first)
446 break; /* truncate these */
448 pgr->grec_nsrcs = htons(scount);
450 igmpv3_sendpack(skb);
451 skb = igmpv3_newpack(dev, dev->mtu);
456 skb = add_grhead(skb, pmc, type, &pgr);
459 psrc = (u32 *)skb_put(skb, sizeof(u32));
460 *psrc = psf->sf_inaddr;
462 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
463 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
465 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
467 psf_prev->sf_next = psf->sf_next;
469 *psf_list = psf->sf_next;
477 pgr->grec_nsrcs = htons(scount);
480 pmc->gsquery = 0; /* clear query state on report */
484 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
486 struct sk_buff *skb = 0;
490 read_lock(&in_dev->lock);
491 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
492 if (pmc->multiaddr == IGMP_ALL_HOSTS)
494 spin_lock_bh(&pmc->lock);
495 if (pmc->sfcount[MCAST_EXCLUDE])
496 type = IGMPV3_MODE_IS_EXCLUDE;
498 type = IGMPV3_MODE_IS_INCLUDE;
499 skb = add_grec(skb, pmc, type, 0, 0);
500 spin_unlock_bh(&pmc->lock);
502 read_unlock(&in_dev->lock);
504 spin_lock_bh(&pmc->lock);
505 if (pmc->sfcount[MCAST_EXCLUDE])
506 type = IGMPV3_MODE_IS_EXCLUDE;
508 type = IGMPV3_MODE_IS_INCLUDE;
509 skb = add_grec(skb, pmc, type, 0, 0);
510 spin_unlock_bh(&pmc->lock);
514 return igmpv3_sendpack(skb);
518 * remove zero-count source records from a source filter list
520 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
522 struct ip_sf_list *psf_prev, *psf_next, *psf;
525 for (psf=*ppsf; psf; psf = psf_next) {
526 psf_next = psf->sf_next;
527 if (psf->sf_crcount == 0) {
529 psf_prev->sf_next = psf->sf_next;
531 *ppsf = psf->sf_next;
538 static void igmpv3_send_cr(struct in_device *in_dev)
540 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
541 struct sk_buff *skb = 0;
544 read_lock(&in_dev->lock);
545 write_lock_bh(&in_dev->mc_lock);
549 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
550 pmc_next = pmc->next;
551 if (pmc->sfmode == MCAST_INCLUDE) {
552 type = IGMPV3_BLOCK_OLD_SOURCES;
553 dtype = IGMPV3_BLOCK_OLD_SOURCES;
554 skb = add_grec(skb, pmc, type, 1, 0);
555 skb = add_grec(skb, pmc, dtype, 1, 1);
559 if (pmc->sfmode == MCAST_EXCLUDE) {
560 type = IGMPV3_CHANGE_TO_INCLUDE;
561 skb = add_grec(skb, pmc, type, 1, 0);
563 if (pmc->crcount == 0) {
564 igmpv3_clear_zeros(&pmc->tomb);
565 igmpv3_clear_zeros(&pmc->sources);
568 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
570 pmc_prev->next = pmc_next;
572 in_dev->mc_tomb = pmc_next;
573 in_dev_put(pmc->interface);
578 write_unlock_bh(&in_dev->mc_lock);
581 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
582 spin_lock_bh(&pmc->lock);
583 if (pmc->sfcount[MCAST_EXCLUDE]) {
584 type = IGMPV3_BLOCK_OLD_SOURCES;
585 dtype = IGMPV3_ALLOW_NEW_SOURCES;
587 type = IGMPV3_ALLOW_NEW_SOURCES;
588 dtype = IGMPV3_BLOCK_OLD_SOURCES;
590 skb = add_grec(skb, pmc, type, 0, 0);
591 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
593 /* filter mode changes */
596 if (pmc->sfmode == MCAST_EXCLUDE)
597 type = IGMPV3_CHANGE_TO_EXCLUDE;
599 type = IGMPV3_CHANGE_TO_INCLUDE;
600 skb = add_grec(skb, pmc, type, 0, 0);
602 spin_unlock_bh(&pmc->lock);
604 read_unlock(&in_dev->lock);
607 (void) igmpv3_sendpack(skb);
610 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
617 struct net_device *dev = in_dev->dev;
618 u32 group = pmc ? pmc->multiaddr : 0;
621 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
622 return igmpv3_send_report(in_dev, pmc);
623 else if (type == IGMP_HOST_LEAVE_MESSAGE)
624 dst = IGMP_ALL_ROUTER;
629 struct flowi fl = { .oif = dev->ifindex,
630 .nl_u = { .ip4_u = { .daddr = dst } },
631 .proto = IPPROTO_IGMP };
632 if (ip_route_output_key(&rt, &fl))
635 if (rt->rt_src == 0) {
640 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
646 skb->dst = &rt->u.dst;
648 skb_reserve(skb, LL_RESERVED_SPACE(dev));
650 skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
653 iph->ihl = (sizeof(struct iphdr)+4)>>2;
655 iph->frag_off = htons(IP_DF);
658 iph->saddr = rt->rt_src;
659 iph->protocol = IPPROTO_IGMP;
660 iph->tot_len = htons(IGMP_SIZE);
661 ip_select_ident(iph, &rt->u.dst, NULL);
662 ((u8*)&iph[1])[0] = IPOPT_RA;
663 ((u8*)&iph[1])[1] = 4;
664 ((u8*)&iph[1])[2] = 0;
665 ((u8*)&iph[1])[3] = 0;
668 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
673 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
675 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
679 static void igmp_gq_timer_expire(unsigned long data)
681 struct in_device *in_dev = (struct in_device *)data;
683 in_dev->mr_gq_running = 0;
684 igmpv3_send_report(in_dev, 0);
685 __in_dev_put(in_dev);
688 static void igmp_ifc_timer_expire(unsigned long data)
690 struct in_device *in_dev = (struct in_device *)data;
692 igmpv3_send_cr(in_dev);
693 if (in_dev->mr_ifc_count) {
694 in_dev->mr_ifc_count--;
695 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
697 __in_dev_put(in_dev);
700 static void igmp_ifc_event(struct in_device *in_dev)
702 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
704 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
705 IGMP_Unsolicited_Report_Count;
706 igmp_ifc_start_timer(in_dev, 1);
710 static void igmp_timer_expire(unsigned long data)
712 struct ip_mc_list *im=(struct ip_mc_list *)data;
713 struct in_device *in_dev = im->interface;
715 spin_lock(&im->lock);
718 if (im->unsolicit_count) {
719 im->unsolicit_count--;
720 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
723 spin_unlock(&im->lock);
725 if (IGMP_V1_SEEN(in_dev))
726 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
727 else if (IGMP_V2_SEEN(in_dev))
728 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
730 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
735 static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
737 struct ip_sf_list *psf;
741 for (psf=pmc->sources; psf; psf=psf->sf_next) {
744 for (i=0; i<nsrcs; i++)
745 if (srcs[i] == psf->sf_inaddr) {
753 static void igmp_heard_report(struct in_device *in_dev, u32 group)
755 struct ip_mc_list *im;
757 /* Timers are only set for non-local groups */
759 if (group == IGMP_ALL_HOSTS)
762 read_lock(&in_dev->lock);
763 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
764 if (im->multiaddr == group) {
769 read_unlock(&in_dev->lock);
772 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
775 struct igmphdr *ih = skb->h.igmph;
776 struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
777 struct ip_mc_list *im;
778 u32 group = ih->group;
785 /* Alas, old v1 router presents here. */
787 max_delay = IGMP_Query_Response_Interval;
788 in_dev->mr_v1_seen = jiffies +
789 IGMP_V1_Router_Present_Timeout;
792 /* v2 router present */
793 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
794 in_dev->mr_v2_seen = jiffies +
795 IGMP_V2_Router_Present_Timeout;
797 /* cancel the interface change timer */
798 in_dev->mr_ifc_count = 0;
799 if (del_timer(&in_dev->mr_ifc_timer))
800 __in_dev_put(in_dev);
801 /* clear deleted report items */
802 igmpv3_clear_delrec(in_dev);
803 } else if (len < 12) {
804 return; /* ignore bogus packet; freed by caller */
806 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
809 ih3 = (struct igmpv3_query *) skb->h.raw;
811 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
812 + ntohs(ih3->nsrcs)*sizeof(__u32)))
814 ih3 = (struct igmpv3_query *) skb->h.raw;
817 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
819 max_delay = 1; /* can't mod w/ 0 */
820 in_dev->mr_maxdelay = max_delay;
822 in_dev->mr_qrv = ih3->qrv;
823 if (!group) { /* general query */
825 return; /* no sources allowed */
826 igmp_gq_start_timer(in_dev);
829 /* mark sources to include, if group & source-specific */
830 mark = ih3->nsrcs != 0;
834 * - Start the timers in all of our membership records
835 * that the query applies to for the interface on
836 * which the query arrived excl. those that belong
837 * to a "local" group (224.0.0.X)
838 * - For timers already running check if they need to
840 * - Use the igmp->igmp_code field as the maximum
843 read_lock(&in_dev->lock);
844 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
845 if (group && group != im->multiaddr)
847 if (im->multiaddr == IGMP_ALL_HOSTS)
849 spin_lock_bh(&im->lock);
851 im->gsquery = im->gsquery && mark;
855 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
856 spin_unlock_bh(&im->lock);
857 igmp_mod_timer(im, max_delay);
859 read_unlock(&in_dev->lock);
862 int igmp_rcv(struct sk_buff *skb)
864 /* This basically follows the spec line by line -- see RFC1112 */
866 struct in_device *in_dev = in_dev_get(skb->dev);
874 if (!pskb_may_pull(skb, sizeof(struct igmphdr)) ||
875 (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
883 case IGMP_HOST_MEMBERSHIP_QUERY:
884 igmp_heard_query(in_dev, skb, len);
886 case IGMP_HOST_MEMBERSHIP_REPORT:
887 case IGMPV2_HOST_MEMBERSHIP_REPORT:
888 case IGMPV3_HOST_MEMBERSHIP_REPORT:
889 /* Is it our report looped back? */
890 if (((struct rtable*)skb->dst)->fl.iif == 0)
892 igmp_heard_report(in_dev, ih->group);
895 #ifdef CONFIG_IP_PIMSM_V1
897 return pim_rcv_v1(skb);
901 case IGMP_HOST_LEAVE_MESSAGE:
903 case IGMP_MTRACE_RESP:
906 NETDEBUG(printk(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type));
917 * Add a filter to a device
920 static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
922 char buf[MAX_ADDR_LEN];
923 struct net_device *dev = in_dev->dev;
925 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
926 We will get multicast token leakage, when IFF_MULTICAST
927 is changed. This check should be done in dev->set_multicast_list
928 routine. Something sort of:
929 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
932 if (arp_mc_map(addr, buf, dev, 0) == 0)
933 dev_mc_add(dev,buf,dev->addr_len,0);
937 * Remove a filter from a device
940 static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
942 char buf[MAX_ADDR_LEN];
943 struct net_device *dev = in_dev->dev;
945 if (arp_mc_map(addr, buf, dev, 0) == 0)
946 dev_mc_delete(dev,buf,dev->addr_len,0);
949 #ifdef CONFIG_IP_MULTICAST
951 * deleted ip_mc_list manipulation
953 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
955 struct ip_mc_list *pmc;
957 /* this is an "ip_mc_list" for convenience; only the fields below
958 * are actually used. In particular, the refcnt and users are not
959 * used for management of the delete list. Using the same structure
960 * for deleted items allows change reports to use common code with
961 * non-deleted or query-response MCA's.
963 pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
966 memset(pmc, 0, sizeof(*pmc));
967 spin_lock_bh(&im->lock);
968 pmc->interface = im->interface;
970 pmc->multiaddr = im->multiaddr;
971 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
972 IGMP_Unsolicited_Report_Count;
973 pmc->sfmode = im->sfmode;
974 if (pmc->sfmode == MCAST_INCLUDE) {
975 struct ip_sf_list *psf;
977 pmc->tomb = im->tomb;
978 pmc->sources = im->sources;
979 im->tomb = im->sources = 0;
980 for (psf=pmc->sources; psf; psf=psf->sf_next)
981 psf->sf_crcount = pmc->crcount;
983 spin_unlock_bh(&im->lock);
985 write_lock_bh(&in_dev->mc_lock);
986 pmc->next = in_dev->mc_tomb;
987 in_dev->mc_tomb = pmc;
988 write_unlock_bh(&in_dev->mc_lock);
991 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
993 struct ip_mc_list *pmc, *pmc_prev;
994 struct ip_sf_list *psf, *psf_next;
996 write_lock_bh(&in_dev->mc_lock);
998 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
999 if (pmc->multiaddr == multiaddr)
1005 pmc_prev->next = pmc->next;
1007 in_dev->mc_tomb = pmc->next;
1009 write_unlock_bh(&in_dev->mc_lock);
1011 for (psf=pmc->tomb; psf; psf=psf_next) {
1012 psf_next = psf->sf_next;
1015 in_dev_put(pmc->interface);
1020 static void igmpv3_clear_delrec(struct in_device *in_dev)
1022 struct ip_mc_list *pmc, *nextpmc;
1024 write_lock_bh(&in_dev->mc_lock);
1025 pmc = in_dev->mc_tomb;
1026 in_dev->mc_tomb = 0;
1027 write_unlock_bh(&in_dev->mc_lock);
1029 for (; pmc; pmc = nextpmc) {
1030 nextpmc = pmc->next;
1031 ip_mc_clear_src(pmc);
1032 in_dev_put(pmc->interface);
1035 /* clear dead sources, too */
1036 read_lock(&in_dev->lock);
1037 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1038 struct ip_sf_list *psf, *psf_next;
1040 spin_lock_bh(&pmc->lock);
1043 spin_unlock_bh(&pmc->lock);
1044 for (; psf; psf=psf_next) {
1045 psf_next = psf->sf_next;
1049 read_unlock(&in_dev->lock);
1053 static void igmp_group_dropped(struct ip_mc_list *im)
1055 struct in_device *in_dev = im->interface;
1056 #ifdef CONFIG_IP_MULTICAST
1062 ip_mc_filter_del(in_dev, im->multiaddr);
1065 #ifdef CONFIG_IP_MULTICAST
1066 if (im->multiaddr == IGMP_ALL_HOSTS)
1069 reporter = im->reporter;
1070 igmp_stop_timer(im);
1072 if (!in_dev->dead) {
1073 if (IGMP_V1_SEEN(in_dev))
1075 if (IGMP_V2_SEEN(in_dev)) {
1077 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1081 igmpv3_add_delrec(in_dev, im);
1083 igmp_ifc_event(in_dev);
1087 ip_mc_clear_src(im);
1090 static void igmp_group_added(struct ip_mc_list *im)
1092 struct in_device *in_dev = im->interface;
1094 if (im->loaded == 0) {
1096 ip_mc_filter_add(in_dev, im->multiaddr);
1099 #ifdef CONFIG_IP_MULTICAST
1100 if (im->multiaddr == IGMP_ALL_HOSTS)
1105 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1106 spin_lock_bh(&im->lock);
1107 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1108 spin_unlock_bh(&im->lock);
1113 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1114 IGMP_Unsolicited_Report_Count;
1115 igmp_ifc_event(in_dev);
1121 * Multicast list managers
1126 * A socket has joined a multicast group on device dev.
1129 void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1131 struct ip_mc_list *im;
1135 for (im=in_dev->mc_list; im; im=im->next) {
1136 if (im->multiaddr == addr) {
1138 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, 0, 0);
1143 im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1148 im->interface=in_dev;
1149 in_dev_hold(in_dev);
1151 /* initial mode is (EX, empty) */
1152 im->sfmode = MCAST_EXCLUDE;
1153 im->sfcount[MCAST_INCLUDE] = 0;
1154 im->sfcount[MCAST_EXCLUDE] = 1;
1158 atomic_set(&im->refcnt, 1);
1159 spin_lock_init(&im->lock);
1160 #ifdef CONFIG_IP_MULTICAST
1162 init_timer(&im->timer);
1163 im->timer.data=(unsigned long)im;
1164 im->timer.function=&igmp_timer_expire;
1165 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1170 write_lock_bh(&in_dev->lock);
1171 im->next=in_dev->mc_list;
1173 write_unlock_bh(&in_dev->lock);
1174 #ifdef CONFIG_IP_MULTICAST
1175 igmpv3_del_delrec(in_dev, im->multiaddr);
1177 igmp_group_added(im);
1179 ip_rt_multicast_event(in_dev);
1185 * A socket has left a multicast group on device dev
1188 void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1190 struct ip_mc_list *i, **ip;
1194 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1195 if (i->multiaddr==addr) {
1196 if (--i->users == 0) {
1197 write_lock_bh(&in_dev->lock);
1199 write_unlock_bh(&in_dev->lock);
1200 igmp_group_dropped(i);
1203 ip_rt_multicast_event(in_dev);
1213 /* Device going down */
1215 void ip_mc_down(struct in_device *in_dev)
1217 struct ip_mc_list *i;
1221 for (i=in_dev->mc_list; i; i=i->next)
1222 igmp_group_dropped(i);
1224 #ifdef CONFIG_IP_MULTICAST
1225 in_dev->mr_ifc_count = 0;
1226 if (del_timer(&in_dev->mr_ifc_timer))
1227 __in_dev_put(in_dev);
1228 in_dev->mr_gq_running = 0;
1229 if (del_timer(&in_dev->mr_gq_timer))
1230 __in_dev_put(in_dev);
1231 igmpv3_clear_delrec(in_dev);
1234 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1237 void ip_mc_init_dev(struct in_device *in_dev)
1241 in_dev->mc_tomb = 0;
1242 #ifdef CONFIG_IP_MULTICAST
1243 in_dev->mr_gq_running = 0;
1244 init_timer(&in_dev->mr_gq_timer);
1245 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1246 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1247 in_dev->mr_ifc_count = 0;
1248 init_timer(&in_dev->mr_ifc_timer);
1249 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1250 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1251 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1254 in_dev->mc_lock = RW_LOCK_UNLOCKED;
1257 /* Device going up */
1259 void ip_mc_up(struct in_device *in_dev)
1261 struct ip_mc_list *i;
1265 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1267 for (i=in_dev->mc_list; i; i=i->next)
1268 igmp_group_added(i);
1272 * Device is about to be destroyed: clean up.
1275 void ip_mc_destroy_dev(struct in_device *in_dev)
1277 struct ip_mc_list *i;
1281 /* Deactivate timers */
1284 write_lock_bh(&in_dev->lock);
1285 while ((i = in_dev->mc_list) != NULL) {
1286 in_dev->mc_list = i->next;
1287 write_unlock_bh(&in_dev->lock);
1289 igmp_group_dropped(i);
1292 write_lock_bh(&in_dev->lock);
1294 write_unlock_bh(&in_dev->lock);
1297 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1299 struct flowi fl = { .nl_u = { .ip4_u =
1300 { .daddr = imr->imr_multiaddr.s_addr } } };
1302 struct net_device *dev = NULL;
1303 struct in_device *idev = NULL;
1305 if (imr->imr_ifindex) {
1306 idev = inetdev_by_index(imr->imr_ifindex);
1311 if (imr->imr_address.s_addr) {
1312 dev = ip_dev_find(imr->imr_address.s_addr);
1318 if (!dev && !ip_route_output_key(&rt, &fl)) {
1319 dev = rt->u.dst.dev;
1323 imr->imr_ifindex = dev->ifindex;
1324 idev = __in_dev_get(dev);
1330 * Join a socket to a group
1332 int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1333 int sysctl_igmp_max_msf = IP_MAX_MSF;
1336 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1339 struct ip_sf_list *psf, *psf_prev;
1343 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1344 if (psf->sf_inaddr == *psfsrc)
1348 if (!psf || psf->sf_count[sfmode] == 0) {
1349 /* source filter not found, or count wrong => bug */
1352 psf->sf_count[sfmode]--;
1353 if (psf->sf_count[sfmode] == 0) {
1354 ip_rt_multicast_event(pmc->interface);
1356 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1357 #ifdef CONFIG_IP_MULTICAST
1358 struct in_device *in_dev = pmc->interface;
1361 /* no more filters for this source */
1363 psf_prev->sf_next = psf->sf_next;
1365 pmc->sources = psf->sf_next;
1366 #ifdef CONFIG_IP_MULTICAST
1367 if (psf->sf_oldin &&
1368 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1369 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1370 IGMP_Unsolicited_Report_Count;
1371 psf->sf_next = pmc->tomb;
1381 #ifndef CONFIG_IP_MULTICAST
1382 #define igmp_ifc_event(x) do { } while (0)
1385 int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1386 int sfcount, __u32 *psfsrc, int delta)
1388 struct ip_mc_list *pmc;
1394 read_lock(&in_dev->lock);
1395 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1396 if (*pmca == pmc->multiaddr)
1400 /* MCA not found?? bug */
1401 read_unlock(&in_dev->lock);
1404 spin_lock_bh(&pmc->lock);
1405 read_unlock(&in_dev->lock);
1406 #ifdef CONFIG_IP_MULTICAST
1411 if (!pmc->sfcount[sfmode])
1413 pmc->sfcount[sfmode]--;
1416 for (i=0; i<sfcount; i++) {
1417 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1419 changerec |= rv > 0;
1423 if (pmc->sfmode == MCAST_EXCLUDE &&
1424 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1425 pmc->sfcount[MCAST_INCLUDE]) {
1426 #ifdef CONFIG_IP_MULTICAST
1427 struct ip_sf_list *psf;
1430 /* filter mode change */
1431 pmc->sfmode = MCAST_INCLUDE;
1432 #ifdef CONFIG_IP_MULTICAST
1433 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1434 IGMP_Unsolicited_Report_Count;
1435 in_dev->mr_ifc_count = pmc->crcount;
1436 for (psf=pmc->sources; psf; psf = psf->sf_next)
1437 psf->sf_crcount = 0;
1438 igmp_ifc_event(pmc->interface);
1439 } else if (sf_setstate(pmc) || changerec) {
1440 igmp_ifc_event(pmc->interface);
1444 spin_unlock_bh(&pmc->lock);
1449 * Add multicast single-source filter to the interface list
1451 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1452 __u32 *psfsrc, int delta)
1454 struct ip_sf_list *psf, *psf_prev;
1457 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1458 if (psf->sf_inaddr == *psfsrc)
1463 psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1466 memset(psf, 0, sizeof(*psf));
1467 psf->sf_inaddr = *psfsrc;
1469 psf_prev->sf_next = psf;
1473 psf->sf_count[sfmode]++;
1474 if (psf->sf_count[sfmode] == 1) {
1475 ip_rt_multicast_event(pmc->interface);
1480 #ifdef CONFIG_IP_MULTICAST
1481 static void sf_markstate(struct ip_mc_list *pmc)
1483 struct ip_sf_list *psf;
1484 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1486 for (psf=pmc->sources; psf; psf=psf->sf_next)
1487 if (pmc->sfcount[MCAST_EXCLUDE]) {
1488 psf->sf_oldin = mca_xcount ==
1489 psf->sf_count[MCAST_EXCLUDE] &&
1490 !psf->sf_count[MCAST_INCLUDE];
1492 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1495 static int sf_setstate(struct ip_mc_list *pmc)
1497 struct ip_sf_list *psf;
1498 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1499 int qrv = pmc->interface->mr_qrv;
1503 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1504 if (pmc->sfcount[MCAST_EXCLUDE]) {
1505 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1506 !psf->sf_count[MCAST_INCLUDE];
1508 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1509 if (new_in != psf->sf_oldin) {
1510 psf->sf_crcount = qrv;
1519 * Add multicast source filter list to the interface list
1521 int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1522 int sfcount, __u32 *psfsrc, int delta)
1524 struct ip_mc_list *pmc;
1530 read_lock(&in_dev->lock);
1531 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1532 if (*pmca == pmc->multiaddr)
1536 /* MCA not found?? bug */
1537 read_unlock(&in_dev->lock);
1540 spin_lock_bh(&pmc->lock);
1541 read_unlock(&in_dev->lock);
1543 #ifdef CONFIG_IP_MULTICAST
1546 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1548 pmc->sfcount[sfmode]++;
1550 for (i=0; i<sfcount; i++) {
1551 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1558 pmc->sfcount[sfmode]--;
1560 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1561 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1562 #ifdef CONFIG_IP_MULTICAST
1563 struct in_device *in_dev = pmc->interface;
1564 struct ip_sf_list *psf;
1567 /* filter mode change */
1568 if (pmc->sfcount[MCAST_EXCLUDE])
1569 pmc->sfmode = MCAST_EXCLUDE;
1570 else if (pmc->sfcount[MCAST_INCLUDE])
1571 pmc->sfmode = MCAST_INCLUDE;
1572 #ifdef CONFIG_IP_MULTICAST
1573 /* else no filters; keep old mode for reports */
1575 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1576 IGMP_Unsolicited_Report_Count;
1577 in_dev->mr_ifc_count = pmc->crcount;
1578 for (psf=pmc->sources; psf; psf = psf->sf_next)
1579 psf->sf_crcount = 0;
1580 igmp_ifc_event(in_dev);
1581 } else if (sf_setstate(pmc)) {
1582 igmp_ifc_event(in_dev);
1585 spin_unlock_bh(&pmc->lock);
1589 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1591 struct ip_sf_list *psf, *nextpsf;
1593 for (psf=pmc->tomb; psf; psf=nextpsf) {
1594 nextpsf = psf->sf_next;
1598 for (psf=pmc->sources; psf; psf=nextpsf) {
1599 nextpsf = psf->sf_next;
1603 pmc->sfmode = MCAST_EXCLUDE;
1604 pmc->sfcount[MCAST_EXCLUDE] = 0;
1605 pmc->sfcount[MCAST_EXCLUDE] = 1;
1610 * Join a multicast group
1612 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1615 u32 addr = imr->imr_multiaddr.s_addr;
1616 struct ip_mc_socklist *iml, *i;
1617 struct in_device *in_dev;
1618 struct inet_opt *inet = inet_sk(sk);
1621 if (!MULTICAST(addr))
1626 in_dev = ip_mc_find_dev(imr);
1634 iml = (struct ip_mc_socklist *)sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1637 for (i = inet->mc_list; i; i = i->next) {
1638 if (memcmp(&i->multi, imr, sizeof(*imr)) == 0) {
1639 /* New style additions are reference counted */
1640 if (imr->imr_address.s_addr == 0) {
1649 if (iml == NULL || count >= sysctl_igmp_max_memberships)
1651 memcpy(&iml->multi, imr, sizeof(*imr));
1652 iml->next = inet->mc_list;
1655 iml->sfmode = MCAST_EXCLUDE;
1656 inet->mc_list = iml;
1657 ip_mc_inc_group(in_dev, addr);
1664 sock_kfree_s(sk, iml, sizeof(*iml));
1668 int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1669 struct in_device *in_dev)
1673 if (iml->sflist == 0) {
1674 /* any-source empty exclude case */
1675 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1676 iml->sfmode, 0, 0, 0);
1678 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1679 iml->sfmode, iml->sflist->sl_count,
1680 iml->sflist->sl_addr, 0);
1681 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1687 * Ask a socket to leave a group.
1690 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1692 struct inet_opt *inet = inet_sk(sk);
1693 struct ip_mc_socklist *iml, **imlp;
1696 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1697 if (iml->multi.imr_multiaddr.s_addr==imr->imr_multiaddr.s_addr &&
1698 iml->multi.imr_address.s_addr==imr->imr_address.s_addr &&
1699 (!imr->imr_ifindex || iml->multi.imr_ifindex==imr->imr_ifindex)) {
1700 struct in_device *in_dev;
1702 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
1704 (void) ip_mc_leave_src(sk, iml, in_dev);
1715 ip_mc_dec_group(in_dev, imr->imr_multiaddr.s_addr);
1719 sock_kfree_s(sk, iml, sizeof(*iml));
1724 return -EADDRNOTAVAIL;
1727 int ip_mc_source(int add, int omode, struct sock *sk, struct
1728 ip_mreq_source *mreqs, int ifindex)
1731 struct ip_mreqn imr;
1732 u32 addr = mreqs->imr_multiaddr;
1733 struct ip_mc_socklist *pmc;
1734 struct in_device *in_dev = 0;
1735 struct inet_opt *inet = inet_sk(sk);
1736 struct ip_sf_socklist *psl;
1739 if (!MULTICAST(addr))
1744 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1745 imr.imr_address.s_addr = mreqs->imr_interface;
1746 imr.imr_ifindex = ifindex;
1747 in_dev = ip_mc_find_dev(&imr);
1753 err = -EADDRNOTAVAIL;
1755 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1756 if (memcmp(&pmc->multi, mreqs, 2*sizeof(__u32)) == 0)
1759 if (!pmc) /* must have a prior join */
1761 /* if a source filter was set, must be the same mode as before */
1763 if (pmc->sfmode != omode)
1765 } else if (pmc->sfmode != omode) {
1766 /* allow mode switches for empty-set filters */
1767 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, 0, 0);
1768 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1770 pmc->sfmode = omode;
1778 for (i=0; i<psl->sl_count; i++) {
1779 rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1784 if (!rv) /* source not found */
1787 /* update the interface filter */
1788 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1789 &mreqs->imr_sourceaddr, 1);
1791 for (j=i+1; j<psl->sl_count; j++)
1792 psl->sl_addr[j-1] = psl->sl_addr[j];
1797 /* else, add a new source to the filter */
1799 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1803 if (!psl || psl->sl_count == psl->sl_max) {
1804 struct ip_sf_socklist *newpsl;
1805 int count = IP_SFBLOCK;
1808 count += psl->sl_max;
1809 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1810 IP_SFLSIZE(count), GFP_KERNEL);
1815 newpsl->sl_max = count;
1816 newpsl->sl_count = count - IP_SFBLOCK;
1818 for (i=0; i<psl->sl_count; i++)
1819 newpsl->sl_addr[i] = psl->sl_addr[i];
1820 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1822 pmc->sflist = psl = newpsl;
1824 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1825 for (i=0; i<psl->sl_count; i++) {
1826 rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1831 if (rv == 0) /* address already there is an error */
1833 for (j=psl->sl_count-1; j>=i; j--)
1834 psl->sl_addr[j+1] = psl->sl_addr[j];
1835 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1838 /* update the interface list */
1839 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1840 &mreqs->imr_sourceaddr, 1);
1846 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1849 struct ip_mreqn imr;
1850 u32 addr = msf->imsf_multiaddr;
1851 struct ip_mc_socklist *pmc;
1852 struct in_device *in_dev;
1853 struct inet_opt *inet = inet_sk(sk);
1854 struct ip_sf_socklist *newpsl, *psl;
1856 if (!MULTICAST(addr))
1858 if (msf->imsf_fmode != MCAST_INCLUDE &&
1859 msf->imsf_fmode != MCAST_EXCLUDE)
1864 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1865 imr.imr_address.s_addr = msf->imsf_interface;
1866 imr.imr_ifindex = ifindex;
1867 in_dev = ip_mc_find_dev(&imr);
1873 err = -EADDRNOTAVAIL;
1875 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1876 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1877 pmc->multi.imr_ifindex == imr.imr_ifindex)
1880 if (!pmc) /* must have a prior join */
1882 if (msf->imsf_numsrc) {
1883 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1884 IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1889 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1890 memcpy(newpsl->sl_addr, msf->imsf_slist,
1891 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1892 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1893 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1895 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1902 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1903 psl->sl_count, psl->sl_addr, 0);
1904 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1906 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1908 pmc->sflist = newpsl;
1909 pmc->sfmode = msf->imsf_fmode;
1915 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1916 struct ip_msfilter __user *optval, int __user *optlen)
1918 int err, len, count, copycount;
1919 struct ip_mreqn imr;
1920 u32 addr = msf->imsf_multiaddr;
1921 struct ip_mc_socklist *pmc;
1922 struct in_device *in_dev;
1923 struct inet_opt *inet = inet_sk(sk);
1924 struct ip_sf_socklist *psl;
1926 if (!MULTICAST(addr))
1931 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1932 imr.imr_address.s_addr = msf->imsf_interface;
1933 imr.imr_ifindex = 0;
1934 in_dev = ip_mc_find_dev(&imr);
1940 err = -EADDRNOTAVAIL;
1942 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1943 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1944 pmc->multi.imr_ifindex == imr.imr_ifindex)
1947 if (!pmc) /* must have a prior join */
1949 msf->imsf_fmode = pmc->sfmode;
1956 count = psl->sl_count;
1958 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1959 len = copycount * sizeof(psl->sl_addr[0]);
1960 msf->imsf_numsrc = count;
1961 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1962 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
1966 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
1974 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
1975 struct group_filter __user *optval, int __user *optlen)
1977 int err, i, count, copycount;
1978 struct sockaddr_in *psin;
1980 struct ip_mc_socklist *pmc;
1981 struct inet_opt *inet = inet_sk(sk);
1982 struct ip_sf_socklist *psl;
1984 psin = (struct sockaddr_in *)&gsf->gf_group;
1985 if (psin->sin_family != AF_INET)
1987 addr = psin->sin_addr.s_addr;
1988 if (!MULTICAST(addr))
1993 err = -EADDRNOTAVAIL;
1995 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1996 if (pmc->multi.imr_multiaddr.s_addr == addr &&
1997 pmc->multi.imr_ifindex == gsf->gf_interface)
2000 if (!pmc) /* must have a prior join */
2002 gsf->gf_fmode = pmc->sfmode;
2005 count = psl ? psl->sl_count : 0;
2006 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2007 gsf->gf_numsrc = count;
2008 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2009 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2012 for (i=0; i<copycount; i++) {
2013 struct sockaddr_in *psin;
2014 struct sockaddr_storage ss;
2016 psin = (struct sockaddr_in *)&ss;
2017 memset(&ss, 0, sizeof(ss));
2018 psin->sin_family = AF_INET;
2019 psin->sin_addr.s_addr = psl->sl_addr[i];
2020 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2030 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2032 int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2034 struct inet_opt *inet = inet_sk(sk);
2035 struct ip_mc_socklist *pmc;
2036 struct ip_sf_socklist *psl;
2039 if (!MULTICAST(loc_addr))
2042 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2043 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2044 pmc->multi.imr_ifindex == dif)
2051 return pmc->sfmode == MCAST_EXCLUDE;
2053 for (i=0; i<psl->sl_count; i++) {
2054 if (psl->sl_addr[i] == rmt_addr)
2057 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2059 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2065 * A socket is closing.
2068 void ip_mc_drop_socket(struct sock *sk)
2070 struct inet_opt *inet = inet_sk(sk);
2071 struct ip_mc_socklist *iml;
2073 if (inet->mc_list == NULL)
2077 while ((iml = inet->mc_list) != NULL) {
2078 struct in_device *in_dev;
2079 inet->mc_list = iml->next;
2081 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2082 (void) ip_mc_leave_src(sk, iml, in_dev);
2083 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2086 sock_kfree_s(sk, iml, sizeof(*iml));
2092 int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2094 struct ip_mc_list *im;
2095 struct ip_sf_list *psf;
2098 read_lock(&in_dev->lock);
2099 for (im=in_dev->mc_list; im; im=im->next) {
2100 if (im->multiaddr == mc_addr)
2103 if (im && proto == IPPROTO_IGMP) {
2107 for (psf=im->sources; psf; psf=psf->sf_next) {
2108 if (psf->sf_inaddr == src_addr)
2112 rv = psf->sf_count[MCAST_INCLUDE] ||
2113 psf->sf_count[MCAST_EXCLUDE] !=
2114 im->sfcount[MCAST_EXCLUDE];
2116 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2118 rv = 1; /* unspecified source; tentatively allow */
2120 read_unlock(&in_dev->lock);
2124 #if defined(CONFIG_PROC_FS)
2125 struct igmp_mc_iter_state {
2126 struct net_device *dev;
2127 struct in_device *in_dev;
2130 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2132 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2134 struct ip_mc_list *im = NULL;
2135 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2137 for (state->dev = dev_base, state->in_dev = NULL;
2139 state->dev = state->dev->next) {
2140 struct in_device *in_dev;
2141 in_dev = in_dev_get(state->dev);
2144 read_lock(&in_dev->lock);
2145 im = in_dev->mc_list;
2147 state->in_dev = in_dev;
2150 read_unlock(&in_dev->lock);
2156 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2158 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2161 if (likely(state->in_dev != NULL)) {
2162 read_unlock(&state->in_dev->lock);
2163 in_dev_put(state->in_dev);
2165 state->dev = state->dev->next;
2167 state->in_dev = NULL;
2170 state->in_dev = in_dev_get(state->dev);
2173 read_lock(&state->in_dev->lock);
2174 im = state->in_dev->mc_list;
2179 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2181 struct ip_mc_list *im = igmp_mc_get_first(seq);
2183 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2185 return pos ? NULL : im;
2188 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2190 read_lock(&dev_base_lock);
2191 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2194 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2196 struct ip_mc_list *im;
2197 if (v == SEQ_START_TOKEN)
2198 im = igmp_mc_get_first(seq);
2200 im = igmp_mc_get_next(seq, v);
2205 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2207 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2208 if (likely(state->in_dev != NULL)) {
2209 read_unlock(&state->in_dev->lock);
2210 in_dev_put(state->in_dev);
2211 state->in_dev = NULL;
2214 read_unlock(&dev_base_lock);
2217 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2219 if (v == SEQ_START_TOKEN)
2221 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2223 struct ip_mc_list *im = (struct ip_mc_list *)v;
2224 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2226 #ifdef CONFIG_IP_MULTICAST
2227 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2228 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2234 if (state->in_dev->mc_list == im) {
2235 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2236 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2240 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2241 im->multiaddr, im->users,
2242 im->tm_running, im->tm_running ?
2243 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2249 static struct seq_operations igmp_mc_seq_ops = {
2250 .start = igmp_mc_seq_start,
2251 .next = igmp_mc_seq_next,
2252 .stop = igmp_mc_seq_stop,
2253 .show = igmp_mc_seq_show,
2256 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2258 struct seq_file *seq;
2260 struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2264 rc = seq_open(file, &igmp_mc_seq_ops);
2268 seq = file->private_data;
2270 memset(s, 0, sizeof(*s));
2278 static struct file_operations igmp_mc_seq_fops = {
2279 .owner = THIS_MODULE,
2280 .open = igmp_mc_seq_open,
2282 .llseek = seq_lseek,
2283 .release = seq_release_private,
2286 struct igmp_mcf_iter_state {
2287 struct net_device *dev;
2288 struct in_device *idev;
2289 struct ip_mc_list *im;
2292 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2294 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2296 struct ip_sf_list *psf = NULL;
2297 struct ip_mc_list *im = NULL;
2298 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2300 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2302 state->dev = state->dev->next) {
2303 struct in_device *idev;
2304 idev = in_dev_get(state->dev);
2305 if (unlikely(idev == NULL))
2307 read_lock_bh(&idev->lock);
2309 if (likely(im != NULL)) {
2310 spin_lock_bh(&im->lock);
2312 if (likely(psf != NULL)) {
2317 spin_unlock_bh(&im->lock);
2319 read_unlock_bh(&idev->lock);
2325 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2327 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2331 spin_unlock_bh(&state->im->lock);
2332 state->im = state->im->next;
2333 while (!state->im) {
2334 if (likely(state->idev != NULL)) {
2335 read_unlock_bh(&state->idev->lock);
2336 in_dev_put(state->idev);
2338 state->dev = state->dev->next;
2343 state->idev = in_dev_get(state->dev);
2346 read_lock_bh(&state->idev->lock);
2347 state->im = state->idev->mc_list;
2351 spin_lock_bh(&state->im->lock);
2352 psf = state->im->sources;
2358 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2360 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2362 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2364 return pos ? NULL : psf;
2367 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2369 read_lock(&dev_base_lock);
2370 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2373 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2375 struct ip_sf_list *psf;
2376 if (v == SEQ_START_TOKEN)
2377 psf = igmp_mcf_get_first(seq);
2379 psf = igmp_mcf_get_next(seq, v);
2384 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2386 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2387 if (likely(state->im != NULL)) {
2388 spin_unlock_bh(&state->im->lock);
2391 if (likely(state->idev != NULL)) {
2392 read_unlock_bh(&state->idev->lock);
2393 in_dev_put(state->idev);
2397 read_unlock(&dev_base_lock);
2400 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2402 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2403 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2405 if (v == SEQ_START_TOKEN) {
2408 "%10s %10s %6s %6s\n", "Idx",
2410 "SRC", "INC", "EXC");
2414 "0x%08x %6lu %6lu\n",
2415 state->dev->ifindex, state->dev->name,
2416 ntohl(state->im->multiaddr),
2417 ntohl(psf->sf_inaddr),
2418 psf->sf_count[MCAST_INCLUDE],
2419 psf->sf_count[MCAST_EXCLUDE]);
2424 static struct seq_operations igmp_mcf_seq_ops = {
2425 .start = igmp_mcf_seq_start,
2426 .next = igmp_mcf_seq_next,
2427 .stop = igmp_mcf_seq_stop,
2428 .show = igmp_mcf_seq_show,
2431 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2433 struct seq_file *seq;
2435 struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2439 rc = seq_open(file, &igmp_mcf_seq_ops);
2443 seq = file->private_data;
2445 memset(s, 0, sizeof(*s));
2453 static struct file_operations igmp_mcf_seq_fops = {
2454 .owner = THIS_MODULE,
2455 .open = igmp_mcf_seq_open,
2457 .llseek = seq_lseek,
2458 .release = seq_release_private,
2461 int __init igmp_mc_proc_init(void)
2463 proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2464 proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2469 EXPORT_SYMBOL(ip_mc_dec_group);
2470 EXPORT_SYMBOL(ip_mc_inc_group);
2471 EXPORT_SYMBOL(ip_mc_join_group);