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)))
878 switch (skb->ip_summed) {
880 if (!(u16)csum_fold(skb->csum))
885 if (__skb_checksum_complete(skb))
891 case IGMP_HOST_MEMBERSHIP_QUERY:
892 igmp_heard_query(in_dev, skb, len);
894 case IGMP_HOST_MEMBERSHIP_REPORT:
895 case IGMPV2_HOST_MEMBERSHIP_REPORT:
896 case IGMPV3_HOST_MEMBERSHIP_REPORT:
897 /* Is it our report looped back? */
898 if (((struct rtable*)skb->dst)->fl.iif == 0)
900 igmp_heard_report(in_dev, ih->group);
903 #ifdef CONFIG_IP_PIMSM_V1
905 return pim_rcv_v1(skb);
909 case IGMP_HOST_LEAVE_MESSAGE:
911 case IGMP_MTRACE_RESP:
914 NETDEBUG(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type);
927 * Add a filter to a device
930 static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
932 char buf[MAX_ADDR_LEN];
933 struct net_device *dev = in_dev->dev;
935 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
936 We will get multicast token leakage, when IFF_MULTICAST
937 is changed. This check should be done in dev->set_multicast_list
938 routine. Something sort of:
939 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
942 if (arp_mc_map(addr, buf, dev, 0) == 0)
943 dev_mc_add(dev,buf,dev->addr_len,0);
947 * Remove a filter from a device
950 static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
952 char buf[MAX_ADDR_LEN];
953 struct net_device *dev = in_dev->dev;
955 if (arp_mc_map(addr, buf, dev, 0) == 0)
956 dev_mc_delete(dev,buf,dev->addr_len,0);
959 #ifdef CONFIG_IP_MULTICAST
961 * deleted ip_mc_list manipulation
963 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
965 struct ip_mc_list *pmc;
967 /* this is an "ip_mc_list" for convenience; only the fields below
968 * are actually used. In particular, the refcnt and users are not
969 * used for management of the delete list. Using the same structure
970 * for deleted items allows change reports to use common code with
971 * non-deleted or query-response MCA's.
973 pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
976 memset(pmc, 0, sizeof(*pmc));
977 spin_lock_bh(&im->lock);
978 pmc->interface = im->interface;
980 pmc->multiaddr = im->multiaddr;
981 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
982 IGMP_Unsolicited_Report_Count;
983 pmc->sfmode = im->sfmode;
984 if (pmc->sfmode == MCAST_INCLUDE) {
985 struct ip_sf_list *psf;
987 pmc->tomb = im->tomb;
988 pmc->sources = im->sources;
989 im->tomb = im->sources = NULL;
990 for (psf=pmc->sources; psf; psf=psf->sf_next)
991 psf->sf_crcount = pmc->crcount;
993 spin_unlock_bh(&im->lock);
995 spin_lock_bh(&in_dev->mc_tomb_lock);
996 pmc->next = in_dev->mc_tomb;
997 in_dev->mc_tomb = pmc;
998 spin_unlock_bh(&in_dev->mc_tomb_lock);
1001 static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
1003 struct ip_mc_list *pmc, *pmc_prev;
1004 struct ip_sf_list *psf, *psf_next;
1006 spin_lock_bh(&in_dev->mc_tomb_lock);
1008 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1009 if (pmc->multiaddr == multiaddr)
1015 pmc_prev->next = pmc->next;
1017 in_dev->mc_tomb = pmc->next;
1019 spin_unlock_bh(&in_dev->mc_tomb_lock);
1021 for (psf=pmc->tomb; psf; psf=psf_next) {
1022 psf_next = psf->sf_next;
1025 in_dev_put(pmc->interface);
1030 static void igmpv3_clear_delrec(struct in_device *in_dev)
1032 struct ip_mc_list *pmc, *nextpmc;
1034 spin_lock_bh(&in_dev->mc_tomb_lock);
1035 pmc = in_dev->mc_tomb;
1036 in_dev->mc_tomb = NULL;
1037 spin_unlock_bh(&in_dev->mc_tomb_lock);
1039 for (; pmc; pmc = nextpmc) {
1040 nextpmc = pmc->next;
1041 ip_mc_clear_src(pmc);
1042 in_dev_put(pmc->interface);
1045 /* clear dead sources, too */
1046 read_lock(&in_dev->mc_list_lock);
1047 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1048 struct ip_sf_list *psf, *psf_next;
1050 spin_lock_bh(&pmc->lock);
1053 spin_unlock_bh(&pmc->lock);
1054 for (; psf; psf=psf_next) {
1055 psf_next = psf->sf_next;
1059 read_unlock(&in_dev->mc_list_lock);
1063 static void igmp_group_dropped(struct ip_mc_list *im)
1065 struct in_device *in_dev = im->interface;
1066 #ifdef CONFIG_IP_MULTICAST
1072 ip_mc_filter_del(in_dev, im->multiaddr);
1075 #ifdef CONFIG_IP_MULTICAST
1076 if (im->multiaddr == IGMP_ALL_HOSTS)
1079 reporter = im->reporter;
1080 igmp_stop_timer(im);
1082 if (!in_dev->dead) {
1083 if (IGMP_V1_SEEN(in_dev))
1085 if (IGMP_V2_SEEN(in_dev)) {
1087 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1091 igmpv3_add_delrec(in_dev, im);
1093 igmp_ifc_event(in_dev);
1097 ip_mc_clear_src(im);
1100 static void igmp_group_added(struct ip_mc_list *im)
1102 struct in_device *in_dev = im->interface;
1104 if (im->loaded == 0) {
1106 ip_mc_filter_add(in_dev, im->multiaddr);
1109 #ifdef CONFIG_IP_MULTICAST
1110 if (im->multiaddr == IGMP_ALL_HOSTS)
1115 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1116 spin_lock_bh(&im->lock);
1117 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1118 spin_unlock_bh(&im->lock);
1123 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1124 IGMP_Unsolicited_Report_Count;
1125 igmp_ifc_event(in_dev);
1131 * Multicast list managers
1136 * A socket has joined a multicast group on device dev.
1139 void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1141 struct ip_mc_list *im;
1145 for (im=in_dev->mc_list; im; im=im->next) {
1146 if (im->multiaddr == addr) {
1148 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1153 im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1158 im->interface=in_dev;
1159 in_dev_hold(in_dev);
1161 /* initial mode is (EX, empty) */
1162 im->sfmode = MCAST_EXCLUDE;
1163 im->sfcount[MCAST_INCLUDE] = 0;
1164 im->sfcount[MCAST_EXCLUDE] = 1;
1168 atomic_set(&im->refcnt, 1);
1169 spin_lock_init(&im->lock);
1170 #ifdef CONFIG_IP_MULTICAST
1172 init_timer(&im->timer);
1173 im->timer.data=(unsigned long)im;
1174 im->timer.function=&igmp_timer_expire;
1175 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1180 write_lock_bh(&in_dev->mc_list_lock);
1181 im->next=in_dev->mc_list;
1183 write_unlock_bh(&in_dev->mc_list_lock);
1184 #ifdef CONFIG_IP_MULTICAST
1185 igmpv3_del_delrec(in_dev, im->multiaddr);
1187 igmp_group_added(im);
1189 ip_rt_multicast_event(in_dev);
1195 * A socket has left a multicast group on device dev
1198 void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1200 struct ip_mc_list *i, **ip;
1204 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1205 if (i->multiaddr==addr) {
1206 if (--i->users == 0) {
1207 write_lock_bh(&in_dev->mc_list_lock);
1209 write_unlock_bh(&in_dev->mc_list_lock);
1210 igmp_group_dropped(i);
1213 ip_rt_multicast_event(in_dev);
1223 /* Device going down */
1225 void ip_mc_down(struct in_device *in_dev)
1227 struct ip_mc_list *i;
1231 for (i=in_dev->mc_list; i; i=i->next)
1232 igmp_group_dropped(i);
1234 #ifdef CONFIG_IP_MULTICAST
1235 in_dev->mr_ifc_count = 0;
1236 if (del_timer(&in_dev->mr_ifc_timer))
1237 __in_dev_put(in_dev);
1238 in_dev->mr_gq_running = 0;
1239 if (del_timer(&in_dev->mr_gq_timer))
1240 __in_dev_put(in_dev);
1241 igmpv3_clear_delrec(in_dev);
1244 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1247 void ip_mc_init_dev(struct in_device *in_dev)
1251 in_dev->mc_tomb = NULL;
1252 #ifdef CONFIG_IP_MULTICAST
1253 in_dev->mr_gq_running = 0;
1254 init_timer(&in_dev->mr_gq_timer);
1255 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1256 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1257 in_dev->mr_ifc_count = 0;
1258 init_timer(&in_dev->mr_ifc_timer);
1259 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1260 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1261 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1264 rwlock_init(&in_dev->mc_list_lock);
1265 spin_lock_init(&in_dev->mc_tomb_lock);
1268 /* Device going up */
1270 void ip_mc_up(struct in_device *in_dev)
1272 struct ip_mc_list *i;
1276 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1278 for (i=in_dev->mc_list; i; i=i->next)
1279 igmp_group_added(i);
1283 * Device is about to be destroyed: clean up.
1286 void ip_mc_destroy_dev(struct in_device *in_dev)
1288 struct ip_mc_list *i;
1292 /* Deactivate timers */
1295 write_lock_bh(&in_dev->mc_list_lock);
1296 while ((i = in_dev->mc_list) != NULL) {
1297 in_dev->mc_list = i->next;
1298 write_unlock_bh(&in_dev->mc_list_lock);
1300 igmp_group_dropped(i);
1303 write_lock_bh(&in_dev->mc_list_lock);
1305 write_unlock_bh(&in_dev->mc_list_lock);
1308 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1310 struct flowi fl = { .nl_u = { .ip4_u =
1311 { .daddr = imr->imr_multiaddr.s_addr } } };
1313 struct net_device *dev = NULL;
1314 struct in_device *idev = NULL;
1316 if (imr->imr_ifindex) {
1317 idev = inetdev_by_index(imr->imr_ifindex);
1322 if (imr->imr_address.s_addr) {
1323 dev = ip_dev_find(imr->imr_address.s_addr);
1329 if (!dev && !ip_route_output_key(&rt, &fl)) {
1330 dev = rt->u.dst.dev;
1334 imr->imr_ifindex = dev->ifindex;
1335 idev = __in_dev_get_rtnl(dev);
1341 * Join a socket to a group
1343 int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1344 int sysctl_igmp_max_msf = IP_MAX_MSF;
1347 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1350 struct ip_sf_list *psf, *psf_prev;
1354 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1355 if (psf->sf_inaddr == *psfsrc)
1359 if (!psf || psf->sf_count[sfmode] == 0) {
1360 /* source filter not found, or count wrong => bug */
1363 psf->sf_count[sfmode]--;
1364 if (psf->sf_count[sfmode] == 0) {
1365 ip_rt_multicast_event(pmc->interface);
1367 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1368 #ifdef CONFIG_IP_MULTICAST
1369 struct in_device *in_dev = pmc->interface;
1372 /* no more filters for this source */
1374 psf_prev->sf_next = psf->sf_next;
1376 pmc->sources = psf->sf_next;
1377 #ifdef CONFIG_IP_MULTICAST
1378 if (psf->sf_oldin &&
1379 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1380 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1381 IGMP_Unsolicited_Report_Count;
1382 psf->sf_next = pmc->tomb;
1392 #ifndef CONFIG_IP_MULTICAST
1393 #define igmp_ifc_event(x) do { } while (0)
1396 static int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1397 int sfcount, __u32 *psfsrc, int delta)
1399 struct ip_mc_list *pmc;
1405 read_lock(&in_dev->mc_list_lock);
1406 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1407 if (*pmca == pmc->multiaddr)
1411 /* MCA not found?? bug */
1412 read_unlock(&in_dev->mc_list_lock);
1415 spin_lock_bh(&pmc->lock);
1416 read_unlock(&in_dev->mc_list_lock);
1417 #ifdef CONFIG_IP_MULTICAST
1422 if (!pmc->sfcount[sfmode])
1424 pmc->sfcount[sfmode]--;
1427 for (i=0; i<sfcount; i++) {
1428 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1430 changerec |= rv > 0;
1434 if (pmc->sfmode == MCAST_EXCLUDE &&
1435 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1436 pmc->sfcount[MCAST_INCLUDE]) {
1437 #ifdef CONFIG_IP_MULTICAST
1438 struct ip_sf_list *psf;
1441 /* filter mode change */
1442 pmc->sfmode = MCAST_INCLUDE;
1443 #ifdef CONFIG_IP_MULTICAST
1444 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1445 IGMP_Unsolicited_Report_Count;
1446 in_dev->mr_ifc_count = pmc->crcount;
1447 for (psf=pmc->sources; psf; psf = psf->sf_next)
1448 psf->sf_crcount = 0;
1449 igmp_ifc_event(pmc->interface);
1450 } else if (sf_setstate(pmc) || changerec) {
1451 igmp_ifc_event(pmc->interface);
1455 spin_unlock_bh(&pmc->lock);
1460 * Add multicast single-source filter to the interface list
1462 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1463 __u32 *psfsrc, int delta)
1465 struct ip_sf_list *psf, *psf_prev;
1468 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1469 if (psf->sf_inaddr == *psfsrc)
1474 psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1477 memset(psf, 0, sizeof(*psf));
1478 psf->sf_inaddr = *psfsrc;
1480 psf_prev->sf_next = psf;
1484 psf->sf_count[sfmode]++;
1485 if (psf->sf_count[sfmode] == 1) {
1486 ip_rt_multicast_event(pmc->interface);
1491 #ifdef CONFIG_IP_MULTICAST
1492 static void sf_markstate(struct ip_mc_list *pmc)
1494 struct ip_sf_list *psf;
1495 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1497 for (psf=pmc->sources; psf; psf=psf->sf_next)
1498 if (pmc->sfcount[MCAST_EXCLUDE]) {
1499 psf->sf_oldin = mca_xcount ==
1500 psf->sf_count[MCAST_EXCLUDE] &&
1501 !psf->sf_count[MCAST_INCLUDE];
1503 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1506 static int sf_setstate(struct ip_mc_list *pmc)
1508 struct ip_sf_list *psf;
1509 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1510 int qrv = pmc->interface->mr_qrv;
1514 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1515 if (pmc->sfcount[MCAST_EXCLUDE]) {
1516 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1517 !psf->sf_count[MCAST_INCLUDE];
1519 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1520 if (new_in != psf->sf_oldin) {
1521 psf->sf_crcount = qrv;
1530 * Add multicast source filter list to the interface list
1532 static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1533 int sfcount, __u32 *psfsrc, int delta)
1535 struct ip_mc_list *pmc;
1541 read_lock(&in_dev->mc_list_lock);
1542 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1543 if (*pmca == pmc->multiaddr)
1547 /* MCA not found?? bug */
1548 read_unlock(&in_dev->mc_list_lock);
1551 spin_lock_bh(&pmc->lock);
1552 read_unlock(&in_dev->mc_list_lock);
1554 #ifdef CONFIG_IP_MULTICAST
1557 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1559 pmc->sfcount[sfmode]++;
1561 for (i=0; i<sfcount; i++) {
1562 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1569 pmc->sfcount[sfmode]--;
1571 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1572 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1573 #ifdef CONFIG_IP_MULTICAST
1574 struct in_device *in_dev = pmc->interface;
1575 struct ip_sf_list *psf;
1578 /* filter mode change */
1579 if (pmc->sfcount[MCAST_EXCLUDE])
1580 pmc->sfmode = MCAST_EXCLUDE;
1581 else if (pmc->sfcount[MCAST_INCLUDE])
1582 pmc->sfmode = MCAST_INCLUDE;
1583 #ifdef CONFIG_IP_MULTICAST
1584 /* else no filters; keep old mode for reports */
1586 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1587 IGMP_Unsolicited_Report_Count;
1588 in_dev->mr_ifc_count = pmc->crcount;
1589 for (psf=pmc->sources; psf; psf = psf->sf_next)
1590 psf->sf_crcount = 0;
1591 igmp_ifc_event(in_dev);
1592 } else if (sf_setstate(pmc)) {
1593 igmp_ifc_event(in_dev);
1596 spin_unlock_bh(&pmc->lock);
1600 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1602 struct ip_sf_list *psf, *nextpsf;
1604 for (psf=pmc->tomb; psf; psf=nextpsf) {
1605 nextpsf = psf->sf_next;
1609 for (psf=pmc->sources; psf; psf=nextpsf) {
1610 nextpsf = psf->sf_next;
1613 pmc->sources = NULL;
1614 pmc->sfmode = MCAST_EXCLUDE;
1615 pmc->sfcount[MCAST_INCLUDE] = 0;
1616 pmc->sfcount[MCAST_EXCLUDE] = 1;
1621 * Join a multicast group
1623 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1626 u32 addr = imr->imr_multiaddr.s_addr;
1627 struct ip_mc_socklist *iml=NULL, *i;
1628 struct in_device *in_dev;
1629 struct inet_sock *inet = inet_sk(sk);
1633 if (!MULTICAST(addr))
1638 in_dev = ip_mc_find_dev(imr);
1647 ifindex = imr->imr_ifindex;
1648 for (i = inet->mc_list; i; i = i->next) {
1649 if (i->multi.imr_multiaddr.s_addr == addr &&
1650 i->multi.imr_ifindex == ifindex)
1655 if (count >= sysctl_igmp_max_memberships)
1657 iml = (struct ip_mc_socklist *)sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1661 memcpy(&iml->multi, imr, sizeof(*imr));
1662 iml->next = inet->mc_list;
1664 iml->sfmode = MCAST_EXCLUDE;
1665 inet->mc_list = iml;
1666 ip_mc_inc_group(in_dev, addr);
1673 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1674 struct in_device *in_dev)
1678 if (iml->sflist == 0) {
1679 /* any-source empty exclude case */
1680 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1681 iml->sfmode, 0, NULL, 0);
1683 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1684 iml->sfmode, iml->sflist->sl_count,
1685 iml->sflist->sl_addr, 0);
1686 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1692 * Ask a socket to leave a group.
1695 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1697 struct inet_sock *inet = inet_sk(sk);
1698 struct ip_mc_socklist *iml, **imlp;
1699 struct in_device *in_dev;
1700 u32 group = imr->imr_multiaddr.s_addr;
1704 in_dev = ip_mc_find_dev(imr);
1709 ifindex = imr->imr_ifindex;
1710 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1711 if (iml->multi.imr_multiaddr.s_addr == group &&
1712 iml->multi.imr_ifindex == ifindex) {
1713 (void) ip_mc_leave_src(sk, iml, in_dev);
1717 ip_mc_dec_group(in_dev, group);
1719 sock_kfree_s(sk, iml, sizeof(*iml));
1724 return -EADDRNOTAVAIL;
1727 int ip_mc_source(int add, int omode, struct sock *sk, struct
1728 ip_mreq_source *mreqs, int ifindex)
1731 struct ip_mreqn imr;
1732 u32 addr = mreqs->imr_multiaddr;
1733 struct ip_mc_socklist *pmc;
1734 struct in_device *in_dev = NULL;
1735 struct inet_sock *inet = inet_sk(sk);
1736 struct ip_sf_socklist *psl;
1740 if (!MULTICAST(addr))
1745 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1746 imr.imr_address.s_addr = mreqs->imr_interface;
1747 imr.imr_ifindex = ifindex;
1748 in_dev = ip_mc_find_dev(&imr);
1754 err = -EADDRNOTAVAIL;
1756 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1757 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1758 && pmc->multi.imr_ifindex == imr.imr_ifindex)
1761 if (!pmc) { /* must have a prior join */
1765 /* if a source filter was set, must be the same mode as before */
1767 if (pmc->sfmode != omode) {
1771 } else if (pmc->sfmode != omode) {
1772 /* allow mode switches for empty-set filters */
1773 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1774 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1776 pmc->sfmode = omode;
1782 goto done; /* err = -EADDRNOTAVAIL */
1784 for (i=0; i<psl->sl_count; i++) {
1785 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1790 if (rv) /* source not found */
1791 goto done; /* err = -EADDRNOTAVAIL */
1793 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1794 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1799 /* update the interface filter */
1800 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1801 &mreqs->imr_sourceaddr, 1);
1803 for (j=i+1; j<psl->sl_count; j++)
1804 psl->sl_addr[j-1] = psl->sl_addr[j];
1809 /* else, add a new source to the filter */
1811 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1815 if (!psl || psl->sl_count == psl->sl_max) {
1816 struct ip_sf_socklist *newpsl;
1817 int count = IP_SFBLOCK;
1820 count += psl->sl_max;
1821 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1822 IP_SFLSIZE(count), GFP_KERNEL);
1827 newpsl->sl_max = count;
1828 newpsl->sl_count = count - IP_SFBLOCK;
1830 for (i=0; i<psl->sl_count; i++)
1831 newpsl->sl_addr[i] = psl->sl_addr[i];
1832 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1834 pmc->sflist = psl = newpsl;
1836 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1837 for (i=0; i<psl->sl_count; i++) {
1838 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1843 if (rv == 0) /* address already there is an error */
1845 for (j=psl->sl_count-1; j>=i; j--)
1846 psl->sl_addr[j+1] = psl->sl_addr[j];
1847 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1850 /* update the interface list */
1851 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1852 &mreqs->imr_sourceaddr, 1);
1856 return ip_mc_leave_group(sk, &imr);
1860 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1863 struct ip_mreqn imr;
1864 u32 addr = msf->imsf_multiaddr;
1865 struct ip_mc_socklist *pmc;
1866 struct in_device *in_dev;
1867 struct inet_sock *inet = inet_sk(sk);
1868 struct ip_sf_socklist *newpsl, *psl;
1871 if (!MULTICAST(addr))
1873 if (msf->imsf_fmode != MCAST_INCLUDE &&
1874 msf->imsf_fmode != MCAST_EXCLUDE)
1879 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1880 imr.imr_address.s_addr = msf->imsf_interface;
1881 imr.imr_ifindex = ifindex;
1882 in_dev = ip_mc_find_dev(&imr);
1889 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1890 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
1895 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1896 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1897 pmc->multi.imr_ifindex == imr.imr_ifindex)
1900 if (!pmc) { /* must have a prior join */
1904 if (msf->imsf_numsrc) {
1905 newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1906 IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1911 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1912 memcpy(newpsl->sl_addr, msf->imsf_slist,
1913 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1914 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1915 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1917 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1922 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1923 msf->imsf_fmode, 0, NULL, 0);
1927 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1928 psl->sl_count, psl->sl_addr, 0);
1929 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1931 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1933 pmc->sflist = newpsl;
1934 pmc->sfmode = msf->imsf_fmode;
1939 err = ip_mc_leave_group(sk, &imr);
1943 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1944 struct ip_msfilter __user *optval, int __user *optlen)
1946 int err, len, count, copycount;
1947 struct ip_mreqn imr;
1948 u32 addr = msf->imsf_multiaddr;
1949 struct ip_mc_socklist *pmc;
1950 struct in_device *in_dev;
1951 struct inet_sock *inet = inet_sk(sk);
1952 struct ip_sf_socklist *psl;
1954 if (!MULTICAST(addr))
1959 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1960 imr.imr_address.s_addr = msf->imsf_interface;
1961 imr.imr_ifindex = 0;
1962 in_dev = ip_mc_find_dev(&imr);
1968 err = -EADDRNOTAVAIL;
1970 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1971 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1972 pmc->multi.imr_ifindex == imr.imr_ifindex)
1975 if (!pmc) /* must have a prior join */
1977 msf->imsf_fmode = pmc->sfmode;
1984 count = psl->sl_count;
1986 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1987 len = copycount * sizeof(psl->sl_addr[0]);
1988 msf->imsf_numsrc = count;
1989 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1990 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
1994 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2002 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2003 struct group_filter __user *optval, int __user *optlen)
2005 int err, i, count, copycount;
2006 struct sockaddr_in *psin;
2008 struct ip_mc_socklist *pmc;
2009 struct inet_sock *inet = inet_sk(sk);
2010 struct ip_sf_socklist *psl;
2012 psin = (struct sockaddr_in *)&gsf->gf_group;
2013 if (psin->sin_family != AF_INET)
2015 addr = psin->sin_addr.s_addr;
2016 if (!MULTICAST(addr))
2021 err = -EADDRNOTAVAIL;
2023 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2024 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2025 pmc->multi.imr_ifindex == gsf->gf_interface)
2028 if (!pmc) /* must have a prior join */
2030 gsf->gf_fmode = pmc->sfmode;
2033 count = psl ? psl->sl_count : 0;
2034 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2035 gsf->gf_numsrc = count;
2036 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2037 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2040 for (i=0; i<copycount; i++) {
2041 struct sockaddr_in *psin;
2042 struct sockaddr_storage ss;
2044 psin = (struct sockaddr_in *)&ss;
2045 memset(&ss, 0, sizeof(ss));
2046 psin->sin_family = AF_INET;
2047 psin->sin_addr.s_addr = psl->sl_addr[i];
2048 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2058 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2060 int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2062 struct inet_sock *inet = inet_sk(sk);
2063 struct ip_mc_socklist *pmc;
2064 struct ip_sf_socklist *psl;
2067 if (!MULTICAST(loc_addr))
2070 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2071 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2072 pmc->multi.imr_ifindex == dif)
2079 return pmc->sfmode == MCAST_EXCLUDE;
2081 for (i=0; i<psl->sl_count; i++) {
2082 if (psl->sl_addr[i] == rmt_addr)
2085 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2087 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2093 * A socket is closing.
2096 void ip_mc_drop_socket(struct sock *sk)
2098 struct inet_sock *inet = inet_sk(sk);
2099 struct ip_mc_socklist *iml;
2101 if (inet->mc_list == NULL)
2105 while ((iml = inet->mc_list) != NULL) {
2106 struct in_device *in_dev;
2107 inet->mc_list = iml->next;
2109 if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2110 (void) ip_mc_leave_src(sk, iml, in_dev);
2111 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2114 sock_kfree_s(sk, iml, sizeof(*iml));
2120 int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2122 struct ip_mc_list *im;
2123 struct ip_sf_list *psf;
2126 read_lock(&in_dev->mc_list_lock);
2127 for (im=in_dev->mc_list; im; im=im->next) {
2128 if (im->multiaddr == mc_addr)
2131 if (im && proto == IPPROTO_IGMP) {
2135 for (psf=im->sources; psf; psf=psf->sf_next) {
2136 if (psf->sf_inaddr == src_addr)
2140 rv = psf->sf_count[MCAST_INCLUDE] ||
2141 psf->sf_count[MCAST_EXCLUDE] !=
2142 im->sfcount[MCAST_EXCLUDE];
2144 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2146 rv = 1; /* unspecified source; tentatively allow */
2148 read_unlock(&in_dev->mc_list_lock);
2152 #if defined(CONFIG_PROC_FS)
2153 struct igmp_mc_iter_state {
2154 struct net_device *dev;
2155 struct in_device *in_dev;
2158 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2160 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2162 struct ip_mc_list *im = NULL;
2163 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2165 for (state->dev = dev_base, state->in_dev = NULL;
2167 state->dev = state->dev->next) {
2168 struct in_device *in_dev;
2169 in_dev = in_dev_get(state->dev);
2172 read_lock(&in_dev->mc_list_lock);
2173 im = in_dev->mc_list;
2175 state->in_dev = in_dev;
2178 read_unlock(&in_dev->mc_list_lock);
2184 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2186 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2189 if (likely(state->in_dev != NULL)) {
2190 read_unlock(&state->in_dev->mc_list_lock);
2191 in_dev_put(state->in_dev);
2193 state->dev = state->dev->next;
2195 state->in_dev = NULL;
2198 state->in_dev = in_dev_get(state->dev);
2201 read_lock(&state->in_dev->mc_list_lock);
2202 im = state->in_dev->mc_list;
2207 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2209 struct ip_mc_list *im = igmp_mc_get_first(seq);
2211 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2213 return pos ? NULL : im;
2216 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2218 read_lock(&dev_base_lock);
2219 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2222 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2224 struct ip_mc_list *im;
2225 if (v == SEQ_START_TOKEN)
2226 im = igmp_mc_get_first(seq);
2228 im = igmp_mc_get_next(seq, v);
2233 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2235 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2236 if (likely(state->in_dev != NULL)) {
2237 read_unlock(&state->in_dev->mc_list_lock);
2238 in_dev_put(state->in_dev);
2239 state->in_dev = NULL;
2242 read_unlock(&dev_base_lock);
2245 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2247 if (v == SEQ_START_TOKEN)
2249 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2251 struct ip_mc_list *im = (struct ip_mc_list *)v;
2252 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2254 #ifdef CONFIG_IP_MULTICAST
2255 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2256 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2262 if (state->in_dev->mc_list == im) {
2263 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2264 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2268 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2269 im->multiaddr, im->users,
2270 im->tm_running, im->tm_running ?
2271 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2277 static struct seq_operations igmp_mc_seq_ops = {
2278 .start = igmp_mc_seq_start,
2279 .next = igmp_mc_seq_next,
2280 .stop = igmp_mc_seq_stop,
2281 .show = igmp_mc_seq_show,
2284 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2286 struct seq_file *seq;
2288 struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2292 rc = seq_open(file, &igmp_mc_seq_ops);
2296 seq = file->private_data;
2298 memset(s, 0, sizeof(*s));
2306 static struct file_operations igmp_mc_seq_fops = {
2307 .owner = THIS_MODULE,
2308 .open = igmp_mc_seq_open,
2310 .llseek = seq_lseek,
2311 .release = seq_release_private,
2314 struct igmp_mcf_iter_state {
2315 struct net_device *dev;
2316 struct in_device *idev;
2317 struct ip_mc_list *im;
2320 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2322 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2324 struct ip_sf_list *psf = NULL;
2325 struct ip_mc_list *im = NULL;
2326 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2328 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2330 state->dev = state->dev->next) {
2331 struct in_device *idev;
2332 idev = in_dev_get(state->dev);
2333 if (unlikely(idev == NULL))
2335 read_lock(&idev->mc_list_lock);
2337 if (likely(im != NULL)) {
2338 spin_lock_bh(&im->lock);
2340 if (likely(psf != NULL)) {
2345 spin_unlock_bh(&im->lock);
2347 read_unlock(&idev->mc_list_lock);
2353 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2355 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2359 spin_unlock_bh(&state->im->lock);
2360 state->im = state->im->next;
2361 while (!state->im) {
2362 if (likely(state->idev != NULL)) {
2363 read_unlock(&state->idev->mc_list_lock);
2364 in_dev_put(state->idev);
2366 state->dev = state->dev->next;
2371 state->idev = in_dev_get(state->dev);
2374 read_lock(&state->idev->mc_list_lock);
2375 state->im = state->idev->mc_list;
2379 spin_lock_bh(&state->im->lock);
2380 psf = state->im->sources;
2386 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2388 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2390 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2392 return pos ? NULL : psf;
2395 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2397 read_lock(&dev_base_lock);
2398 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2401 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2403 struct ip_sf_list *psf;
2404 if (v == SEQ_START_TOKEN)
2405 psf = igmp_mcf_get_first(seq);
2407 psf = igmp_mcf_get_next(seq, v);
2412 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2414 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2415 if (likely(state->im != NULL)) {
2416 spin_unlock_bh(&state->im->lock);
2419 if (likely(state->idev != NULL)) {
2420 read_unlock(&state->idev->mc_list_lock);
2421 in_dev_put(state->idev);
2425 read_unlock(&dev_base_lock);
2428 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2430 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2431 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2433 if (v == SEQ_START_TOKEN) {
2436 "%10s %10s %6s %6s\n", "Idx",
2438 "SRC", "INC", "EXC");
2442 "0x%08x %6lu %6lu\n",
2443 state->dev->ifindex, state->dev->name,
2444 ntohl(state->im->multiaddr),
2445 ntohl(psf->sf_inaddr),
2446 psf->sf_count[MCAST_INCLUDE],
2447 psf->sf_count[MCAST_EXCLUDE]);
2452 static struct seq_operations igmp_mcf_seq_ops = {
2453 .start = igmp_mcf_seq_start,
2454 .next = igmp_mcf_seq_next,
2455 .stop = igmp_mcf_seq_stop,
2456 .show = igmp_mcf_seq_show,
2459 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2461 struct seq_file *seq;
2463 struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2467 rc = seq_open(file, &igmp_mcf_seq_ops);
2471 seq = file->private_data;
2473 memset(s, 0, sizeof(*s));
2481 static struct file_operations igmp_mcf_seq_fops = {
2482 .owner = THIS_MODULE,
2483 .open = igmp_mcf_seq_open,
2485 .llseek = seq_lseek,
2486 .release = seq_release_private,
2489 int __init igmp_mc_proc_init(void)
2491 proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2492 proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2497 EXPORT_SYMBOL(ip_mc_dec_group);
2498 EXPORT_SYMBOL(ip_mc_inc_group);
2499 EXPORT_SYMBOL(ip_mc_join_group);