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
76 #include <linux/config.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>
94 #include <net/protocol.h>
95 #include <net/route.h>
97 #include <net/checksum.h>
98 #include <linux/netfilter_ipv4.h>
99 #ifdef CONFIG_IP_MROUTE
100 #include <linux/mroute.h>
102 #ifdef CONFIG_PROC_FS
103 #include <linux/proc_fs.h>
104 #include <linux/seq_file.h>
107 #define IP_MAX_MEMBERSHIPS 20
109 #ifdef CONFIG_IP_MULTICAST
110 /* Parameter names and values are taken from igmp-v2-06 draft */
112 #define IGMP_V1_Router_Present_Timeout (400*HZ)
113 #define IGMP_V2_Router_Present_Timeout (400*HZ)
114 #define IGMP_Unsolicited_Report_Interval (10*HZ)
115 #define IGMP_Query_Response_Interval (10*HZ)
116 #define IGMP_Unsolicited_Report_Count 2
119 #define IGMP_Initial_Report_Delay (1)
121 /* IGMP_Initial_Report_Delay is not from IGMP specs!
122 * IGMP specs require to report membership immediately after
123 * joining a group, but we delay the first report by a
124 * small interval. It seems more natural and still does not
125 * contradict to specs provided this delay is small enough.
128 #define IGMP_V1_SEEN(in_dev) ((in_dev)->mr_v1_seen && \
129 time_before(jiffies, (in_dev)->mr_v1_seen))
130 #define IGMP_V2_SEEN(in_dev) ((in_dev)->mr_v2_seen && \
131 time_before(jiffies, (in_dev)->mr_v2_seen))
133 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
134 static void igmpv3_del_delrec(struct in_device
*in_dev
, __u32 multiaddr
);
135 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
136 static int sf_setstate(struct ip_mc_list
*pmc
);
137 static void sf_markstate(struct ip_mc_list
*pmc
);
139 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
140 int ip_mc_add_src(struct in_device
*in_dev
, __u32
*pmca
, int sfmode
,
141 int sfcount
, __u32
*psfsrc
, int delta
);
143 static void ip_ma_put(struct ip_mc_list
*im
)
145 if (atomic_dec_and_test(&im
->refcnt
)) {
146 in_dev_put(im
->interface
);
151 #ifdef CONFIG_IP_MULTICAST
157 static __inline__
void igmp_stop_timer(struct ip_mc_list
*im
)
159 spin_lock_bh(&im
->lock
);
160 if (del_timer(&im
->timer
))
161 atomic_dec(&im
->refcnt
);
164 im
->unsolicit_count
= 0;
165 spin_unlock_bh(&im
->lock
);
168 /* It must be called with locked im->lock */
169 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
171 int tv
=net_random() % max_delay
;
174 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
175 atomic_inc(&im
->refcnt
);
178 static void igmp_gq_start_timer(struct in_device
*in_dev
)
180 int tv
= net_random() % in_dev
->mr_maxdelay
;
182 in_dev
->mr_gq_running
= 1;
183 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
187 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
189 int tv
= net_random() % delay
;
191 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
195 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
197 spin_lock_bh(&im
->lock
);
198 im
->unsolicit_count
= 0;
199 if (del_timer(&im
->timer
)) {
200 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
201 add_timer(&im
->timer
);
203 spin_unlock_bh(&im
->lock
);
206 atomic_dec(&im
->refcnt
);
208 igmp_start_timer(im
, max_delay
);
209 spin_unlock_bh(&im
->lock
);
214 * Send an IGMP report.
217 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
220 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
221 int gdeleted
, int sdeleted
)
224 case IGMPV3_MODE_IS_INCLUDE
:
225 case IGMPV3_MODE_IS_EXCLUDE
:
226 if (gdeleted
|| sdeleted
)
228 return !(pmc
->gsquery
&& !psf
->sf_gsresp
);
229 case IGMPV3_CHANGE_TO_INCLUDE
:
230 if (gdeleted
|| sdeleted
)
232 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
233 case IGMPV3_CHANGE_TO_EXCLUDE
:
234 if (gdeleted
|| sdeleted
)
236 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
237 psf
->sf_count
[MCAST_INCLUDE
])
239 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
240 psf
->sf_count
[MCAST_EXCLUDE
];
241 case IGMPV3_ALLOW_NEW_SOURCES
:
242 if (gdeleted
|| !psf
->sf_crcount
)
244 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
245 case IGMPV3_BLOCK_OLD_SOURCES
:
246 if (pmc
->sfmode
== MCAST_INCLUDE
)
247 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
248 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
254 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
256 struct ip_sf_list
*psf
;
259 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
260 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
267 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, int size
)
272 struct igmpv3_report
*pig
;
274 skb
= alloc_skb(size
+ dev
->hard_header_len
+ 15, GFP_ATOMIC
);
279 struct flowi fl
= { .oif
= dev
->ifindex
,
281 .daddr
= IGMPV3_ALL_MCR
} },
282 .proto
= IPPROTO_IGMP
};
283 if (ip_route_output_key(&rt
, &fl
))
286 if (rt
->rt_src
== 0) {
291 skb
->dst
= &rt
->u
.dst
;
294 skb_reserve(skb
, (dev
->hard_header_len
+15)&~15);
296 skb
->nh
.iph
= pip
=(struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
)+4);
299 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
301 pip
->frag_off
= htons(IP_DF
);
303 pip
->daddr
= rt
->rt_dst
;
304 pip
->saddr
= rt
->rt_src
;
305 pip
->protocol
= IPPROTO_IGMP
;
306 pip
->tot_len
= 0; /* filled in later */
307 ip_select_ident(pip
, &rt
->u
.dst
, NULL
);
308 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
309 ((u8
*)&pip
[1])[1] = 4;
310 ((u8
*)&pip
[1])[2] = 0;
311 ((u8
*)&pip
[1])[3] = 0;
313 pig
=(struct igmpv3_report
*)skb_put(skb
, sizeof(*pig
));
314 skb
->h
.igmph
= (struct igmphdr
*)pig
;
315 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
323 static int igmpv3_sendpack(struct sk_buff
*skb
)
325 struct iphdr
*pip
= skb
->nh
.iph
;
326 struct igmphdr
*pig
= skb
->h
.igmph
;
329 iplen
= skb
->tail
- (unsigned char *)skb
->nh
.iph
;
330 pip
->tot_len
= htons(iplen
);
333 igmplen
= skb
->tail
- (unsigned char *)skb
->h
.igmph
;
334 pig
->csum
= ip_compute_csum((void *)skb
->h
.igmph
, igmplen
);
336 return NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
340 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
342 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
,type
,gdel
,sdel
);
345 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
346 int type
, struct igmpv3_grec
**ppgr
)
348 struct net_device
*dev
= pmc
->interface
->dev
;
349 struct igmpv3_report
*pih
;
350 struct igmpv3_grec
*pgr
;
353 skb
= igmpv3_newpack(dev
, dev
->mtu
);
356 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
357 pgr
->grec_type
= type
;
358 pgr
->grec_auxwords
= 0;
360 pgr
->grec_mca
= pmc
->multiaddr
;
361 pih
= (struct igmpv3_report
*)skb
->h
.igmph
;
362 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
367 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
368 skb_tailroom(skb)) : 0)
370 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
371 int type
, int gdeleted
, int sdeleted
)
373 struct net_device
*dev
= pmc
->interface
->dev
;
374 struct igmpv3_report
*pih
;
375 struct igmpv3_grec
*pgr
= 0;
376 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, *psf_list
;
377 int scount
, first
, isquery
, truncate
;
379 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
382 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
383 type
== IGMPV3_MODE_IS_EXCLUDE
;
384 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
385 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
387 psf_list
= sdeleted
? pmc
->tomb
: pmc
->sources
;
390 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
391 type
== IGMPV3_BLOCK_OLD_SOURCES
)
393 if (pmc
->crcount
|| isquery
) {
394 /* make sure we have room for group header and at
397 if (skb
&& AVAILABLE(skb
) < sizeof(struct igmpv3_grec
)+
399 igmpv3_sendpack(skb
);
400 skb
= 0; /* add_grhead will get a new one */
402 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
406 pih
= skb
? (struct igmpv3_report
*)skb
->h
.igmph
: 0;
408 /* EX and TO_EX get a fresh packet, if needed */
410 if (pih
&& pih
->ngrec
&&
411 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
413 igmpv3_sendpack(skb
);
414 skb
= igmpv3_newpack(dev
, dev
->mtu
);
420 for (psf
=psf_list
; psf
; psf
=psf_next
) {
423 psf_next
= psf
->sf_next
;
425 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
430 /* clear marks on query responses */
434 if (AVAILABLE(skb
) < sizeof(u32
) +
435 first
*sizeof(struct igmpv3_grec
)) {
436 if (truncate
&& !first
)
437 break; /* truncate these */
439 pgr
->grec_nsrcs
= htons(scount
);
441 igmpv3_sendpack(skb
);
442 skb
= igmpv3_newpack(dev
, dev
->mtu
);
447 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
450 psrc
= (u32
*)skb_put(skb
, sizeof(u32
));
451 *psrc
= psf
->sf_inaddr
;
453 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
454 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
456 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
458 psf_prev
->sf_next
= psf
->sf_next
;
460 pmc
->tomb
= psf
->sf_next
;
468 pgr
->grec_nsrcs
= htons(scount
);
471 pmc
->gsquery
= 0; /* clear query state on report */
475 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
477 struct sk_buff
*skb
= 0;
481 read_lock(&in_dev
->lock
);
482 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
483 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
485 spin_lock_bh(&pmc
->lock
);
486 if (pmc
->sfcount
[MCAST_EXCLUDE
])
487 type
= IGMPV3_MODE_IS_EXCLUDE
;
489 type
= IGMPV3_MODE_IS_INCLUDE
;
490 skb
= add_grec(skb
, pmc
, type
, 0, 0);
491 spin_unlock_bh(&pmc
->lock
);
493 read_unlock(&in_dev
->lock
);
495 spin_lock_bh(&pmc
->lock
);
496 if (pmc
->sfcount
[MCAST_EXCLUDE
])
497 type
= IGMPV3_MODE_IS_EXCLUDE
;
499 type
= IGMPV3_MODE_IS_INCLUDE
;
500 skb
= add_grec(skb
, pmc
, type
, 0, 0);
501 spin_unlock_bh(&pmc
->lock
);
505 return igmpv3_sendpack(skb
);
509 * remove zero-count source records from a source filter list
511 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
513 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
516 for (psf
=*ppsf
; psf
; psf
= psf_next
) {
517 psf_next
= psf
->sf_next
;
518 if (psf
->sf_crcount
== 0) {
520 psf_prev
->sf_next
= psf
->sf_next
;
522 *ppsf
= psf
->sf_next
;
529 static void igmpv3_send_cr(struct in_device
*in_dev
)
531 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
532 struct sk_buff
*skb
= 0;
535 read_lock(&in_dev
->lock
);
536 write_lock_bh(&in_dev
->mc_lock
);
540 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc_next
) {
541 pmc_next
= pmc
->next
;
542 if (pmc
->sfmode
== MCAST_INCLUDE
) {
543 type
= IGMPV3_BLOCK_OLD_SOURCES
;
544 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
545 skb
= add_grec(skb
, pmc
, type
, 1, 0);
546 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
550 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
551 type
= IGMPV3_CHANGE_TO_INCLUDE
;
552 skb
= add_grec(skb
, pmc
, type
, 1, 0);
554 if (pmc
->crcount
== 0) {
555 igmpv3_clear_zeros(&pmc
->tomb
);
556 igmpv3_clear_zeros(&pmc
->sources
);
559 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
561 pmc_prev
->next
= pmc_next
;
563 in_dev
->mc_tomb
= pmc_next
;
564 in_dev_put(pmc
->interface
);
569 write_unlock_bh(&in_dev
->mc_lock
);
572 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
573 spin_lock_bh(&pmc
->lock
);
574 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
575 type
= IGMPV3_BLOCK_OLD_SOURCES
;
576 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
578 type
= IGMPV3_ALLOW_NEW_SOURCES
;
579 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
581 skb
= add_grec(skb
, pmc
, type
, 0, 0);
582 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
584 /* filter mode changes */
587 if (pmc
->sfmode
== MCAST_EXCLUDE
)
588 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
590 type
= IGMPV3_CHANGE_TO_INCLUDE
;
591 skb
= add_grec(skb
, pmc
, type
, 0, 0);
593 spin_unlock_bh(&pmc
->lock
);
595 read_unlock(&in_dev
->lock
);
598 (void) igmpv3_sendpack(skb
);
601 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
608 struct net_device
*dev
= in_dev
->dev
;
609 u32 group
= pmc
? pmc
->multiaddr
: 0;
612 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
613 return igmpv3_send_report(in_dev
, pmc
);
614 else if (type
== IGMP_HOST_LEAVE_MESSAGE
)
615 dst
= IGMP_ALL_ROUTER
;
620 struct flowi fl
= { .oif
= dev
->ifindex
,
621 .nl_u
= { .ip4_u
= { .daddr
= dst
} },
622 .proto
= IPPROTO_IGMP
};
623 if (ip_route_output_key(&rt
, &fl
))
626 if (rt
->rt_src
== 0) {
631 skb
=alloc_skb(IGMP_SIZE
+LL_RESERVED_SPACE(dev
), GFP_ATOMIC
);
637 skb
->dst
= &rt
->u
.dst
;
639 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
641 skb
->nh
.iph
= iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
)+4);
644 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
646 iph
->frag_off
= htons(IP_DF
);
649 iph
->saddr
= rt
->rt_src
;
650 iph
->protocol
= IPPROTO_IGMP
;
651 iph
->tot_len
= htons(IGMP_SIZE
);
652 ip_select_ident(iph
, &rt
->u
.dst
, NULL
);
653 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
654 ((u8
*)&iph
[1])[1] = 4;
655 ((u8
*)&iph
[1])[2] = 0;
656 ((u8
*)&iph
[1])[3] = 0;
659 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
664 ih
->csum
=ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
666 return NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->u
.dst
.dev
,
670 static void igmp_gq_timer_expire(unsigned long data
)
672 struct in_device
*in_dev
= (struct in_device
*)data
;
674 in_dev
->mr_gq_running
= 0;
675 igmpv3_send_report(in_dev
, 0);
676 __in_dev_put(in_dev
);
679 static void igmp_ifc_timer_expire(unsigned long data
)
681 struct in_device
*in_dev
= (struct in_device
*)data
;
683 igmpv3_send_cr(in_dev
);
684 if (in_dev
->mr_ifc_count
) {
685 in_dev
->mr_ifc_count
--;
686 igmp_ifc_start_timer(in_dev
, IGMP_Unsolicited_Report_Interval
);
688 __in_dev_put(in_dev
);
691 static void igmp_ifc_event(struct in_device
*in_dev
)
693 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
695 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
696 IGMP_Unsolicited_Report_Count
;
697 igmp_ifc_start_timer(in_dev
, 1);
701 static void igmp_timer_expire(unsigned long data
)
703 struct ip_mc_list
*im
=(struct ip_mc_list
*)data
;
704 struct in_device
*in_dev
= im
->interface
;
706 spin_lock(&im
->lock
);
709 if (im
->unsolicit_count
) {
710 im
->unsolicit_count
--;
711 igmp_start_timer(im
, IGMP_Unsolicited_Report_Interval
);
714 spin_unlock(&im
->lock
);
716 if (IGMP_V1_SEEN(in_dev
))
717 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
718 else if (IGMP_V2_SEEN(in_dev
))
719 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
721 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
726 static void igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __u32
*srcs
)
728 struct ip_sf_list
*psf
;
732 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
735 for (i
=0; i
<nsrcs
; i
++)
736 if (srcs
[i
] == psf
->sf_inaddr
) {
744 static void igmp_heard_report(struct in_device
*in_dev
, u32 group
)
746 struct ip_mc_list
*im
;
748 /* Timers are only set for non-local groups */
750 if (group
== IGMP_ALL_HOSTS
)
753 read_lock(&in_dev
->lock
);
754 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
755 if (im
->multiaddr
== group
) {
760 read_unlock(&in_dev
->lock
);
763 static void igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
766 struct igmphdr
*ih
= skb
->h
.igmph
;
767 struct igmpv3_query
*ih3
= (struct igmpv3_query
*)ih
;
768 struct ip_mc_list
*im
;
769 u32 group
= ih
->group
;
776 /* Alas, old v1 router presents here. */
778 max_delay
= IGMP_Query_Response_Interval
;
779 in_dev
->mr_v1_seen
= jiffies
+
780 IGMP_V1_Router_Present_Timeout
;
783 /* v2 router present */
784 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
785 in_dev
->mr_v2_seen
= jiffies
+
786 IGMP_V2_Router_Present_Timeout
;
788 /* cancel the interface change timer */
789 in_dev
->mr_ifc_count
= 0;
790 if (del_timer(&in_dev
->mr_ifc_timer
))
791 __in_dev_put(in_dev
);
792 /* clear deleted report items */
793 igmpv3_clear_delrec(in_dev
);
794 } else if (len
< 12) {
795 return; /* ignore bogus packet; freed by caller */
797 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
800 ih3
= (struct igmpv3_query
*) skb
->h
.raw
;
802 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
803 + ntohs(ih3
->nsrcs
)*sizeof(__u32
)))
805 ih3
= (struct igmpv3_query
*) skb
->h
.raw
;
808 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
810 max_delay
= 1; /* can't mod w/ 0 */
811 in_dev
->mr_maxdelay
= max_delay
;
813 in_dev
->mr_qrv
= ih3
->qrv
;
814 if (!group
) { /* general query */
816 return; /* no sources allowed */
817 igmp_gq_start_timer(in_dev
);
820 /* mark sources to include, if group & source-specific */
821 mark
= ih3
->nsrcs
!= 0;
825 * - Start the timers in all of our membership records
826 * that the query applies to for the interface on
827 * which the query arrived excl. those that belong
828 * to a "local" group (224.0.0.X)
829 * - For timers already running check if they need to
831 * - Use the igmp->igmp_code field as the maximum
834 read_lock(&in_dev
->lock
);
835 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
836 if (group
&& group
!= im
->multiaddr
)
838 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
840 spin_lock_bh(&im
->lock
);
842 im
->gsquery
= im
->gsquery
&& mark
;
846 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
847 spin_unlock_bh(&im
->lock
);
848 igmp_mod_timer(im
, max_delay
);
850 read_unlock(&in_dev
->lock
);
853 int igmp_rcv(struct sk_buff
*skb
)
855 /* This basically follows the spec line by line -- see RFC1112 */
857 struct in_device
*in_dev
= in_dev_get(skb
->dev
);
865 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)) ||
866 (u16
)csum_fold(skb_checksum(skb
, 0, len
, 0))) {
874 case IGMP_HOST_MEMBERSHIP_QUERY
:
875 igmp_heard_query(in_dev
, skb
, len
);
877 case IGMP_HOST_MEMBERSHIP_REPORT
:
878 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
879 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
880 /* Is it our report looped back? */
881 if (((struct rtable
*)skb
->dst
)->fl
.iif
== 0)
883 igmp_heard_report(in_dev
, ih
->group
);
886 #ifdef CONFIG_IP_PIMSM_V1
888 return pim_rcv_v1(skb
);
892 case IGMP_HOST_LEAVE_MESSAGE
:
894 case IGMP_MTRACE_RESP
:
897 NETDEBUG(printk(KERN_DEBUG
"New IGMP type=%d, why we do not know about it?\n", ih
->type
));
908 * Add a filter to a device
911 static void ip_mc_filter_add(struct in_device
*in_dev
, u32 addr
)
913 char buf
[MAX_ADDR_LEN
];
914 struct net_device
*dev
= in_dev
->dev
;
916 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
917 We will get multicast token leakage, when IFF_MULTICAST
918 is changed. This check should be done in dev->set_multicast_list
919 routine. Something sort of:
920 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
923 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
924 dev_mc_add(dev
,buf
,dev
->addr_len
,0);
928 * Remove a filter from a device
931 static void ip_mc_filter_del(struct in_device
*in_dev
, u32 addr
)
933 char buf
[MAX_ADDR_LEN
];
934 struct net_device
*dev
= in_dev
->dev
;
936 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
937 dev_mc_delete(dev
,buf
,dev
->addr_len
,0);
940 #ifdef CONFIG_IP_MULTICAST
942 * deleted ip_mc_list manipulation
944 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
946 struct ip_mc_list
*pmc
;
948 /* this is an "ip_mc_list" for convenience; only the fields below
949 * are actually used. In particular, the refcnt and users are not
950 * used for management of the delete list. Using the same structure
951 * for deleted items allows change reports to use common code with
952 * non-deleted or query-response MCA's.
954 pmc
= (struct ip_mc_list
*)kmalloc(sizeof(*pmc
), GFP_KERNEL
);
957 memset(pmc
, 0, sizeof(*pmc
));
958 spin_lock_bh(&im
->lock
);
959 pmc
->interface
= im
->interface
;
961 pmc
->multiaddr
= im
->multiaddr
;
962 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
963 IGMP_Unsolicited_Report_Count
;
964 pmc
->sfmode
= im
->sfmode
;
965 if (pmc
->sfmode
== MCAST_INCLUDE
) {
966 struct ip_sf_list
*psf
;
968 pmc
->tomb
= im
->tomb
;
969 pmc
->sources
= im
->sources
;
970 im
->tomb
= im
->sources
= 0;
971 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
972 psf
->sf_crcount
= pmc
->crcount
;
974 spin_unlock_bh(&im
->lock
);
976 write_lock_bh(&in_dev
->mc_lock
);
977 pmc
->next
= in_dev
->mc_tomb
;
978 in_dev
->mc_tomb
= pmc
;
979 write_unlock_bh(&in_dev
->mc_lock
);
982 static void igmpv3_del_delrec(struct in_device
*in_dev
, __u32 multiaddr
)
984 struct ip_mc_list
*pmc
, *pmc_prev
;
985 struct ip_sf_list
*psf
, *psf_next
;
987 write_lock_bh(&in_dev
->mc_lock
);
989 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
990 if (pmc
->multiaddr
== multiaddr
)
996 pmc_prev
->next
= pmc
->next
;
998 in_dev
->mc_tomb
= pmc
->next
;
1000 write_unlock_bh(&in_dev
->mc_lock
);
1002 for (psf
=pmc
->tomb
; psf
; psf
=psf_next
) {
1003 psf_next
= psf
->sf_next
;
1006 in_dev_put(pmc
->interface
);
1011 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1013 struct ip_mc_list
*pmc
, *nextpmc
;
1015 write_lock_bh(&in_dev
->mc_lock
);
1016 pmc
= in_dev
->mc_tomb
;
1017 in_dev
->mc_tomb
= 0;
1018 write_unlock_bh(&in_dev
->mc_lock
);
1020 for (; pmc
; pmc
= nextpmc
) {
1021 nextpmc
= pmc
->next
;
1022 ip_mc_clear_src(pmc
);
1023 in_dev_put(pmc
->interface
);
1026 /* clear dead sources, too */
1027 read_lock(&in_dev
->lock
);
1028 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1029 struct ip_sf_list
*psf
, *psf_next
;
1031 spin_lock_bh(&pmc
->lock
);
1034 spin_unlock_bh(&pmc
->lock
);
1035 for (; psf
; psf
=psf_next
) {
1036 psf_next
= psf
->sf_next
;
1040 read_unlock(&in_dev
->lock
);
1044 static void igmp_group_dropped(struct ip_mc_list
*im
)
1046 struct in_device
*in_dev
= im
->interface
;
1047 #ifdef CONFIG_IP_MULTICAST
1053 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1056 #ifdef CONFIG_IP_MULTICAST
1057 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1060 reporter
= im
->reporter
;
1061 igmp_stop_timer(im
);
1063 if (IGMP_V1_SEEN(in_dev
))
1065 if (IGMP_V2_SEEN(in_dev
)) {
1067 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1071 igmpv3_add_delrec(in_dev
, im
);
1073 igmp_ifc_event(in_dev
);
1076 ip_mc_clear_src(im
);
1079 static void igmp_group_added(struct ip_mc_list
*im
)
1081 struct in_device
*in_dev
= im
->interface
;
1083 if (im
->loaded
== 0) {
1085 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1088 #ifdef CONFIG_IP_MULTICAST
1089 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1092 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1093 spin_lock_bh(&im
->lock
);
1094 igmp_start_timer(im
, IGMP_Initial_Report_Delay
);
1095 spin_unlock_bh(&im
->lock
);
1100 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1101 IGMP_Unsolicited_Report_Count
;
1102 igmp_ifc_event(in_dev
);
1108 * Multicast list managers
1113 * A socket has joined a multicast group on device dev.
1116 void ip_mc_inc_group(struct in_device
*in_dev
, u32 addr
)
1118 struct ip_mc_list
*im
;
1122 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
1123 if (im
->multiaddr
== addr
) {
1125 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, 0, 0);
1130 im
= (struct ip_mc_list
*)kmalloc(sizeof(*im
), GFP_KERNEL
);
1135 im
->interface
=in_dev
;
1136 in_dev_hold(in_dev
);
1138 /* initial mode is (EX, empty) */
1139 im
->sfmode
= MCAST_EXCLUDE
;
1140 im
->sfcount
[MCAST_INCLUDE
] = 0;
1141 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1145 atomic_set(&im
->refcnt
, 1);
1146 spin_lock_init(&im
->lock
);
1147 #ifdef CONFIG_IP_MULTICAST
1149 init_timer(&im
->timer
);
1150 im
->timer
.data
=(unsigned long)im
;
1151 im
->timer
.function
=&igmp_timer_expire
;
1152 im
->unsolicit_count
= IGMP_Unsolicited_Report_Count
;
1157 write_lock_bh(&in_dev
->lock
);
1158 im
->next
=in_dev
->mc_list
;
1160 write_unlock_bh(&in_dev
->lock
);
1161 #ifdef CONFIG_IP_MULTICAST
1162 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1164 igmp_group_added(im
);
1165 if (in_dev
->dev
->flags
& IFF_UP
)
1166 ip_rt_multicast_event(in_dev
);
1172 * A socket has left a multicast group on device dev
1175 void ip_mc_dec_group(struct in_device
*in_dev
, u32 addr
)
1177 struct ip_mc_list
*i
, **ip
;
1181 for (ip
=&in_dev
->mc_list
; (i
=*ip
)!=NULL
; ip
=&i
->next
) {
1182 if (i
->multiaddr
==addr
) {
1183 if (--i
->users
== 0) {
1184 write_lock_bh(&in_dev
->lock
);
1186 write_unlock_bh(&in_dev
->lock
);
1187 igmp_group_dropped(i
);
1189 if (in_dev
->dev
->flags
& IFF_UP
)
1190 ip_rt_multicast_event(in_dev
);
1200 /* Device going down */
1202 void ip_mc_down(struct in_device
*in_dev
)
1204 struct ip_mc_list
*i
;
1208 #ifdef CONFIG_IP_MULTICAST
1209 in_dev
->mr_ifc_count
= 0;
1210 if (del_timer(&in_dev
->mr_ifc_timer
))
1211 __in_dev_put(in_dev
);
1212 in_dev
->mr_gq_running
= 0;
1213 if (del_timer(&in_dev
->mr_gq_timer
))
1214 __in_dev_put(in_dev
);
1217 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1218 igmp_group_dropped(i
);
1220 #ifdef CONFIG_IP_MULTICAST
1221 igmpv3_clear_delrec(in_dev
);
1224 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1227 /* Device going up */
1229 void ip_mc_up(struct in_device
*in_dev
)
1231 struct ip_mc_list
*i
;
1235 in_dev
->mc_tomb
= 0;
1236 #ifdef CONFIG_IP_MULTICAST
1237 in_dev
->mr_gq_running
= 0;
1238 init_timer(&in_dev
->mr_gq_timer
);
1239 in_dev
->mr_gq_timer
.data
=(unsigned long) in_dev
;
1240 in_dev
->mr_gq_timer
.function
=&igmp_gq_timer_expire
;
1241 in_dev
->mr_ifc_count
= 0;
1242 init_timer(&in_dev
->mr_ifc_timer
);
1243 in_dev
->mr_ifc_timer
.data
=(unsigned long) in_dev
;
1244 in_dev
->mr_ifc_timer
.function
=&igmp_ifc_timer_expire
;
1245 in_dev
->mr_qrv
= IGMP_Unsolicited_Report_Count
;
1248 in_dev
->mc_lock
= RW_LOCK_UNLOCKED
;
1249 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1251 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1252 igmp_group_added(i
);
1256 * Device is about to be destroyed: clean up.
1259 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1261 struct ip_mc_list
*i
;
1265 write_lock_bh(&in_dev
->lock
);
1266 while ((i
= in_dev
->mc_list
) != NULL
) {
1267 in_dev
->mc_list
= i
->next
;
1268 write_unlock_bh(&in_dev
->lock
);
1270 igmp_group_dropped(i
);
1273 write_lock_bh(&in_dev
->lock
);
1275 write_unlock_bh(&in_dev
->lock
);
1278 static struct in_device
* ip_mc_find_dev(struct ip_mreqn
*imr
)
1280 struct flowi fl
= { .nl_u
= { .ip4_u
=
1281 { .daddr
= imr
->imr_multiaddr
.s_addr
} } };
1283 struct net_device
*dev
= NULL
;
1284 struct in_device
*idev
= NULL
;
1286 if (imr
->imr_ifindex
) {
1287 idev
= inetdev_by_index(imr
->imr_ifindex
);
1292 if (imr
->imr_address
.s_addr
) {
1293 dev
= ip_dev_find(imr
->imr_address
.s_addr
);
1299 if (!dev
&& !ip_route_output_key(&rt
, &fl
)) {
1300 dev
= rt
->u
.dst
.dev
;
1304 imr
->imr_ifindex
= dev
->ifindex
;
1305 idev
= __in_dev_get(dev
);
1311 * Join a socket to a group
1313 int sysctl_igmp_max_memberships
= IP_MAX_MEMBERSHIPS
;
1316 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1319 struct ip_sf_list
*psf
, *psf_prev
;
1323 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1324 if (psf
->sf_inaddr
== *psfsrc
)
1328 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1329 /* source filter not found, or count wrong => bug */
1332 psf
->sf_count
[sfmode
]--;
1333 if (psf
->sf_count
[sfmode
] == 0) {
1334 ip_rt_multicast_event(pmc
->interface
);
1336 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1337 #ifdef CONFIG_IP_MULTICAST
1338 struct in_device
*in_dev
= pmc
->interface
;
1341 /* no more filters for this source */
1343 psf_prev
->sf_next
= psf
->sf_next
;
1345 pmc
->sources
= psf
->sf_next
;
1346 #ifdef CONFIG_IP_MULTICAST
1347 if (psf
->sf_oldin
&&
1348 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1349 psf
->sf_crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1350 IGMP_Unsolicited_Report_Count
;
1351 psf
->sf_next
= pmc
->tomb
;
1361 #ifndef CONFIG_IP_MULTICAST
1362 #define igmp_ifc_event(x) do { } while (0)
1365 int ip_mc_del_src(struct in_device
*in_dev
, __u32
*pmca
, int sfmode
,
1366 int sfcount
, __u32
*psfsrc
, int delta
)
1368 struct ip_mc_list
*pmc
;
1374 read_lock(&in_dev
->lock
);
1375 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1376 if (*pmca
== pmc
->multiaddr
)
1380 /* MCA not found?? bug */
1381 read_unlock(&in_dev
->lock
);
1384 spin_lock_bh(&pmc
->lock
);
1385 read_unlock(&in_dev
->lock
);
1386 #ifdef CONFIG_IP_MULTICAST
1390 if (!pmc
->sfcount
[sfmode
])
1392 pmc
->sfcount
[sfmode
]--;
1395 for (i
=0; i
<sfcount
; i
++) {
1396 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1398 changerec
|= rv
> 0;
1402 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1403 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1404 pmc
->sfcount
[MCAST_INCLUDE
]) {
1405 #ifdef CONFIG_IP_MULTICAST
1406 struct ip_sf_list
*psf
;
1409 /* filter mode change */
1410 pmc
->sfmode
= MCAST_INCLUDE
;
1411 #ifdef CONFIG_IP_MULTICAST
1412 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1413 IGMP_Unsolicited_Report_Count
;
1414 in_dev
->mr_ifc_count
= pmc
->crcount
;
1415 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1416 psf
->sf_crcount
= 0;
1417 igmp_ifc_event(pmc
->interface
);
1418 } else if (sf_setstate(pmc
) || changerec
) {
1419 igmp_ifc_event(pmc
->interface
);
1422 spin_unlock_bh(&pmc
->lock
);
1427 * Add multicast single-source filter to the interface list
1429 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1430 __u32
*psfsrc
, int delta
)
1432 struct ip_sf_list
*psf
, *psf_prev
;
1435 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1436 if (psf
->sf_inaddr
== *psfsrc
)
1441 psf
= (struct ip_sf_list
*)kmalloc(sizeof(*psf
), GFP_ATOMIC
);
1444 memset(psf
, 0, sizeof(*psf
));
1445 psf
->sf_inaddr
= *psfsrc
;
1447 psf_prev
->sf_next
= psf
;
1451 psf
->sf_count
[sfmode
]++;
1452 if (psf
->sf_count
[sfmode
] == 1) {
1453 ip_rt_multicast_event(pmc
->interface
);
1458 #ifdef CONFIG_IP_MULTICAST
1459 static void sf_markstate(struct ip_mc_list
*pmc
)
1461 struct ip_sf_list
*psf
;
1462 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1464 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1465 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1466 psf
->sf_oldin
= mca_xcount
==
1467 psf
->sf_count
[MCAST_EXCLUDE
] &&
1468 !psf
->sf_count
[MCAST_INCLUDE
];
1470 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1473 static int sf_setstate(struct ip_mc_list
*pmc
)
1475 struct ip_sf_list
*psf
;
1476 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1477 int qrv
= pmc
->interface
->mr_qrv
;
1481 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1482 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1483 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1484 !psf
->sf_count
[MCAST_INCLUDE
];
1486 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1487 if (new_in
!= psf
->sf_oldin
) {
1488 psf
->sf_crcount
= qrv
;
1497 * Add multicast source filter list to the interface list
1499 int ip_mc_add_src(struct in_device
*in_dev
, __u32
*pmca
, int sfmode
,
1500 int sfcount
, __u32
*psfsrc
, int delta
)
1502 struct ip_mc_list
*pmc
;
1508 read_lock(&in_dev
->lock
);
1509 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1510 if (*pmca
== pmc
->multiaddr
)
1514 /* MCA not found?? bug */
1515 read_unlock(&in_dev
->lock
);
1518 spin_lock_bh(&pmc
->lock
);
1519 read_unlock(&in_dev
->lock
);
1521 #ifdef CONFIG_IP_MULTICAST
1524 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1526 pmc
->sfcount
[sfmode
]++;
1528 for (i
=0; i
<sfcount
; i
++) {
1529 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1536 pmc
->sfcount
[sfmode
]--;
1538 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1539 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1540 #ifdef CONFIG_IP_MULTICAST
1541 struct in_device
*in_dev
= pmc
->interface
;
1542 struct ip_sf_list
*psf
;
1545 /* filter mode change */
1546 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1547 pmc
->sfmode
= MCAST_EXCLUDE
;
1548 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1549 pmc
->sfmode
= MCAST_INCLUDE
;
1550 #ifdef CONFIG_IP_MULTICAST
1551 /* else no filters; keep old mode for reports */
1553 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1554 IGMP_Unsolicited_Report_Count
;
1555 in_dev
->mr_ifc_count
= pmc
->crcount
;
1556 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1557 psf
->sf_crcount
= 0;
1558 igmp_ifc_event(in_dev
);
1559 } else if (sf_setstate(pmc
)) {
1560 igmp_ifc_event(in_dev
);
1563 spin_unlock_bh(&pmc
->lock
);
1567 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1569 struct ip_sf_list
*psf
, *nextpsf
;
1571 for (psf
=pmc
->tomb
; psf
; psf
=nextpsf
) {
1572 nextpsf
= psf
->sf_next
;
1576 for (psf
=pmc
->sources
; psf
; psf
=nextpsf
) {
1577 nextpsf
= psf
->sf_next
;
1581 pmc
->sfmode
= MCAST_EXCLUDE
;
1582 pmc
->sfcount
[MCAST_EXCLUDE
] = 0;
1583 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1588 * Join a multicast group
1590 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1593 u32 addr
= imr
->imr_multiaddr
.s_addr
;
1594 struct ip_mc_socklist
*iml
, *i
;
1595 struct in_device
*in_dev
;
1596 struct inet_opt
*inet
= inet_sk(sk
);
1599 if (!MULTICAST(addr
))
1604 in_dev
= ip_mc_find_dev(imr
);
1612 iml
= (struct ip_mc_socklist
*)sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
1615 for (i
= inet
->mc_list
; i
; i
= i
->next
) {
1616 if (memcmp(&i
->multi
, imr
, sizeof(*imr
)) == 0) {
1617 /* New style additions are reference counted */
1618 if (imr
->imr_address
.s_addr
== 0) {
1627 if (iml
== NULL
|| count
>= sysctl_igmp_max_memberships
)
1629 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1630 iml
->next
= inet
->mc_list
;
1633 iml
->sfmode
= MCAST_EXCLUDE
;
1634 inet
->mc_list
= iml
;
1635 ip_mc_inc_group(in_dev
, addr
);
1642 sock_kfree_s(sk
, iml
, sizeof(*iml
));
1646 int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1647 struct in_device
*in_dev
)
1651 if (iml
->sflist
== 0) {
1652 /* any-source empty exclude case */
1653 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1654 iml
->sfmode
, 0, 0, 0);
1656 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1657 iml
->sfmode
, iml
->sflist
->sl_count
,
1658 iml
->sflist
->sl_addr
, 0);
1659 sock_kfree_s(sk
, iml
->sflist
, IP_SFLSIZE(iml
->sflist
->sl_max
));
1665 * Ask a socket to leave a group.
1668 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1670 struct inet_opt
*inet
= inet_sk(sk
);
1671 struct ip_mc_socklist
*iml
, **imlp
;
1674 for (imlp
= &inet
->mc_list
; (iml
= *imlp
) != NULL
; imlp
= &iml
->next
) {
1675 if (iml
->multi
.imr_multiaddr
.s_addr
==imr
->imr_multiaddr
.s_addr
&&
1676 iml
->multi
.imr_address
.s_addr
==imr
->imr_address
.s_addr
&&
1677 (!imr
->imr_ifindex
|| iml
->multi
.imr_ifindex
==imr
->imr_ifindex
)) {
1678 struct in_device
*in_dev
;
1680 in_dev
= inetdev_by_index(iml
->multi
.imr_ifindex
);
1682 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1693 ip_mc_dec_group(in_dev
, imr
->imr_multiaddr
.s_addr
);
1697 sock_kfree_s(sk
, iml
, sizeof(*iml
));
1702 return -EADDRNOTAVAIL
;
1705 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1706 ip_mreq_source
*mreqs
, int ifindex
)
1709 struct ip_mreqn imr
;
1710 u32 addr
= mreqs
->imr_multiaddr
;
1711 struct ip_mc_socklist
*pmc
;
1712 struct in_device
*in_dev
= 0;
1713 struct inet_opt
*inet
= inet_sk(sk
);
1714 struct ip_sf_socklist
*psl
;
1717 if (!MULTICAST(addr
))
1722 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1723 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1724 imr
.imr_ifindex
= ifindex
;
1725 in_dev
= ip_mc_find_dev(&imr
);
1731 err
= -EADDRNOTAVAIL
;
1733 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1734 if (memcmp(&pmc
->multi
, mreqs
, 2*sizeof(__u32
)) == 0)
1737 if (!pmc
) /* must have a prior join */
1739 /* if a source filter was set, must be the same mode as before */
1741 if (pmc
->sfmode
!= omode
)
1743 } else if (pmc
->sfmode
!= omode
) {
1744 /* allow mode switches for empty-set filters */
1745 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
1747 pmc
->sfmode
= omode
;
1748 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
1757 for (i
=0; i
<psl
->sl_count
; i
++) {
1758 rv
= memcmp(&psl
->sl_addr
, &mreqs
->imr_multiaddr
,
1763 if (!rv
) /* source not found */
1766 /* update the interface filter */
1767 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1768 &mreqs
->imr_sourceaddr
, 1);
1770 for (j
=i
+1; j
<psl
->sl_count
; j
++)
1771 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
1776 /* else, add a new source to the filter */
1778 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
1779 struct ip_sf_socklist
*newpsl
;
1780 int count
= IP_SFBLOCK
;
1783 count
+= psl
->sl_max
;
1784 newpsl
= (struct ip_sf_socklist
*)sock_kmalloc(sk
,
1785 IP_SFLSIZE(count
), GFP_KERNEL
);
1790 newpsl
->sl_max
= count
;
1791 newpsl
->sl_count
= count
- IP_SFBLOCK
;
1793 for (i
=0; i
<psl
->sl_count
; i
++)
1794 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
1795 sock_kfree_s(sk
, psl
, IP_SFLSIZE(psl
->sl_max
));
1797 pmc
->sflist
= psl
= newpsl
;
1799 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
1800 for (i
=0; i
<psl
->sl_count
; i
++) {
1801 rv
= memcmp(&psl
->sl_addr
, &mreqs
->imr_multiaddr
,
1806 if (rv
== 0) /* address already there is an error */
1808 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
1809 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
1810 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
1813 /* update the interface list */
1814 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1815 &mreqs
->imr_sourceaddr
, 1);
1821 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
1824 struct ip_mreqn imr
;
1825 u32 addr
= msf
->imsf_multiaddr
;
1826 struct ip_mc_socklist
*pmc
;
1827 struct in_device
*in_dev
;
1828 struct inet_opt
*inet
= inet_sk(sk
);
1829 struct ip_sf_socklist
*newpsl
, *psl
;
1831 if (!MULTICAST(addr
))
1833 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
1834 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
1839 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
1840 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
1841 imr
.imr_ifindex
= ifindex
;
1842 in_dev
= ip_mc_find_dev(&imr
);
1848 err
= -EADDRNOTAVAIL
;
1850 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1851 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
1852 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
1855 if (!pmc
) /* must have a prior join */
1857 if (msf
->imsf_numsrc
) {
1858 newpsl
= (struct ip_sf_socklist
*)sock_kmalloc(sk
,
1859 IP_SFLSIZE(msf
->imsf_numsrc
), GFP_KERNEL
);
1864 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
1865 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
1866 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
1867 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
1868 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
1870 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
1877 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
1878 psl
->sl_count
, psl
->sl_addr
, 0);
1879 sock_kfree_s(sk
, psl
, IP_SFLSIZE(psl
->sl_max
));
1881 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
1883 pmc
->sflist
= newpsl
;
1884 pmc
->sfmode
= msf
->imsf_fmode
;
1890 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
1891 struct ip_msfilter
*optval
, int *optlen
)
1893 int err
, len
, count
, copycount
;
1894 struct ip_mreqn imr
;
1895 u32 addr
= msf
->imsf_multiaddr
;
1896 struct ip_mc_socklist
*pmc
;
1897 struct in_device
*in_dev
;
1898 struct inet_opt
*inet
= inet_sk(sk
);
1899 struct ip_sf_socklist
*psl
;
1901 if (!MULTICAST(addr
))
1906 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
1907 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
1908 imr
.imr_ifindex
= 0;
1909 in_dev
= ip_mc_find_dev(&imr
);
1915 err
= -EADDRNOTAVAIL
;
1917 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1918 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
1919 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
1922 if (!pmc
) /* must have a prior join */
1924 msf
->imsf_fmode
= pmc
->sfmode
;
1931 count
= psl
->sl_count
;
1933 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
1934 len
= copycount
* sizeof(psl
->sl_addr
[0]);
1935 msf
->imsf_numsrc
= count
;
1936 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
1937 copy_to_user((void *)optval
, msf
, IP_MSFILTER_SIZE(0))) {
1941 copy_to_user((void *)&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
1949 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
1950 struct group_filter
*optval
, int *optlen
)
1952 int err
, i
, count
, copycount
;
1953 struct sockaddr_in
*psin
;
1955 struct ip_mc_socklist
*pmc
;
1956 struct inet_opt
*inet
= inet_sk(sk
);
1957 struct ip_sf_socklist
*psl
;
1959 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
1960 if (psin
->sin_family
!= AF_INET
)
1962 addr
= psin
->sin_addr
.s_addr
;
1963 if (!MULTICAST(addr
))
1968 err
= -EADDRNOTAVAIL
;
1970 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1971 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
1972 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
1975 if (!pmc
) /* must have a prior join */
1977 gsf
->gf_fmode
= pmc
->sfmode
;
1980 count
= psl
? psl
->sl_count
: 0;
1981 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
1982 gsf
->gf_numsrc
= count
;
1983 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
1984 copy_to_user((void *)optval
, gsf
, GROUP_FILTER_SIZE(0))) {
1987 for (i
=0; i
<copycount
; i
++) {
1988 struct sockaddr_in
*psin
;
1989 struct sockaddr_storage ss
;
1991 psin
= (struct sockaddr_in
*)&ss
;
1992 memset(&ss
, 0, sizeof(ss
));
1993 psin
->sin_family
= AF_INET
;
1994 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
1995 if (copy_to_user((void *)&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2005 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2007 int ip_mc_sf_allow(struct sock
*sk
, u32 loc_addr
, u32 rmt_addr
, int dif
)
2009 struct inet_opt
*inet
= inet_sk(sk
);
2010 struct ip_mc_socklist
*pmc
;
2011 struct ip_sf_socklist
*psl
;
2014 if (!MULTICAST(loc_addr
))
2017 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2018 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2019 pmc
->multi
.imr_ifindex
== dif
)
2026 return pmc
->sfmode
== MCAST_EXCLUDE
;
2028 for (i
=0; i
<psl
->sl_count
; i
++) {
2029 if (psl
->sl_addr
[i
] == rmt_addr
)
2032 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2034 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2040 * A socket is closing.
2043 void ip_mc_drop_socket(struct sock
*sk
)
2045 struct inet_opt
*inet
= inet_sk(sk
);
2046 struct ip_mc_socklist
*iml
;
2048 if (inet
->mc_list
== NULL
)
2052 while ((iml
= inet
->mc_list
) != NULL
) {
2053 struct in_device
*in_dev
;
2054 inet
->mc_list
= iml
->next
;
2056 if ((in_dev
= inetdev_by_index(iml
->multi
.imr_ifindex
)) != NULL
) {
2057 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2058 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2061 sock_kfree_s(sk
, iml
, sizeof(*iml
));
2067 int ip_check_mc(struct in_device
*in_dev
, u32 mc_addr
, u32 src_addr
, u16 proto
)
2069 struct ip_mc_list
*im
;
2070 struct ip_sf_list
*psf
;
2073 read_lock(&in_dev
->lock
);
2074 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
2075 if (im
->multiaddr
== mc_addr
)
2078 if (im
&& proto
== IPPROTO_IGMP
) {
2081 for (psf
=im
->sources
; psf
; psf
=psf
->sf_next
) {
2082 if (psf
->sf_inaddr
== src_addr
)
2086 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2087 psf
->sf_count
[MCAST_EXCLUDE
] !=
2088 im
->sfcount
[MCAST_EXCLUDE
];
2090 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2092 read_unlock(&in_dev
->lock
);
2096 #if defined(CONFIG_PROC_FS)
2097 struct igmp_mc_iter_state
{
2098 struct net_device
*dev
;
2099 struct in_device
*in_dev
;
2102 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)&seq->private)
2104 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2106 struct ip_mc_list
*im
= NULL
;
2107 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2109 for (state
->dev
= dev_base
, state
->in_dev
= NULL
;
2111 state
->dev
= state
->dev
->next
) {
2112 struct in_device
*in_dev
;
2113 in_dev
= in_dev_get(state
->dev
);
2116 read_lock(&in_dev
->lock
);
2117 im
= in_dev
->mc_list
;
2119 state
->in_dev
= in_dev
;
2122 read_unlock(&in_dev
->lock
);
2127 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2129 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2132 if (likely(state
->in_dev
!= NULL
)) {
2133 read_unlock(&state
->in_dev
->lock
);
2134 in_dev_put(state
->in_dev
);
2136 state
->dev
= state
->dev
->next
;
2138 state
->in_dev
= NULL
;
2141 state
->in_dev
= in_dev_get(state
->dev
);
2144 read_lock(&state
->in_dev
->lock
);
2145 im
= state
->in_dev
->mc_list
;
2150 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2152 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2154 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2156 return pos
? NULL
: im
;
2159 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2161 read_lock(&dev_base_lock
);
2162 return *pos
? igmp_mc_get_idx(seq
, *pos
) : (void *)1;
2165 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2167 struct ip_mc_list
*im
;
2169 im
= igmp_mc_get_first(seq
);
2171 im
= igmp_mc_get_next(seq
, v
);
2176 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2178 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2179 if (likely(state
->in_dev
!= NULL
)) {
2180 read_unlock(&state
->in_dev
->lock
);
2181 in_dev_put(state
->in_dev
);
2183 read_unlock(&dev_base_lock
);
2186 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2190 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2192 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2193 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2195 #ifdef CONFIG_IP_MULTICAST
2196 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" : "V2";
2201 if (state
->in_dev
->mc_list
== im
) {
2202 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2203 state
->dev
->ifindex
, state
->dev
->name
, state
->dev
->mc_count
, querier
);
2207 "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2208 im
->multiaddr
, im
->users
,
2209 im
->tm_running
, im
->timer
.expires
-jiffies
, im
->reporter
);
2214 static struct seq_operations igmp_mc_seq_ops
= {
2215 .start
= igmp_mc_seq_start
,
2216 .next
= igmp_mc_seq_next
,
2217 .stop
= igmp_mc_seq_stop
,
2218 .show
= igmp_mc_seq_show
,
2221 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2223 struct seq_file
*seq
;
2225 struct igmp_mc_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2229 rc
= seq_open(file
, &igmp_mc_seq_ops
);
2233 seq
= file
->private_data
;
2235 memset(s
, 0, sizeof(*s
));
2243 static struct file_operations igmp_mc_seq_fops
= {
2244 .owner
= THIS_MODULE
,
2245 .open
= igmp_mc_seq_open
,
2247 .llseek
= seq_lseek
,
2248 .release
= seq_release_private
,
2251 struct igmp_mcf_iter_state
{
2252 struct net_device
*dev
;
2253 struct in_device
*idev
;
2254 struct ip_mc_list
*im
;
2257 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)&seq->private)
2259 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2261 struct ip_sf_list
*psf
= NULL
;
2262 struct ip_mc_list
*im
= NULL
;
2263 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2265 for (state
->dev
= dev_base
, state
->idev
= NULL
, state
->im
= NULL
;
2267 state
->dev
= state
->dev
->next
) {
2268 struct in_device
*idev
;
2269 idev
= in_dev_get(state
->dev
);
2270 if (unlikely(idev
== NULL
))
2272 read_lock_bh(&idev
->lock
);
2274 if (likely(im
!= NULL
)) {
2275 spin_lock_bh(&im
->lock
);
2277 if (likely(psf
!= NULL
)) {
2282 spin_unlock_bh(&im
->lock
);
2284 read_unlock_bh(&idev
->lock
);
2289 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2291 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2295 spin_unlock_bh(&state
->im
->lock
);
2296 state
->im
= state
->im
->next
;
2297 while (!state
->im
) {
2298 if (likely(state
->idev
!= NULL
)) {
2299 read_unlock_bh(&state
->idev
->lock
);
2300 in_dev_put(state
->idev
);
2302 state
->dev
= state
->dev
->next
;
2307 state
->idev
= in_dev_get(state
->dev
);
2310 read_lock_bh(&state
->idev
->lock
);
2311 state
->im
= state
->idev
->mc_list
;
2315 spin_lock_bh(&state
->im
->lock
);
2316 psf
= state
->im
->sources
;
2322 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2324 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2326 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2328 return pos
? NULL
: psf
;
2331 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2333 read_lock(&dev_base_lock
);
2334 return *pos
? igmp_mcf_get_idx(seq
, *pos
) : (void *)1;
2337 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2339 struct ip_sf_list
*psf
;
2341 psf
= igmp_mcf_get_first(seq
);
2343 psf
= igmp_mcf_get_next(seq
, v
);
2348 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2350 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2351 if (likely(state
->im
!= NULL
))
2352 spin_unlock_bh(&state
->im
->lock
);
2353 if (likely(state
->idev
!= NULL
)) {
2354 read_unlock_bh(&state
->idev
->lock
);
2355 in_dev_put(state
->idev
);
2357 read_unlock(&dev_base_lock
);
2360 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2362 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2363 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2365 if (v
== (void *)1) {
2368 "%10s %10s %6s %6s\n", "Idx",
2370 "SRC", "INC", "EXC");
2374 "0x%08x %6lu %6lu\n",
2375 state
->dev
->ifindex
, state
->dev
->name
,
2376 ntohl(state
->im
->multiaddr
),
2377 ntohl(psf
->sf_inaddr
),
2378 psf
->sf_count
[MCAST_INCLUDE
],
2379 psf
->sf_count
[MCAST_EXCLUDE
]);
2384 static struct seq_operations igmp_mcf_seq_ops
= {
2385 .start
= igmp_mcf_seq_start
,
2386 .next
= igmp_mcf_seq_next
,
2387 .stop
= igmp_mcf_seq_stop
,
2388 .show
= igmp_mcf_seq_show
,
2391 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2393 struct seq_file
*seq
;
2395 struct igmp_mcf_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2399 rc
= seq_open(file
, &igmp_mcf_seq_ops
);
2403 seq
= file
->private_data
;
2405 memset(s
, 0, sizeof(*s
));
2413 static struct file_operations igmp_mcf_seq_fops
= {
2414 .owner
= THIS_MODULE
,
2415 .open
= igmp_mcf_seq_open
,
2417 .llseek
= seq_lseek
,
2418 .release
= seq_release_private
,
2421 int __init
igmp_mc_proc_init(void)
2423 struct proc_dir_entry
*p
;
2425 p
= create_proc_entry("igmp", S_IRUGO
, proc_net
);
2427 p
->proc_fops
= &igmp_mc_seq_fops
;
2429 p
= create_proc_entry("mcfilter", S_IRUGO
, proc_net
);
2431 p
->proc_fops
= &igmp_mcf_seq_fops
;