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>
97 #include <net/protocol.h>
98 #include <net/route.h>
100 #include <net/checksum.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
110 #define IP_MAX_MEMBERSHIPS 20
111 #define IP_MAX_MSF 10
113 #ifdef CONFIG_IP_MULTICAST
114 /* Parameter names and values are taken from igmp-v2-06 draft */
116 #define IGMP_V1_Router_Present_Timeout (400*HZ)
117 #define IGMP_V2_Router_Present_Timeout (400*HZ)
118 #define IGMP_Unsolicited_Report_Interval (10*HZ)
119 #define IGMP_Query_Response_Interval (10*HZ)
120 #define IGMP_Unsolicited_Report_Count 2
123 #define IGMP_Initial_Report_Delay (1)
125 /* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
132 #define IGMP_V1_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 1 || \
133 (in_dev)->cnf.force_igmp_version == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 2 || \
137 (in_dev)->cnf.force_igmp_version == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
141 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
142 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr);
143 static void igmpv3_clear_delrec(struct in_device *in_dev);
144 static int sf_setstate(struct ip_mc_list *pmc);
145 static void sf_markstate(struct ip_mc_list *pmc);
147 static void ip_mc_clear_src(struct ip_mc_list *pmc);
148 static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
149 int sfcount, __u32 *psfsrc, int delta);
151 static void ip_ma_put(struct ip_mc_list *im)
153 if (atomic_dec_and_test(&im->refcnt)) {
154 in_dev_put(im->interface);
159 #ifdef CONFIG_IP_MULTICAST
165 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
167 spin_lock_bh(&im->lock);
168 if (del_timer(&im->timer))
169 atomic_dec(&im->refcnt);
172 im->unsolicit_count = 0;
173 spin_unlock_bh(&im->lock);
176 /* It must be called with locked im->lock */
177 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
179 int tv=net_random() % max_delay;
182 if (!mod_timer(&im->timer, jiffies+tv+2))
183 atomic_inc(&im->refcnt);
186 static void igmp_gq_start_timer(struct in_device *in_dev)
188 int tv = net_random() % in_dev->mr_maxdelay;
190 in_dev->mr_gq_running = 1;
191 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
195 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
197 int tv = net_random() % delay;
199 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
203 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
205 spin_lock_bh(&im->lock);
206 im->unsolicit_count = 0;
207 if (del_timer(&im->timer)) {
208 if ((long)(im->timer.expires-jiffies) < max_delay) {
209 add_timer(&im->timer);
211 spin_unlock_bh(&im->lock);
214 atomic_dec(&im->refcnt);
216 igmp_start_timer(im, max_delay);
217 spin_unlock_bh(&im->lock);
222 * Send an IGMP report.
225 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
228 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
229 int gdeleted, int sdeleted)
232 case IGMPV3_MODE_IS_INCLUDE:
233 case IGMPV3_MODE_IS_EXCLUDE:
234 if (gdeleted || sdeleted)
236 return !(pmc->gsquery && !psf->sf_gsresp);
237 case IGMPV3_CHANGE_TO_INCLUDE:
238 if (gdeleted || sdeleted)
240 return psf->sf_count[MCAST_INCLUDE] != 0;
241 case IGMPV3_CHANGE_TO_EXCLUDE:
242 if (gdeleted || sdeleted)
244 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
245 psf->sf_count[MCAST_INCLUDE])
247 return pmc->sfcount[MCAST_EXCLUDE] ==
248 psf->sf_count[MCAST_EXCLUDE];
249 case IGMPV3_ALLOW_NEW_SOURCES:
250 if (gdeleted || !psf->sf_crcount)
252 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
253 case IGMPV3_BLOCK_OLD_SOURCES:
254 if (pmc->sfmode == MCAST_INCLUDE)
255 return gdeleted || (psf->sf_crcount && sdeleted);
256 return psf->sf_crcount && !gdeleted && !sdeleted;
262 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
264 struct ip_sf_list *psf;
267 for (psf=pmc->sources; psf; psf=psf->sf_next) {
268 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
275 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
280 struct igmpv3_report *pig;
282 skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
287 struct flowi fl = { .oif = dev->ifindex,
289 .daddr = IGMPV3_ALL_MCR } },
290 .proto = IPPROTO_IGMP };
291 if (ip_route_output_key(&rt, &fl)) {
296 if (rt->rt_src == 0) {
302 skb->dst = &rt->u.dst;
305 skb_reserve(skb, LL_RESERVED_SPACE(dev));
307 skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
310 pip->ihl = (sizeof(struct iphdr)+4)>>2;
312 pip->frag_off = htons(IP_DF);
314 pip->daddr = rt->rt_dst;
315 pip->saddr = rt->rt_src;
316 pip->protocol = IPPROTO_IGMP;
317 pip->tot_len = 0; /* filled in later */
318 ip_select_ident(pip, &rt->u.dst, NULL);
319 ((u8*)&pip[1])[0] = IPOPT_RA;
320 ((u8*)&pip[1])[1] = 4;
321 ((u8*)&pip[1])[2] = 0;
322 ((u8*)&pip[1])[3] = 0;
324 pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
325 skb->h.igmph = (struct igmphdr *)pig;
326 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
334 static int igmpv3_sendpack(struct sk_buff *skb)
336 struct iphdr *pip = skb->nh.iph;
337 struct igmphdr *pig = skb->h.igmph;
340 iplen = skb->tail - (unsigned char *)skb->nh.iph;
341 pip->tot_len = htons(iplen);
344 igmplen = skb->tail - (unsigned char *)skb->h.igmph;
345 pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
347 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
351 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
353 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
356 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
357 int type, struct igmpv3_grec **ppgr)
359 struct net_device *dev = pmc->interface->dev;
360 struct igmpv3_report *pih;
361 struct igmpv3_grec *pgr;
364 skb = igmpv3_newpack(dev, dev->mtu);
367 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
368 pgr->grec_type = type;
369 pgr->grec_auxwords = 0;
371 pgr->grec_mca = pmc->multiaddr;
372 pih = (struct igmpv3_report *)skb->h.igmph;
373 pih->ngrec = htons(ntohs(pih->ngrec)+1);
378 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
379 skb_tailroom(skb)) : 0)
381 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
382 int type, int gdeleted, int sdeleted)
384 struct net_device *dev = pmc->interface->dev;
385 struct igmpv3_report *pih;
386 struct igmpv3_grec *pgr = NULL;
387 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
388 int scount, first, isquery, truncate;
390 if (pmc->multiaddr == IGMP_ALL_HOSTS)
393 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
394 type == IGMPV3_MODE_IS_EXCLUDE;
395 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
396 type == IGMPV3_CHANGE_TO_EXCLUDE;
398 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
401 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
402 type == IGMPV3_BLOCK_OLD_SOURCES)
404 if (pmc->crcount || isquery) {
405 /* make sure we have room for group header and at
408 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)+
410 igmpv3_sendpack(skb);
411 skb = NULL; /* add_grhead will get a new one */
413 skb = add_grhead(skb, pmc, type, &pgr);
417 pih = skb ? (struct igmpv3_report *)skb->h.igmph : NULL;
419 /* EX and TO_EX get a fresh packet, if needed */
421 if (pih && pih->ngrec &&
422 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
424 igmpv3_sendpack(skb);
425 skb = igmpv3_newpack(dev, dev->mtu);
431 for (psf=*psf_list; psf; psf=psf_next) {
434 psf_next = psf->sf_next;
436 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
441 /* clear marks on query responses */
445 if (AVAILABLE(skb) < sizeof(u32) +
446 first*sizeof(struct igmpv3_grec)) {
447 if (truncate && !first)
448 break; /* truncate these */
450 pgr->grec_nsrcs = htons(scount);
452 igmpv3_sendpack(skb);
453 skb = igmpv3_newpack(dev, dev->mtu);
458 skb = add_grhead(skb, pmc, type, &pgr);
461 psrc = (u32 *)skb_put(skb, sizeof(u32));
462 *psrc = psf->sf_inaddr;
464 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
465 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
467 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
469 psf_prev->sf_next = psf->sf_next;
471 *psf_list = psf->sf_next;
479 pgr->grec_nsrcs = htons(scount);
482 pmc->gsquery = 0; /* clear query state on report */
486 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
488 struct sk_buff *skb = NULL;
492 read_lock(&in_dev->mc_list_lock);
493 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
494 if (pmc->multiaddr == IGMP_ALL_HOSTS)
496 spin_lock_bh(&pmc->lock);
497 if (pmc->sfcount[MCAST_EXCLUDE])
498 type = IGMPV3_MODE_IS_EXCLUDE;
500 type = IGMPV3_MODE_IS_INCLUDE;
501 skb = add_grec(skb, pmc, type, 0, 0);
502 spin_unlock_bh(&pmc->lock);
504 read_unlock(&in_dev->mc_list_lock);
506 spin_lock_bh(&pmc->lock);
507 if (pmc->sfcount[MCAST_EXCLUDE])
508 type = IGMPV3_MODE_IS_EXCLUDE;
510 type = IGMPV3_MODE_IS_INCLUDE;
511 skb = add_grec(skb, pmc, type, 0, 0);
512 spin_unlock_bh(&pmc->lock);
516 return igmpv3_sendpack(skb);
520 * remove zero-count source records from a source filter list
522 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
524 struct ip_sf_list *psf_prev, *psf_next, *psf;
527 for (psf=*ppsf; psf; psf = psf_next) {
528 psf_next = psf->sf_next;
529 if (psf->sf_crcount == 0) {
531 psf_prev->sf_next = psf->sf_next;
533 *ppsf = psf->sf_next;
540 static void igmpv3_send_cr(struct in_device *in_dev)
542 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
543 struct sk_buff *skb = NULL;
546 read_lock(&in_dev->mc_list_lock);
547 spin_lock_bh(&in_dev->mc_tomb_lock);
551 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
552 pmc_next = pmc->next;
553 if (pmc->sfmode == MCAST_INCLUDE) {
554 type = IGMPV3_BLOCK_OLD_SOURCES;
555 dtype = IGMPV3_BLOCK_OLD_SOURCES;
556 skb = add_grec(skb, pmc, type, 1, 0);
557 skb = add_grec(skb, pmc, dtype, 1, 1);
561 if (pmc->sfmode == MCAST_EXCLUDE) {
562 type = IGMPV3_CHANGE_TO_INCLUDE;
563 skb = add_grec(skb, pmc, type, 1, 0);
565 if (pmc->crcount == 0) {
566 igmpv3_clear_zeros(&pmc->tomb);
567 igmpv3_clear_zeros(&pmc->sources);
570 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
572 pmc_prev->next = pmc_next;
574 in_dev->mc_tomb = pmc_next;
575 in_dev_put(pmc->interface);
580 spin_unlock_bh(&in_dev->mc_tomb_lock);
583 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
584 spin_lock_bh(&pmc->lock);
585 if (pmc->sfcount[MCAST_EXCLUDE]) {
586 type = IGMPV3_BLOCK_OLD_SOURCES;
587 dtype = IGMPV3_ALLOW_NEW_SOURCES;
589 type = IGMPV3_ALLOW_NEW_SOURCES;
590 dtype = IGMPV3_BLOCK_OLD_SOURCES;
592 skb = add_grec(skb, pmc, type, 0, 0);
593 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
595 /* filter mode changes */
598 if (pmc->sfmode == MCAST_EXCLUDE)
599 type = IGMPV3_CHANGE_TO_EXCLUDE;
601 type = IGMPV3_CHANGE_TO_INCLUDE;
602 skb = add_grec(skb, pmc, type, 0, 0);
604 spin_unlock_bh(&pmc->lock);
606 read_unlock(&in_dev->mc_list_lock);
610 (void) igmpv3_sendpack(skb);
613 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
620 struct net_device *dev = in_dev->dev;
621 u32 group = pmc ? pmc->multiaddr : 0;
624 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
625 return igmpv3_send_report(in_dev, pmc);
626 else if (type == IGMP_HOST_LEAVE_MESSAGE)
627 dst = IGMP_ALL_ROUTER;
632 struct flowi fl = { .oif = dev->ifindex,
633 .nl_u = { .ip4_u = { .daddr = dst } },
634 .proto = IPPROTO_IGMP };
635 if (ip_route_output_key(&rt, &fl))
638 if (rt->rt_src == 0) {
643 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
649 skb->dst = &rt->u.dst;
651 skb_reserve(skb, LL_RESERVED_SPACE(dev));
653 skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
656 iph->ihl = (sizeof(struct iphdr)+4)>>2;
658 iph->frag_off = htons(IP_DF);
661 iph->saddr = rt->rt_src;
662 iph->protocol = IPPROTO_IGMP;
663 iph->tot_len = htons(IGMP_SIZE);
664 ip_select_ident(iph, &rt->u.dst, NULL);
665 ((u8*)&iph[1])[0] = IPOPT_RA;
666 ((u8*)&iph[1])[1] = 4;
667 ((u8*)&iph[1])[2] = 0;
668 ((u8*)&iph[1])[3] = 0;
671 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
676 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
678 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
682 static void igmp_gq_timer_expire(unsigned long data)
684 struct in_device *in_dev = (struct in_device *)data;
686 in_dev->mr_gq_running = 0;
687 igmpv3_send_report(in_dev, NULL);
688 __in_dev_put(in_dev);
691 static void igmp_ifc_timer_expire(unsigned long data)
693 struct in_device *in_dev = (struct in_device *)data;
695 igmpv3_send_cr(in_dev);
696 if (in_dev->mr_ifc_count) {
697 in_dev->mr_ifc_count--;
698 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
700 __in_dev_put(in_dev);
703 static void igmp_ifc_event(struct in_device *in_dev)
705 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
707 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
708 IGMP_Unsolicited_Report_Count;
709 igmp_ifc_start_timer(in_dev, 1);
713 static void igmp_timer_expire(unsigned long data)
715 struct ip_mc_list *im=(struct ip_mc_list *)data;
716 struct in_device *in_dev = im->interface;
718 spin_lock(&im->lock);
721 if (im->unsolicit_count) {
722 im->unsolicit_count--;
723 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
726 spin_unlock(&im->lock);
728 if (IGMP_V1_SEEN(in_dev))
729 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
730 else if (IGMP_V2_SEEN(in_dev))
731 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
733 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
738 static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
740 struct ip_sf_list *psf;
744 for (psf=pmc->sources; psf; psf=psf->sf_next) {
747 for (i=0; i<nsrcs; i++)
748 if (srcs[i] == psf->sf_inaddr) {
756 static void igmp_heard_report(struct in_device *in_dev, u32 group)
758 struct ip_mc_list *im;
760 /* Timers are only set for non-local groups */
762 if (group == IGMP_ALL_HOSTS)
765 read_lock(&in_dev->mc_list_lock);
766 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
767 if (im->multiaddr == group) {
772 read_unlock(&in_dev->mc_list_lock);
775 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
778 struct igmphdr *ih = skb->h.igmph;
779 struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
780 struct ip_mc_list *im;
781 u32 group = ih->group;
788 /* Alas, old v1 router presents here. */
790 max_delay = IGMP_Query_Response_Interval;
791 in_dev->mr_v1_seen = jiffies +
792 IGMP_V1_Router_Present_Timeout;
795 /* v2 router present */
796 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
797 in_dev->mr_v2_seen = jiffies +
798 IGMP_V2_Router_Present_Timeout;
800 /* cancel the interface change timer */
801 in_dev->mr_ifc_count = 0;
802 if (del_timer(&in_dev->mr_ifc_timer))
803 __in_dev_put(in_dev);
804 /* clear deleted report items */
805 igmpv3_clear_delrec(in_dev);
806 } else if (len < 12) {
807 return; /* ignore bogus packet; freed by caller */
809 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
812 ih3 = (struct igmpv3_query *) skb->h.raw;
814 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
815 + ntohs(ih3->nsrcs)*sizeof(__u32)))
817 ih3 = (struct igmpv3_query *) skb->h.raw;
820 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
822 max_delay = 1; /* can't mod w/ 0 */
823 in_dev->mr_maxdelay = max_delay;
825 in_dev->mr_qrv = ih3->qrv;
826 if (!group) { /* general query */
828 return; /* no sources allowed */
829 igmp_gq_start_timer(in_dev);
832 /* mark sources to include, if group & source-specific */
833 mark = ih3->nsrcs != 0;
837 * - Start the timers in all of our membership records
838 * that the query applies to for the interface on
839 * which the query arrived excl. those that belong
840 * to a "local" group (224.0.0.X)
841 * - For timers already running check if they need to
843 * - Use the igmp->igmp_code field as the maximum
846 read_lock(&in_dev->mc_list_lock);
847 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
848 if (group && group != im->multiaddr)
850 if (im->multiaddr == IGMP_ALL_HOSTS)
852 spin_lock_bh(&im->lock);
854 im->gsquery = im->gsquery && mark;
858 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
859 spin_unlock_bh(&im->lock);
860 igmp_mod_timer(im, max_delay);
862 read_unlock(&in_dev->mc_list_lock);
865 int igmp_rcv(struct sk_buff *skb)
867 /* This basically follows the spec line by line -- see RFC1112 */
869 struct in_device *in_dev = in_dev_get(skb->dev);
877 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
880 switch (skb->ip_summed) {
882 if (!(u16)csum_fold(skb->csum))
887 if (__skb_checksum_complete(skb))
893 case IGMP_HOST_MEMBERSHIP_QUERY:
894 igmp_heard_query(in_dev, skb, len);
896 case IGMP_HOST_MEMBERSHIP_REPORT:
897 case IGMPV2_HOST_MEMBERSHIP_REPORT:
898 case IGMPV3_HOST_MEMBERSHIP_REPORT:
899 /* Is it our report looped back? */
900 if (((struct rtable*)skb->dst)->fl.iif == 0)
902 /* don't rely on MC router hearing unicast reports */
903 if (skb->pkt_type == PACKET_MULTICAST ||
904 skb->pkt_type == PACKET_BROADCAST)
905 igmp_heard_report(in_dev, ih->group);
908 #ifdef CONFIG_IP_PIMSM_V1
910 return pim_rcv_v1(skb);
914 case IGMP_HOST_LEAVE_MESSAGE:
916 case IGMP_MTRACE_RESP:
919 NETDEBUG(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type);
932 * Add a filter to a device
935 static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
937 char buf[MAX_ADDR_LEN];
938 struct net_device *dev = in_dev->dev;
940 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
941 We will get multicast token leakage, when IFF_MULTICAST
942 is changed. This check should be done in dev->set_multicast_list
943 routine. Something sort of:
944 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
947 if (arp_mc_map(addr, buf, dev, 0) == 0)
948 dev_mc_add(dev,buf,dev->addr_len,0);
952 * Remove a filter from a device
955 static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
957 char buf[MAX_ADDR_LEN];
958 struct net_device *dev = in_dev->dev;
960 if (arp_mc_map(addr, buf, dev, 0) == 0)
961 dev_mc_delete(dev,buf,dev->addr_len,0);
964 #ifdef CONFIG_IP_MULTICAST
966 * deleted ip_mc_list manipulation
968 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
970 struct ip_mc_list *pmc;
972 /* this is an "ip_mc_list" for convenience; only the fields below
973 * are actually used. In particular, the refcnt and users are not
974 * used for management of the delete list. Using the same structure
975 * for deleted items allows change reports to use common code with
976 * non-deleted or query-response MCA's.
978 pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
981 memset(pmc, 0, sizeof(*pmc));
982 spin_lock_bh(&im->lock);
983 pmc->interface = im->interface;
985 pmc->multiaddr = im->multiaddr;
986 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
987 IGMP_Unsolicited_Report_Count;
988 pmc->sfmode = im->sfmode;
989 if (pmc->sfmode == MCAST_INCLUDE) {
990 struct ip_sf_list *psf;
992 pmc->tomb = im->tomb;
993 pmc->sources = im->sources;
994 im->tomb = im->sources = NULL;
995 for (psf=pmc->sources; psf; psf=psf->sf_next)
996 psf->sf_crcount = pmc->crcount;
998 spin_unlock_bh(&im->lock);
1000 spin_lock_bh(&in_dev->mc_tomb_lock);
1001 pmc->next = in_dev->mc_tomb;
1002 in_dev->mc_tomb = pmc;
1003 spin_unlock_bh(&in_dev->mc_tomb_lock);
1006 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
1008 struct ip_mc_list *pmc, *pmc_prev;
1009 struct ip_sf_list *psf, *psf_next;
1011 spin_lock_bh(&in_dev->mc_tomb_lock);
1013 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1014 if (pmc->multiaddr == multiaddr)
1020 pmc_prev->next = pmc->next;
1022 in_dev->mc_tomb = pmc->next;
1024 spin_unlock_bh(&in_dev->mc_tomb_lock);
1026 for (psf=pmc->tomb; psf; psf=psf_next) {
1027 psf_next = psf->sf_next;
1030 in_dev_put(pmc->interface);
1035 static void igmpv3_clear_delrec(struct in_device *in_dev)
1037 struct ip_mc_list *pmc, *nextpmc;
1039 spin_lock_bh(&in_dev->mc_tomb_lock);
1040 pmc = in_dev->mc_tomb;
1041 in_dev->mc_tomb = NULL;
1042 spin_unlock_bh(&in_dev->mc_tomb_lock);
1044 for (; pmc; pmc = nextpmc) {
1045 nextpmc = pmc->next;
1046 ip_mc_clear_src(pmc);
1047 in_dev_put(pmc->interface);
1050 /* clear dead sources, too */
1051 read_lock(&in_dev->mc_list_lock);
1052 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1053 struct ip_sf_list *psf, *psf_next;
1055 spin_lock_bh(&pmc->lock);
1058 spin_unlock_bh(&pmc->lock);
1059 for (; psf; psf=psf_next) {
1060 psf_next = psf->sf_next;
1064 read_unlock(&in_dev->mc_list_lock);
1068 static void igmp_group_dropped(struct ip_mc_list *im)
1070 struct in_device *in_dev = im->interface;
1071 #ifdef CONFIG_IP_MULTICAST
1077 ip_mc_filter_del(in_dev, im->multiaddr);
1080 #ifdef CONFIG_IP_MULTICAST
1081 if (im->multiaddr == IGMP_ALL_HOSTS)
1084 reporter = im->reporter;
1085 igmp_stop_timer(im);
1087 if (!in_dev->dead) {
1088 if (IGMP_V1_SEEN(in_dev))
1090 if (IGMP_V2_SEEN(in_dev)) {
1092 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1096 igmpv3_add_delrec(in_dev, im);
1098 igmp_ifc_event(in_dev);
1102 ip_mc_clear_src(im);
1105 static void igmp_group_added(struct ip_mc_list *im)
1107 struct in_device *in_dev = im->interface;
1109 if (im->loaded == 0) {
1111 ip_mc_filter_add(in_dev, im->multiaddr);
1114 #ifdef CONFIG_IP_MULTICAST
1115 if (im->multiaddr == IGMP_ALL_HOSTS)
1120 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1121 spin_lock_bh(&im->lock);
1122 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1123 spin_unlock_bh(&im->lock);
1128 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1129 IGMP_Unsolicited_Report_Count;
1130 igmp_ifc_event(in_dev);
1136 * Multicast list managers
1141 * A socket has joined a multicast group on device dev.
1144 void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1146 struct ip_mc_list *im;
1150 for (im=in_dev->mc_list; im; im=im->next) {
1151 if (im->multiaddr == addr) {
1153 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1158 im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1163 im->interface=in_dev;
1164 in_dev_hold(in_dev);
1166 /* initial mode is (EX, empty) */
1167 im->sfmode = MCAST_EXCLUDE;
1168 im->sfcount[MCAST_INCLUDE] = 0;
1169 im->sfcount[MCAST_EXCLUDE] = 1;
1173 atomic_set(&im->refcnt, 1);
1174 spin_lock_init(&im->lock);
1175 #ifdef CONFIG_IP_MULTICAST
1177 init_timer(&im->timer);
1178 im->timer.data=(unsigned long)im;
1179 im->timer.function=&igmp_timer_expire;
1180 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1185 write_lock_bh(&in_dev->mc_list_lock);
1186 im->next=in_dev->mc_list;
1188 write_unlock_bh(&in_dev->mc_list_lock);
1189 #ifdef CONFIG_IP_MULTICAST
1190 igmpv3_del_delrec(in_dev, im->multiaddr);
1192 igmp_group_added(im);
1194 ip_rt_multicast_event(in_dev);
1200 * A socket has left a multicast group on device dev
1203 void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1205 struct ip_mc_list *i, **ip;
1209 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1210 if (i->multiaddr==addr) {
1211 if (--i->users == 0) {
1212 write_lock_bh(&in_dev->mc_list_lock);
1214 write_unlock_bh(&in_dev->mc_list_lock);
1215 igmp_group_dropped(i);
1218 ip_rt_multicast_event(in_dev);
1228 /* Device going down */
1230 void ip_mc_down(struct in_device *in_dev)
1232 struct ip_mc_list *i;
1236 for (i=in_dev->mc_list; i; i=i->next)
1237 igmp_group_dropped(i);
1239 #ifdef CONFIG_IP_MULTICAST
1240 in_dev->mr_ifc_count = 0;
1241 if (del_timer(&in_dev->mr_ifc_timer))
1242 __in_dev_put(in_dev);
1243 in_dev->mr_gq_running = 0;
1244 if (del_timer(&in_dev->mr_gq_timer))
1245 __in_dev_put(in_dev);
1246 igmpv3_clear_delrec(in_dev);
1249 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1252 void ip_mc_init_dev(struct in_device *in_dev)
1256 in_dev->mc_tomb = NULL;
1257 #ifdef CONFIG_IP_MULTICAST
1258 in_dev->mr_gq_running = 0;
1259 init_timer(&in_dev->mr_gq_timer);
1260 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1261 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1262 in_dev->mr_ifc_count = 0;
1263 init_timer(&in_dev->mr_ifc_timer);
1264 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1265 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1266 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1269 rwlock_init(&in_dev->mc_list_lock);
1270 spin_lock_init(&in_dev->mc_tomb_lock);
1273 /* Device going up */
1275 void ip_mc_up(struct in_device *in_dev)
1277 struct ip_mc_list *i;
1281 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1283 for (i=in_dev->mc_list; i; i=i->next)
1284 igmp_group_added(i);
1288 * Device is about to be destroyed: clean up.
1291 void ip_mc_destroy_dev(struct in_device *in_dev)
1293 struct ip_mc_list *i;
1297 /* Deactivate timers */
1300 write_lock_bh(&in_dev->mc_list_lock);
1301 while ((i = in_dev->mc_list) != NULL) {
1302 in_dev->mc_list = i->next;
1303 write_unlock_bh(&in_dev->mc_list_lock);
1305 igmp_group_dropped(i);
1308 write_lock_bh(&in_dev->mc_list_lock);
1310 write_unlock_bh(&in_dev->mc_list_lock);
1313 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1315 struct flowi fl = { .nl_u = { .ip4_u =
1316 { .daddr = imr->imr_multiaddr.s_addr } } };
1318 struct net_device *dev = NULL;
1319 struct in_device *idev = NULL;
1321 if (imr->imr_ifindex) {
1322 idev = inetdev_by_index(imr->imr_ifindex);
1327 if (imr->imr_address.s_addr) {
1328 dev = ip_dev_find(imr->imr_address.s_addr);
1334 if (!dev && !ip_route_output_key(&rt, &fl)) {
1335 dev = rt->u.dst.dev;
1339 imr->imr_ifindex = dev->ifindex;
1340 idev = __in_dev_get_rtnl(dev);
1346 * Join a socket to a group
1348 int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1349 int sysctl_igmp_max_msf = IP_MAX_MSF;
1352 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1355 struct ip_sf_list *psf, *psf_prev;
1359 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1360 if (psf->sf_inaddr == *psfsrc)
1364 if (!psf || psf->sf_count[sfmode] == 0) {
1365 /* source filter not found, or count wrong => bug */
1368 psf->sf_count[sfmode]--;
1369 if (psf->sf_count[sfmode] == 0) {
1370 ip_rt_multicast_event(pmc->interface);
1372 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1373 #ifdef CONFIG_IP_MULTICAST
1374 struct in_device *in_dev = pmc->interface;
1377 /* no more filters for this source */
1379 psf_prev->sf_next = psf->sf_next;
1381 pmc->sources = psf->sf_next;
1382 #ifdef CONFIG_IP_MULTICAST
1383 if (psf->sf_oldin &&
1384 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1385 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1386 IGMP_Unsolicited_Report_Count;
1387 psf->sf_next = pmc->tomb;
1397 #ifndef CONFIG_IP_MULTICAST
1398 #define igmp_ifc_event(x) do { } while (0)
1401 static int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1402 int sfcount, __u32 *psfsrc, int delta)
1404 struct ip_mc_list *pmc;
1410 read_lock(&in_dev->mc_list_lock);
1411 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1412 if (*pmca == pmc->multiaddr)
1416 /* MCA not found?? bug */
1417 read_unlock(&in_dev->mc_list_lock);
1420 spin_lock_bh(&pmc->lock);
1421 read_unlock(&in_dev->mc_list_lock);
1422 #ifdef CONFIG_IP_MULTICAST
1427 if (!pmc->sfcount[sfmode])
1429 pmc->sfcount[sfmode]--;
1432 for (i=0; i<sfcount; i++) {
1433 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1435 changerec |= rv > 0;
1439 if (pmc->sfmode == MCAST_EXCLUDE &&
1440 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1441 pmc->sfcount[MCAST_INCLUDE]) {
1442 #ifdef CONFIG_IP_MULTICAST
1443 struct ip_sf_list *psf;
1446 /* filter mode change */
1447 pmc->sfmode = MCAST_INCLUDE;
1448 #ifdef CONFIG_IP_MULTICAST
1449 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1450 IGMP_Unsolicited_Report_Count;
1451 in_dev->mr_ifc_count = pmc->crcount;
1452 for (psf=pmc->sources; psf; psf = psf->sf_next)
1453 psf->sf_crcount = 0;
1454 igmp_ifc_event(pmc->interface);
1455 } else if (sf_setstate(pmc) || changerec) {
1456 igmp_ifc_event(pmc->interface);
1460 spin_unlock_bh(&pmc->lock);
1465 * Add multicast single-source filter to the interface list
1467 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1468 __u32 *psfsrc, int delta)
1470 struct ip_sf_list *psf, *psf_prev;
1473 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1474 if (psf->sf_inaddr == *psfsrc)
1479 psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1482 memset(psf, 0, sizeof(*psf));
1483 psf->sf_inaddr = *psfsrc;
1485 psf_prev->sf_next = psf;
1489 psf->sf_count[sfmode]++;
1490 if (psf->sf_count[sfmode] == 1) {
1491 ip_rt_multicast_event(pmc->interface);
1496 #ifdef CONFIG_IP_MULTICAST
1497 static void sf_markstate(struct ip_mc_list *pmc)
1499 struct ip_sf_list *psf;
1500 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1502 for (psf=pmc->sources; psf; psf=psf->sf_next)
1503 if (pmc->sfcount[MCAST_EXCLUDE]) {
1504 psf->sf_oldin = mca_xcount ==
1505 psf->sf_count[MCAST_EXCLUDE] &&
1506 !psf->sf_count[MCAST_INCLUDE];
1508 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1511 static int sf_setstate(struct ip_mc_list *pmc)
1513 struct ip_sf_list *psf;
1514 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1515 int qrv = pmc->interface->mr_qrv;
1519 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1520 if (pmc->sfcount[MCAST_EXCLUDE]) {
1521 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1522 !psf->sf_count[MCAST_INCLUDE];
1524 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1525 if (new_in != psf->sf_oldin) {
1526 psf->sf_crcount = qrv;
1535 * Add multicast source filter list to the interface list
1537 static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1538 int sfcount, __u32 *psfsrc, int delta)
1540 struct ip_mc_list *pmc;
1546 read_lock(&in_dev->mc_list_lock);
1547 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1548 if (*pmca == pmc->multiaddr)
1552 /* MCA not found?? bug */
1553 read_unlock(&in_dev->mc_list_lock);
1556 spin_lock_bh(&pmc->lock);
1557 read_unlock(&in_dev->mc_list_lock);
1559 #ifdef CONFIG_IP_MULTICAST
1562 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1564 pmc->sfcount[sfmode]++;
1566 for (i=0; i<sfcount; i++) {
1567 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1574 pmc->sfcount[sfmode]--;
1576 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1577 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1578 #ifdef CONFIG_IP_MULTICAST
1579 struct in_device *in_dev = pmc->interface;
1580 struct ip_sf_list *psf;
1583 /* filter mode change */
1584 if (pmc->sfcount[MCAST_EXCLUDE])
1585 pmc->sfmode = MCAST_EXCLUDE;
1586 else if (pmc->sfcount[MCAST_INCLUDE])
1587 pmc->sfmode = MCAST_INCLUDE;
1588 #ifdef CONFIG_IP_MULTICAST
1589 /* else no filters; keep old mode for reports */
1591 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1592 IGMP_Unsolicited_Report_Count;
1593 in_dev->mr_ifc_count = pmc->crcount;
1594 for (psf=pmc->sources; psf; psf = psf->sf_next)
1595 psf->sf_crcount = 0;
1596 igmp_ifc_event(in_dev);
1597 } else if (sf_setstate(pmc)) {
1598 igmp_ifc_event(in_dev);
1601 spin_unlock_bh(&pmc->lock);
1605 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1607 struct ip_sf_list *psf, *nextpsf;
1609 for (psf=pmc->tomb; psf; psf=nextpsf) {
1610 nextpsf = psf->sf_next;
1614 for (psf=pmc->sources; psf; psf=nextpsf) {
1615 nextpsf = psf->sf_next;
1618 pmc->sources = NULL;
1619 pmc->sfmode = MCAST_EXCLUDE;
1620 pmc->sfcount[MCAST_INCLUDE] = 0;
1621 pmc->sfcount[MCAST_EXCLUDE] = 1;
1626 * Join a multicast group
1628 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1631 u32 addr = imr->imr_multiaddr.s_addr;
1632 struct ip_mc_socklist *iml=NULL, *i;
1633 struct in_device *in_dev;
1634 struct inet_sock *inet = inet_sk(sk);
1638 if (!MULTICAST(addr))
1643 in_dev = ip_mc_find_dev(imr);
1652 ifindex = imr->imr_ifindex;
1653 for (i = inet->mc_list; i; i = i->next) {
1654 if (i->multi.imr_multiaddr.s_addr == addr &&
1655 i->multi.imr_ifindex == ifindex)
1660 if (count >= sysctl_igmp_max_memberships)
1662 iml = (struct ip_mc_socklist *)sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1666 memcpy(&iml->multi, imr, sizeof(*imr));
1667 iml->next = inet->mc_list;
1669 iml->sfmode = MCAST_EXCLUDE;
1670 inet->mc_list = iml;
1671 ip_mc_inc_group(in_dev, addr);
1678 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1679 struct in_device *in_dev)
1683 if (iml->sflist == 0) {
1684 /* any-source empty exclude case */
1685 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1686 iml->sfmode, 0, NULL, 0);
1688 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1689 iml->sfmode, iml->sflist->sl_count,
1690 iml->sflist->sl_addr, 0);
1691 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1697 * Ask a socket to leave a group.
1700 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1702 struct inet_sock *inet = inet_sk(sk);
1703 struct ip_mc_socklist *iml, **imlp;
1704 struct in_device *in_dev;
1705 u32 group = imr->imr_multiaddr.s_addr;
1709 in_dev = ip_mc_find_dev(imr);
1714 ifindex = imr->imr_ifindex;
1715 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1716 if (iml->multi.imr_multiaddr.s_addr == group &&
1717 iml->multi.imr_ifindex == ifindex) {
1718 (void) ip_mc_leave_src(sk, iml, in_dev);
1722 ip_mc_dec_group(in_dev, group);
1724 sock_kfree_s(sk, iml, sizeof(*iml));
1729 return -EADDRNOTAVAIL;
1732 int ip_mc_source(int add, int omode, struct sock *sk, struct
1733 ip_mreq_source *mreqs, int ifindex)
1736 struct ip_mreqn imr;
1737 u32 addr = mreqs->imr_multiaddr;
1738 struct ip_mc_socklist *pmc;
1739 struct in_device *in_dev = NULL;
1740 struct inet_sock *inet = inet_sk(sk);
1741 struct ip_sf_socklist *psl;
1745 if (!MULTICAST(addr))
1750 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1751 imr.imr_address.s_addr = mreqs->imr_interface;
1752 imr.imr_ifindex = ifindex;
1753 in_dev = ip_mc_find_dev(&imr);
1759 err = -EADDRNOTAVAIL;
1761 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1762 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1763 && pmc->multi.imr_ifindex == imr.imr_ifindex)
1766 if (!pmc) { /* must have a prior join */
1770 /* if a source filter was set, must be the same mode as before */
1772 if (pmc->sfmode != omode) {
1776 } else if (pmc->sfmode != omode) {
1777 /* allow mode switches for empty-set filters */
1778 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1779 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1781 pmc->sfmode = omode;
1787 goto done; /* err = -EADDRNOTAVAIL */
1789 for (i=0; i<psl->sl_count; i++) {
1790 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1795 if (rv) /* source not found */
1796 goto done; /* err = -EADDRNOTAVAIL */
1798 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1799 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1804 /* update the interface filter */
1805 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1806 &mreqs->imr_sourceaddr, 1);
1808 for (j=i+1; j<psl->sl_count; j++)
1809 psl->sl_addr[j-1] = psl->sl_addr[j];
1814 /* else, add a new source to the filter */
1816 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1820 if (!psl || psl->sl_count == psl->sl_max) {
1821 struct ip_sf_socklist *newpsl;
1822 int count = IP_SFBLOCK;
1825 count += psl->sl_max;
1826 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1827 IP_SFLSIZE(count), GFP_KERNEL);
1832 newpsl->sl_max = count;
1833 newpsl->sl_count = count - IP_SFBLOCK;
1835 for (i=0; i<psl->sl_count; i++)
1836 newpsl->sl_addr[i] = psl->sl_addr[i];
1837 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1839 pmc->sflist = psl = newpsl;
1841 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1842 for (i=0; i<psl->sl_count; i++) {
1843 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1848 if (rv == 0) /* address already there is an error */
1850 for (j=psl->sl_count-1; j>=i; j--)
1851 psl->sl_addr[j+1] = psl->sl_addr[j];
1852 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1855 /* update the interface list */
1856 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1857 &mreqs->imr_sourceaddr, 1);
1861 return ip_mc_leave_group(sk, &imr);
1865 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1868 struct ip_mreqn imr;
1869 u32 addr = msf->imsf_multiaddr;
1870 struct ip_mc_socklist *pmc;
1871 struct in_device *in_dev;
1872 struct inet_sock *inet = inet_sk(sk);
1873 struct ip_sf_socklist *newpsl, *psl;
1876 if (!MULTICAST(addr))
1878 if (msf->imsf_fmode != MCAST_INCLUDE &&
1879 msf->imsf_fmode != MCAST_EXCLUDE)
1884 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1885 imr.imr_address.s_addr = msf->imsf_interface;
1886 imr.imr_ifindex = ifindex;
1887 in_dev = ip_mc_find_dev(&imr);
1894 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1895 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
1900 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1901 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1902 pmc->multi.imr_ifindex == imr.imr_ifindex)
1905 if (!pmc) { /* must have a prior join */
1909 if (msf->imsf_numsrc) {
1910 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1911 IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1916 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1917 memcpy(newpsl->sl_addr, msf->imsf_slist,
1918 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1919 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1920 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1922 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1927 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1928 msf->imsf_fmode, 0, NULL, 0);
1932 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1933 psl->sl_count, psl->sl_addr, 0);
1934 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1936 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1938 pmc->sflist = newpsl;
1939 pmc->sfmode = msf->imsf_fmode;
1944 err = ip_mc_leave_group(sk, &imr);
1948 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1949 struct ip_msfilter __user *optval, int __user *optlen)
1951 int err, len, count, copycount;
1952 struct ip_mreqn imr;
1953 u32 addr = msf->imsf_multiaddr;
1954 struct ip_mc_socklist *pmc;
1955 struct in_device *in_dev;
1956 struct inet_sock *inet = inet_sk(sk);
1957 struct ip_sf_socklist *psl;
1959 if (!MULTICAST(addr))
1964 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1965 imr.imr_address.s_addr = msf->imsf_interface;
1966 imr.imr_ifindex = 0;
1967 in_dev = ip_mc_find_dev(&imr);
1973 err = -EADDRNOTAVAIL;
1975 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1976 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1977 pmc->multi.imr_ifindex == imr.imr_ifindex)
1980 if (!pmc) /* must have a prior join */
1982 msf->imsf_fmode = pmc->sfmode;
1989 count = psl->sl_count;
1991 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1992 len = copycount * sizeof(psl->sl_addr[0]);
1993 msf->imsf_numsrc = count;
1994 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1995 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
1999 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2007 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2008 struct group_filter __user *optval, int __user *optlen)
2010 int err, i, count, copycount;
2011 struct sockaddr_in *psin;
2013 struct ip_mc_socklist *pmc;
2014 struct inet_sock *inet = inet_sk(sk);
2015 struct ip_sf_socklist *psl;
2017 psin = (struct sockaddr_in *)&gsf->gf_group;
2018 if (psin->sin_family != AF_INET)
2020 addr = psin->sin_addr.s_addr;
2021 if (!MULTICAST(addr))
2026 err = -EADDRNOTAVAIL;
2028 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2029 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2030 pmc->multi.imr_ifindex == gsf->gf_interface)
2033 if (!pmc) /* must have a prior join */
2035 gsf->gf_fmode = pmc->sfmode;
2038 count = psl ? psl->sl_count : 0;
2039 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2040 gsf->gf_numsrc = count;
2041 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2042 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2045 for (i=0; i<copycount; i++) {
2046 struct sockaddr_in *psin;
2047 struct sockaddr_storage ss;
2049 psin = (struct sockaddr_in *)&ss;
2050 memset(&ss, 0, sizeof(ss));
2051 psin->sin_family = AF_INET;
2052 psin->sin_addr.s_addr = psl->sl_addr[i];
2053 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2063 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2065 int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2067 struct inet_sock *inet = inet_sk(sk);
2068 struct ip_mc_socklist *pmc;
2069 struct ip_sf_socklist *psl;
2072 if (!MULTICAST(loc_addr))
2075 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2076 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2077 pmc->multi.imr_ifindex == dif)
2084 return pmc->sfmode == MCAST_EXCLUDE;
2086 for (i=0; i<psl->sl_count; i++) {
2087 if (psl->sl_addr[i] == rmt_addr)
2090 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2092 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2098 * A socket is closing.
2101 void ip_mc_drop_socket(struct sock *sk)
2103 struct inet_sock *inet = inet_sk(sk);
2104 struct ip_mc_socklist *iml;
2106 if (inet->mc_list == NULL)
2110 while ((iml = inet->mc_list) != NULL) {
2111 struct in_device *in_dev;
2112 inet->mc_list = iml->next;
2114 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2115 (void) ip_mc_leave_src(sk, iml, in_dev);
2116 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2119 sock_kfree_s(sk, iml, sizeof(*iml));
2125 int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2127 struct ip_mc_list *im;
2128 struct ip_sf_list *psf;
2131 read_lock(&in_dev->mc_list_lock);
2132 for (im=in_dev->mc_list; im; im=im->next) {
2133 if (im->multiaddr == mc_addr)
2136 if (im && proto == IPPROTO_IGMP) {
2140 for (psf=im->sources; psf; psf=psf->sf_next) {
2141 if (psf->sf_inaddr == src_addr)
2145 rv = psf->sf_count[MCAST_INCLUDE] ||
2146 psf->sf_count[MCAST_EXCLUDE] !=
2147 im->sfcount[MCAST_EXCLUDE];
2149 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2151 rv = 1; /* unspecified source; tentatively allow */
2153 read_unlock(&in_dev->mc_list_lock);
2157 #if defined(CONFIG_PROC_FS)
2158 struct igmp_mc_iter_state {
2159 struct net_device *dev;
2160 struct in_device *in_dev;
2163 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2165 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2167 struct ip_mc_list *im = NULL;
2168 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2170 for (state->dev = dev_base, state->in_dev = NULL;
2172 state->dev = state->dev->next) {
2173 struct in_device *in_dev;
2174 in_dev = in_dev_get(state->dev);
2177 read_lock(&in_dev->mc_list_lock);
2178 im = in_dev->mc_list;
2180 state->in_dev = in_dev;
2183 read_unlock(&in_dev->mc_list_lock);
2189 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2191 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2194 if (likely(state->in_dev != NULL)) {
2195 read_unlock(&state->in_dev->mc_list_lock);
2196 in_dev_put(state->in_dev);
2198 state->dev = state->dev->next;
2200 state->in_dev = NULL;
2203 state->in_dev = in_dev_get(state->dev);
2206 read_lock(&state->in_dev->mc_list_lock);
2207 im = state->in_dev->mc_list;
2212 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2214 struct ip_mc_list *im = igmp_mc_get_first(seq);
2216 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2218 return pos ? NULL : im;
2221 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2223 read_lock(&dev_base_lock);
2224 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2227 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2229 struct ip_mc_list *im;
2230 if (v == SEQ_START_TOKEN)
2231 im = igmp_mc_get_first(seq);
2233 im = igmp_mc_get_next(seq, v);
2238 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2240 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2241 if (likely(state->in_dev != NULL)) {
2242 read_unlock(&state->in_dev->mc_list_lock);
2243 in_dev_put(state->in_dev);
2244 state->in_dev = NULL;
2247 read_unlock(&dev_base_lock);
2250 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2252 if (v == SEQ_START_TOKEN)
2254 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2256 struct ip_mc_list *im = (struct ip_mc_list *)v;
2257 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2259 #ifdef CONFIG_IP_MULTICAST
2260 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2261 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2267 if (state->in_dev->mc_list == im) {
2268 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2269 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2273 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2274 im->multiaddr, im->users,
2275 im->tm_running, im->tm_running ?
2276 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2282 static struct seq_operations igmp_mc_seq_ops = {
2283 .start = igmp_mc_seq_start,
2284 .next = igmp_mc_seq_next,
2285 .stop = igmp_mc_seq_stop,
2286 .show = igmp_mc_seq_show,
2289 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2291 struct seq_file *seq;
2293 struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2297 rc = seq_open(file, &igmp_mc_seq_ops);
2301 seq = file->private_data;
2303 memset(s, 0, sizeof(*s));
2311 static struct file_operations igmp_mc_seq_fops = {
2312 .owner = THIS_MODULE,
2313 .open = igmp_mc_seq_open,
2315 .llseek = seq_lseek,
2316 .release = seq_release_private,
2319 struct igmp_mcf_iter_state {
2320 struct net_device *dev;
2321 struct in_device *idev;
2322 struct ip_mc_list *im;
2325 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2327 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2329 struct ip_sf_list *psf = NULL;
2330 struct ip_mc_list *im = NULL;
2331 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2333 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2335 state->dev = state->dev->next) {
2336 struct in_device *idev;
2337 idev = in_dev_get(state->dev);
2338 if (unlikely(idev == NULL))
2340 read_lock(&idev->mc_list_lock);
2342 if (likely(im != NULL)) {
2343 spin_lock_bh(&im->lock);
2345 if (likely(psf != NULL)) {
2350 spin_unlock_bh(&im->lock);
2352 read_unlock(&idev->mc_list_lock);
2358 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2360 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2364 spin_unlock_bh(&state->im->lock);
2365 state->im = state->im->next;
2366 while (!state->im) {
2367 if (likely(state->idev != NULL)) {
2368 read_unlock(&state->idev->mc_list_lock);
2369 in_dev_put(state->idev);
2371 state->dev = state->dev->next;
2376 state->idev = in_dev_get(state->dev);
2379 read_lock(&state->idev->mc_list_lock);
2380 state->im = state->idev->mc_list;
2384 spin_lock_bh(&state->im->lock);
2385 psf = state->im->sources;
2391 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2393 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2395 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2397 return pos ? NULL : psf;
2400 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2402 read_lock(&dev_base_lock);
2403 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2406 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2408 struct ip_sf_list *psf;
2409 if (v == SEQ_START_TOKEN)
2410 psf = igmp_mcf_get_first(seq);
2412 psf = igmp_mcf_get_next(seq, v);
2417 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2419 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2420 if (likely(state->im != NULL)) {
2421 spin_unlock_bh(&state->im->lock);
2424 if (likely(state->idev != NULL)) {
2425 read_unlock(&state->idev->mc_list_lock);
2426 in_dev_put(state->idev);
2430 read_unlock(&dev_base_lock);
2433 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2435 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2436 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2438 if (v == SEQ_START_TOKEN) {
2441 "%10s %10s %6s %6s\n", "Idx",
2443 "SRC", "INC", "EXC");
2447 "0x%08x %6lu %6lu\n",
2448 state->dev->ifindex, state->dev->name,
2449 ntohl(state->im->multiaddr),
2450 ntohl(psf->sf_inaddr),
2451 psf->sf_count[MCAST_INCLUDE],
2452 psf->sf_count[MCAST_EXCLUDE]);
2457 static struct seq_operations igmp_mcf_seq_ops = {
2458 .start = igmp_mcf_seq_start,
2459 .next = igmp_mcf_seq_next,
2460 .stop = igmp_mcf_seq_stop,
2461 .show = igmp_mcf_seq_show,
2464 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2466 struct seq_file *seq;
2468 struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2472 rc = seq_open(file, &igmp_mcf_seq_ops);
2476 seq = file->private_data;
2478 memset(s, 0, sizeof(*s));
2486 static struct file_operations igmp_mcf_seq_fops = {
2487 .owner = THIS_MODULE,
2488 .open = igmp_mcf_seq_open,
2490 .llseek = seq_lseek,
2491 .release = seq_release_private,
2494 int __init igmp_mc_proc_init(void)
2496 proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2497 proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2502 EXPORT_SYMBOL(ip_mc_dec_group);
2503 EXPORT_SYMBOL(ip_mc_inc_group);
2504 EXPORT_SYMBOL(ip_mc_join_group);