2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
11 * Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
14 * Alan Cox <Alan.Cox@linux.org>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
23 * Alan Cox : Added lots of __inline__ to optimise
24 * the memory usage of all the tiny little
26 * Alan Cox : Dumped the header building experiment.
27 * Alan Cox : Minor tweaks ready for multicast routing
28 * and extended IGMP protocol.
29 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
30 * writes utterly bogus code otherwise (sigh)
31 * fixed IGMP loopback to behave in the manner
32 * desired by mrouted, fixed the fact it has been
33 * broken since 1.3.6 and cleaned up a few minor
36 * Chih-Jen Chang : Tried to revise IGMP to Version 2
37 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
38 * The enhancements are mainly based on Steve Deering's
39 * ipmulti-3.5 source code.
40 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
41 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
42 * the mrouted version on that device.
43 * Chih-Jen Chang : Added the max_resp_time parameter to
44 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
45 * to identify the multicast router version
46 * and do what the IGMP version 2 specified.
47 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
48 * Tsu-Sheng Tsao if the specified time expired.
49 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
50 * Alan Cox : Use GFP_ATOMIC in the right places.
51 * Christian Daudt : igmp timer wasn't set for local group
52 * memberships but was being deleted,
53 * which caused a "del_timer() called
54 * from %p with timer not initialized\n"
56 * Christian Daudt : removed del_timer from
57 * igmp_timer_expire function (960205).
58 * Christian Daudt : igmp_heard_report now only calls
59 * igmp_timer_expire if tm->running is
61 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
62 * igmp_heard_query never trigger. Expiry
63 * miscalculation fixed in igmp_heard_query
64 * and random() made to return unsigned to
65 * prevent negative expiry times.
66 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
67 * fix from pending 2.1.x patches.
68 * Alan Cox: Forget to enable FDDI support earlier.
69 * Alexey Kuznetsov: Fixed leaving groups on device down.
70 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
71 * David L Stevens: IGMPv3 support, with help from
75 #include <linux/config.h>
76 #include <linux/module.h>
77 #include <asm/uaccess.h>
78 #include <asm/system.h>
79 #include <linux/types.h>
80 #include <linux/kernel.h>
81 #include <linux/jiffies.h>
82 #include <linux/string.h>
83 #include <linux/socket.h>
84 #include <linux/sockios.h>
86 #include <linux/inet.h>
87 #include <linux/netdevice.h>
88 #include <linux/skbuff.h>
89 #include <linux/inetdevice.h>
90 #include <linux/igmp.h>
91 #include <linux/if_arp.h>
92 #include <linux/rtnetlink.h>
93 #include <linux/times.h>
97 #include <net/protocol.h>
98 #include <net/route.h>
100 #include <net/checksum.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
110 #define IP_MAX_MEMBERSHIPS 20
111 #define IP_MAX_MSF 10
113 #ifdef CONFIG_IP_MULTICAST
114 /* Parameter names and values are taken from igmp-v2-06 draft */
116 #define IGMP_V1_Router_Present_Timeout (400*HZ)
117 #define IGMP_V2_Router_Present_Timeout (400*HZ)
118 #define IGMP_Unsolicited_Report_Interval (10*HZ)
119 #define IGMP_Query_Response_Interval (10*HZ)
120 #define IGMP_Unsolicited_Report_Count 2
123 #define IGMP_Initial_Report_Delay (1)
125 /* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
132 #define IGMP_V1_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 1 || \
133 (in_dev)->cnf.force_igmp_version == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 2 || \
137 (in_dev)->cnf.force_igmp_version == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
141 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
142 static void igmpv3_del_delrec(struct in_device
*in_dev
, __u32 multiaddr
);
143 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
144 static int sf_setstate(struct ip_mc_list
*pmc
);
145 static void sf_markstate(struct ip_mc_list
*pmc
);
147 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
148 static int ip_mc_add_src(struct in_device
*in_dev
, __u32
*pmca
, int sfmode
,
149 int sfcount
, __u32
*psfsrc
, int delta
);
151 static void ip_ma_put(struct ip_mc_list
*im
)
153 if (atomic_dec_and_test(&im
->refcnt
)) {
154 in_dev_put(im
->interface
);
159 #ifdef CONFIG_IP_MULTICAST
165 static __inline__
void igmp_stop_timer(struct ip_mc_list
*im
)
167 spin_lock_bh(&im
->lock
);
168 if (del_timer(&im
->timer
))
169 atomic_dec(&im
->refcnt
);
172 im
->unsolicit_count
= 0;
173 spin_unlock_bh(&im
->lock
);
176 /* It must be called with locked im->lock */
177 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
179 int tv
=net_random() % max_delay
;
182 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
183 atomic_inc(&im
->refcnt
);
186 static void igmp_gq_start_timer(struct in_device
*in_dev
)
188 int tv
= net_random() % in_dev
->mr_maxdelay
;
190 in_dev
->mr_gq_running
= 1;
191 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
195 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
197 int tv
= net_random() % delay
;
199 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
203 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
205 spin_lock_bh(&im
->lock
);
206 im
->unsolicit_count
= 0;
207 if (del_timer(&im
->timer
)) {
208 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
209 add_timer(&im
->timer
);
211 spin_unlock_bh(&im
->lock
);
214 atomic_dec(&im
->refcnt
);
216 igmp_start_timer(im
, max_delay
);
217 spin_unlock_bh(&im
->lock
);
222 * Send an IGMP report.
225 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
228 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
229 int gdeleted
, int sdeleted
)
232 case IGMPV3_MODE_IS_INCLUDE
:
233 case IGMPV3_MODE_IS_EXCLUDE
:
234 if (gdeleted
|| sdeleted
)
236 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
237 if (pmc
->sfmode
== MCAST_INCLUDE
)
239 /* don't include if this source is excluded
242 if (psf
->sf_count
[MCAST_INCLUDE
])
243 return type
== IGMPV3_MODE_IS_INCLUDE
;
244 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
245 psf
->sf_count
[MCAST_EXCLUDE
];
248 case IGMPV3_CHANGE_TO_INCLUDE
:
249 if (gdeleted
|| sdeleted
)
251 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
252 case IGMPV3_CHANGE_TO_EXCLUDE
:
253 if (gdeleted
|| sdeleted
)
255 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
256 psf
->sf_count
[MCAST_INCLUDE
])
258 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
259 psf
->sf_count
[MCAST_EXCLUDE
];
260 case IGMPV3_ALLOW_NEW_SOURCES
:
261 if (gdeleted
|| !psf
->sf_crcount
)
263 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
264 case IGMPV3_BLOCK_OLD_SOURCES
:
265 if (pmc
->sfmode
== MCAST_INCLUDE
)
266 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
267 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
273 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
275 struct ip_sf_list
*psf
;
278 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
279 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
286 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, int size
)
291 struct igmpv3_report
*pig
;
293 skb
= alloc_skb(size
+ LL_RESERVED_SPACE(dev
), GFP_ATOMIC
);
298 struct flowi fl
= { .oif
= dev
->ifindex
,
300 .daddr
= IGMPV3_ALL_MCR
} },
301 .proto
= IPPROTO_IGMP
};
302 if (ip_route_output_key(&rt
, &fl
)) {
307 if (rt
->rt_src
== 0) {
313 skb
->dst
= &rt
->u
.dst
;
316 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
318 skb
->nh
.iph
= pip
=(struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
)+4);
321 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
323 pip
->frag_off
= htons(IP_DF
);
325 pip
->daddr
= rt
->rt_dst
;
326 pip
->saddr
= rt
->rt_src
;
327 pip
->protocol
= IPPROTO_IGMP
;
328 pip
->tot_len
= 0; /* filled in later */
329 ip_select_ident(pip
, &rt
->u
.dst
, NULL
);
330 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
331 ((u8
*)&pip
[1])[1] = 4;
332 ((u8
*)&pip
[1])[2] = 0;
333 ((u8
*)&pip
[1])[3] = 0;
335 pig
=(struct igmpv3_report
*)skb_put(skb
, sizeof(*pig
));
336 skb
->h
.igmph
= (struct igmphdr
*)pig
;
337 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
345 static int igmpv3_sendpack(struct sk_buff
*skb
)
347 struct iphdr
*pip
= skb
->nh
.iph
;
348 struct igmphdr
*pig
= skb
->h
.igmph
;
351 iplen
= skb
->tail
- (unsigned char *)skb
->nh
.iph
;
352 pip
->tot_len
= htons(iplen
);
355 igmplen
= skb
->tail
- (unsigned char *)skb
->h
.igmph
;
356 pig
->csum
= ip_compute_csum((void *)skb
->h
.igmph
, igmplen
);
358 return NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
362 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
364 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
,type
,gdel
,sdel
);
367 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
368 int type
, struct igmpv3_grec
**ppgr
)
370 struct net_device
*dev
= pmc
->interface
->dev
;
371 struct igmpv3_report
*pih
;
372 struct igmpv3_grec
*pgr
;
375 skb
= igmpv3_newpack(dev
, dev
->mtu
);
378 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
379 pgr
->grec_type
= type
;
380 pgr
->grec_auxwords
= 0;
382 pgr
->grec_mca
= pmc
->multiaddr
;
383 pih
= (struct igmpv3_report
*)skb
->h
.igmph
;
384 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
389 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
390 skb_tailroom(skb)) : 0)
392 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
393 int type
, int gdeleted
, int sdeleted
)
395 struct net_device
*dev
= pmc
->interface
->dev
;
396 struct igmpv3_report
*pih
;
397 struct igmpv3_grec
*pgr
= NULL
;
398 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
399 int scount
, stotal
, first
, isquery
, truncate
;
401 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
404 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
405 type
== IGMPV3_MODE_IS_EXCLUDE
;
406 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
407 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
411 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
416 pih
= skb
? (struct igmpv3_report
*)skb
->h
.igmph
: NULL
;
418 /* EX and TO_EX get a fresh packet, if needed */
420 if (pih
&& pih
->ngrec
&&
421 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
423 igmpv3_sendpack(skb
);
424 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
;
479 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
480 type
== IGMPV3_BLOCK_OLD_SOURCES
)
482 if (pmc
->crcount
|| isquery
) {
483 /* make sure we have room for group header */
484 if (skb
&& AVAILABLE(skb
)<sizeof(struct igmpv3_grec
)) {
485 igmpv3_sendpack(skb
);
486 skb
= NULL
; /* add_grhead will get a new one */
488 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
492 pgr
->grec_nsrcs
= htons(scount
);
495 pmc
->gsquery
= 0; /* clear query state on report */
499 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
501 struct sk_buff
*skb
= NULL
;
505 read_lock(&in_dev
->mc_list_lock
);
506 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
507 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
509 spin_lock_bh(&pmc
->lock
);
510 if (pmc
->sfcount
[MCAST_EXCLUDE
])
511 type
= IGMPV3_MODE_IS_EXCLUDE
;
513 type
= IGMPV3_MODE_IS_INCLUDE
;
514 skb
= add_grec(skb
, pmc
, type
, 0, 0);
515 spin_unlock_bh(&pmc
->lock
);
517 read_unlock(&in_dev
->mc_list_lock
);
519 spin_lock_bh(&pmc
->lock
);
520 if (pmc
->sfcount
[MCAST_EXCLUDE
])
521 type
= IGMPV3_MODE_IS_EXCLUDE
;
523 type
= IGMPV3_MODE_IS_INCLUDE
;
524 skb
= add_grec(skb
, pmc
, type
, 0, 0);
525 spin_unlock_bh(&pmc
->lock
);
529 return igmpv3_sendpack(skb
);
533 * remove zero-count source records from a source filter list
535 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
537 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
540 for (psf
=*ppsf
; psf
; psf
= psf_next
) {
541 psf_next
= psf
->sf_next
;
542 if (psf
->sf_crcount
== 0) {
544 psf_prev
->sf_next
= psf
->sf_next
;
546 *ppsf
= psf
->sf_next
;
553 static void igmpv3_send_cr(struct in_device
*in_dev
)
555 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
556 struct sk_buff
*skb
= NULL
;
559 read_lock(&in_dev
->mc_list_lock
);
560 spin_lock_bh(&in_dev
->mc_tomb_lock
);
564 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc_next
) {
565 pmc_next
= pmc
->next
;
566 if (pmc
->sfmode
== MCAST_INCLUDE
) {
567 type
= IGMPV3_BLOCK_OLD_SOURCES
;
568 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
569 skb
= add_grec(skb
, pmc
, type
, 1, 0);
570 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
573 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
574 type
= IGMPV3_CHANGE_TO_INCLUDE
;
575 skb
= add_grec(skb
, pmc
, type
, 1, 0);
578 if (pmc
->crcount
== 0) {
579 igmpv3_clear_zeros(&pmc
->tomb
);
580 igmpv3_clear_zeros(&pmc
->sources
);
583 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
585 pmc_prev
->next
= pmc_next
;
587 in_dev
->mc_tomb
= pmc_next
;
588 in_dev_put(pmc
->interface
);
593 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
596 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
597 spin_lock_bh(&pmc
->lock
);
598 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
599 type
= IGMPV3_BLOCK_OLD_SOURCES
;
600 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
602 type
= IGMPV3_ALLOW_NEW_SOURCES
;
603 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
605 skb
= add_grec(skb
, pmc
, type
, 0, 0);
606 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
608 /* filter mode changes */
610 if (pmc
->sfmode
== MCAST_EXCLUDE
)
611 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
613 type
= IGMPV3_CHANGE_TO_INCLUDE
;
614 skb
= add_grec(skb
, pmc
, type
, 0, 0);
617 spin_unlock_bh(&pmc
->lock
);
619 read_unlock(&in_dev
->mc_list_lock
);
623 (void) igmpv3_sendpack(skb
);
626 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
633 struct net_device
*dev
= in_dev
->dev
;
634 u32 group
= pmc
? pmc
->multiaddr
: 0;
637 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
638 return igmpv3_send_report(in_dev
, pmc
);
639 else if (type
== IGMP_HOST_LEAVE_MESSAGE
)
640 dst
= IGMP_ALL_ROUTER
;
645 struct flowi fl
= { .oif
= dev
->ifindex
,
646 .nl_u
= { .ip4_u
= { .daddr
= dst
} },
647 .proto
= IPPROTO_IGMP
};
648 if (ip_route_output_key(&rt
, &fl
))
651 if (rt
->rt_src
== 0) {
656 skb
=alloc_skb(IGMP_SIZE
+LL_RESERVED_SPACE(dev
), GFP_ATOMIC
);
662 skb
->dst
= &rt
->u
.dst
;
664 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
666 skb
->nh
.iph
= iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
)+4);
669 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
671 iph
->frag_off
= htons(IP_DF
);
674 iph
->saddr
= rt
->rt_src
;
675 iph
->protocol
= IPPROTO_IGMP
;
676 iph
->tot_len
= htons(IGMP_SIZE
);
677 ip_select_ident(iph
, &rt
->u
.dst
, NULL
);
678 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
679 ((u8
*)&iph
[1])[1] = 4;
680 ((u8
*)&iph
[1])[2] = 0;
681 ((u8
*)&iph
[1])[3] = 0;
684 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
689 ih
->csum
=ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
691 return NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->u
.dst
.dev
,
695 static void igmp_gq_timer_expire(unsigned long data
)
697 struct in_device
*in_dev
= (struct in_device
*)data
;
699 in_dev
->mr_gq_running
= 0;
700 igmpv3_send_report(in_dev
, NULL
);
701 __in_dev_put(in_dev
);
704 static void igmp_ifc_timer_expire(unsigned long data
)
706 struct in_device
*in_dev
= (struct in_device
*)data
;
708 igmpv3_send_cr(in_dev
);
709 if (in_dev
->mr_ifc_count
) {
710 in_dev
->mr_ifc_count
--;
711 igmp_ifc_start_timer(in_dev
, IGMP_Unsolicited_Report_Interval
);
713 __in_dev_put(in_dev
);
716 static void igmp_ifc_event(struct in_device
*in_dev
)
718 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
720 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
721 IGMP_Unsolicited_Report_Count
;
722 igmp_ifc_start_timer(in_dev
, 1);
726 static void igmp_timer_expire(unsigned long data
)
728 struct ip_mc_list
*im
=(struct ip_mc_list
*)data
;
729 struct in_device
*in_dev
= im
->interface
;
731 spin_lock(&im
->lock
);
734 if (im
->unsolicit_count
) {
735 im
->unsolicit_count
--;
736 igmp_start_timer(im
, IGMP_Unsolicited_Report_Interval
);
739 spin_unlock(&im
->lock
);
741 if (IGMP_V1_SEEN(in_dev
))
742 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
743 else if (IGMP_V2_SEEN(in_dev
))
744 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
746 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
751 /* mark EXCLUDE-mode sources */
752 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __u32
*srcs
)
754 struct ip_sf_list
*psf
;
758 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
761 for (i
=0; i
<nsrcs
; i
++) {
762 /* skip inactive filters */
763 if (pmc
->sfcount
[MCAST_INCLUDE
] ||
764 pmc
->sfcount
[MCAST_EXCLUDE
] !=
765 psf
->sf_count
[MCAST_EXCLUDE
])
767 if (srcs
[i
] == psf
->sf_inaddr
) {
774 if (scount
== nsrcs
) /* all sources excluded */
779 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __u32
*srcs
)
781 struct ip_sf_list
*psf
;
784 if (pmc
->sfmode
== MCAST_EXCLUDE
)
785 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
787 /* mark INCLUDE-mode sources */
789 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
792 for (i
=0; i
<nsrcs
; i
++)
793 if (srcs
[i
] == psf
->sf_inaddr
) {
807 static void igmp_heard_report(struct in_device
*in_dev
, u32 group
)
809 struct ip_mc_list
*im
;
811 /* Timers are only set for non-local groups */
813 if (group
== IGMP_ALL_HOSTS
)
816 read_lock(&in_dev
->mc_list_lock
);
817 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
818 if (im
->multiaddr
== group
) {
823 read_unlock(&in_dev
->mc_list_lock
);
826 static void igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
829 struct igmphdr
*ih
= skb
->h
.igmph
;
830 struct igmpv3_query
*ih3
= (struct igmpv3_query
*)ih
;
831 struct ip_mc_list
*im
;
832 u32 group
= ih
->group
;
839 /* Alas, old v1 router presents here. */
841 max_delay
= IGMP_Query_Response_Interval
;
842 in_dev
->mr_v1_seen
= jiffies
+
843 IGMP_V1_Router_Present_Timeout
;
846 /* v2 router present */
847 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
848 in_dev
->mr_v2_seen
= jiffies
+
849 IGMP_V2_Router_Present_Timeout
;
851 /* cancel the interface change timer */
852 in_dev
->mr_ifc_count
= 0;
853 if (del_timer(&in_dev
->mr_ifc_timer
))
854 __in_dev_put(in_dev
);
855 /* clear deleted report items */
856 igmpv3_clear_delrec(in_dev
);
857 } else if (len
< 12) {
858 return; /* ignore bogus packet; freed by caller */
860 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
863 ih3
= (struct igmpv3_query
*) skb
->h
.raw
;
865 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
866 + ntohs(ih3
->nsrcs
)*sizeof(__u32
)))
868 ih3
= (struct igmpv3_query
*) skb
->h
.raw
;
871 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
873 max_delay
= 1; /* can't mod w/ 0 */
874 in_dev
->mr_maxdelay
= max_delay
;
876 in_dev
->mr_qrv
= ih3
->qrv
;
877 if (!group
) { /* general query */
879 return; /* no sources allowed */
880 igmp_gq_start_timer(in_dev
);
883 /* mark sources to include, if group & source-specific */
884 mark
= ih3
->nsrcs
!= 0;
888 * - Start the timers in all of our membership records
889 * that the query applies to for the interface on
890 * which the query arrived excl. those that belong
891 * to a "local" group (224.0.0.X)
892 * - For timers already running check if they need to
894 * - Use the igmp->igmp_code field as the maximum
897 read_lock(&in_dev
->mc_list_lock
);
898 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
901 if (group
&& group
!= im
->multiaddr
)
903 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
905 spin_lock_bh(&im
->lock
);
907 im
->gsquery
= im
->gsquery
&& mark
;
910 changed
= !im
->gsquery
||
911 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
912 spin_unlock_bh(&im
->lock
);
914 igmp_mod_timer(im
, max_delay
);
916 read_unlock(&in_dev
->mc_list_lock
);
919 int igmp_rcv(struct sk_buff
*skb
)
921 /* This basically follows the spec line by line -- see RFC1112 */
923 struct in_device
*in_dev
= in_dev_get(skb
->dev
);
931 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
934 switch (skb
->ip_summed
) {
936 if (!(u16
)csum_fold(skb
->csum
))
941 if (__skb_checksum_complete(skb
))
947 case IGMP_HOST_MEMBERSHIP_QUERY
:
948 igmp_heard_query(in_dev
, skb
, len
);
950 case IGMP_HOST_MEMBERSHIP_REPORT
:
951 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
952 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
953 /* Is it our report looped back? */
954 if (((struct rtable
*)skb
->dst
)->fl
.iif
== 0)
956 /* don't rely on MC router hearing unicast reports */
957 if (skb
->pkt_type
== PACKET_MULTICAST
||
958 skb
->pkt_type
== PACKET_BROADCAST
)
959 igmp_heard_report(in_dev
, ih
->group
);
962 #ifdef CONFIG_IP_PIMSM_V1
964 return pim_rcv_v1(skb
);
968 case IGMP_HOST_LEAVE_MESSAGE
:
970 case IGMP_MTRACE_RESP
:
985 * Add a filter to a device
988 static void ip_mc_filter_add(struct in_device
*in_dev
, u32 addr
)
990 char buf
[MAX_ADDR_LEN
];
991 struct net_device
*dev
= in_dev
->dev
;
993 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
994 We will get multicast token leakage, when IFF_MULTICAST
995 is changed. This check should be done in dev->set_multicast_list
996 routine. Something sort of:
997 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1000 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1001 dev_mc_add(dev
,buf
,dev
->addr_len
,0);
1005 * Remove a filter from a device
1008 static void ip_mc_filter_del(struct in_device
*in_dev
, u32 addr
)
1010 char buf
[MAX_ADDR_LEN
];
1011 struct net_device
*dev
= in_dev
->dev
;
1013 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1014 dev_mc_delete(dev
,buf
,dev
->addr_len
,0);
1017 #ifdef CONFIG_IP_MULTICAST
1019 * deleted ip_mc_list manipulation
1021 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1023 struct ip_mc_list
*pmc
;
1025 /* this is an "ip_mc_list" for convenience; only the fields below
1026 * are actually used. In particular, the refcnt and users are not
1027 * used for management of the delete list. Using the same structure
1028 * for deleted items allows change reports to use common code with
1029 * non-deleted or query-response MCA's.
1031 pmc
= kmalloc(sizeof(*pmc
), GFP_KERNEL
);
1034 memset(pmc
, 0, sizeof(*pmc
));
1035 spin_lock_bh(&im
->lock
);
1036 pmc
->interface
= im
->interface
;
1037 in_dev_hold(in_dev
);
1038 pmc
->multiaddr
= im
->multiaddr
;
1039 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1040 IGMP_Unsolicited_Report_Count
;
1041 pmc
->sfmode
= im
->sfmode
;
1042 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1043 struct ip_sf_list
*psf
;
1045 pmc
->tomb
= im
->tomb
;
1046 pmc
->sources
= im
->sources
;
1047 im
->tomb
= im
->sources
= NULL
;
1048 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1049 psf
->sf_crcount
= pmc
->crcount
;
1051 spin_unlock_bh(&im
->lock
);
1053 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1054 pmc
->next
= in_dev
->mc_tomb
;
1055 in_dev
->mc_tomb
= pmc
;
1056 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1059 static void igmpv3_del_delrec(struct in_device
*in_dev
, __u32 multiaddr
)
1061 struct ip_mc_list
*pmc
, *pmc_prev
;
1062 struct ip_sf_list
*psf
, *psf_next
;
1064 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1066 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
1067 if (pmc
->multiaddr
== multiaddr
)
1073 pmc_prev
->next
= pmc
->next
;
1075 in_dev
->mc_tomb
= pmc
->next
;
1077 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1079 for (psf
=pmc
->tomb
; psf
; psf
=psf_next
) {
1080 psf_next
= psf
->sf_next
;
1083 in_dev_put(pmc
->interface
);
1088 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1090 struct ip_mc_list
*pmc
, *nextpmc
;
1092 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1093 pmc
= in_dev
->mc_tomb
;
1094 in_dev
->mc_tomb
= NULL
;
1095 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1097 for (; pmc
; pmc
= nextpmc
) {
1098 nextpmc
= pmc
->next
;
1099 ip_mc_clear_src(pmc
);
1100 in_dev_put(pmc
->interface
);
1103 /* clear dead sources, too */
1104 read_lock(&in_dev
->mc_list_lock
);
1105 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1106 struct ip_sf_list
*psf
, *psf_next
;
1108 spin_lock_bh(&pmc
->lock
);
1111 spin_unlock_bh(&pmc
->lock
);
1112 for (; psf
; psf
=psf_next
) {
1113 psf_next
= psf
->sf_next
;
1117 read_unlock(&in_dev
->mc_list_lock
);
1121 static void igmp_group_dropped(struct ip_mc_list
*im
)
1123 struct in_device
*in_dev
= im
->interface
;
1124 #ifdef CONFIG_IP_MULTICAST
1130 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1133 #ifdef CONFIG_IP_MULTICAST
1134 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1137 reporter
= im
->reporter
;
1138 igmp_stop_timer(im
);
1140 if (!in_dev
->dead
) {
1141 if (IGMP_V1_SEEN(in_dev
))
1143 if (IGMP_V2_SEEN(in_dev
)) {
1145 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1149 igmpv3_add_delrec(in_dev
, im
);
1151 igmp_ifc_event(in_dev
);
1155 ip_mc_clear_src(im
);
1158 static void igmp_group_added(struct ip_mc_list
*im
)
1160 struct in_device
*in_dev
= im
->interface
;
1162 if (im
->loaded
== 0) {
1164 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1167 #ifdef CONFIG_IP_MULTICAST
1168 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1173 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1174 spin_lock_bh(&im
->lock
);
1175 igmp_start_timer(im
, IGMP_Initial_Report_Delay
);
1176 spin_unlock_bh(&im
->lock
);
1181 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1182 IGMP_Unsolicited_Report_Count
;
1183 igmp_ifc_event(in_dev
);
1189 * Multicast list managers
1194 * A socket has joined a multicast group on device dev.
1197 void ip_mc_inc_group(struct in_device
*in_dev
, u32 addr
)
1199 struct ip_mc_list
*im
;
1203 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
1204 if (im
->multiaddr
== addr
) {
1206 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1211 im
= kmalloc(sizeof(*im
), GFP_KERNEL
);
1216 im
->interface
=in_dev
;
1217 in_dev_hold(in_dev
);
1219 /* initial mode is (EX, empty) */
1220 im
->sfmode
= MCAST_EXCLUDE
;
1221 im
->sfcount
[MCAST_INCLUDE
] = 0;
1222 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1226 atomic_set(&im
->refcnt
, 1);
1227 spin_lock_init(&im
->lock
);
1228 #ifdef CONFIG_IP_MULTICAST
1230 init_timer(&im
->timer
);
1231 im
->timer
.data
=(unsigned long)im
;
1232 im
->timer
.function
=&igmp_timer_expire
;
1233 im
->unsolicit_count
= IGMP_Unsolicited_Report_Count
;
1238 write_lock_bh(&in_dev
->mc_list_lock
);
1239 im
->next
=in_dev
->mc_list
;
1241 write_unlock_bh(&in_dev
->mc_list_lock
);
1242 #ifdef CONFIG_IP_MULTICAST
1243 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1245 igmp_group_added(im
);
1247 ip_rt_multicast_event(in_dev
);
1253 * A socket has left a multicast group on device dev
1256 void ip_mc_dec_group(struct in_device
*in_dev
, u32 addr
)
1258 struct ip_mc_list
*i
, **ip
;
1262 for (ip
=&in_dev
->mc_list
; (i
=*ip
)!=NULL
; ip
=&i
->next
) {
1263 if (i
->multiaddr
==addr
) {
1264 if (--i
->users
== 0) {
1265 write_lock_bh(&in_dev
->mc_list_lock
);
1267 write_unlock_bh(&in_dev
->mc_list_lock
);
1268 igmp_group_dropped(i
);
1271 ip_rt_multicast_event(in_dev
);
1281 /* Device going down */
1283 void ip_mc_down(struct in_device
*in_dev
)
1285 struct ip_mc_list
*i
;
1289 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1290 igmp_group_dropped(i
);
1292 #ifdef CONFIG_IP_MULTICAST
1293 in_dev
->mr_ifc_count
= 0;
1294 if (del_timer(&in_dev
->mr_ifc_timer
))
1295 __in_dev_put(in_dev
);
1296 in_dev
->mr_gq_running
= 0;
1297 if (del_timer(&in_dev
->mr_gq_timer
))
1298 __in_dev_put(in_dev
);
1299 igmpv3_clear_delrec(in_dev
);
1302 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1305 void ip_mc_init_dev(struct in_device
*in_dev
)
1309 in_dev
->mc_tomb
= NULL
;
1310 #ifdef CONFIG_IP_MULTICAST
1311 in_dev
->mr_gq_running
= 0;
1312 init_timer(&in_dev
->mr_gq_timer
);
1313 in_dev
->mr_gq_timer
.data
=(unsigned long) in_dev
;
1314 in_dev
->mr_gq_timer
.function
=&igmp_gq_timer_expire
;
1315 in_dev
->mr_ifc_count
= 0;
1316 init_timer(&in_dev
->mr_ifc_timer
);
1317 in_dev
->mr_ifc_timer
.data
=(unsigned long) in_dev
;
1318 in_dev
->mr_ifc_timer
.function
=&igmp_ifc_timer_expire
;
1319 in_dev
->mr_qrv
= IGMP_Unsolicited_Report_Count
;
1322 rwlock_init(&in_dev
->mc_list_lock
);
1323 spin_lock_init(&in_dev
->mc_tomb_lock
);
1326 /* Device going up */
1328 void ip_mc_up(struct in_device
*in_dev
)
1330 struct ip_mc_list
*i
;
1334 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1336 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1337 igmp_group_added(i
);
1341 * Device is about to be destroyed: clean up.
1344 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1346 struct ip_mc_list
*i
;
1350 /* Deactivate timers */
1353 write_lock_bh(&in_dev
->mc_list_lock
);
1354 while ((i
= in_dev
->mc_list
) != NULL
) {
1355 in_dev
->mc_list
= i
->next
;
1356 write_unlock_bh(&in_dev
->mc_list_lock
);
1358 igmp_group_dropped(i
);
1361 write_lock_bh(&in_dev
->mc_list_lock
);
1363 write_unlock_bh(&in_dev
->mc_list_lock
);
1366 static struct in_device
* ip_mc_find_dev(struct ip_mreqn
*imr
)
1368 struct flowi fl
= { .nl_u
= { .ip4_u
=
1369 { .daddr
= imr
->imr_multiaddr
.s_addr
} } };
1371 struct net_device
*dev
= NULL
;
1372 struct in_device
*idev
= NULL
;
1374 if (imr
->imr_ifindex
) {
1375 idev
= inetdev_by_index(imr
->imr_ifindex
);
1380 if (imr
->imr_address
.s_addr
) {
1381 dev
= ip_dev_find(imr
->imr_address
.s_addr
);
1387 if (!dev
&& !ip_route_output_key(&rt
, &fl
)) {
1388 dev
= rt
->u
.dst
.dev
;
1392 imr
->imr_ifindex
= dev
->ifindex
;
1393 idev
= __in_dev_get_rtnl(dev
);
1399 * Join a socket to a group
1401 int sysctl_igmp_max_memberships
= IP_MAX_MEMBERSHIPS
;
1402 int sysctl_igmp_max_msf
= IP_MAX_MSF
;
1405 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1408 struct ip_sf_list
*psf
, *psf_prev
;
1412 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1413 if (psf
->sf_inaddr
== *psfsrc
)
1417 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1418 /* source filter not found, or count wrong => bug */
1421 psf
->sf_count
[sfmode
]--;
1422 if (psf
->sf_count
[sfmode
] == 0) {
1423 ip_rt_multicast_event(pmc
->interface
);
1425 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1426 #ifdef CONFIG_IP_MULTICAST
1427 struct in_device
*in_dev
= pmc
->interface
;
1430 /* no more filters for this source */
1432 psf_prev
->sf_next
= psf
->sf_next
;
1434 pmc
->sources
= psf
->sf_next
;
1435 #ifdef CONFIG_IP_MULTICAST
1436 if (psf
->sf_oldin
&&
1437 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1438 psf
->sf_crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1439 IGMP_Unsolicited_Report_Count
;
1440 psf
->sf_next
= pmc
->tomb
;
1450 #ifndef CONFIG_IP_MULTICAST
1451 #define igmp_ifc_event(x) do { } while (0)
1454 static int ip_mc_del_src(struct in_device
*in_dev
, __u32
*pmca
, int sfmode
,
1455 int sfcount
, __u32
*psfsrc
, int delta
)
1457 struct ip_mc_list
*pmc
;
1463 read_lock(&in_dev
->mc_list_lock
);
1464 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1465 if (*pmca
== pmc
->multiaddr
)
1469 /* MCA not found?? bug */
1470 read_unlock(&in_dev
->mc_list_lock
);
1473 spin_lock_bh(&pmc
->lock
);
1474 read_unlock(&in_dev
->mc_list_lock
);
1475 #ifdef CONFIG_IP_MULTICAST
1480 if (!pmc
->sfcount
[sfmode
])
1482 pmc
->sfcount
[sfmode
]--;
1485 for (i
=0; i
<sfcount
; i
++) {
1486 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1488 changerec
|= rv
> 0;
1492 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1493 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1494 pmc
->sfcount
[MCAST_INCLUDE
]) {
1495 #ifdef CONFIG_IP_MULTICAST
1496 struct ip_sf_list
*psf
;
1499 /* filter mode change */
1500 pmc
->sfmode
= MCAST_INCLUDE
;
1501 #ifdef CONFIG_IP_MULTICAST
1502 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1503 IGMP_Unsolicited_Report_Count
;
1504 in_dev
->mr_ifc_count
= pmc
->crcount
;
1505 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1506 psf
->sf_crcount
= 0;
1507 igmp_ifc_event(pmc
->interface
);
1508 } else if (sf_setstate(pmc
) || changerec
) {
1509 igmp_ifc_event(pmc
->interface
);
1513 spin_unlock_bh(&pmc
->lock
);
1518 * Add multicast single-source filter to the interface list
1520 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1521 __u32
*psfsrc
, int delta
)
1523 struct ip_sf_list
*psf
, *psf_prev
;
1526 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1527 if (psf
->sf_inaddr
== *psfsrc
)
1532 psf
= kmalloc(sizeof(*psf
), GFP_ATOMIC
);
1535 memset(psf
, 0, sizeof(*psf
));
1536 psf
->sf_inaddr
= *psfsrc
;
1538 psf_prev
->sf_next
= psf
;
1542 psf
->sf_count
[sfmode
]++;
1543 if (psf
->sf_count
[sfmode
] == 1) {
1544 ip_rt_multicast_event(pmc
->interface
);
1549 #ifdef CONFIG_IP_MULTICAST
1550 static void sf_markstate(struct ip_mc_list
*pmc
)
1552 struct ip_sf_list
*psf
;
1553 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1555 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1556 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1557 psf
->sf_oldin
= mca_xcount
==
1558 psf
->sf_count
[MCAST_EXCLUDE
] &&
1559 !psf
->sf_count
[MCAST_INCLUDE
];
1561 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1564 static int sf_setstate(struct ip_mc_list
*pmc
)
1566 struct ip_sf_list
*psf
, *dpsf
;
1567 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1568 int qrv
= pmc
->interface
->mr_qrv
;
1572 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1573 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1574 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1575 !psf
->sf_count
[MCAST_INCLUDE
];
1577 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1579 if (!psf
->sf_oldin
) {
1580 struct ip_sf_list
*prev
= 0;
1582 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
) {
1583 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1589 prev
->sf_next
= dpsf
->sf_next
;
1591 pmc
->tomb
= dpsf
->sf_next
;
1594 psf
->sf_crcount
= qrv
;
1597 } else if (psf
->sf_oldin
) {
1599 psf
->sf_crcount
= 0;
1601 * add or update "delete" records if an active filter
1604 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
)
1605 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1608 dpsf
= (struct ip_sf_list
*)
1609 kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1613 /* pmc->lock held by callers */
1614 dpsf
->sf_next
= pmc
->tomb
;
1617 dpsf
->sf_crcount
= qrv
;
1626 * Add multicast source filter list to the interface list
1628 static int ip_mc_add_src(struct in_device
*in_dev
, __u32
*pmca
, int sfmode
,
1629 int sfcount
, __u32
*psfsrc
, int delta
)
1631 struct ip_mc_list
*pmc
;
1637 read_lock(&in_dev
->mc_list_lock
);
1638 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1639 if (*pmca
== pmc
->multiaddr
)
1643 /* MCA not found?? bug */
1644 read_unlock(&in_dev
->mc_list_lock
);
1647 spin_lock_bh(&pmc
->lock
);
1648 read_unlock(&in_dev
->mc_list_lock
);
1650 #ifdef CONFIG_IP_MULTICAST
1653 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1655 pmc
->sfcount
[sfmode
]++;
1657 for (i
=0; i
<sfcount
; i
++) {
1658 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1665 pmc
->sfcount
[sfmode
]--;
1667 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1668 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1669 #ifdef CONFIG_IP_MULTICAST
1670 struct in_device
*in_dev
= pmc
->interface
;
1671 struct ip_sf_list
*psf
;
1674 /* filter mode change */
1675 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1676 pmc
->sfmode
= MCAST_EXCLUDE
;
1677 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1678 pmc
->sfmode
= MCAST_INCLUDE
;
1679 #ifdef CONFIG_IP_MULTICAST
1680 /* else no filters; keep old mode for reports */
1682 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1683 IGMP_Unsolicited_Report_Count
;
1684 in_dev
->mr_ifc_count
= pmc
->crcount
;
1685 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1686 psf
->sf_crcount
= 0;
1687 igmp_ifc_event(in_dev
);
1688 } else if (sf_setstate(pmc
)) {
1689 igmp_ifc_event(in_dev
);
1692 spin_unlock_bh(&pmc
->lock
);
1696 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1698 struct ip_sf_list
*psf
, *nextpsf
;
1700 for (psf
=pmc
->tomb
; psf
; psf
=nextpsf
) {
1701 nextpsf
= psf
->sf_next
;
1705 for (psf
=pmc
->sources
; psf
; psf
=nextpsf
) {
1706 nextpsf
= psf
->sf_next
;
1709 pmc
->sources
= NULL
;
1710 pmc
->sfmode
= MCAST_EXCLUDE
;
1711 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1712 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1717 * Join a multicast group
1719 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1722 u32 addr
= imr
->imr_multiaddr
.s_addr
;
1723 struct ip_mc_socklist
*iml
=NULL
, *i
;
1724 struct in_device
*in_dev
;
1725 struct inet_sock
*inet
= inet_sk(sk
);
1729 if (!MULTICAST(addr
))
1734 in_dev
= ip_mc_find_dev(imr
);
1743 ifindex
= imr
->imr_ifindex
;
1744 for (i
= inet
->mc_list
; i
; i
= i
->next
) {
1745 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1746 i
->multi
.imr_ifindex
== ifindex
)
1751 if (count
>= sysctl_igmp_max_memberships
)
1753 iml
= sock_kmalloc(sk
,sizeof(*iml
),GFP_KERNEL
);
1757 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1758 iml
->next
= inet
->mc_list
;
1760 iml
->sfmode
= MCAST_EXCLUDE
;
1761 inet
->mc_list
= iml
;
1762 ip_mc_inc_group(in_dev
, addr
);
1769 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1770 struct in_device
*in_dev
)
1774 if (iml
->sflist
== 0) {
1775 /* any-source empty exclude case */
1776 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1777 iml
->sfmode
, 0, NULL
, 0);
1779 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1780 iml
->sfmode
, iml
->sflist
->sl_count
,
1781 iml
->sflist
->sl_addr
, 0);
1782 sock_kfree_s(sk
, iml
->sflist
, IP_SFLSIZE(iml
->sflist
->sl_max
));
1788 * Ask a socket to leave a group.
1791 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1793 struct inet_sock
*inet
= inet_sk(sk
);
1794 struct ip_mc_socklist
*iml
, **imlp
;
1795 struct in_device
*in_dev
;
1796 u32 group
= imr
->imr_multiaddr
.s_addr
;
1800 in_dev
= ip_mc_find_dev(imr
);
1805 ifindex
= imr
->imr_ifindex
;
1806 for (imlp
= &inet
->mc_list
; (iml
= *imlp
) != NULL
; imlp
= &iml
->next
) {
1807 if (iml
->multi
.imr_multiaddr
.s_addr
== group
&&
1808 iml
->multi
.imr_ifindex
== ifindex
) {
1809 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1813 ip_mc_dec_group(in_dev
, group
);
1815 sock_kfree_s(sk
, iml
, sizeof(*iml
));
1820 return -EADDRNOTAVAIL
;
1823 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1824 ip_mreq_source
*mreqs
, int ifindex
)
1827 struct ip_mreqn imr
;
1828 u32 addr
= mreqs
->imr_multiaddr
;
1829 struct ip_mc_socklist
*pmc
;
1830 struct in_device
*in_dev
= NULL
;
1831 struct inet_sock
*inet
= inet_sk(sk
);
1832 struct ip_sf_socklist
*psl
;
1836 if (!MULTICAST(addr
))
1841 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1842 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1843 imr
.imr_ifindex
= ifindex
;
1844 in_dev
= ip_mc_find_dev(&imr
);
1850 err
= -EADDRNOTAVAIL
;
1852 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1853 if (pmc
->multi
.imr_multiaddr
.s_addr
== imr
.imr_multiaddr
.s_addr
1854 && pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
1857 if (!pmc
) { /* must have a prior join */
1861 /* if a source filter was set, must be the same mode as before */
1863 if (pmc
->sfmode
!= omode
) {
1867 } else if (pmc
->sfmode
!= omode
) {
1868 /* allow mode switches for empty-set filters */
1869 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
1870 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
1872 pmc
->sfmode
= omode
;
1878 goto done
; /* err = -EADDRNOTAVAIL */
1880 for (i
=0; i
<psl
->sl_count
; i
++) {
1881 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1886 if (rv
) /* source not found */
1887 goto done
; /* err = -EADDRNOTAVAIL */
1889 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1890 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
1895 /* update the interface filter */
1896 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1897 &mreqs
->imr_sourceaddr
, 1);
1899 for (j
=i
+1; j
<psl
->sl_count
; j
++)
1900 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
1905 /* else, add a new source to the filter */
1907 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
1911 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
1912 struct ip_sf_socklist
*newpsl
;
1913 int count
= IP_SFBLOCK
;
1916 count
+= psl
->sl_max
;
1917 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
1922 newpsl
->sl_max
= count
;
1923 newpsl
->sl_count
= count
- IP_SFBLOCK
;
1925 for (i
=0; i
<psl
->sl_count
; i
++)
1926 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
1927 sock_kfree_s(sk
, psl
, IP_SFLSIZE(psl
->sl_max
));
1929 pmc
->sflist
= psl
= newpsl
;
1931 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
1932 for (i
=0; i
<psl
->sl_count
; i
++) {
1933 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1938 if (rv
== 0) /* address already there is an error */
1940 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
1941 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
1942 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
1945 /* update the interface list */
1946 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1947 &mreqs
->imr_sourceaddr
, 1);
1951 return ip_mc_leave_group(sk
, &imr
);
1955 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
1958 struct ip_mreqn imr
;
1959 u32 addr
= msf
->imsf_multiaddr
;
1960 struct ip_mc_socklist
*pmc
;
1961 struct in_device
*in_dev
;
1962 struct inet_sock
*inet
= inet_sk(sk
);
1963 struct ip_sf_socklist
*newpsl
, *psl
;
1966 if (!MULTICAST(addr
))
1968 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
1969 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
1974 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
1975 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
1976 imr
.imr_ifindex
= ifindex
;
1977 in_dev
= ip_mc_find_dev(&imr
);
1984 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1985 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
1990 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1991 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
1992 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
1995 if (!pmc
) { /* must have a prior join */
1999 if (msf
->imsf_numsrc
) {
2000 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2006 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2007 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2008 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2009 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2010 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2012 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2017 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2018 msf
->imsf_fmode
, 0, NULL
, 0);
2022 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2023 psl
->sl_count
, psl
->sl_addr
, 0);
2024 sock_kfree_s(sk
, psl
, IP_SFLSIZE(psl
->sl_max
));
2026 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2028 pmc
->sflist
= newpsl
;
2029 pmc
->sfmode
= msf
->imsf_fmode
;
2034 err
= ip_mc_leave_group(sk
, &imr
);
2038 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2039 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2041 int err
, len
, count
, copycount
;
2042 struct ip_mreqn imr
;
2043 u32 addr
= msf
->imsf_multiaddr
;
2044 struct ip_mc_socklist
*pmc
;
2045 struct in_device
*in_dev
;
2046 struct inet_sock
*inet
= inet_sk(sk
);
2047 struct ip_sf_socklist
*psl
;
2049 if (!MULTICAST(addr
))
2054 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2055 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2056 imr
.imr_ifindex
= 0;
2057 in_dev
= ip_mc_find_dev(&imr
);
2063 err
= -EADDRNOTAVAIL
;
2065 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2066 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2067 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2070 if (!pmc
) /* must have a prior join */
2072 msf
->imsf_fmode
= pmc
->sfmode
;
2079 count
= psl
->sl_count
;
2081 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2082 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2083 msf
->imsf_numsrc
= count
;
2084 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2085 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2089 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2097 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2098 struct group_filter __user
*optval
, int __user
*optlen
)
2100 int err
, i
, count
, copycount
;
2101 struct sockaddr_in
*psin
;
2103 struct ip_mc_socklist
*pmc
;
2104 struct inet_sock
*inet
= inet_sk(sk
);
2105 struct ip_sf_socklist
*psl
;
2107 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2108 if (psin
->sin_family
!= AF_INET
)
2110 addr
= psin
->sin_addr
.s_addr
;
2111 if (!MULTICAST(addr
))
2116 err
= -EADDRNOTAVAIL
;
2118 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2119 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2120 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2123 if (!pmc
) /* must have a prior join */
2125 gsf
->gf_fmode
= pmc
->sfmode
;
2128 count
= psl
? psl
->sl_count
: 0;
2129 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2130 gsf
->gf_numsrc
= count
;
2131 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2132 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2135 for (i
=0; i
<copycount
; i
++) {
2136 struct sockaddr_in
*psin
;
2137 struct sockaddr_storage ss
;
2139 psin
= (struct sockaddr_in
*)&ss
;
2140 memset(&ss
, 0, sizeof(ss
));
2141 psin
->sin_family
= AF_INET
;
2142 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2143 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2153 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2155 int ip_mc_sf_allow(struct sock
*sk
, u32 loc_addr
, u32 rmt_addr
, int dif
)
2157 struct inet_sock
*inet
= inet_sk(sk
);
2158 struct ip_mc_socklist
*pmc
;
2159 struct ip_sf_socklist
*psl
;
2162 if (!MULTICAST(loc_addr
))
2165 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2166 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2167 pmc
->multi
.imr_ifindex
== dif
)
2174 return pmc
->sfmode
== MCAST_EXCLUDE
;
2176 for (i
=0; i
<psl
->sl_count
; i
++) {
2177 if (psl
->sl_addr
[i
] == rmt_addr
)
2180 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2182 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2188 * A socket is closing.
2191 void ip_mc_drop_socket(struct sock
*sk
)
2193 struct inet_sock
*inet
= inet_sk(sk
);
2194 struct ip_mc_socklist
*iml
;
2196 if (inet
->mc_list
== NULL
)
2200 while ((iml
= inet
->mc_list
) != NULL
) {
2201 struct in_device
*in_dev
;
2202 inet
->mc_list
= iml
->next
;
2204 if ((in_dev
= inetdev_by_index(iml
->multi
.imr_ifindex
)) != NULL
) {
2205 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2206 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2209 sock_kfree_s(sk
, iml
, sizeof(*iml
));
2215 int ip_check_mc(struct in_device
*in_dev
, u32 mc_addr
, u32 src_addr
, u16 proto
)
2217 struct ip_mc_list
*im
;
2218 struct ip_sf_list
*psf
;
2221 read_lock(&in_dev
->mc_list_lock
);
2222 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
2223 if (im
->multiaddr
== mc_addr
)
2226 if (im
&& proto
== IPPROTO_IGMP
) {
2230 for (psf
=im
->sources
; psf
; psf
=psf
->sf_next
) {
2231 if (psf
->sf_inaddr
== src_addr
)
2235 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2236 psf
->sf_count
[MCAST_EXCLUDE
] !=
2237 im
->sfcount
[MCAST_EXCLUDE
];
2239 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2241 rv
= 1; /* unspecified source; tentatively allow */
2243 read_unlock(&in_dev
->mc_list_lock
);
2247 #if defined(CONFIG_PROC_FS)
2248 struct igmp_mc_iter_state
{
2249 struct net_device
*dev
;
2250 struct in_device
*in_dev
;
2253 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2255 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2257 struct ip_mc_list
*im
= NULL
;
2258 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2260 for (state
->dev
= dev_base
, state
->in_dev
= NULL
;
2262 state
->dev
= state
->dev
->next
) {
2263 struct in_device
*in_dev
;
2264 in_dev
= in_dev_get(state
->dev
);
2267 read_lock(&in_dev
->mc_list_lock
);
2268 im
= in_dev
->mc_list
;
2270 state
->in_dev
= in_dev
;
2273 read_unlock(&in_dev
->mc_list_lock
);
2279 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2281 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2284 if (likely(state
->in_dev
!= NULL
)) {
2285 read_unlock(&state
->in_dev
->mc_list_lock
);
2286 in_dev_put(state
->in_dev
);
2288 state
->dev
= state
->dev
->next
;
2290 state
->in_dev
= NULL
;
2293 state
->in_dev
= in_dev_get(state
->dev
);
2296 read_lock(&state
->in_dev
->mc_list_lock
);
2297 im
= state
->in_dev
->mc_list
;
2302 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2304 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2306 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2308 return pos
? NULL
: im
;
2311 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2313 read_lock(&dev_base_lock
);
2314 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2317 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2319 struct ip_mc_list
*im
;
2320 if (v
== SEQ_START_TOKEN
)
2321 im
= igmp_mc_get_first(seq
);
2323 im
= igmp_mc_get_next(seq
, v
);
2328 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2330 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2331 if (likely(state
->in_dev
!= NULL
)) {
2332 read_unlock(&state
->in_dev
->mc_list_lock
);
2333 in_dev_put(state
->in_dev
);
2334 state
->in_dev
= NULL
;
2337 read_unlock(&dev_base_lock
);
2340 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2342 if (v
== SEQ_START_TOKEN
)
2344 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2346 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2347 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2349 #ifdef CONFIG_IP_MULTICAST
2350 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2351 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2357 if (state
->in_dev
->mc_list
== im
) {
2358 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2359 state
->dev
->ifindex
, state
->dev
->name
, state
->dev
->mc_count
, querier
);
2363 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2364 im
->multiaddr
, im
->users
,
2365 im
->tm_running
, im
->tm_running
?
2366 jiffies_to_clock_t(im
->timer
.expires
-jiffies
) : 0,
2372 static struct seq_operations igmp_mc_seq_ops
= {
2373 .start
= igmp_mc_seq_start
,
2374 .next
= igmp_mc_seq_next
,
2375 .stop
= igmp_mc_seq_stop
,
2376 .show
= igmp_mc_seq_show
,
2379 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2381 struct seq_file
*seq
;
2383 struct igmp_mc_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2387 rc
= seq_open(file
, &igmp_mc_seq_ops
);
2391 seq
= file
->private_data
;
2393 memset(s
, 0, sizeof(*s
));
2401 static struct file_operations igmp_mc_seq_fops
= {
2402 .owner
= THIS_MODULE
,
2403 .open
= igmp_mc_seq_open
,
2405 .llseek
= seq_lseek
,
2406 .release
= seq_release_private
,
2409 struct igmp_mcf_iter_state
{
2410 struct net_device
*dev
;
2411 struct in_device
*idev
;
2412 struct ip_mc_list
*im
;
2415 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2417 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2419 struct ip_sf_list
*psf
= NULL
;
2420 struct ip_mc_list
*im
= NULL
;
2421 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2423 for (state
->dev
= dev_base
, state
->idev
= NULL
, state
->im
= NULL
;
2425 state
->dev
= state
->dev
->next
) {
2426 struct in_device
*idev
;
2427 idev
= in_dev_get(state
->dev
);
2428 if (unlikely(idev
== NULL
))
2430 read_lock(&idev
->mc_list_lock
);
2432 if (likely(im
!= NULL
)) {
2433 spin_lock_bh(&im
->lock
);
2435 if (likely(psf
!= NULL
)) {
2440 spin_unlock_bh(&im
->lock
);
2442 read_unlock(&idev
->mc_list_lock
);
2448 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2450 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2454 spin_unlock_bh(&state
->im
->lock
);
2455 state
->im
= state
->im
->next
;
2456 while (!state
->im
) {
2457 if (likely(state
->idev
!= NULL
)) {
2458 read_unlock(&state
->idev
->mc_list_lock
);
2459 in_dev_put(state
->idev
);
2461 state
->dev
= state
->dev
->next
;
2466 state
->idev
= in_dev_get(state
->dev
);
2469 read_lock(&state
->idev
->mc_list_lock
);
2470 state
->im
= state
->idev
->mc_list
;
2474 spin_lock_bh(&state
->im
->lock
);
2475 psf
= state
->im
->sources
;
2481 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2483 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2485 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2487 return pos
? NULL
: psf
;
2490 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2492 read_lock(&dev_base_lock
);
2493 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2496 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2498 struct ip_sf_list
*psf
;
2499 if (v
== SEQ_START_TOKEN
)
2500 psf
= igmp_mcf_get_first(seq
);
2502 psf
= igmp_mcf_get_next(seq
, v
);
2507 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2509 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2510 if (likely(state
->im
!= NULL
)) {
2511 spin_unlock_bh(&state
->im
->lock
);
2514 if (likely(state
->idev
!= NULL
)) {
2515 read_unlock(&state
->idev
->mc_list_lock
);
2516 in_dev_put(state
->idev
);
2520 read_unlock(&dev_base_lock
);
2523 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2525 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2526 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2528 if (v
== SEQ_START_TOKEN
) {
2531 "%10s %10s %6s %6s\n", "Idx",
2533 "SRC", "INC", "EXC");
2537 "0x%08x %6lu %6lu\n",
2538 state
->dev
->ifindex
, state
->dev
->name
,
2539 ntohl(state
->im
->multiaddr
),
2540 ntohl(psf
->sf_inaddr
),
2541 psf
->sf_count
[MCAST_INCLUDE
],
2542 psf
->sf_count
[MCAST_EXCLUDE
]);
2547 static struct seq_operations igmp_mcf_seq_ops
= {
2548 .start
= igmp_mcf_seq_start
,
2549 .next
= igmp_mcf_seq_next
,
2550 .stop
= igmp_mcf_seq_stop
,
2551 .show
= igmp_mcf_seq_show
,
2554 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2556 struct seq_file
*seq
;
2558 struct igmp_mcf_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2562 rc
= seq_open(file
, &igmp_mcf_seq_ops
);
2566 seq
= file
->private_data
;
2568 memset(s
, 0, sizeof(*s
));
2576 static struct file_operations igmp_mcf_seq_fops
= {
2577 .owner
= THIS_MODULE
,
2578 .open
= igmp_mcf_seq_open
,
2580 .llseek
= seq_lseek
,
2581 .release
= seq_release_private
,
2584 int __init
igmp_mc_proc_init(void)
2586 proc_net_fops_create("igmp", S_IRUGO
, &igmp_mc_seq_fops
);
2587 proc_net_fops_create("mcfilter", S_IRUGO
, &igmp_mcf_seq_fops
);
2592 EXPORT_SYMBOL(ip_mc_dec_group
);
2593 EXPORT_SYMBOL(ip_mc_inc_group
);
2594 EXPORT_SYMBOL(ip_mc_join_group
);