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) {
299 skb->dst = &rt->u.dst;
302 skb_reserve(skb, LL_RESERVED_SPACE(dev));
304 skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
307 pip->ihl = (sizeof(struct iphdr)+4)>>2;
309 pip->frag_off = htons(IP_DF);
311 pip->daddr = rt->rt_dst;
312 pip->saddr = rt->rt_src;
313 pip->protocol = IPPROTO_IGMP;
314 pip->tot_len = 0; /* filled in later */
315 ip_select_ident(pip, &rt->u.dst, NULL);
316 ((u8*)&pip[1])[0] = IPOPT_RA;
317 ((u8*)&pip[1])[1] = 4;
318 ((u8*)&pip[1])[2] = 0;
319 ((u8*)&pip[1])[3] = 0;
321 pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
322 skb->h.igmph = (struct igmphdr *)pig;
323 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
331 static int igmpv3_sendpack(struct sk_buff *skb)
333 struct iphdr *pip = skb->nh.iph;
334 struct igmphdr *pig = skb->h.igmph;
337 iplen = skb->tail - (unsigned char *)skb->nh.iph;
338 pip->tot_len = htons(iplen);
341 igmplen = skb->tail - (unsigned char *)skb->h.igmph;
342 pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
344 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
348 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
350 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
353 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
354 int type, struct igmpv3_grec **ppgr)
356 struct net_device *dev = pmc->interface->dev;
357 struct igmpv3_report *pih;
358 struct igmpv3_grec *pgr;
361 skb = igmpv3_newpack(dev, dev->mtu);
364 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
365 pgr->grec_type = type;
366 pgr->grec_auxwords = 0;
368 pgr->grec_mca = pmc->multiaddr;
369 pih = (struct igmpv3_report *)skb->h.igmph;
370 pih->ngrec = htons(ntohs(pih->ngrec)+1);
375 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
376 skb_tailroom(skb)) : 0)
378 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
379 int type, int gdeleted, int sdeleted)
381 struct net_device *dev = pmc->interface->dev;
382 struct igmpv3_report *pih;
383 struct igmpv3_grec *pgr = 0;
384 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
385 int scount, first, isquery, truncate;
387 if (pmc->multiaddr == IGMP_ALL_HOSTS)
390 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
391 type == IGMPV3_MODE_IS_EXCLUDE;
392 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
393 type == IGMPV3_CHANGE_TO_EXCLUDE;
395 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
398 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
399 type == IGMPV3_BLOCK_OLD_SOURCES)
401 if (pmc->crcount || isquery) {
402 /* make sure we have room for group header and at
405 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)+
407 igmpv3_sendpack(skb);
408 skb = 0; /* add_grhead will get a new one */
410 skb = add_grhead(skb, pmc, type, &pgr);
414 pih = skb ? (struct igmpv3_report *)skb->h.igmph : 0;
416 /* EX and TO_EX get a fresh packet, if needed */
418 if (pih && pih->ngrec &&
419 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
421 igmpv3_sendpack(skb);
422 skb = igmpv3_newpack(dev, dev->mtu);
428 for (psf=*psf_list; psf; psf=psf_next) {
431 psf_next = psf->sf_next;
433 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
438 /* clear marks on query responses */
442 if (AVAILABLE(skb) < sizeof(u32) +
443 first*sizeof(struct igmpv3_grec)) {
444 if (truncate && !first)
445 break; /* truncate these */
447 pgr->grec_nsrcs = htons(scount);
449 igmpv3_sendpack(skb);
450 skb = igmpv3_newpack(dev, dev->mtu);
455 skb = add_grhead(skb, pmc, type, &pgr);
458 psrc = (u32 *)skb_put(skb, sizeof(u32));
459 *psrc = psf->sf_inaddr;
461 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
462 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
464 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
466 psf_prev->sf_next = psf->sf_next;
468 *psf_list = psf->sf_next;
476 pgr->grec_nsrcs = htons(scount);
479 pmc->gsquery = 0; /* clear query state on report */
483 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
485 struct sk_buff *skb = 0;
489 read_lock(&in_dev->lock);
490 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
491 if (pmc->multiaddr == IGMP_ALL_HOSTS)
493 spin_lock_bh(&pmc->lock);
494 if (pmc->sfcount[MCAST_EXCLUDE])
495 type = IGMPV3_MODE_IS_EXCLUDE;
497 type = IGMPV3_MODE_IS_INCLUDE;
498 skb = add_grec(skb, pmc, type, 0, 0);
499 spin_unlock_bh(&pmc->lock);
501 read_unlock(&in_dev->lock);
503 spin_lock_bh(&pmc->lock);
504 if (pmc->sfcount[MCAST_EXCLUDE])
505 type = IGMPV3_MODE_IS_EXCLUDE;
507 type = IGMPV3_MODE_IS_INCLUDE;
508 skb = add_grec(skb, pmc, type, 0, 0);
509 spin_unlock_bh(&pmc->lock);
513 return igmpv3_sendpack(skb);
517 * remove zero-count source records from a source filter list
519 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
521 struct ip_sf_list *psf_prev, *psf_next, *psf;
524 for (psf=*ppsf; psf; psf = psf_next) {
525 psf_next = psf->sf_next;
526 if (psf->sf_crcount == 0) {
528 psf_prev->sf_next = psf->sf_next;
530 *ppsf = psf->sf_next;
537 static void igmpv3_send_cr(struct in_device *in_dev)
539 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
540 struct sk_buff *skb = 0;
543 read_lock(&in_dev->lock);
544 write_lock_bh(&in_dev->mc_lock);
548 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
549 pmc_next = pmc->next;
550 if (pmc->sfmode == MCAST_INCLUDE) {
551 type = IGMPV3_BLOCK_OLD_SOURCES;
552 dtype = IGMPV3_BLOCK_OLD_SOURCES;
553 skb = add_grec(skb, pmc, type, 1, 0);
554 skb = add_grec(skb, pmc, dtype, 1, 1);
558 if (pmc->sfmode == MCAST_EXCLUDE) {
559 type = IGMPV3_CHANGE_TO_INCLUDE;
560 skb = add_grec(skb, pmc, type, 1, 0);
562 if (pmc->crcount == 0) {
563 igmpv3_clear_zeros(&pmc->tomb);
564 igmpv3_clear_zeros(&pmc->sources);
567 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
569 pmc_prev->next = pmc_next;
571 in_dev->mc_tomb = pmc_next;
572 in_dev_put(pmc->interface);
577 write_unlock_bh(&in_dev->mc_lock);
580 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
581 spin_lock_bh(&pmc->lock);
582 if (pmc->sfcount[MCAST_EXCLUDE]) {
583 type = IGMPV3_BLOCK_OLD_SOURCES;
584 dtype = IGMPV3_ALLOW_NEW_SOURCES;
586 type = IGMPV3_ALLOW_NEW_SOURCES;
587 dtype = IGMPV3_BLOCK_OLD_SOURCES;
589 skb = add_grec(skb, pmc, type, 0, 0);
590 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
592 /* filter mode changes */
595 if (pmc->sfmode == MCAST_EXCLUDE)
596 type = IGMPV3_CHANGE_TO_EXCLUDE;
598 type = IGMPV3_CHANGE_TO_INCLUDE;
599 skb = add_grec(skb, pmc, type, 0, 0);
601 spin_unlock_bh(&pmc->lock);
603 read_unlock(&in_dev->lock);
606 (void) igmpv3_sendpack(skb);
609 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
616 struct net_device *dev = in_dev->dev;
617 u32 group = pmc ? pmc->multiaddr : 0;
620 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
621 return igmpv3_send_report(in_dev, pmc);
622 else if (type == IGMP_HOST_LEAVE_MESSAGE)
623 dst = IGMP_ALL_ROUTER;
628 struct flowi fl = { .oif = dev->ifindex,
629 .nl_u = { .ip4_u = { .daddr = dst } },
630 .proto = IPPROTO_IGMP };
631 if (ip_route_output_key(&rt, &fl))
634 if (rt->rt_src == 0) {
639 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
645 skb->dst = &rt->u.dst;
647 skb_reserve(skb, LL_RESERVED_SPACE(dev));
649 skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
652 iph->ihl = (sizeof(struct iphdr)+4)>>2;
654 iph->frag_off = htons(IP_DF);
657 iph->saddr = rt->rt_src;
658 iph->protocol = IPPROTO_IGMP;
659 iph->tot_len = htons(IGMP_SIZE);
660 ip_select_ident(iph, &rt->u.dst, NULL);
661 ((u8*)&iph[1])[0] = IPOPT_RA;
662 ((u8*)&iph[1])[1] = 4;
663 ((u8*)&iph[1])[2] = 0;
664 ((u8*)&iph[1])[3] = 0;
667 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
672 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
674 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
678 static void igmp_gq_timer_expire(unsigned long data)
680 struct in_device *in_dev = (struct in_device *)data;
682 in_dev->mr_gq_running = 0;
683 igmpv3_send_report(in_dev, 0);
684 __in_dev_put(in_dev);
687 static void igmp_ifc_timer_expire(unsigned long data)
689 struct in_device *in_dev = (struct in_device *)data;
691 igmpv3_send_cr(in_dev);
692 if (in_dev->mr_ifc_count) {
693 in_dev->mr_ifc_count--;
694 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
696 __in_dev_put(in_dev);
699 static void igmp_ifc_event(struct in_device *in_dev)
701 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
703 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
704 IGMP_Unsolicited_Report_Count;
705 igmp_ifc_start_timer(in_dev, 1);
709 static void igmp_timer_expire(unsigned long data)
711 struct ip_mc_list *im=(struct ip_mc_list *)data;
712 struct in_device *in_dev = im->interface;
714 spin_lock(&im->lock);
717 if (im->unsolicit_count) {
718 im->unsolicit_count--;
719 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
722 spin_unlock(&im->lock);
724 if (IGMP_V1_SEEN(in_dev))
725 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
726 else if (IGMP_V2_SEEN(in_dev))
727 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
729 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
734 static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
736 struct ip_sf_list *psf;
740 for (psf=pmc->sources; psf; psf=psf->sf_next) {
743 for (i=0; i<nsrcs; i++)
744 if (srcs[i] == psf->sf_inaddr) {
752 static void igmp_heard_report(struct in_device *in_dev, u32 group)
754 struct ip_mc_list *im;
756 /* Timers are only set for non-local groups */
758 if (group == IGMP_ALL_HOSTS)
761 read_lock(&in_dev->lock);
762 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
763 if (im->multiaddr == group) {
768 read_unlock(&in_dev->lock);
771 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
774 struct igmphdr *ih = skb->h.igmph;
775 struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
776 struct ip_mc_list *im;
777 u32 group = ih->group;
784 /* Alas, old v1 router presents here. */
786 max_delay = IGMP_Query_Response_Interval;
787 in_dev->mr_v1_seen = jiffies +
788 IGMP_V1_Router_Present_Timeout;
791 /* v2 router present */
792 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
793 in_dev->mr_v2_seen = jiffies +
794 IGMP_V2_Router_Present_Timeout;
796 /* cancel the interface change timer */
797 in_dev->mr_ifc_count = 0;
798 if (del_timer(&in_dev->mr_ifc_timer))
799 __in_dev_put(in_dev);
800 /* clear deleted report items */
801 igmpv3_clear_delrec(in_dev);
802 } else if (len < 12) {
803 return; /* ignore bogus packet; freed by caller */
805 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
808 ih3 = (struct igmpv3_query *) skb->h.raw;
810 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
811 + ntohs(ih3->nsrcs)*sizeof(__u32)))
813 ih3 = (struct igmpv3_query *) skb->h.raw;
816 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
818 max_delay = 1; /* can't mod w/ 0 */
819 in_dev->mr_maxdelay = max_delay;
821 in_dev->mr_qrv = ih3->qrv;
822 if (!group) { /* general query */
824 return; /* no sources allowed */
825 igmp_gq_start_timer(in_dev);
828 /* mark sources to include, if group & source-specific */
829 mark = ih3->nsrcs != 0;
833 * - Start the timers in all of our membership records
834 * that the query applies to for the interface on
835 * which the query arrived excl. those that belong
836 * to a "local" group (224.0.0.X)
837 * - For timers already running check if they need to
839 * - Use the igmp->igmp_code field as the maximum
842 read_lock(&in_dev->lock);
843 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
844 if (group && group != im->multiaddr)
846 if (im->multiaddr == IGMP_ALL_HOSTS)
848 spin_lock_bh(&im->lock);
850 im->gsquery = im->gsquery && mark;
854 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
855 spin_unlock_bh(&im->lock);
856 igmp_mod_timer(im, max_delay);
858 read_unlock(&in_dev->lock);
861 int igmp_rcv(struct sk_buff *skb)
863 /* This basically follows the spec line by line -- see RFC1112 */
865 struct in_device *in_dev = in_dev_get(skb->dev);
873 if (!pskb_may_pull(skb, sizeof(struct igmphdr)) ||
874 (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
882 case IGMP_HOST_MEMBERSHIP_QUERY:
883 igmp_heard_query(in_dev, skb, len);
885 case IGMP_HOST_MEMBERSHIP_REPORT:
886 case IGMPV2_HOST_MEMBERSHIP_REPORT:
887 case IGMPV3_HOST_MEMBERSHIP_REPORT:
888 /* Is it our report looped back? */
889 if (((struct rtable*)skb->dst)->fl.iif == 0)
891 igmp_heard_report(in_dev, ih->group);
894 #ifdef CONFIG_IP_PIMSM_V1
896 return pim_rcv_v1(skb);
900 case IGMP_HOST_LEAVE_MESSAGE:
902 case IGMP_MTRACE_RESP:
905 NETDEBUG(printk(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type));
916 * Add a filter to a device
919 static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
921 char buf[MAX_ADDR_LEN];
922 struct net_device *dev = in_dev->dev;
924 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
925 We will get multicast token leakage, when IFF_MULTICAST
926 is changed. This check should be done in dev->set_multicast_list
927 routine. Something sort of:
928 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
931 if (arp_mc_map(addr, buf, dev, 0) == 0)
932 dev_mc_add(dev,buf,dev->addr_len,0);
936 * Remove a filter from a device
939 static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
941 char buf[MAX_ADDR_LEN];
942 struct net_device *dev = in_dev->dev;
944 if (arp_mc_map(addr, buf, dev, 0) == 0)
945 dev_mc_delete(dev,buf,dev->addr_len,0);
948 #ifdef CONFIG_IP_MULTICAST
950 * deleted ip_mc_list manipulation
952 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
954 struct ip_mc_list *pmc;
956 /* this is an "ip_mc_list" for convenience; only the fields below
957 * are actually used. In particular, the refcnt and users are not
958 * used for management of the delete list. Using the same structure
959 * for deleted items allows change reports to use common code with
960 * non-deleted or query-response MCA's.
962 pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
965 memset(pmc, 0, sizeof(*pmc));
966 spin_lock_bh(&im->lock);
967 pmc->interface = im->interface;
969 pmc->multiaddr = im->multiaddr;
970 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
971 IGMP_Unsolicited_Report_Count;
972 pmc->sfmode = im->sfmode;
973 if (pmc->sfmode == MCAST_INCLUDE) {
974 struct ip_sf_list *psf;
976 pmc->tomb = im->tomb;
977 pmc->sources = im->sources;
978 im->tomb = im->sources = 0;
979 for (psf=pmc->sources; psf; psf=psf->sf_next)
980 psf->sf_crcount = pmc->crcount;
982 spin_unlock_bh(&im->lock);
984 write_lock_bh(&in_dev->mc_lock);
985 pmc->next = in_dev->mc_tomb;
986 in_dev->mc_tomb = pmc;
987 write_unlock_bh(&in_dev->mc_lock);
990 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
992 struct ip_mc_list *pmc, *pmc_prev;
993 struct ip_sf_list *psf, *psf_next;
995 write_lock_bh(&in_dev->mc_lock);
997 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
998 if (pmc->multiaddr == multiaddr)
1004 pmc_prev->next = pmc->next;
1006 in_dev->mc_tomb = pmc->next;
1008 write_unlock_bh(&in_dev->mc_lock);
1010 for (psf=pmc->tomb; psf; psf=psf_next) {
1011 psf_next = psf->sf_next;
1014 in_dev_put(pmc->interface);
1019 static void igmpv3_clear_delrec(struct in_device *in_dev)
1021 struct ip_mc_list *pmc, *nextpmc;
1023 write_lock_bh(&in_dev->mc_lock);
1024 pmc = in_dev->mc_tomb;
1025 in_dev->mc_tomb = 0;
1026 write_unlock_bh(&in_dev->mc_lock);
1028 for (; pmc; pmc = nextpmc) {
1029 nextpmc = pmc->next;
1030 ip_mc_clear_src(pmc);
1031 in_dev_put(pmc->interface);
1034 /* clear dead sources, too */
1035 read_lock(&in_dev->lock);
1036 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1037 struct ip_sf_list *psf, *psf_next;
1039 spin_lock_bh(&pmc->lock);
1042 spin_unlock_bh(&pmc->lock);
1043 for (; psf; psf=psf_next) {
1044 psf_next = psf->sf_next;
1048 read_unlock(&in_dev->lock);
1052 static void igmp_group_dropped(struct ip_mc_list *im)
1054 struct in_device *in_dev = im->interface;
1055 #ifdef CONFIG_IP_MULTICAST
1061 ip_mc_filter_del(in_dev, im->multiaddr);
1064 #ifdef CONFIG_IP_MULTICAST
1065 if (im->multiaddr == IGMP_ALL_HOSTS)
1068 reporter = im->reporter;
1069 igmp_stop_timer(im);
1071 if (!in_dev->dead) {
1072 if (IGMP_V1_SEEN(in_dev))
1074 if (IGMP_V2_SEEN(in_dev)) {
1076 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1080 igmpv3_add_delrec(in_dev, im);
1082 igmp_ifc_event(in_dev);
1086 ip_mc_clear_src(im);
1089 static void igmp_group_added(struct ip_mc_list *im)
1091 struct in_device *in_dev = im->interface;
1093 if (im->loaded == 0) {
1095 ip_mc_filter_add(in_dev, im->multiaddr);
1098 #ifdef CONFIG_IP_MULTICAST
1099 if (im->multiaddr == IGMP_ALL_HOSTS)
1104 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1105 spin_lock_bh(&im->lock);
1106 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1107 spin_unlock_bh(&im->lock);
1112 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1113 IGMP_Unsolicited_Report_Count;
1114 igmp_ifc_event(in_dev);
1120 * Multicast list managers
1125 * A socket has joined a multicast group on device dev.
1128 void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1130 struct ip_mc_list *im;
1134 for (im=in_dev->mc_list; im; im=im->next) {
1135 if (im->multiaddr == addr) {
1137 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, 0, 0);
1142 im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1147 im->interface=in_dev;
1148 in_dev_hold(in_dev);
1150 /* initial mode is (EX, empty) */
1151 im->sfmode = MCAST_EXCLUDE;
1152 im->sfcount[MCAST_INCLUDE] = 0;
1153 im->sfcount[MCAST_EXCLUDE] = 1;
1157 atomic_set(&im->refcnt, 1);
1158 spin_lock_init(&im->lock);
1159 #ifdef CONFIG_IP_MULTICAST
1161 init_timer(&im->timer);
1162 im->timer.data=(unsigned long)im;
1163 im->timer.function=&igmp_timer_expire;
1164 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1169 write_lock_bh(&in_dev->lock);
1170 im->next=in_dev->mc_list;
1172 write_unlock_bh(&in_dev->lock);
1173 #ifdef CONFIG_IP_MULTICAST
1174 igmpv3_del_delrec(in_dev, im->multiaddr);
1176 igmp_group_added(im);
1178 ip_rt_multicast_event(in_dev);
1184 * A socket has left a multicast group on device dev
1187 void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1189 struct ip_mc_list *i, **ip;
1193 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1194 if (i->multiaddr==addr) {
1195 if (--i->users == 0) {
1196 write_lock_bh(&in_dev->lock);
1198 write_unlock_bh(&in_dev->lock);
1199 igmp_group_dropped(i);
1202 ip_rt_multicast_event(in_dev);
1212 /* Device going down */
1214 void ip_mc_down(struct in_device *in_dev)
1216 struct ip_mc_list *i;
1220 for (i=in_dev->mc_list; i; i=i->next)
1221 igmp_group_dropped(i);
1223 #ifdef CONFIG_IP_MULTICAST
1224 in_dev->mr_ifc_count = 0;
1225 if (del_timer(&in_dev->mr_ifc_timer))
1226 __in_dev_put(in_dev);
1227 in_dev->mr_gq_running = 0;
1228 if (del_timer(&in_dev->mr_gq_timer))
1229 __in_dev_put(in_dev);
1230 igmpv3_clear_delrec(in_dev);
1233 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1236 void ip_mc_init_dev(struct in_device *in_dev)
1240 in_dev->mc_tomb = 0;
1241 #ifdef CONFIG_IP_MULTICAST
1242 in_dev->mr_gq_running = 0;
1243 init_timer(&in_dev->mr_gq_timer);
1244 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1245 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1246 in_dev->mr_ifc_count = 0;
1247 init_timer(&in_dev->mr_ifc_timer);
1248 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1249 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1250 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1253 in_dev->mc_lock = RW_LOCK_UNLOCKED;
1256 /* Device going up */
1258 void ip_mc_up(struct in_device *in_dev)
1260 struct ip_mc_list *i;
1264 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1266 for (i=in_dev->mc_list; i; i=i->next)
1267 igmp_group_added(i);
1271 * Device is about to be destroyed: clean up.
1274 void ip_mc_destroy_dev(struct in_device *in_dev)
1276 struct ip_mc_list *i;
1280 /* Deactivate timers */
1283 write_lock_bh(&in_dev->lock);
1284 while ((i = in_dev->mc_list) != NULL) {
1285 in_dev->mc_list = i->next;
1286 write_unlock_bh(&in_dev->lock);
1288 igmp_group_dropped(i);
1291 write_lock_bh(&in_dev->lock);
1293 write_unlock_bh(&in_dev->lock);
1296 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1298 struct flowi fl = { .nl_u = { .ip4_u =
1299 { .daddr = imr->imr_multiaddr.s_addr } } };
1301 struct net_device *dev = NULL;
1302 struct in_device *idev = NULL;
1304 if (imr->imr_ifindex) {
1305 idev = inetdev_by_index(imr->imr_ifindex);
1310 if (imr->imr_address.s_addr) {
1311 dev = ip_dev_find(imr->imr_address.s_addr);
1317 if (!dev && !ip_route_output_key(&rt, &fl)) {
1318 dev = rt->u.dst.dev;
1322 imr->imr_ifindex = dev->ifindex;
1323 idev = __in_dev_get(dev);
1329 * Join a socket to a group
1331 int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1332 int sysctl_igmp_max_msf = IP_MAX_MSF;
1335 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1338 struct ip_sf_list *psf, *psf_prev;
1342 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1343 if (psf->sf_inaddr == *psfsrc)
1347 if (!psf || psf->sf_count[sfmode] == 0) {
1348 /* source filter not found, or count wrong => bug */
1351 psf->sf_count[sfmode]--;
1352 if (psf->sf_count[sfmode] == 0) {
1353 ip_rt_multicast_event(pmc->interface);
1355 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1356 #ifdef CONFIG_IP_MULTICAST
1357 struct in_device *in_dev = pmc->interface;
1360 /* no more filters for this source */
1362 psf_prev->sf_next = psf->sf_next;
1364 pmc->sources = psf->sf_next;
1365 #ifdef CONFIG_IP_MULTICAST
1366 if (psf->sf_oldin &&
1367 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1368 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1369 IGMP_Unsolicited_Report_Count;
1370 psf->sf_next = pmc->tomb;
1380 #ifndef CONFIG_IP_MULTICAST
1381 #define igmp_ifc_event(x) do { } while (0)
1384 int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1385 int sfcount, __u32 *psfsrc, int delta)
1387 struct ip_mc_list *pmc;
1393 read_lock(&in_dev->lock);
1394 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1395 if (*pmca == pmc->multiaddr)
1399 /* MCA not found?? bug */
1400 read_unlock(&in_dev->lock);
1403 spin_lock_bh(&pmc->lock);
1404 read_unlock(&in_dev->lock);
1405 #ifdef CONFIG_IP_MULTICAST
1410 if (!pmc->sfcount[sfmode])
1412 pmc->sfcount[sfmode]--;
1415 for (i=0; i<sfcount; i++) {
1416 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1418 changerec |= rv > 0;
1422 if (pmc->sfmode == MCAST_EXCLUDE &&
1423 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1424 pmc->sfcount[MCAST_INCLUDE]) {
1425 #ifdef CONFIG_IP_MULTICAST
1426 struct ip_sf_list *psf;
1429 /* filter mode change */
1430 pmc->sfmode = MCAST_INCLUDE;
1431 #ifdef CONFIG_IP_MULTICAST
1432 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1433 IGMP_Unsolicited_Report_Count;
1434 in_dev->mr_ifc_count = pmc->crcount;
1435 for (psf=pmc->sources; psf; psf = psf->sf_next)
1436 psf->sf_crcount = 0;
1437 igmp_ifc_event(pmc->interface);
1438 } else if (sf_setstate(pmc) || changerec) {
1439 igmp_ifc_event(pmc->interface);
1443 spin_unlock_bh(&pmc->lock);
1448 * Add multicast single-source filter to the interface list
1450 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1451 __u32 *psfsrc, int delta)
1453 struct ip_sf_list *psf, *psf_prev;
1456 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1457 if (psf->sf_inaddr == *psfsrc)
1462 psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1465 memset(psf, 0, sizeof(*psf));
1466 psf->sf_inaddr = *psfsrc;
1468 psf_prev->sf_next = psf;
1472 psf->sf_count[sfmode]++;
1473 if (psf->sf_count[sfmode] == 1) {
1474 ip_rt_multicast_event(pmc->interface);
1479 #ifdef CONFIG_IP_MULTICAST
1480 static void sf_markstate(struct ip_mc_list *pmc)
1482 struct ip_sf_list *psf;
1483 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1485 for (psf=pmc->sources; psf; psf=psf->sf_next)
1486 if (pmc->sfcount[MCAST_EXCLUDE]) {
1487 psf->sf_oldin = mca_xcount ==
1488 psf->sf_count[MCAST_EXCLUDE] &&
1489 !psf->sf_count[MCAST_INCLUDE];
1491 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1494 static int sf_setstate(struct ip_mc_list *pmc)
1496 struct ip_sf_list *psf;
1497 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1498 int qrv = pmc->interface->mr_qrv;
1502 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1503 if (pmc->sfcount[MCAST_EXCLUDE]) {
1504 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1505 !psf->sf_count[MCAST_INCLUDE];
1507 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1508 if (new_in != psf->sf_oldin) {
1509 psf->sf_crcount = qrv;
1518 * Add multicast source filter list to the interface list
1520 int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1521 int sfcount, __u32 *psfsrc, int delta)
1523 struct ip_mc_list *pmc;
1529 read_lock(&in_dev->lock);
1530 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1531 if (*pmca == pmc->multiaddr)
1535 /* MCA not found?? bug */
1536 read_unlock(&in_dev->lock);
1539 spin_lock_bh(&pmc->lock);
1540 read_unlock(&in_dev->lock);
1542 #ifdef CONFIG_IP_MULTICAST
1545 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1547 pmc->sfcount[sfmode]++;
1549 for (i=0; i<sfcount; i++) {
1550 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1557 pmc->sfcount[sfmode]--;
1559 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1560 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1561 #ifdef CONFIG_IP_MULTICAST
1562 struct in_device *in_dev = pmc->interface;
1563 struct ip_sf_list *psf;
1566 /* filter mode change */
1567 if (pmc->sfcount[MCAST_EXCLUDE])
1568 pmc->sfmode = MCAST_EXCLUDE;
1569 else if (pmc->sfcount[MCAST_INCLUDE])
1570 pmc->sfmode = MCAST_INCLUDE;
1571 #ifdef CONFIG_IP_MULTICAST
1572 /* else no filters; keep old mode for reports */
1574 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1575 IGMP_Unsolicited_Report_Count;
1576 in_dev->mr_ifc_count = pmc->crcount;
1577 for (psf=pmc->sources; psf; psf = psf->sf_next)
1578 psf->sf_crcount = 0;
1579 igmp_ifc_event(in_dev);
1580 } else if (sf_setstate(pmc)) {
1581 igmp_ifc_event(in_dev);
1584 spin_unlock_bh(&pmc->lock);
1588 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1590 struct ip_sf_list *psf, *nextpsf;
1592 for (psf=pmc->tomb; psf; psf=nextpsf) {
1593 nextpsf = psf->sf_next;
1597 for (psf=pmc->sources; psf; psf=nextpsf) {
1598 nextpsf = psf->sf_next;
1602 pmc->sfmode = MCAST_EXCLUDE;
1603 pmc->sfcount[MCAST_EXCLUDE] = 0;
1604 pmc->sfcount[MCAST_EXCLUDE] = 1;
1609 * Join a multicast group
1611 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1614 u32 addr = imr->imr_multiaddr.s_addr;
1615 struct ip_mc_socklist *iml, *i;
1616 struct in_device *in_dev;
1617 struct inet_opt *inet = inet_sk(sk);
1620 if (!MULTICAST(addr))
1625 in_dev = ip_mc_find_dev(imr);
1633 iml = (struct ip_mc_socklist *)sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1636 for (i = inet->mc_list; i; i = i->next) {
1637 if (memcmp(&i->multi, imr, sizeof(*imr)) == 0) {
1638 /* New style additions are reference counted */
1639 if (imr->imr_address.s_addr == 0) {
1648 if (iml == NULL || count >= sysctl_igmp_max_memberships)
1650 memcpy(&iml->multi, imr, sizeof(*imr));
1651 iml->next = inet->mc_list;
1654 iml->sfmode = MCAST_EXCLUDE;
1655 inet->mc_list = iml;
1656 ip_mc_inc_group(in_dev, addr);
1663 sock_kfree_s(sk, iml, sizeof(*iml));
1667 int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1668 struct in_device *in_dev)
1672 if (iml->sflist == 0) {
1673 /* any-source empty exclude case */
1674 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1675 iml->sfmode, 0, 0, 0);
1677 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1678 iml->sfmode, iml->sflist->sl_count,
1679 iml->sflist->sl_addr, 0);
1680 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1686 * Ask a socket to leave a group.
1689 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1691 struct inet_opt *inet = inet_sk(sk);
1692 struct ip_mc_socklist *iml, **imlp;
1695 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1696 if (iml->multi.imr_multiaddr.s_addr==imr->imr_multiaddr.s_addr &&
1697 iml->multi.imr_address.s_addr==imr->imr_address.s_addr &&
1698 (!imr->imr_ifindex || iml->multi.imr_ifindex==imr->imr_ifindex)) {
1699 struct in_device *in_dev;
1701 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
1703 (void) ip_mc_leave_src(sk, iml, in_dev);
1714 ip_mc_dec_group(in_dev, imr->imr_multiaddr.s_addr);
1718 sock_kfree_s(sk, iml, sizeof(*iml));
1723 return -EADDRNOTAVAIL;
1726 int ip_mc_source(int add, int omode, struct sock *sk, struct
1727 ip_mreq_source *mreqs, int ifindex)
1730 struct ip_mreqn imr;
1731 u32 addr = mreqs->imr_multiaddr;
1732 struct ip_mc_socklist *pmc;
1733 struct in_device *in_dev = 0;
1734 struct inet_opt *inet = inet_sk(sk);
1735 struct ip_sf_socklist *psl;
1738 if (!MULTICAST(addr))
1743 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1744 imr.imr_address.s_addr = mreqs->imr_interface;
1745 imr.imr_ifindex = ifindex;
1746 in_dev = ip_mc_find_dev(&imr);
1752 err = -EADDRNOTAVAIL;
1754 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1755 if (memcmp(&pmc->multi, mreqs, 2*sizeof(__u32)) == 0)
1758 if (!pmc) /* must have a prior join */
1760 /* if a source filter was set, must be the same mode as before */
1762 if (pmc->sfmode != omode)
1764 } else if (pmc->sfmode != omode) {
1765 /* allow mode switches for empty-set filters */
1766 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, 0, 0);
1767 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1769 pmc->sfmode = omode;
1777 for (i=0; i<psl->sl_count; i++) {
1778 rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1783 if (!rv) /* source not found */
1786 /* update the interface filter */
1787 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1788 &mreqs->imr_sourceaddr, 1);
1790 for (j=i+1; j<psl->sl_count; j++)
1791 psl->sl_addr[j-1] = psl->sl_addr[j];
1796 /* else, add a new source to the filter */
1798 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1802 if (!psl || psl->sl_count == psl->sl_max) {
1803 struct ip_sf_socklist *newpsl;
1804 int count = IP_SFBLOCK;
1807 count += psl->sl_max;
1808 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1809 IP_SFLSIZE(count), GFP_KERNEL);
1814 newpsl->sl_max = count;
1815 newpsl->sl_count = count - IP_SFBLOCK;
1817 for (i=0; i<psl->sl_count; i++)
1818 newpsl->sl_addr[i] = psl->sl_addr[i];
1819 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1821 pmc->sflist = psl = newpsl;
1823 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1824 for (i=0; i<psl->sl_count; i++) {
1825 rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1830 if (rv == 0) /* address already there is an error */
1832 for (j=psl->sl_count-1; j>=i; j--)
1833 psl->sl_addr[j+1] = psl->sl_addr[j];
1834 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1837 /* update the interface list */
1838 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1839 &mreqs->imr_sourceaddr, 1);
1845 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1848 struct ip_mreqn imr;
1849 u32 addr = msf->imsf_multiaddr;
1850 struct ip_mc_socklist *pmc;
1851 struct in_device *in_dev;
1852 struct inet_opt *inet = inet_sk(sk);
1853 struct ip_sf_socklist *newpsl, *psl;
1855 if (!MULTICAST(addr))
1857 if (msf->imsf_fmode != MCAST_INCLUDE &&
1858 msf->imsf_fmode != MCAST_EXCLUDE)
1863 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1864 imr.imr_address.s_addr = msf->imsf_interface;
1865 imr.imr_ifindex = ifindex;
1866 in_dev = ip_mc_find_dev(&imr);
1872 err = -EADDRNOTAVAIL;
1874 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1875 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1876 pmc->multi.imr_ifindex == imr.imr_ifindex)
1879 if (!pmc) /* must have a prior join */
1881 if (msf->imsf_numsrc) {
1882 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1883 IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1888 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1889 memcpy(newpsl->sl_addr, msf->imsf_slist,
1890 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1891 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1892 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1894 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1901 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1902 psl->sl_count, psl->sl_addr, 0);
1903 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1905 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1907 pmc->sflist = newpsl;
1908 pmc->sfmode = msf->imsf_fmode;
1914 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1915 struct ip_msfilter *optval, int *optlen)
1917 int err, len, count, copycount;
1918 struct ip_mreqn imr;
1919 u32 addr = msf->imsf_multiaddr;
1920 struct ip_mc_socklist *pmc;
1921 struct in_device *in_dev;
1922 struct inet_opt *inet = inet_sk(sk);
1923 struct ip_sf_socklist *psl;
1925 if (!MULTICAST(addr))
1930 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1931 imr.imr_address.s_addr = msf->imsf_interface;
1932 imr.imr_ifindex = 0;
1933 in_dev = ip_mc_find_dev(&imr);
1939 err = -EADDRNOTAVAIL;
1941 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1942 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1943 pmc->multi.imr_ifindex == imr.imr_ifindex)
1946 if (!pmc) /* must have a prior join */
1948 msf->imsf_fmode = pmc->sfmode;
1955 count = psl->sl_count;
1957 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1958 len = copycount * sizeof(psl->sl_addr[0]);
1959 msf->imsf_numsrc = count;
1960 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1961 copy_to_user((void *)optval, msf, IP_MSFILTER_SIZE(0))) {
1965 copy_to_user((void *)&optval->imsf_slist[0], psl->sl_addr, len))
1973 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
1974 struct group_filter *optval, int *optlen)
1976 int err, i, count, copycount;
1977 struct sockaddr_in *psin;
1979 struct ip_mc_socklist *pmc;
1980 struct inet_opt *inet = inet_sk(sk);
1981 struct ip_sf_socklist *psl;
1983 psin = (struct sockaddr_in *)&gsf->gf_group;
1984 if (psin->sin_family != AF_INET)
1986 addr = psin->sin_addr.s_addr;
1987 if (!MULTICAST(addr))
1992 err = -EADDRNOTAVAIL;
1994 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1995 if (pmc->multi.imr_multiaddr.s_addr == addr &&
1996 pmc->multi.imr_ifindex == gsf->gf_interface)
1999 if (!pmc) /* must have a prior join */
2001 gsf->gf_fmode = pmc->sfmode;
2004 count = psl ? psl->sl_count : 0;
2005 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2006 gsf->gf_numsrc = count;
2007 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2008 copy_to_user((void *)optval, gsf, GROUP_FILTER_SIZE(0))) {
2011 for (i=0; i<copycount; i++) {
2012 struct sockaddr_in *psin;
2013 struct sockaddr_storage ss;
2015 psin = (struct sockaddr_in *)&ss;
2016 memset(&ss, 0, sizeof(ss));
2017 psin->sin_family = AF_INET;
2018 psin->sin_addr.s_addr = psl->sl_addr[i];
2019 if (copy_to_user((void *)&optval->gf_slist[i], &ss, sizeof(ss)))
2029 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2031 int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2033 struct inet_opt *inet = inet_sk(sk);
2034 struct ip_mc_socklist *pmc;
2035 struct ip_sf_socklist *psl;
2038 if (!MULTICAST(loc_addr))
2041 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2042 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2043 pmc->multi.imr_ifindex == dif)
2050 return pmc->sfmode == MCAST_EXCLUDE;
2052 for (i=0; i<psl->sl_count; i++) {
2053 if (psl->sl_addr[i] == rmt_addr)
2056 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2058 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2064 * A socket is closing.
2067 void ip_mc_drop_socket(struct sock *sk)
2069 struct inet_opt *inet = inet_sk(sk);
2070 struct ip_mc_socklist *iml;
2072 if (inet->mc_list == NULL)
2076 while ((iml = inet->mc_list) != NULL) {
2077 struct in_device *in_dev;
2078 inet->mc_list = iml->next;
2080 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2081 (void) ip_mc_leave_src(sk, iml, in_dev);
2082 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2085 sock_kfree_s(sk, iml, sizeof(*iml));
2091 int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2093 struct ip_mc_list *im;
2094 struct ip_sf_list *psf;
2097 read_lock(&in_dev->lock);
2098 for (im=in_dev->mc_list; im; im=im->next) {
2099 if (im->multiaddr == mc_addr)
2102 if (im && proto == IPPROTO_IGMP) {
2106 for (psf=im->sources; psf; psf=psf->sf_next) {
2107 if (psf->sf_inaddr == src_addr)
2111 rv = psf->sf_count[MCAST_INCLUDE] ||
2112 psf->sf_count[MCAST_EXCLUDE] !=
2113 im->sfcount[MCAST_EXCLUDE];
2115 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2117 rv = 1; /* unspecified source; tentatively allow */
2119 read_unlock(&in_dev->lock);
2123 #if defined(CONFIG_PROC_FS)
2124 struct igmp_mc_iter_state {
2125 struct net_device *dev;
2126 struct in_device *in_dev;
2129 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2131 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2133 struct ip_mc_list *im = NULL;
2134 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2136 for (state->dev = dev_base, state->in_dev = NULL;
2138 state->dev = state->dev->next) {
2139 struct in_device *in_dev;
2140 in_dev = in_dev_get(state->dev);
2143 read_lock(&in_dev->lock);
2144 im = in_dev->mc_list;
2146 state->in_dev = in_dev;
2149 read_unlock(&in_dev->lock);
2155 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2157 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2160 if (likely(state->in_dev != NULL)) {
2161 read_unlock(&state->in_dev->lock);
2162 in_dev_put(state->in_dev);
2164 state->dev = state->dev->next;
2166 state->in_dev = NULL;
2169 state->in_dev = in_dev_get(state->dev);
2172 read_lock(&state->in_dev->lock);
2173 im = state->in_dev->mc_list;
2178 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2180 struct ip_mc_list *im = igmp_mc_get_first(seq);
2182 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2184 return pos ? NULL : im;
2187 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2189 read_lock(&dev_base_lock);
2190 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2193 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2195 struct ip_mc_list *im;
2196 if (v == SEQ_START_TOKEN)
2197 im = igmp_mc_get_first(seq);
2199 im = igmp_mc_get_next(seq, v);
2204 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2206 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2207 if (likely(state->in_dev != NULL)) {
2208 read_unlock(&state->in_dev->lock);
2209 in_dev_put(state->in_dev);
2210 state->in_dev = NULL;
2213 read_unlock(&dev_base_lock);
2216 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2218 if (v == SEQ_START_TOKEN)
2220 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2222 struct ip_mc_list *im = (struct ip_mc_list *)v;
2223 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2225 #ifdef CONFIG_IP_MULTICAST
2226 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2227 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2233 if (state->in_dev->mc_list == im) {
2234 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2235 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2239 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2240 im->multiaddr, im->users,
2241 im->tm_running, im->tm_running ?
2242 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2248 static struct seq_operations igmp_mc_seq_ops = {
2249 .start = igmp_mc_seq_start,
2250 .next = igmp_mc_seq_next,
2251 .stop = igmp_mc_seq_stop,
2252 .show = igmp_mc_seq_show,
2255 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2257 struct seq_file *seq;
2259 struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2263 rc = seq_open(file, &igmp_mc_seq_ops);
2267 seq = file->private_data;
2269 memset(s, 0, sizeof(*s));
2277 static struct file_operations igmp_mc_seq_fops = {
2278 .owner = THIS_MODULE,
2279 .open = igmp_mc_seq_open,
2281 .llseek = seq_lseek,
2282 .release = seq_release_private,
2285 struct igmp_mcf_iter_state {
2286 struct net_device *dev;
2287 struct in_device *idev;
2288 struct ip_mc_list *im;
2291 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2293 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2295 struct ip_sf_list *psf = NULL;
2296 struct ip_mc_list *im = NULL;
2297 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2299 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2301 state->dev = state->dev->next) {
2302 struct in_device *idev;
2303 idev = in_dev_get(state->dev);
2304 if (unlikely(idev == NULL))
2306 read_lock_bh(&idev->lock);
2308 if (likely(im != NULL)) {
2309 spin_lock_bh(&im->lock);
2311 if (likely(psf != NULL)) {
2316 spin_unlock_bh(&im->lock);
2318 read_unlock_bh(&idev->lock);
2324 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2326 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2330 spin_unlock_bh(&state->im->lock);
2331 state->im = state->im->next;
2332 while (!state->im) {
2333 if (likely(state->idev != NULL)) {
2334 read_unlock_bh(&state->idev->lock);
2335 in_dev_put(state->idev);
2337 state->dev = state->dev->next;
2342 state->idev = in_dev_get(state->dev);
2345 read_lock_bh(&state->idev->lock);
2346 state->im = state->idev->mc_list;
2350 spin_lock_bh(&state->im->lock);
2351 psf = state->im->sources;
2357 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2359 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2361 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2363 return pos ? NULL : psf;
2366 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2368 read_lock(&dev_base_lock);
2369 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2372 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2374 struct ip_sf_list *psf;
2375 if (v == SEQ_START_TOKEN)
2376 psf = igmp_mcf_get_first(seq);
2378 psf = igmp_mcf_get_next(seq, v);
2383 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2385 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2386 if (likely(state->im != NULL)) {
2387 spin_unlock_bh(&state->im->lock);
2390 if (likely(state->idev != NULL)) {
2391 read_unlock_bh(&state->idev->lock);
2392 in_dev_put(state->idev);
2396 read_unlock(&dev_base_lock);
2399 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2401 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2402 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2404 if (v == SEQ_START_TOKEN) {
2407 "%10s %10s %6s %6s\n", "Idx",
2409 "SRC", "INC", "EXC");
2413 "0x%08x %6lu %6lu\n",
2414 state->dev->ifindex, state->dev->name,
2415 ntohl(state->im->multiaddr),
2416 ntohl(psf->sf_inaddr),
2417 psf->sf_count[MCAST_INCLUDE],
2418 psf->sf_count[MCAST_EXCLUDE]);
2423 static struct seq_operations igmp_mcf_seq_ops = {
2424 .start = igmp_mcf_seq_start,
2425 .next = igmp_mcf_seq_next,
2426 .stop = igmp_mcf_seq_stop,
2427 .show = igmp_mcf_seq_show,
2430 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2432 struct seq_file *seq;
2434 struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2438 rc = seq_open(file, &igmp_mcf_seq_ops);
2442 seq = file->private_data;
2444 memset(s, 0, sizeof(*s));
2452 static struct file_operations igmp_mcf_seq_fops = {
2453 .owner = THIS_MODULE,
2454 .open = igmp_mcf_seq_open,
2456 .llseek = seq_lseek,
2457 .release = seq_release_private,
2460 int __init igmp_mc_proc_init(void)
2462 proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2463 proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2468 EXPORT_SYMBOL(ip_mc_dec_group);
2469 EXPORT_SYMBOL(ip_mc_inc_group);
2470 EXPORT_SYMBOL(ip_mc_join_group);