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 static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
147 int sfcount, __u32 *psfsrc, int delta);
149 static void ip_ma_put(struct ip_mc_list *im)
151 if (atomic_dec_and_test(&im->refcnt)) {
152 in_dev_put(im->interface);
157 #ifdef CONFIG_IP_MULTICAST
163 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
165 spin_lock_bh(&im->lock);
166 if (del_timer(&im->timer))
167 atomic_dec(&im->refcnt);
170 im->unsolicit_count = 0;
171 spin_unlock_bh(&im->lock);
174 /* It must be called with locked im->lock */
175 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
177 int tv=net_random() % max_delay;
180 if (!mod_timer(&im->timer, jiffies+tv+2))
181 atomic_inc(&im->refcnt);
184 static void igmp_gq_start_timer(struct in_device *in_dev)
186 int tv = net_random() % in_dev->mr_maxdelay;
188 in_dev->mr_gq_running = 1;
189 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
193 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
195 int tv = net_random() % delay;
197 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
201 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
203 spin_lock_bh(&im->lock);
204 im->unsolicit_count = 0;
205 if (del_timer(&im->timer)) {
206 if ((long)(im->timer.expires-jiffies) < max_delay) {
207 add_timer(&im->timer);
209 spin_unlock_bh(&im->lock);
212 atomic_dec(&im->refcnt);
214 igmp_start_timer(im, max_delay);
215 spin_unlock_bh(&im->lock);
220 * Send an IGMP report.
223 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
226 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
227 int gdeleted, int sdeleted)
230 case IGMPV3_MODE_IS_INCLUDE:
231 case IGMPV3_MODE_IS_EXCLUDE:
232 if (gdeleted || sdeleted)
234 return !(pmc->gsquery && !psf->sf_gsresp);
235 case IGMPV3_CHANGE_TO_INCLUDE:
236 if (gdeleted || sdeleted)
238 return psf->sf_count[MCAST_INCLUDE] != 0;
239 case IGMPV3_CHANGE_TO_EXCLUDE:
240 if (gdeleted || sdeleted)
242 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
243 psf->sf_count[MCAST_INCLUDE])
245 return pmc->sfcount[MCAST_EXCLUDE] ==
246 psf->sf_count[MCAST_EXCLUDE];
247 case IGMPV3_ALLOW_NEW_SOURCES:
248 if (gdeleted || !psf->sf_crcount)
250 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
251 case IGMPV3_BLOCK_OLD_SOURCES:
252 if (pmc->sfmode == MCAST_INCLUDE)
253 return gdeleted || (psf->sf_crcount && sdeleted);
254 return psf->sf_crcount && !gdeleted && !sdeleted;
260 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
262 struct ip_sf_list *psf;
265 for (psf=pmc->sources; psf; psf=psf->sf_next) {
266 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
273 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
278 struct igmpv3_report *pig;
280 skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
285 struct flowi fl = { .oif = dev->ifindex,
287 .daddr = IGMPV3_ALL_MCR } },
288 .proto = IPPROTO_IGMP };
289 if (ip_route_output_key(&rt, &fl)) {
294 if (rt->rt_src == 0) {
300 skb->dst = &rt->u.dst;
303 skb_reserve(skb, LL_RESERVED_SPACE(dev));
305 skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
308 pip->ihl = (sizeof(struct iphdr)+4)>>2;
310 pip->frag_off = htons(IP_DF);
312 pip->daddr = rt->rt_dst;
313 pip->saddr = rt->rt_src;
314 pip->protocol = IPPROTO_IGMP;
315 pip->tot_len = 0; /* filled in later */
316 ip_select_ident(pip, &rt->u.dst, NULL);
317 ((u8*)&pip[1])[0] = IPOPT_RA;
318 ((u8*)&pip[1])[1] = 4;
319 ((u8*)&pip[1])[2] = 0;
320 ((u8*)&pip[1])[3] = 0;
322 pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
323 skb->h.igmph = (struct igmphdr *)pig;
324 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
332 static int igmpv3_sendpack(struct sk_buff *skb)
334 struct iphdr *pip = skb->nh.iph;
335 struct igmphdr *pig = skb->h.igmph;
338 iplen = skb->tail - (unsigned char *)skb->nh.iph;
339 pip->tot_len = htons(iplen);
342 igmplen = skb->tail - (unsigned char *)skb->h.igmph;
343 pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
345 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
349 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
351 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
354 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
355 int type, struct igmpv3_grec **ppgr)
357 struct net_device *dev = pmc->interface->dev;
358 struct igmpv3_report *pih;
359 struct igmpv3_grec *pgr;
362 skb = igmpv3_newpack(dev, dev->mtu);
365 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
366 pgr->grec_type = type;
367 pgr->grec_auxwords = 0;
369 pgr->grec_mca = pmc->multiaddr;
370 pih = (struct igmpv3_report *)skb->h.igmph;
371 pih->ngrec = htons(ntohs(pih->ngrec)+1);
376 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
377 skb_tailroom(skb)) : 0)
379 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
380 int type, int gdeleted, int sdeleted)
382 struct net_device *dev = pmc->interface->dev;
383 struct igmpv3_report *pih;
384 struct igmpv3_grec *pgr = NULL;
385 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
386 int scount, first, isquery, truncate;
388 if (pmc->multiaddr == IGMP_ALL_HOSTS)
391 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
392 type == IGMPV3_MODE_IS_EXCLUDE;
393 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
394 type == IGMPV3_CHANGE_TO_EXCLUDE;
396 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
399 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
400 type == IGMPV3_BLOCK_OLD_SOURCES)
402 if (pmc->crcount || isquery) {
403 /* make sure we have room for group header and at
406 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)+
408 igmpv3_sendpack(skb);
409 skb = NULL; /* add_grhead will get a new one */
411 skb = add_grhead(skb, pmc, type, &pgr);
415 pih = skb ? (struct igmpv3_report *)skb->h.igmph : NULL;
417 /* EX and TO_EX get a fresh packet, if needed */
419 if (pih && pih->ngrec &&
420 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
422 igmpv3_sendpack(skb);
423 skb = igmpv3_newpack(dev, dev->mtu);
429 for (psf=*psf_list; psf; psf=psf_next) {
432 psf_next = psf->sf_next;
434 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
439 /* clear marks on query responses */
443 if (AVAILABLE(skb) < sizeof(u32) +
444 first*sizeof(struct igmpv3_grec)) {
445 if (truncate && !first)
446 break; /* truncate these */
448 pgr->grec_nsrcs = htons(scount);
450 igmpv3_sendpack(skb);
451 skb = igmpv3_newpack(dev, dev->mtu);
456 skb = add_grhead(skb, pmc, type, &pgr);
459 psrc = (u32 *)skb_put(skb, sizeof(u32));
460 *psrc = psf->sf_inaddr;
462 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
463 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
465 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
467 psf_prev->sf_next = psf->sf_next;
469 *psf_list = psf->sf_next;
477 pgr->grec_nsrcs = htons(scount);
480 pmc->gsquery = 0; /* clear query state on report */
484 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
486 struct sk_buff *skb = NULL;
490 read_lock(&in_dev->mc_list_lock);
491 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
492 if (pmc->multiaddr == IGMP_ALL_HOSTS)
494 spin_lock_bh(&pmc->lock);
495 if (pmc->sfcount[MCAST_EXCLUDE])
496 type = IGMPV3_MODE_IS_EXCLUDE;
498 type = IGMPV3_MODE_IS_INCLUDE;
499 skb = add_grec(skb, pmc, type, 0, 0);
500 spin_unlock_bh(&pmc->lock);
502 read_unlock(&in_dev->mc_list_lock);
504 spin_lock_bh(&pmc->lock);
505 if (pmc->sfcount[MCAST_EXCLUDE])
506 type = IGMPV3_MODE_IS_EXCLUDE;
508 type = IGMPV3_MODE_IS_INCLUDE;
509 skb = add_grec(skb, pmc, type, 0, 0);
510 spin_unlock_bh(&pmc->lock);
514 return igmpv3_sendpack(skb);
518 * remove zero-count source records from a source filter list
520 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
522 struct ip_sf_list *psf_prev, *psf_next, *psf;
525 for (psf=*ppsf; psf; psf = psf_next) {
526 psf_next = psf->sf_next;
527 if (psf->sf_crcount == 0) {
529 psf_prev->sf_next = psf->sf_next;
531 *ppsf = psf->sf_next;
538 static void igmpv3_send_cr(struct in_device *in_dev)
540 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
541 struct sk_buff *skb = NULL;
544 read_lock(&in_dev->mc_list_lock);
545 spin_lock_bh(&in_dev->mc_tomb_lock);
549 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
550 pmc_next = pmc->next;
551 if (pmc->sfmode == MCAST_INCLUDE) {
552 type = IGMPV3_BLOCK_OLD_SOURCES;
553 dtype = IGMPV3_BLOCK_OLD_SOURCES;
554 skb = add_grec(skb, pmc, type, 1, 0);
555 skb = add_grec(skb, pmc, dtype, 1, 1);
559 if (pmc->sfmode == MCAST_EXCLUDE) {
560 type = IGMPV3_CHANGE_TO_INCLUDE;
561 skb = add_grec(skb, pmc, type, 1, 0);
563 if (pmc->crcount == 0) {
564 igmpv3_clear_zeros(&pmc->tomb);
565 igmpv3_clear_zeros(&pmc->sources);
568 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
570 pmc_prev->next = pmc_next;
572 in_dev->mc_tomb = pmc_next;
573 in_dev_put(pmc->interface);
578 spin_unlock_bh(&in_dev->mc_tomb_lock);
581 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
582 spin_lock_bh(&pmc->lock);
583 if (pmc->sfcount[MCAST_EXCLUDE]) {
584 type = IGMPV3_BLOCK_OLD_SOURCES;
585 dtype = IGMPV3_ALLOW_NEW_SOURCES;
587 type = IGMPV3_ALLOW_NEW_SOURCES;
588 dtype = IGMPV3_BLOCK_OLD_SOURCES;
590 skb = add_grec(skb, pmc, type, 0, 0);
591 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
593 /* filter mode changes */
596 if (pmc->sfmode == MCAST_EXCLUDE)
597 type = IGMPV3_CHANGE_TO_EXCLUDE;
599 type = IGMPV3_CHANGE_TO_INCLUDE;
600 skb = add_grec(skb, pmc, type, 0, 0);
602 spin_unlock_bh(&pmc->lock);
604 read_unlock(&in_dev->mc_list_lock);
608 (void) igmpv3_sendpack(skb);
611 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
618 struct net_device *dev = in_dev->dev;
619 u32 group = pmc ? pmc->multiaddr : 0;
622 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
623 return igmpv3_send_report(in_dev, pmc);
624 else if (type == IGMP_HOST_LEAVE_MESSAGE)
625 dst = IGMP_ALL_ROUTER;
630 struct flowi fl = { .oif = dev->ifindex,
631 .nl_u = { .ip4_u = { .daddr = dst } },
632 .proto = IPPROTO_IGMP };
633 if (ip_route_output_key(&rt, &fl))
636 if (rt->rt_src == 0) {
641 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
647 skb->dst = &rt->u.dst;
649 skb_reserve(skb, LL_RESERVED_SPACE(dev));
651 skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
654 iph->ihl = (sizeof(struct iphdr)+4)>>2;
656 iph->frag_off = htons(IP_DF);
659 iph->saddr = rt->rt_src;
660 iph->protocol = IPPROTO_IGMP;
661 iph->tot_len = htons(IGMP_SIZE);
662 ip_select_ident(iph, &rt->u.dst, NULL);
663 ((u8*)&iph[1])[0] = IPOPT_RA;
664 ((u8*)&iph[1])[1] = 4;
665 ((u8*)&iph[1])[2] = 0;
666 ((u8*)&iph[1])[3] = 0;
669 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
674 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
676 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
680 static void igmp_gq_timer_expire(unsigned long data)
682 struct in_device *in_dev = (struct in_device *)data;
684 in_dev->mr_gq_running = 0;
685 igmpv3_send_report(in_dev, NULL);
686 __in_dev_put(in_dev);
689 static void igmp_ifc_timer_expire(unsigned long data)
691 struct in_device *in_dev = (struct in_device *)data;
693 igmpv3_send_cr(in_dev);
694 if (in_dev->mr_ifc_count) {
695 in_dev->mr_ifc_count--;
696 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
698 __in_dev_put(in_dev);
701 static void igmp_ifc_event(struct in_device *in_dev)
703 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
705 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
706 IGMP_Unsolicited_Report_Count;
707 igmp_ifc_start_timer(in_dev, 1);
711 static void igmp_timer_expire(unsigned long data)
713 struct ip_mc_list *im=(struct ip_mc_list *)data;
714 struct in_device *in_dev = im->interface;
716 spin_lock(&im->lock);
719 if (im->unsolicit_count) {
720 im->unsolicit_count--;
721 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
724 spin_unlock(&im->lock);
726 if (IGMP_V1_SEEN(in_dev))
727 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
728 else if (IGMP_V2_SEEN(in_dev))
729 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
731 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
736 static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
738 struct ip_sf_list *psf;
742 for (psf=pmc->sources; psf; psf=psf->sf_next) {
745 for (i=0; i<nsrcs; i++)
746 if (srcs[i] == psf->sf_inaddr) {
754 static void igmp_heard_report(struct in_device *in_dev, u32 group)
756 struct ip_mc_list *im;
758 /* Timers are only set for non-local groups */
760 if (group == IGMP_ALL_HOSTS)
763 read_lock(&in_dev->mc_list_lock);
764 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
765 if (im->multiaddr == group) {
770 read_unlock(&in_dev->mc_list_lock);
773 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
776 struct igmphdr *ih = skb->h.igmph;
777 struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
778 struct ip_mc_list *im;
779 u32 group = ih->group;
786 /* Alas, old v1 router presents here. */
788 max_delay = IGMP_Query_Response_Interval;
789 in_dev->mr_v1_seen = jiffies +
790 IGMP_V1_Router_Present_Timeout;
793 /* v2 router present */
794 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
795 in_dev->mr_v2_seen = jiffies +
796 IGMP_V2_Router_Present_Timeout;
798 /* cancel the interface change timer */
799 in_dev->mr_ifc_count = 0;
800 if (del_timer(&in_dev->mr_ifc_timer))
801 __in_dev_put(in_dev);
802 /* clear deleted report items */
803 igmpv3_clear_delrec(in_dev);
804 } else if (len < 12) {
805 return; /* ignore bogus packet; freed by caller */
807 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
810 ih3 = (struct igmpv3_query *) skb->h.raw;
812 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
813 + ntohs(ih3->nsrcs)*sizeof(__u32)))
815 ih3 = (struct igmpv3_query *) skb->h.raw;
818 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
820 max_delay = 1; /* can't mod w/ 0 */
821 in_dev->mr_maxdelay = max_delay;
823 in_dev->mr_qrv = ih3->qrv;
824 if (!group) { /* general query */
826 return; /* no sources allowed */
827 igmp_gq_start_timer(in_dev);
830 /* mark sources to include, if group & source-specific */
831 mark = ih3->nsrcs != 0;
835 * - Start the timers in all of our membership records
836 * that the query applies to for the interface on
837 * which the query arrived excl. those that belong
838 * to a "local" group (224.0.0.X)
839 * - For timers already running check if they need to
841 * - Use the igmp->igmp_code field as the maximum
844 read_lock(&in_dev->mc_list_lock);
845 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
846 if (group && group != im->multiaddr)
848 if (im->multiaddr == IGMP_ALL_HOSTS)
850 spin_lock_bh(&im->lock);
852 im->gsquery = im->gsquery && mark;
856 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
857 spin_unlock_bh(&im->lock);
858 igmp_mod_timer(im, max_delay);
860 read_unlock(&in_dev->mc_list_lock);
863 int igmp_rcv(struct sk_buff *skb)
865 /* This basically follows the spec line by line -- see RFC1112 */
867 struct in_device *in_dev = in_dev_get(skb->dev);
875 if (!pskb_may_pull(skb, sizeof(struct igmphdr)) ||
876 (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
884 case IGMP_HOST_MEMBERSHIP_QUERY:
885 igmp_heard_query(in_dev, skb, len);
887 case IGMP_HOST_MEMBERSHIP_REPORT:
888 case IGMPV2_HOST_MEMBERSHIP_REPORT:
889 case IGMPV3_HOST_MEMBERSHIP_REPORT:
890 /* Is it our report looped back? */
891 if (((struct rtable*)skb->dst)->fl.iif == 0)
893 igmp_heard_report(in_dev, ih->group);
896 #ifdef CONFIG_IP_PIMSM_V1
898 return pim_rcv_v1(skb);
902 case IGMP_HOST_LEAVE_MESSAGE:
904 case IGMP_MTRACE_RESP:
907 NETDEBUG(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type);
918 * Add a filter to a device
921 static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
923 char buf[MAX_ADDR_LEN];
924 struct net_device *dev = in_dev->dev;
926 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
927 We will get multicast token leakage, when IFF_MULTICAST
928 is changed. This check should be done in dev->set_multicast_list
929 routine. Something sort of:
930 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
933 if (arp_mc_map(addr, buf, dev, 0) == 0)
934 dev_mc_add(dev,buf,dev->addr_len,0);
938 * Remove a filter from a device
941 static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
943 char buf[MAX_ADDR_LEN];
944 struct net_device *dev = in_dev->dev;
946 if (arp_mc_map(addr, buf, dev, 0) == 0)
947 dev_mc_delete(dev,buf,dev->addr_len,0);
950 #ifdef CONFIG_IP_MULTICAST
952 * deleted ip_mc_list manipulation
954 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
956 struct ip_mc_list *pmc;
958 /* this is an "ip_mc_list" for convenience; only the fields below
959 * are actually used. In particular, the refcnt and users are not
960 * used for management of the delete list. Using the same structure
961 * for deleted items allows change reports to use common code with
962 * non-deleted or query-response MCA's.
964 pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
967 memset(pmc, 0, sizeof(*pmc));
968 spin_lock_bh(&im->lock);
969 pmc->interface = im->interface;
971 pmc->multiaddr = im->multiaddr;
972 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
973 IGMP_Unsolicited_Report_Count;
974 pmc->sfmode = im->sfmode;
975 if (pmc->sfmode == MCAST_INCLUDE) {
976 struct ip_sf_list *psf;
978 pmc->tomb = im->tomb;
979 pmc->sources = im->sources;
980 im->tomb = im->sources = NULL;
981 for (psf=pmc->sources; psf; psf=psf->sf_next)
982 psf->sf_crcount = pmc->crcount;
984 spin_unlock_bh(&im->lock);
986 spin_lock_bh(&in_dev->mc_tomb_lock);
987 pmc->next = in_dev->mc_tomb;
988 in_dev->mc_tomb = pmc;
989 spin_unlock_bh(&in_dev->mc_tomb_lock);
992 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
994 struct ip_mc_list *pmc, *pmc_prev;
995 struct ip_sf_list *psf, *psf_next;
997 spin_lock_bh(&in_dev->mc_tomb_lock);
999 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1000 if (pmc->multiaddr == multiaddr)
1006 pmc_prev->next = pmc->next;
1008 in_dev->mc_tomb = pmc->next;
1010 spin_unlock_bh(&in_dev->mc_tomb_lock);
1012 for (psf=pmc->tomb; psf; psf=psf_next) {
1013 psf_next = psf->sf_next;
1016 in_dev_put(pmc->interface);
1021 static void igmpv3_clear_delrec(struct in_device *in_dev)
1023 struct ip_mc_list *pmc, *nextpmc;
1025 spin_lock_bh(&in_dev->mc_tomb_lock);
1026 pmc = in_dev->mc_tomb;
1027 in_dev->mc_tomb = NULL;
1028 spin_unlock_bh(&in_dev->mc_tomb_lock);
1030 for (; pmc; pmc = nextpmc) {
1031 nextpmc = pmc->next;
1032 ip_mc_clear_src(pmc);
1033 in_dev_put(pmc->interface);
1036 /* clear dead sources, too */
1037 read_lock(&in_dev->mc_list_lock);
1038 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1039 struct ip_sf_list *psf, *psf_next;
1041 spin_lock_bh(&pmc->lock);
1044 spin_unlock_bh(&pmc->lock);
1045 for (; psf; psf=psf_next) {
1046 psf_next = psf->sf_next;
1050 read_unlock(&in_dev->mc_list_lock);
1054 static void igmp_group_dropped(struct ip_mc_list *im)
1056 struct in_device *in_dev = im->interface;
1057 #ifdef CONFIG_IP_MULTICAST
1063 ip_mc_filter_del(in_dev, im->multiaddr);
1066 #ifdef CONFIG_IP_MULTICAST
1067 if (im->multiaddr == IGMP_ALL_HOSTS)
1070 reporter = im->reporter;
1071 igmp_stop_timer(im);
1073 if (!in_dev->dead) {
1074 if (IGMP_V1_SEEN(in_dev))
1076 if (IGMP_V2_SEEN(in_dev)) {
1078 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1082 igmpv3_add_delrec(in_dev, im);
1084 igmp_ifc_event(in_dev);
1088 ip_mc_clear_src(im);
1091 static void igmp_group_added(struct ip_mc_list *im)
1093 struct in_device *in_dev = im->interface;
1095 if (im->loaded == 0) {
1097 ip_mc_filter_add(in_dev, im->multiaddr);
1100 #ifdef CONFIG_IP_MULTICAST
1101 if (im->multiaddr == IGMP_ALL_HOSTS)
1106 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1107 spin_lock_bh(&im->lock);
1108 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1109 spin_unlock_bh(&im->lock);
1114 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1115 IGMP_Unsolicited_Report_Count;
1116 igmp_ifc_event(in_dev);
1122 * Multicast list managers
1127 * A socket has joined a multicast group on device dev.
1130 void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1132 struct ip_mc_list *im;
1136 for (im=in_dev->mc_list; im; im=im->next) {
1137 if (im->multiaddr == addr) {
1139 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1144 im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1149 im->interface=in_dev;
1150 in_dev_hold(in_dev);
1152 /* initial mode is (EX, empty) */
1153 im->sfmode = MCAST_EXCLUDE;
1154 im->sfcount[MCAST_INCLUDE] = 0;
1155 im->sfcount[MCAST_EXCLUDE] = 1;
1159 atomic_set(&im->refcnt, 1);
1160 spin_lock_init(&im->lock);
1161 #ifdef CONFIG_IP_MULTICAST
1163 init_timer(&im->timer);
1164 im->timer.data=(unsigned long)im;
1165 im->timer.function=&igmp_timer_expire;
1166 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1171 write_lock_bh(&in_dev->mc_list_lock);
1172 im->next=in_dev->mc_list;
1174 write_unlock_bh(&in_dev->mc_list_lock);
1175 #ifdef CONFIG_IP_MULTICAST
1176 igmpv3_del_delrec(in_dev, im->multiaddr);
1178 igmp_group_added(im);
1180 ip_rt_multicast_event(in_dev);
1186 * A socket has left a multicast group on device dev
1189 void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1191 struct ip_mc_list *i, **ip;
1195 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1196 if (i->multiaddr==addr) {
1197 if (--i->users == 0) {
1198 write_lock_bh(&in_dev->mc_list_lock);
1200 write_unlock_bh(&in_dev->mc_list_lock);
1201 igmp_group_dropped(i);
1204 ip_rt_multicast_event(in_dev);
1214 /* Device going down */
1216 void ip_mc_down(struct in_device *in_dev)
1218 struct ip_mc_list *i;
1222 for (i=in_dev->mc_list; i; i=i->next)
1223 igmp_group_dropped(i);
1225 #ifdef CONFIG_IP_MULTICAST
1226 in_dev->mr_ifc_count = 0;
1227 if (del_timer(&in_dev->mr_ifc_timer))
1228 __in_dev_put(in_dev);
1229 in_dev->mr_gq_running = 0;
1230 if (del_timer(&in_dev->mr_gq_timer))
1231 __in_dev_put(in_dev);
1232 igmpv3_clear_delrec(in_dev);
1235 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1238 void ip_mc_init_dev(struct in_device *in_dev)
1242 in_dev->mc_tomb = NULL;
1243 #ifdef CONFIG_IP_MULTICAST
1244 in_dev->mr_gq_running = 0;
1245 init_timer(&in_dev->mr_gq_timer);
1246 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1247 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1248 in_dev->mr_ifc_count = 0;
1249 init_timer(&in_dev->mr_ifc_timer);
1250 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1251 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1252 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1255 rwlock_init(&in_dev->mc_list_lock);
1256 spin_lock_init(&in_dev->mc_tomb_lock);
1259 /* Device going up */
1261 void ip_mc_up(struct in_device *in_dev)
1263 struct ip_mc_list *i;
1267 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1269 for (i=in_dev->mc_list; i; i=i->next)
1270 igmp_group_added(i);
1274 * Device is about to be destroyed: clean up.
1277 void ip_mc_destroy_dev(struct in_device *in_dev)
1279 struct ip_mc_list *i;
1283 /* Deactivate timers */
1286 write_lock_bh(&in_dev->mc_list_lock);
1287 while ((i = in_dev->mc_list) != NULL) {
1288 in_dev->mc_list = i->next;
1289 write_unlock_bh(&in_dev->mc_list_lock);
1291 igmp_group_dropped(i);
1294 write_lock_bh(&in_dev->mc_list_lock);
1296 write_unlock_bh(&in_dev->mc_list_lock);
1299 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1301 struct flowi fl = { .nl_u = { .ip4_u =
1302 { .daddr = imr->imr_multiaddr.s_addr } } };
1304 struct net_device *dev = NULL;
1305 struct in_device *idev = NULL;
1307 if (imr->imr_ifindex) {
1308 idev = inetdev_by_index(imr->imr_ifindex);
1313 if (imr->imr_address.s_addr) {
1314 dev = ip_dev_find(imr->imr_address.s_addr);
1320 if (!dev && !ip_route_output_key(&rt, &fl)) {
1321 dev = rt->u.dst.dev;
1325 imr->imr_ifindex = dev->ifindex;
1326 idev = __in_dev_get_rtnl(dev);
1332 * Join a socket to a group
1334 int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1335 int sysctl_igmp_max_msf = IP_MAX_MSF;
1338 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1341 struct ip_sf_list *psf, *psf_prev;
1345 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1346 if (psf->sf_inaddr == *psfsrc)
1350 if (!psf || psf->sf_count[sfmode] == 0) {
1351 /* source filter not found, or count wrong => bug */
1354 psf->sf_count[sfmode]--;
1355 if (psf->sf_count[sfmode] == 0) {
1356 ip_rt_multicast_event(pmc->interface);
1358 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1359 #ifdef CONFIG_IP_MULTICAST
1360 struct in_device *in_dev = pmc->interface;
1363 /* no more filters for this source */
1365 psf_prev->sf_next = psf->sf_next;
1367 pmc->sources = psf->sf_next;
1368 #ifdef CONFIG_IP_MULTICAST
1369 if (psf->sf_oldin &&
1370 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1371 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1372 IGMP_Unsolicited_Report_Count;
1373 psf->sf_next = pmc->tomb;
1383 #ifndef CONFIG_IP_MULTICAST
1384 #define igmp_ifc_event(x) do { } while (0)
1387 static int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1388 int sfcount, __u32 *psfsrc, int delta)
1390 struct ip_mc_list *pmc;
1396 read_lock(&in_dev->mc_list_lock);
1397 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1398 if (*pmca == pmc->multiaddr)
1402 /* MCA not found?? bug */
1403 read_unlock(&in_dev->mc_list_lock);
1406 spin_lock_bh(&pmc->lock);
1407 read_unlock(&in_dev->mc_list_lock);
1408 #ifdef CONFIG_IP_MULTICAST
1413 if (!pmc->sfcount[sfmode])
1415 pmc->sfcount[sfmode]--;
1418 for (i=0; i<sfcount; i++) {
1419 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1421 changerec |= rv > 0;
1425 if (pmc->sfmode == MCAST_EXCLUDE &&
1426 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1427 pmc->sfcount[MCAST_INCLUDE]) {
1428 #ifdef CONFIG_IP_MULTICAST
1429 struct ip_sf_list *psf;
1432 /* filter mode change */
1433 pmc->sfmode = MCAST_INCLUDE;
1434 #ifdef CONFIG_IP_MULTICAST
1435 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1436 IGMP_Unsolicited_Report_Count;
1437 in_dev->mr_ifc_count = pmc->crcount;
1438 for (psf=pmc->sources; psf; psf = psf->sf_next)
1439 psf->sf_crcount = 0;
1440 igmp_ifc_event(pmc->interface);
1441 } else if (sf_setstate(pmc) || changerec) {
1442 igmp_ifc_event(pmc->interface);
1446 spin_unlock_bh(&pmc->lock);
1451 * Add multicast single-source filter to the interface list
1453 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1454 __u32 *psfsrc, int delta)
1456 struct ip_sf_list *psf, *psf_prev;
1459 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1460 if (psf->sf_inaddr == *psfsrc)
1465 psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1468 memset(psf, 0, sizeof(*psf));
1469 psf->sf_inaddr = *psfsrc;
1471 psf_prev->sf_next = psf;
1475 psf->sf_count[sfmode]++;
1476 if (psf->sf_count[sfmode] == 1) {
1477 ip_rt_multicast_event(pmc->interface);
1482 #ifdef CONFIG_IP_MULTICAST
1483 static void sf_markstate(struct ip_mc_list *pmc)
1485 struct ip_sf_list *psf;
1486 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1488 for (psf=pmc->sources; psf; psf=psf->sf_next)
1489 if (pmc->sfcount[MCAST_EXCLUDE]) {
1490 psf->sf_oldin = mca_xcount ==
1491 psf->sf_count[MCAST_EXCLUDE] &&
1492 !psf->sf_count[MCAST_INCLUDE];
1494 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1497 static int sf_setstate(struct ip_mc_list *pmc)
1499 struct ip_sf_list *psf;
1500 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1501 int qrv = pmc->interface->mr_qrv;
1505 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1506 if (pmc->sfcount[MCAST_EXCLUDE]) {
1507 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1508 !psf->sf_count[MCAST_INCLUDE];
1510 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1511 if (new_in != psf->sf_oldin) {
1512 psf->sf_crcount = qrv;
1521 * Add multicast source filter list to the interface list
1523 static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1524 int sfcount, __u32 *psfsrc, int delta)
1526 struct ip_mc_list *pmc;
1532 read_lock(&in_dev->mc_list_lock);
1533 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1534 if (*pmca == pmc->multiaddr)
1538 /* MCA not found?? bug */
1539 read_unlock(&in_dev->mc_list_lock);
1542 spin_lock_bh(&pmc->lock);
1543 read_unlock(&in_dev->mc_list_lock);
1545 #ifdef CONFIG_IP_MULTICAST
1548 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1550 pmc->sfcount[sfmode]++;
1552 for (i=0; i<sfcount; i++) {
1553 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1560 pmc->sfcount[sfmode]--;
1562 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1563 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1564 #ifdef CONFIG_IP_MULTICAST
1565 struct in_device *in_dev = pmc->interface;
1566 struct ip_sf_list *psf;
1569 /* filter mode change */
1570 if (pmc->sfcount[MCAST_EXCLUDE])
1571 pmc->sfmode = MCAST_EXCLUDE;
1572 else if (pmc->sfcount[MCAST_INCLUDE])
1573 pmc->sfmode = MCAST_INCLUDE;
1574 #ifdef CONFIG_IP_MULTICAST
1575 /* else no filters; keep old mode for reports */
1577 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1578 IGMP_Unsolicited_Report_Count;
1579 in_dev->mr_ifc_count = pmc->crcount;
1580 for (psf=pmc->sources; psf; psf = psf->sf_next)
1581 psf->sf_crcount = 0;
1582 igmp_ifc_event(in_dev);
1583 } else if (sf_setstate(pmc)) {
1584 igmp_ifc_event(in_dev);
1587 spin_unlock_bh(&pmc->lock);
1591 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1593 struct ip_sf_list *psf, *nextpsf;
1595 for (psf=pmc->tomb; psf; psf=nextpsf) {
1596 nextpsf = psf->sf_next;
1600 for (psf=pmc->sources; psf; psf=nextpsf) {
1601 nextpsf = psf->sf_next;
1604 pmc->sources = NULL;
1605 pmc->sfmode = MCAST_EXCLUDE;
1606 pmc->sfcount[MCAST_INCLUDE] = 0;
1607 pmc->sfcount[MCAST_EXCLUDE] = 1;
1612 * Join a multicast group
1614 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1617 u32 addr = imr->imr_multiaddr.s_addr;
1618 struct ip_mc_socklist *iml=NULL, *i;
1619 struct in_device *in_dev;
1620 struct inet_sock *inet = inet_sk(sk);
1624 if (!MULTICAST(addr))
1629 in_dev = ip_mc_find_dev(imr);
1638 ifindex = imr->imr_ifindex;
1639 for (i = inet->mc_list; i; i = i->next) {
1640 if (i->multi.imr_multiaddr.s_addr == addr &&
1641 i->multi.imr_ifindex == ifindex)
1646 if (count >= sysctl_igmp_max_memberships)
1648 iml = (struct ip_mc_socklist *)sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1652 memcpy(&iml->multi, imr, sizeof(*imr));
1653 iml->next = inet->mc_list;
1655 iml->sfmode = MCAST_EXCLUDE;
1656 inet->mc_list = iml;
1657 ip_mc_inc_group(in_dev, addr);
1664 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1665 struct in_device *in_dev)
1669 if (iml->sflist == 0) {
1670 /* any-source empty exclude case */
1671 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1672 iml->sfmode, 0, NULL, 0);
1674 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1675 iml->sfmode, iml->sflist->sl_count,
1676 iml->sflist->sl_addr, 0);
1677 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1683 * Ask a socket to leave a group.
1686 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1688 struct inet_sock *inet = inet_sk(sk);
1689 struct ip_mc_socklist *iml, **imlp;
1690 struct in_device *in_dev;
1691 u32 group = imr->imr_multiaddr.s_addr;
1695 in_dev = ip_mc_find_dev(imr);
1700 ifindex = imr->imr_ifindex;
1701 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1702 if (iml->multi.imr_multiaddr.s_addr == group &&
1703 iml->multi.imr_ifindex == ifindex) {
1704 (void) ip_mc_leave_src(sk, iml, in_dev);
1708 ip_mc_dec_group(in_dev, group);
1710 sock_kfree_s(sk, iml, sizeof(*iml));
1715 return -EADDRNOTAVAIL;
1718 int ip_mc_source(int add, int omode, struct sock *sk, struct
1719 ip_mreq_source *mreqs, int ifindex)
1722 struct ip_mreqn imr;
1723 u32 addr = mreqs->imr_multiaddr;
1724 struct ip_mc_socklist *pmc;
1725 struct in_device *in_dev = NULL;
1726 struct inet_sock *inet = inet_sk(sk);
1727 struct ip_sf_socklist *psl;
1731 if (!MULTICAST(addr))
1736 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1737 imr.imr_address.s_addr = mreqs->imr_interface;
1738 imr.imr_ifindex = ifindex;
1739 in_dev = ip_mc_find_dev(&imr);
1745 err = -EADDRNOTAVAIL;
1747 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1748 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1749 && pmc->multi.imr_ifindex == imr.imr_ifindex)
1752 if (!pmc) { /* must have a prior join */
1756 /* if a source filter was set, must be the same mode as before */
1758 if (pmc->sfmode != omode) {
1762 } else if (pmc->sfmode != omode) {
1763 /* allow mode switches for empty-set filters */
1764 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1765 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1767 pmc->sfmode = omode;
1773 goto done; /* err = -EADDRNOTAVAIL */
1775 for (i=0; i<psl->sl_count; i++) {
1776 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1781 if (rv) /* source not found */
1782 goto done; /* err = -EADDRNOTAVAIL */
1784 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1785 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1790 /* update the interface filter */
1791 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1792 &mreqs->imr_sourceaddr, 1);
1794 for (j=i+1; j<psl->sl_count; j++)
1795 psl->sl_addr[j-1] = psl->sl_addr[j];
1800 /* else, add a new source to the filter */
1802 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1806 if (!psl || psl->sl_count == psl->sl_max) {
1807 struct ip_sf_socklist *newpsl;
1808 int count = IP_SFBLOCK;
1811 count += psl->sl_max;
1812 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1813 IP_SFLSIZE(count), GFP_KERNEL);
1818 newpsl->sl_max = count;
1819 newpsl->sl_count = count - IP_SFBLOCK;
1821 for (i=0; i<psl->sl_count; i++)
1822 newpsl->sl_addr[i] = psl->sl_addr[i];
1823 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1825 pmc->sflist = psl = newpsl;
1827 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1828 for (i=0; i<psl->sl_count; i++) {
1829 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1834 if (rv == 0) /* address already there is an error */
1836 for (j=psl->sl_count-1; j>=i; j--)
1837 psl->sl_addr[j+1] = psl->sl_addr[j];
1838 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1841 /* update the interface list */
1842 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1843 &mreqs->imr_sourceaddr, 1);
1847 return ip_mc_leave_group(sk, &imr);
1851 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1854 struct ip_mreqn imr;
1855 u32 addr = msf->imsf_multiaddr;
1856 struct ip_mc_socklist *pmc;
1857 struct in_device *in_dev;
1858 struct inet_sock *inet = inet_sk(sk);
1859 struct ip_sf_socklist *newpsl, *psl;
1862 if (!MULTICAST(addr))
1864 if (msf->imsf_fmode != MCAST_INCLUDE &&
1865 msf->imsf_fmode != MCAST_EXCLUDE)
1870 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1871 imr.imr_address.s_addr = msf->imsf_interface;
1872 imr.imr_ifindex = ifindex;
1873 in_dev = ip_mc_find_dev(&imr);
1880 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1881 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
1886 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1887 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1888 pmc->multi.imr_ifindex == imr.imr_ifindex)
1891 if (!pmc) { /* must have a prior join */
1895 if (msf->imsf_numsrc) {
1896 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1897 IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1902 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1903 memcpy(newpsl->sl_addr, msf->imsf_slist,
1904 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1905 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1906 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1908 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1915 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1916 psl->sl_count, psl->sl_addr, 0);
1917 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1919 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1921 pmc->sflist = newpsl;
1922 pmc->sfmode = msf->imsf_fmode;
1927 err = ip_mc_leave_group(sk, &imr);
1931 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1932 struct ip_msfilter __user *optval, int __user *optlen)
1934 int err, len, count, copycount;
1935 struct ip_mreqn imr;
1936 u32 addr = msf->imsf_multiaddr;
1937 struct ip_mc_socklist *pmc;
1938 struct in_device *in_dev;
1939 struct inet_sock *inet = inet_sk(sk);
1940 struct ip_sf_socklist *psl;
1942 if (!MULTICAST(addr))
1947 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1948 imr.imr_address.s_addr = msf->imsf_interface;
1949 imr.imr_ifindex = 0;
1950 in_dev = ip_mc_find_dev(&imr);
1956 err = -EADDRNOTAVAIL;
1958 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1959 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1960 pmc->multi.imr_ifindex == imr.imr_ifindex)
1963 if (!pmc) /* must have a prior join */
1965 msf->imsf_fmode = pmc->sfmode;
1972 count = psl->sl_count;
1974 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1975 len = copycount * sizeof(psl->sl_addr[0]);
1976 msf->imsf_numsrc = count;
1977 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1978 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
1982 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
1990 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
1991 struct group_filter __user *optval, int __user *optlen)
1993 int err, i, count, copycount;
1994 struct sockaddr_in *psin;
1996 struct ip_mc_socklist *pmc;
1997 struct inet_sock *inet = inet_sk(sk);
1998 struct ip_sf_socklist *psl;
2000 psin = (struct sockaddr_in *)&gsf->gf_group;
2001 if (psin->sin_family != AF_INET)
2003 addr = psin->sin_addr.s_addr;
2004 if (!MULTICAST(addr))
2009 err = -EADDRNOTAVAIL;
2011 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2012 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2013 pmc->multi.imr_ifindex == gsf->gf_interface)
2016 if (!pmc) /* must have a prior join */
2018 gsf->gf_fmode = pmc->sfmode;
2021 count = psl ? psl->sl_count : 0;
2022 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2023 gsf->gf_numsrc = count;
2024 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2025 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2028 for (i=0; i<copycount; i++) {
2029 struct sockaddr_in *psin;
2030 struct sockaddr_storage ss;
2032 psin = (struct sockaddr_in *)&ss;
2033 memset(&ss, 0, sizeof(ss));
2034 psin->sin_family = AF_INET;
2035 psin->sin_addr.s_addr = psl->sl_addr[i];
2036 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2046 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2048 int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2050 struct inet_sock *inet = inet_sk(sk);
2051 struct ip_mc_socklist *pmc;
2052 struct ip_sf_socklist *psl;
2055 if (!MULTICAST(loc_addr))
2058 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2059 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2060 pmc->multi.imr_ifindex == dif)
2067 return pmc->sfmode == MCAST_EXCLUDE;
2069 for (i=0; i<psl->sl_count; i++) {
2070 if (psl->sl_addr[i] == rmt_addr)
2073 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2075 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2081 * A socket is closing.
2084 void ip_mc_drop_socket(struct sock *sk)
2086 struct inet_sock *inet = inet_sk(sk);
2087 struct ip_mc_socklist *iml;
2089 if (inet->mc_list == NULL)
2093 while ((iml = inet->mc_list) != NULL) {
2094 struct in_device *in_dev;
2095 inet->mc_list = iml->next;
2097 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2098 (void) ip_mc_leave_src(sk, iml, in_dev);
2099 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2102 sock_kfree_s(sk, iml, sizeof(*iml));
2108 int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2110 struct ip_mc_list *im;
2111 struct ip_sf_list *psf;
2114 read_lock(&in_dev->mc_list_lock);
2115 for (im=in_dev->mc_list; im; im=im->next) {
2116 if (im->multiaddr == mc_addr)
2119 if (im && proto == IPPROTO_IGMP) {
2123 for (psf=im->sources; psf; psf=psf->sf_next) {
2124 if (psf->sf_inaddr == src_addr)
2128 rv = psf->sf_count[MCAST_INCLUDE] ||
2129 psf->sf_count[MCAST_EXCLUDE] !=
2130 im->sfcount[MCAST_EXCLUDE];
2132 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2134 rv = 1; /* unspecified source; tentatively allow */
2136 read_unlock(&in_dev->mc_list_lock);
2140 #if defined(CONFIG_PROC_FS)
2141 struct igmp_mc_iter_state {
2142 struct net_device *dev;
2143 struct in_device *in_dev;
2146 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2148 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2150 struct ip_mc_list *im = NULL;
2151 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2153 for (state->dev = dev_base, state->in_dev = NULL;
2155 state->dev = state->dev->next) {
2156 struct in_device *in_dev;
2157 in_dev = in_dev_get(state->dev);
2160 read_lock(&in_dev->mc_list_lock);
2161 im = in_dev->mc_list;
2163 state->in_dev = in_dev;
2166 read_unlock(&in_dev->mc_list_lock);
2172 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2174 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2177 if (likely(state->in_dev != NULL)) {
2178 read_unlock(&state->in_dev->mc_list_lock);
2179 in_dev_put(state->in_dev);
2181 state->dev = state->dev->next;
2183 state->in_dev = NULL;
2186 state->in_dev = in_dev_get(state->dev);
2189 read_lock(&state->in_dev->mc_list_lock);
2190 im = state->in_dev->mc_list;
2195 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2197 struct ip_mc_list *im = igmp_mc_get_first(seq);
2199 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2201 return pos ? NULL : im;
2204 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2206 read_lock(&dev_base_lock);
2207 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2210 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2212 struct ip_mc_list *im;
2213 if (v == SEQ_START_TOKEN)
2214 im = igmp_mc_get_first(seq);
2216 im = igmp_mc_get_next(seq, v);
2221 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2223 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2224 if (likely(state->in_dev != NULL)) {
2225 read_unlock(&state->in_dev->mc_list_lock);
2226 in_dev_put(state->in_dev);
2227 state->in_dev = NULL;
2230 read_unlock(&dev_base_lock);
2233 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2235 if (v == SEQ_START_TOKEN)
2237 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2239 struct ip_mc_list *im = (struct ip_mc_list *)v;
2240 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2242 #ifdef CONFIG_IP_MULTICAST
2243 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2244 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2250 if (state->in_dev->mc_list == im) {
2251 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2252 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2256 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2257 im->multiaddr, im->users,
2258 im->tm_running, im->tm_running ?
2259 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2265 static struct seq_operations igmp_mc_seq_ops = {
2266 .start = igmp_mc_seq_start,
2267 .next = igmp_mc_seq_next,
2268 .stop = igmp_mc_seq_stop,
2269 .show = igmp_mc_seq_show,
2272 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2274 struct seq_file *seq;
2276 struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2280 rc = seq_open(file, &igmp_mc_seq_ops);
2284 seq = file->private_data;
2286 memset(s, 0, sizeof(*s));
2294 static struct file_operations igmp_mc_seq_fops = {
2295 .owner = THIS_MODULE,
2296 .open = igmp_mc_seq_open,
2298 .llseek = seq_lseek,
2299 .release = seq_release_private,
2302 struct igmp_mcf_iter_state {
2303 struct net_device *dev;
2304 struct in_device *idev;
2305 struct ip_mc_list *im;
2308 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2310 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2312 struct ip_sf_list *psf = NULL;
2313 struct ip_mc_list *im = NULL;
2314 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2316 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2318 state->dev = state->dev->next) {
2319 struct in_device *idev;
2320 idev = in_dev_get(state->dev);
2321 if (unlikely(idev == NULL))
2323 read_lock(&idev->mc_list_lock);
2325 if (likely(im != NULL)) {
2326 spin_lock_bh(&im->lock);
2328 if (likely(psf != NULL)) {
2333 spin_unlock_bh(&im->lock);
2335 read_unlock(&idev->mc_list_lock);
2341 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2343 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2347 spin_unlock_bh(&state->im->lock);
2348 state->im = state->im->next;
2349 while (!state->im) {
2350 if (likely(state->idev != NULL)) {
2351 read_unlock(&state->idev->mc_list_lock);
2352 in_dev_put(state->idev);
2354 state->dev = state->dev->next;
2359 state->idev = in_dev_get(state->dev);
2362 read_lock(&state->idev->mc_list_lock);
2363 state->im = state->idev->mc_list;
2367 spin_lock_bh(&state->im->lock);
2368 psf = state->im->sources;
2374 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2376 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2378 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2380 return pos ? NULL : psf;
2383 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2385 read_lock(&dev_base_lock);
2386 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2389 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2391 struct ip_sf_list *psf;
2392 if (v == SEQ_START_TOKEN)
2393 psf = igmp_mcf_get_first(seq);
2395 psf = igmp_mcf_get_next(seq, v);
2400 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2402 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2403 if (likely(state->im != NULL)) {
2404 spin_unlock_bh(&state->im->lock);
2407 if (likely(state->idev != NULL)) {
2408 read_unlock(&state->idev->mc_list_lock);
2409 in_dev_put(state->idev);
2413 read_unlock(&dev_base_lock);
2416 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2418 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2419 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2421 if (v == SEQ_START_TOKEN) {
2424 "%10s %10s %6s %6s\n", "Idx",
2426 "SRC", "INC", "EXC");
2430 "0x%08x %6lu %6lu\n",
2431 state->dev->ifindex, state->dev->name,
2432 ntohl(state->im->multiaddr),
2433 ntohl(psf->sf_inaddr),
2434 psf->sf_count[MCAST_INCLUDE],
2435 psf->sf_count[MCAST_EXCLUDE]);
2440 static struct seq_operations igmp_mcf_seq_ops = {
2441 .start = igmp_mcf_seq_start,
2442 .next = igmp_mcf_seq_next,
2443 .stop = igmp_mcf_seq_stop,
2444 .show = igmp_mcf_seq_show,
2447 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2449 struct seq_file *seq;
2451 struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2455 rc = seq_open(file, &igmp_mcf_seq_ops);
2459 seq = file->private_data;
2461 memset(s, 0, sizeof(*s));
2469 static struct file_operations igmp_mcf_seq_fops = {
2470 .owner = THIS_MODULE,
2471 .open = igmp_mcf_seq_open,
2473 .llseek = seq_lseek,
2474 .release = seq_release_private,
2477 int __init igmp_mc_proc_init(void)
2479 proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2480 proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2485 EXPORT_SYMBOL(ip_mc_dec_group);
2486 EXPORT_SYMBOL(ip_mc_inc_group);
2487 EXPORT_SYMBOL(ip_mc_join_group);