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/module.h>
76 #include <asm/uaccess.h>
77 #include <asm/system.h>
78 #include <linux/types.h>
79 #include <linux/kernel.h>
80 #include <linux/jiffies.h>
81 #include <linux/string.h>
82 #include <linux/socket.h>
83 #include <linux/sockios.h>
85 #include <linux/inet.h>
86 #include <linux/netdevice.h>
87 #include <linux/skbuff.h>
88 #include <linux/inetdevice.h>
89 #include <linux/igmp.h>
90 #include <linux/if_arp.h>
91 #include <linux/rtnetlink.h>
92 #include <linux/times.h>
94 #include <net/net_namespace.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) \
133 (IPV4_DEVCONF_ALL(in_dev->dev->nd_net, FORCE_IGMP_VERSION) == 1 || \
134 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135 ((in_dev)->mr_v1_seen && \
136 time_before(jiffies, (in_dev)->mr_v1_seen)))
137 #define IGMP_V2_SEEN(in_dev) \
138 (IPV4_DEVCONF_ALL(in_dev->dev->nd_net, FORCE_IGMP_VERSION) == 2 || \
139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140 ((in_dev)->mr_v2_seen && \
141 time_before(jiffies, (in_dev)->mr_v2_seen)))
143 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
144 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
);
145 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
146 static int sf_setstate(struct ip_mc_list
*pmc
);
147 static void sf_markstate(struct ip_mc_list
*pmc
);
149 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
150 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
151 int sfcount
, __be32
*psfsrc
, int delta
);
153 static void ip_ma_put(struct ip_mc_list
*im
)
155 if (atomic_dec_and_test(&im
->refcnt
)) {
156 in_dev_put(im
->interface
);
161 #ifdef CONFIG_IP_MULTICAST
167 static __inline__
void igmp_stop_timer(struct ip_mc_list
*im
)
169 spin_lock_bh(&im
->lock
);
170 if (del_timer(&im
->timer
))
171 atomic_dec(&im
->refcnt
);
174 im
->unsolicit_count
= 0;
175 spin_unlock_bh(&im
->lock
);
178 /* It must be called with locked im->lock */
179 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
181 int tv
=net_random() % max_delay
;
184 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
185 atomic_inc(&im
->refcnt
);
188 static void igmp_gq_start_timer(struct in_device
*in_dev
)
190 int tv
= net_random() % in_dev
->mr_maxdelay
;
192 in_dev
->mr_gq_running
= 1;
193 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
197 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
199 int tv
= net_random() % delay
;
201 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
205 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
207 spin_lock_bh(&im
->lock
);
208 im
->unsolicit_count
= 0;
209 if (del_timer(&im
->timer
)) {
210 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
211 add_timer(&im
->timer
);
213 spin_unlock_bh(&im
->lock
);
216 atomic_dec(&im
->refcnt
);
218 igmp_start_timer(im
, max_delay
);
219 spin_unlock_bh(&im
->lock
);
224 * Send an IGMP report.
227 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
230 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
231 int gdeleted
, int sdeleted
)
234 case IGMPV3_MODE_IS_INCLUDE
:
235 case IGMPV3_MODE_IS_EXCLUDE
:
236 if (gdeleted
|| sdeleted
)
238 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
239 if (pmc
->sfmode
== MCAST_INCLUDE
)
241 /* don't include if this source is excluded
244 if (psf
->sf_count
[MCAST_INCLUDE
])
245 return type
== IGMPV3_MODE_IS_INCLUDE
;
246 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
247 psf
->sf_count
[MCAST_EXCLUDE
];
250 case IGMPV3_CHANGE_TO_INCLUDE
:
251 if (gdeleted
|| sdeleted
)
253 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
254 case IGMPV3_CHANGE_TO_EXCLUDE
:
255 if (gdeleted
|| sdeleted
)
257 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
258 psf
->sf_count
[MCAST_INCLUDE
])
260 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
261 psf
->sf_count
[MCAST_EXCLUDE
];
262 case IGMPV3_ALLOW_NEW_SOURCES
:
263 if (gdeleted
|| !psf
->sf_crcount
)
265 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
266 case IGMPV3_BLOCK_OLD_SOURCES
:
267 if (pmc
->sfmode
== MCAST_INCLUDE
)
268 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
269 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
275 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
277 struct ip_sf_list
*psf
;
280 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
281 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
288 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, int size
)
293 struct igmpv3_report
*pig
;
295 skb
= alloc_skb(size
+ LL_RESERVED_SPACE(dev
), GFP_ATOMIC
);
300 struct flowi fl
= { .oif
= dev
->ifindex
,
302 .daddr
= IGMPV3_ALL_MCR
} },
303 .proto
= IPPROTO_IGMP
};
304 if (ip_route_output_key(&init_net
, &rt
, &fl
)) {
309 if (rt
->rt_src
== 0) {
315 skb
->dst
= &rt
->u
.dst
;
318 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
320 skb_reset_network_header(skb
);
322 skb_put(skb
, sizeof(struct iphdr
) + 4);
325 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
327 pip
->frag_off
= htons(IP_DF
);
329 pip
->daddr
= rt
->rt_dst
;
330 pip
->saddr
= rt
->rt_src
;
331 pip
->protocol
= IPPROTO_IGMP
;
332 pip
->tot_len
= 0; /* filled in later */
333 ip_select_ident(pip
, &rt
->u
.dst
, NULL
);
334 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
335 ((u8
*)&pip
[1])[1] = 4;
336 ((u8
*)&pip
[1])[2] = 0;
337 ((u8
*)&pip
[1])[3] = 0;
339 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
340 skb_put(skb
, sizeof(*pig
));
341 pig
= igmpv3_report_hdr(skb
);
342 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
350 static int igmpv3_sendpack(struct sk_buff
*skb
)
352 struct igmphdr
*pig
= igmp_hdr(skb
);
353 const int igmplen
= skb
->tail
- skb
->transport_header
;
355 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
357 return ip_local_out(skb
);
360 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
362 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
,type
,gdel
,sdel
);
365 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
366 int type
, struct igmpv3_grec
**ppgr
)
368 struct net_device
*dev
= pmc
->interface
->dev
;
369 struct igmpv3_report
*pih
;
370 struct igmpv3_grec
*pgr
;
373 skb
= igmpv3_newpack(dev
, dev
->mtu
);
376 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
377 pgr
->grec_type
= type
;
378 pgr
->grec_auxwords
= 0;
380 pgr
->grec_mca
= pmc
->multiaddr
;
381 pih
= igmpv3_report_hdr(skb
);
382 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
387 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
388 skb_tailroom(skb)) : 0)
390 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
391 int type
, int gdeleted
, int sdeleted
)
393 struct net_device
*dev
= pmc
->interface
->dev
;
394 struct igmpv3_report
*pih
;
395 struct igmpv3_grec
*pgr
= NULL
;
396 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
397 int scount
, stotal
, first
, isquery
, truncate
;
399 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
402 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
403 type
== IGMPV3_MODE_IS_EXCLUDE
;
404 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
405 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
409 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
414 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
416 /* EX and TO_EX get a fresh packet, if needed */
418 if (pih
&& pih
->ngrec
&&
419 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
421 igmpv3_sendpack(skb
);
422 skb
= igmpv3_newpack(dev
, dev
->mtu
);
427 for (psf
=*psf_list
; psf
; psf
=psf_next
) {
430 psf_next
= psf
->sf_next
;
432 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
437 /* clear marks on query responses */
441 if (AVAILABLE(skb
) < sizeof(__be32
) +
442 first
*sizeof(struct igmpv3_grec
)) {
443 if (truncate
&& !first
)
444 break; /* truncate these */
446 pgr
->grec_nsrcs
= htons(scount
);
448 igmpv3_sendpack(skb
);
449 skb
= igmpv3_newpack(dev
, dev
->mtu
);
454 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
459 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
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 __be32 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(&init_net
, &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_reset_network_header(skb
);
668 skb_put(skb
, sizeof(struct iphdr
) + 4);
671 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
673 iph
->frag_off
= htons(IP_DF
);
676 iph
->saddr
= rt
->rt_src
;
677 iph
->protocol
= IPPROTO_IGMP
;
678 ip_select_ident(iph
, &rt
->u
.dst
, NULL
);
679 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
680 ((u8
*)&iph
[1])[1] = 4;
681 ((u8
*)&iph
[1])[2] = 0;
682 ((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 ip_local_out(skb
);
694 static void igmp_gq_timer_expire(unsigned long data
)
696 struct in_device
*in_dev
= (struct in_device
*)data
;
698 in_dev
->mr_gq_running
= 0;
699 igmpv3_send_report(in_dev
, NULL
);
700 __in_dev_put(in_dev
);
703 static void igmp_ifc_timer_expire(unsigned long data
)
705 struct in_device
*in_dev
= (struct in_device
*)data
;
707 igmpv3_send_cr(in_dev
);
708 if (in_dev
->mr_ifc_count
) {
709 in_dev
->mr_ifc_count
--;
710 igmp_ifc_start_timer(in_dev
, IGMP_Unsolicited_Report_Interval
);
712 __in_dev_put(in_dev
);
715 static void igmp_ifc_event(struct in_device
*in_dev
)
717 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
719 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
720 IGMP_Unsolicited_Report_Count
;
721 igmp_ifc_start_timer(in_dev
, 1);
725 static void igmp_timer_expire(unsigned long data
)
727 struct ip_mc_list
*im
=(struct ip_mc_list
*)data
;
728 struct in_device
*in_dev
= im
->interface
;
730 spin_lock(&im
->lock
);
733 if (im
->unsolicit_count
) {
734 im
->unsolicit_count
--;
735 igmp_start_timer(im
, IGMP_Unsolicited_Report_Interval
);
738 spin_unlock(&im
->lock
);
740 if (IGMP_V1_SEEN(in_dev
))
741 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
742 else if (IGMP_V2_SEEN(in_dev
))
743 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
745 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
750 /* mark EXCLUDE-mode sources */
751 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
753 struct ip_sf_list
*psf
;
757 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
760 for (i
=0; i
<nsrcs
; i
++) {
761 /* skip inactive filters */
762 if (pmc
->sfcount
[MCAST_INCLUDE
] ||
763 pmc
->sfcount
[MCAST_EXCLUDE
] !=
764 psf
->sf_count
[MCAST_EXCLUDE
])
766 if (srcs
[i
] == psf
->sf_inaddr
) {
773 if (scount
== nsrcs
) /* all sources excluded */
778 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
780 struct ip_sf_list
*psf
;
783 if (pmc
->sfmode
== MCAST_EXCLUDE
)
784 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
786 /* mark INCLUDE-mode sources */
788 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
791 for (i
=0; i
<nsrcs
; i
++)
792 if (srcs
[i
] == psf
->sf_inaddr
) {
806 static void igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
808 struct ip_mc_list
*im
;
810 /* Timers are only set for non-local groups */
812 if (group
== IGMP_ALL_HOSTS
)
815 read_lock(&in_dev
->mc_list_lock
);
816 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
817 if (im
->multiaddr
== group
) {
822 read_unlock(&in_dev
->mc_list_lock
);
825 static void igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
828 struct igmphdr
*ih
= igmp_hdr(skb
);
829 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
830 struct ip_mc_list
*im
;
831 __be32 group
= ih
->group
;
838 /* Alas, old v1 router presents here. */
840 max_delay
= IGMP_Query_Response_Interval
;
841 in_dev
->mr_v1_seen
= jiffies
+
842 IGMP_V1_Router_Present_Timeout
;
845 /* v2 router present */
846 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
847 in_dev
->mr_v2_seen
= jiffies
+
848 IGMP_V2_Router_Present_Timeout
;
850 /* cancel the interface change timer */
851 in_dev
->mr_ifc_count
= 0;
852 if (del_timer(&in_dev
->mr_ifc_timer
))
853 __in_dev_put(in_dev
);
854 /* clear deleted report items */
855 igmpv3_clear_delrec(in_dev
);
856 } else if (len
< 12) {
857 return; /* ignore bogus packet; freed by caller */
859 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
862 ih3
= igmpv3_query_hdr(skb
);
864 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
865 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
867 ih3
= igmpv3_query_hdr(skb
);
870 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
872 max_delay
= 1; /* can't mod w/ 0 */
873 in_dev
->mr_maxdelay
= max_delay
;
875 in_dev
->mr_qrv
= ih3
->qrv
;
876 if (!group
) { /* general query */
878 return; /* no sources allowed */
879 igmp_gq_start_timer(in_dev
);
882 /* mark sources to include, if group & source-specific */
883 mark
= ih3
->nsrcs
!= 0;
887 * - Start the timers in all of our membership records
888 * that the query applies to for the interface on
889 * which the query arrived excl. those that belong
890 * to a "local" group (224.0.0.X)
891 * - For timers already running check if they need to
893 * - Use the igmp->igmp_code field as the maximum
896 read_lock(&in_dev
->mc_list_lock
);
897 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
900 if (group
&& group
!= im
->multiaddr
)
902 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
904 spin_lock_bh(&im
->lock
);
906 im
->gsquery
= im
->gsquery
&& mark
;
909 changed
= !im
->gsquery
||
910 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
911 spin_unlock_bh(&im
->lock
);
913 igmp_mod_timer(im
, max_delay
);
915 read_unlock(&in_dev
->mc_list_lock
);
918 int igmp_rcv(struct sk_buff
*skb
)
920 /* This basically follows the spec line by line -- see RFC1112 */
922 struct in_device
*in_dev
= in_dev_get(skb
->dev
);
928 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
931 switch (skb
->ip_summed
) {
932 case CHECKSUM_COMPLETE
:
933 if (!csum_fold(skb
->csum
))
938 if (__skb_checksum_complete(skb
))
944 case IGMP_HOST_MEMBERSHIP_QUERY
:
945 igmp_heard_query(in_dev
, skb
, len
);
947 case IGMP_HOST_MEMBERSHIP_REPORT
:
948 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
949 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
950 /* Is it our report looped back? */
951 if (((struct rtable
*)skb
->dst
)->fl
.iif
== 0)
953 /* don't rely on MC router hearing unicast reports */
954 if (skb
->pkt_type
== PACKET_MULTICAST
||
955 skb
->pkt_type
== PACKET_BROADCAST
)
956 igmp_heard_report(in_dev
, ih
->group
);
959 #ifdef CONFIG_IP_PIMSM_V1
961 return pim_rcv_v1(skb
);
965 case IGMP_HOST_LEAVE_MESSAGE
:
967 case IGMP_MTRACE_RESP
:
984 * Add a filter to a device
987 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
989 char buf
[MAX_ADDR_LEN
];
990 struct net_device
*dev
= in_dev
->dev
;
992 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
993 We will get multicast token leakage, when IFF_MULTICAST
994 is changed. This check should be done in dev->set_multicast_list
995 routine. Something sort of:
996 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
999 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1000 dev_mc_add(dev
,buf
,dev
->addr_len
,0);
1004 * Remove a filter from a device
1007 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1009 char buf
[MAX_ADDR_LEN
];
1010 struct net_device
*dev
= in_dev
->dev
;
1012 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1013 dev_mc_delete(dev
,buf
,dev
->addr_len
,0);
1016 #ifdef CONFIG_IP_MULTICAST
1018 * deleted ip_mc_list manipulation
1020 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1022 struct ip_mc_list
*pmc
;
1024 /* this is an "ip_mc_list" for convenience; only the fields below
1025 * are actually used. In particular, the refcnt and users are not
1026 * used for management of the delete list. Using the same structure
1027 * for deleted items allows change reports to use common code with
1028 * non-deleted or query-response MCA's.
1030 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1033 spin_lock_bh(&im
->lock
);
1034 pmc
->interface
= im
->interface
;
1035 in_dev_hold(in_dev
);
1036 pmc
->multiaddr
= im
->multiaddr
;
1037 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1038 IGMP_Unsolicited_Report_Count
;
1039 pmc
->sfmode
= im
->sfmode
;
1040 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1041 struct ip_sf_list
*psf
;
1043 pmc
->tomb
= im
->tomb
;
1044 pmc
->sources
= im
->sources
;
1045 im
->tomb
= im
->sources
= NULL
;
1046 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1047 psf
->sf_crcount
= pmc
->crcount
;
1049 spin_unlock_bh(&im
->lock
);
1051 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1052 pmc
->next
= in_dev
->mc_tomb
;
1053 in_dev
->mc_tomb
= pmc
;
1054 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1057 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1059 struct ip_mc_list
*pmc
, *pmc_prev
;
1060 struct ip_sf_list
*psf
, *psf_next
;
1062 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1064 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
1065 if (pmc
->multiaddr
== multiaddr
)
1071 pmc_prev
->next
= pmc
->next
;
1073 in_dev
->mc_tomb
= pmc
->next
;
1075 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1077 for (psf
=pmc
->tomb
; psf
; psf
=psf_next
) {
1078 psf_next
= psf
->sf_next
;
1081 in_dev_put(pmc
->interface
);
1086 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1088 struct ip_mc_list
*pmc
, *nextpmc
;
1090 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1091 pmc
= in_dev
->mc_tomb
;
1092 in_dev
->mc_tomb
= NULL
;
1093 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1095 for (; pmc
; pmc
= nextpmc
) {
1096 nextpmc
= pmc
->next
;
1097 ip_mc_clear_src(pmc
);
1098 in_dev_put(pmc
->interface
);
1101 /* clear dead sources, too */
1102 read_lock(&in_dev
->mc_list_lock
);
1103 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1104 struct ip_sf_list
*psf
, *psf_next
;
1106 spin_lock_bh(&pmc
->lock
);
1109 spin_unlock_bh(&pmc
->lock
);
1110 for (; psf
; psf
=psf_next
) {
1111 psf_next
= psf
->sf_next
;
1115 read_unlock(&in_dev
->mc_list_lock
);
1119 static void igmp_group_dropped(struct ip_mc_list
*im
)
1121 struct in_device
*in_dev
= im
->interface
;
1122 #ifdef CONFIG_IP_MULTICAST
1128 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1131 #ifdef CONFIG_IP_MULTICAST
1132 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1135 reporter
= im
->reporter
;
1136 igmp_stop_timer(im
);
1138 if (!in_dev
->dead
) {
1139 if (IGMP_V1_SEEN(in_dev
))
1141 if (IGMP_V2_SEEN(in_dev
)) {
1143 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1147 igmpv3_add_delrec(in_dev
, im
);
1149 igmp_ifc_event(in_dev
);
1153 ip_mc_clear_src(im
);
1156 static void igmp_group_added(struct ip_mc_list
*im
)
1158 struct in_device
*in_dev
= im
->interface
;
1160 if (im
->loaded
== 0) {
1162 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1165 #ifdef CONFIG_IP_MULTICAST
1166 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1171 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1172 spin_lock_bh(&im
->lock
);
1173 igmp_start_timer(im
, IGMP_Initial_Report_Delay
);
1174 spin_unlock_bh(&im
->lock
);
1179 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1180 IGMP_Unsolicited_Report_Count
;
1181 igmp_ifc_event(in_dev
);
1187 * Multicast list managers
1192 * A socket has joined a multicast group on device dev.
1195 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1197 struct ip_mc_list
*im
;
1201 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
1202 if (im
->multiaddr
== addr
) {
1204 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1209 im
= kmalloc(sizeof(*im
), GFP_KERNEL
);
1214 im
->interface
=in_dev
;
1215 in_dev_hold(in_dev
);
1217 /* initial mode is (EX, empty) */
1218 im
->sfmode
= MCAST_EXCLUDE
;
1219 im
->sfcount
[MCAST_INCLUDE
] = 0;
1220 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1224 atomic_set(&im
->refcnt
, 1);
1225 spin_lock_init(&im
->lock
);
1226 #ifdef CONFIG_IP_MULTICAST
1228 setup_timer(&im
->timer
, &igmp_timer_expire
, (unsigned long)im
);
1229 im
->unsolicit_count
= IGMP_Unsolicited_Report_Count
;
1234 write_lock_bh(&in_dev
->mc_list_lock
);
1235 im
->next
=in_dev
->mc_list
;
1237 write_unlock_bh(&in_dev
->mc_list_lock
);
1238 #ifdef CONFIG_IP_MULTICAST
1239 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1241 igmp_group_added(im
);
1243 ip_rt_multicast_event(in_dev
);
1249 * Resend IGMP JOIN report; used for bonding.
1251 void ip_mc_rejoin_group(struct ip_mc_list
*im
)
1253 #ifdef CONFIG_IP_MULTICAST
1254 struct in_device
*in_dev
= im
->interface
;
1256 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1259 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1260 igmp_mod_timer(im
, IGMP_Initial_Report_Delay
);
1264 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1265 IGMP_Unsolicited_Report_Count
;
1266 igmp_ifc_event(in_dev
);
1271 * A socket has left a multicast group on device dev
1274 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1276 struct ip_mc_list
*i
, **ip
;
1280 for (ip
=&in_dev
->mc_list
; (i
=*ip
)!=NULL
; ip
=&i
->next
) {
1281 if (i
->multiaddr
==addr
) {
1282 if (--i
->users
== 0) {
1283 write_lock_bh(&in_dev
->mc_list_lock
);
1285 write_unlock_bh(&in_dev
->mc_list_lock
);
1286 igmp_group_dropped(i
);
1289 ip_rt_multicast_event(in_dev
);
1299 /* Device going down */
1301 void ip_mc_down(struct in_device
*in_dev
)
1303 struct ip_mc_list
*i
;
1307 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1308 igmp_group_dropped(i
);
1310 #ifdef CONFIG_IP_MULTICAST
1311 in_dev
->mr_ifc_count
= 0;
1312 if (del_timer(&in_dev
->mr_ifc_timer
))
1313 __in_dev_put(in_dev
);
1314 in_dev
->mr_gq_running
= 0;
1315 if (del_timer(&in_dev
->mr_gq_timer
))
1316 __in_dev_put(in_dev
);
1317 igmpv3_clear_delrec(in_dev
);
1320 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1323 void ip_mc_init_dev(struct in_device
*in_dev
)
1327 in_dev
->mc_tomb
= NULL
;
1328 #ifdef CONFIG_IP_MULTICAST
1329 in_dev
->mr_gq_running
= 0;
1330 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1331 (unsigned long)in_dev
);
1332 in_dev
->mr_ifc_count
= 0;
1333 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1334 (unsigned long)in_dev
);
1335 in_dev
->mr_qrv
= IGMP_Unsolicited_Report_Count
;
1338 rwlock_init(&in_dev
->mc_list_lock
);
1339 spin_lock_init(&in_dev
->mc_tomb_lock
);
1342 /* Device going up */
1344 void ip_mc_up(struct in_device
*in_dev
)
1346 struct ip_mc_list
*i
;
1350 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1352 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1353 igmp_group_added(i
);
1357 * Device is about to be destroyed: clean up.
1360 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1362 struct ip_mc_list
*i
;
1366 /* Deactivate timers */
1369 write_lock_bh(&in_dev
->mc_list_lock
);
1370 while ((i
= in_dev
->mc_list
) != NULL
) {
1371 in_dev
->mc_list
= i
->next
;
1372 write_unlock_bh(&in_dev
->mc_list_lock
);
1374 igmp_group_dropped(i
);
1377 write_lock_bh(&in_dev
->mc_list_lock
);
1379 write_unlock_bh(&in_dev
->mc_list_lock
);
1382 static struct in_device
* ip_mc_find_dev(struct ip_mreqn
*imr
)
1384 struct flowi fl
= { .nl_u
= { .ip4_u
=
1385 { .daddr
= imr
->imr_multiaddr
.s_addr
} } };
1387 struct net_device
*dev
= NULL
;
1388 struct in_device
*idev
= NULL
;
1390 if (imr
->imr_ifindex
) {
1391 idev
= inetdev_by_index(&init_net
, imr
->imr_ifindex
);
1396 if (imr
->imr_address
.s_addr
) {
1397 dev
= ip_dev_find(&init_net
, imr
->imr_address
.s_addr
);
1403 if (!dev
&& !ip_route_output_key(&init_net
, &rt
, &fl
)) {
1404 dev
= rt
->u
.dst
.dev
;
1408 imr
->imr_ifindex
= dev
->ifindex
;
1409 idev
= __in_dev_get_rtnl(dev
);
1415 * Join a socket to a group
1417 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1418 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1421 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1424 struct ip_sf_list
*psf
, *psf_prev
;
1428 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1429 if (psf
->sf_inaddr
== *psfsrc
)
1433 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1434 /* source filter not found, or count wrong => bug */
1437 psf
->sf_count
[sfmode
]--;
1438 if (psf
->sf_count
[sfmode
] == 0) {
1439 ip_rt_multicast_event(pmc
->interface
);
1441 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1442 #ifdef CONFIG_IP_MULTICAST
1443 struct in_device
*in_dev
= pmc
->interface
;
1446 /* no more filters for this source */
1448 psf_prev
->sf_next
= psf
->sf_next
;
1450 pmc
->sources
= psf
->sf_next
;
1451 #ifdef CONFIG_IP_MULTICAST
1452 if (psf
->sf_oldin
&&
1453 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1454 psf
->sf_crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1455 IGMP_Unsolicited_Report_Count
;
1456 psf
->sf_next
= pmc
->tomb
;
1466 #ifndef CONFIG_IP_MULTICAST
1467 #define igmp_ifc_event(x) do { } while (0)
1470 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1471 int sfcount
, __be32
*psfsrc
, int delta
)
1473 struct ip_mc_list
*pmc
;
1479 read_lock(&in_dev
->mc_list_lock
);
1480 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1481 if (*pmca
== pmc
->multiaddr
)
1485 /* MCA not found?? bug */
1486 read_unlock(&in_dev
->mc_list_lock
);
1489 spin_lock_bh(&pmc
->lock
);
1490 read_unlock(&in_dev
->mc_list_lock
);
1491 #ifdef CONFIG_IP_MULTICAST
1496 if (!pmc
->sfcount
[sfmode
])
1498 pmc
->sfcount
[sfmode
]--;
1501 for (i
=0; i
<sfcount
; i
++) {
1502 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1504 changerec
|= rv
> 0;
1508 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1509 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1510 pmc
->sfcount
[MCAST_INCLUDE
]) {
1511 #ifdef CONFIG_IP_MULTICAST
1512 struct ip_sf_list
*psf
;
1515 /* filter mode change */
1516 pmc
->sfmode
= MCAST_INCLUDE
;
1517 #ifdef CONFIG_IP_MULTICAST
1518 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1519 IGMP_Unsolicited_Report_Count
;
1520 in_dev
->mr_ifc_count
= pmc
->crcount
;
1521 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1522 psf
->sf_crcount
= 0;
1523 igmp_ifc_event(pmc
->interface
);
1524 } else if (sf_setstate(pmc
) || changerec
) {
1525 igmp_ifc_event(pmc
->interface
);
1529 spin_unlock_bh(&pmc
->lock
);
1534 * Add multicast single-source filter to the interface list
1536 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1537 __be32
*psfsrc
, int delta
)
1539 struct ip_sf_list
*psf
, *psf_prev
;
1542 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1543 if (psf
->sf_inaddr
== *psfsrc
)
1548 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1551 psf
->sf_inaddr
= *psfsrc
;
1553 psf_prev
->sf_next
= psf
;
1557 psf
->sf_count
[sfmode
]++;
1558 if (psf
->sf_count
[sfmode
] == 1) {
1559 ip_rt_multicast_event(pmc
->interface
);
1564 #ifdef CONFIG_IP_MULTICAST
1565 static void sf_markstate(struct ip_mc_list
*pmc
)
1567 struct ip_sf_list
*psf
;
1568 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1570 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1571 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1572 psf
->sf_oldin
= mca_xcount
==
1573 psf
->sf_count
[MCAST_EXCLUDE
] &&
1574 !psf
->sf_count
[MCAST_INCLUDE
];
1576 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1579 static int sf_setstate(struct ip_mc_list
*pmc
)
1581 struct ip_sf_list
*psf
, *dpsf
;
1582 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1583 int qrv
= pmc
->interface
->mr_qrv
;
1587 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1588 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1589 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1590 !psf
->sf_count
[MCAST_INCLUDE
];
1592 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1594 if (!psf
->sf_oldin
) {
1595 struct ip_sf_list
*prev
= NULL
;
1597 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
) {
1598 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1604 prev
->sf_next
= dpsf
->sf_next
;
1606 pmc
->tomb
= dpsf
->sf_next
;
1609 psf
->sf_crcount
= qrv
;
1612 } else if (psf
->sf_oldin
) {
1614 psf
->sf_crcount
= 0;
1616 * add or update "delete" records if an active filter
1619 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
)
1620 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1623 dpsf
= (struct ip_sf_list
*)
1624 kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1628 /* pmc->lock held by callers */
1629 dpsf
->sf_next
= pmc
->tomb
;
1632 dpsf
->sf_crcount
= qrv
;
1641 * Add multicast source filter list to the interface list
1643 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1644 int sfcount
, __be32
*psfsrc
, int delta
)
1646 struct ip_mc_list
*pmc
;
1652 read_lock(&in_dev
->mc_list_lock
);
1653 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1654 if (*pmca
== pmc
->multiaddr
)
1658 /* MCA not found?? bug */
1659 read_unlock(&in_dev
->mc_list_lock
);
1662 spin_lock_bh(&pmc
->lock
);
1663 read_unlock(&in_dev
->mc_list_lock
);
1665 #ifdef CONFIG_IP_MULTICAST
1668 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1670 pmc
->sfcount
[sfmode
]++;
1672 for (i
=0; i
<sfcount
; i
++) {
1673 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1680 pmc
->sfcount
[sfmode
]--;
1682 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1683 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1684 #ifdef CONFIG_IP_MULTICAST
1685 struct ip_sf_list
*psf
;
1686 in_dev
= pmc
->interface
;
1689 /* filter mode change */
1690 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1691 pmc
->sfmode
= MCAST_EXCLUDE
;
1692 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1693 pmc
->sfmode
= MCAST_INCLUDE
;
1694 #ifdef CONFIG_IP_MULTICAST
1695 /* else no filters; keep old mode for reports */
1697 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1698 IGMP_Unsolicited_Report_Count
;
1699 in_dev
->mr_ifc_count
= pmc
->crcount
;
1700 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1701 psf
->sf_crcount
= 0;
1702 igmp_ifc_event(in_dev
);
1703 } else if (sf_setstate(pmc
)) {
1704 igmp_ifc_event(in_dev
);
1707 spin_unlock_bh(&pmc
->lock
);
1711 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1713 struct ip_sf_list
*psf
, *nextpsf
;
1715 for (psf
=pmc
->tomb
; psf
; psf
=nextpsf
) {
1716 nextpsf
= psf
->sf_next
;
1720 for (psf
=pmc
->sources
; psf
; psf
=nextpsf
) {
1721 nextpsf
= psf
->sf_next
;
1724 pmc
->sources
= NULL
;
1725 pmc
->sfmode
= MCAST_EXCLUDE
;
1726 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1727 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1732 * Join a multicast group
1734 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1737 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
1738 struct ip_mc_socklist
*iml
=NULL
, *i
;
1739 struct in_device
*in_dev
;
1740 struct inet_sock
*inet
= inet_sk(sk
);
1744 if (!ipv4_is_multicast(addr
))
1749 in_dev
= ip_mc_find_dev(imr
);
1758 ifindex
= imr
->imr_ifindex
;
1759 for (i
= inet
->mc_list
; i
; i
= i
->next
) {
1760 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1761 i
->multi
.imr_ifindex
== ifindex
)
1766 if (count
>= sysctl_igmp_max_memberships
)
1768 iml
= sock_kmalloc(sk
,sizeof(*iml
),GFP_KERNEL
);
1772 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1773 iml
->next
= inet
->mc_list
;
1775 iml
->sfmode
= MCAST_EXCLUDE
;
1776 inet
->mc_list
= iml
;
1777 ip_mc_inc_group(in_dev
, addr
);
1784 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1785 struct in_device
*in_dev
)
1789 if (iml
->sflist
== NULL
) {
1790 /* any-source empty exclude case */
1791 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1792 iml
->sfmode
, 0, NULL
, 0);
1794 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1795 iml
->sfmode
, iml
->sflist
->sl_count
,
1796 iml
->sflist
->sl_addr
, 0);
1797 sock_kfree_s(sk
, iml
->sflist
, IP_SFLSIZE(iml
->sflist
->sl_max
));
1803 * Ask a socket to leave a group.
1806 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1808 struct inet_sock
*inet
= inet_sk(sk
);
1809 struct ip_mc_socklist
*iml
, **imlp
;
1810 struct in_device
*in_dev
;
1811 __be32 group
= imr
->imr_multiaddr
.s_addr
;
1813 int ret
= -EADDRNOTAVAIL
;
1816 in_dev
= ip_mc_find_dev(imr
);
1817 ifindex
= imr
->imr_ifindex
;
1818 for (imlp
= &inet
->mc_list
; (iml
= *imlp
) != NULL
; imlp
= &iml
->next
) {
1819 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
1822 if (iml
->multi
.imr_ifindex
!= ifindex
)
1824 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
1825 iml
->multi
.imr_address
.s_addr
)
1828 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1833 ip_mc_dec_group(in_dev
, group
);
1835 sock_kfree_s(sk
, iml
, sizeof(*iml
));
1844 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1845 ip_mreq_source
*mreqs
, int ifindex
)
1848 struct ip_mreqn imr
;
1849 __be32 addr
= mreqs
->imr_multiaddr
;
1850 struct ip_mc_socklist
*pmc
;
1851 struct in_device
*in_dev
= NULL
;
1852 struct inet_sock
*inet
= inet_sk(sk
);
1853 struct ip_sf_socklist
*psl
;
1857 if (!ipv4_is_multicast(addr
))
1862 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1863 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1864 imr
.imr_ifindex
= ifindex
;
1865 in_dev
= ip_mc_find_dev(&imr
);
1871 err
= -EADDRNOTAVAIL
;
1873 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1874 if (pmc
->multi
.imr_multiaddr
.s_addr
== imr
.imr_multiaddr
.s_addr
1875 && pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
1878 if (!pmc
) { /* must have a prior join */
1882 /* if a source filter was set, must be the same mode as before */
1884 if (pmc
->sfmode
!= omode
) {
1888 } else if (pmc
->sfmode
!= omode
) {
1889 /* allow mode switches for empty-set filters */
1890 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
1891 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
1893 pmc
->sfmode
= omode
;
1899 goto done
; /* err = -EADDRNOTAVAIL */
1901 for (i
=0; i
<psl
->sl_count
; i
++) {
1902 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1907 if (rv
) /* source not found */
1908 goto done
; /* err = -EADDRNOTAVAIL */
1910 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1911 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
1916 /* update the interface filter */
1917 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1918 &mreqs
->imr_sourceaddr
, 1);
1920 for (j
=i
+1; j
<psl
->sl_count
; j
++)
1921 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
1926 /* else, add a new source to the filter */
1928 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
1932 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
1933 struct ip_sf_socklist
*newpsl
;
1934 int count
= IP_SFBLOCK
;
1937 count
+= psl
->sl_max
;
1938 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
1943 newpsl
->sl_max
= count
;
1944 newpsl
->sl_count
= count
- IP_SFBLOCK
;
1946 for (i
=0; i
<psl
->sl_count
; i
++)
1947 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
1948 sock_kfree_s(sk
, psl
, IP_SFLSIZE(psl
->sl_max
));
1950 pmc
->sflist
= psl
= newpsl
;
1952 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
1953 for (i
=0; i
<psl
->sl_count
; i
++) {
1954 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1959 if (rv
== 0) /* address already there is an error */
1961 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
1962 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
1963 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
1966 /* update the interface list */
1967 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1968 &mreqs
->imr_sourceaddr
, 1);
1972 return ip_mc_leave_group(sk
, &imr
);
1976 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
1979 struct ip_mreqn imr
;
1980 __be32 addr
= msf
->imsf_multiaddr
;
1981 struct ip_mc_socklist
*pmc
;
1982 struct in_device
*in_dev
;
1983 struct inet_sock
*inet
= inet_sk(sk
);
1984 struct ip_sf_socklist
*newpsl
, *psl
;
1987 if (!ipv4_is_multicast(addr
))
1989 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
1990 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
1995 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
1996 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
1997 imr
.imr_ifindex
= ifindex
;
1998 in_dev
= ip_mc_find_dev(&imr
);
2005 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2006 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2011 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2012 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2013 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2016 if (!pmc
) { /* must have a prior join */
2020 if (msf
->imsf_numsrc
) {
2021 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2027 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2028 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2029 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2030 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2031 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2033 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2038 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2039 msf
->imsf_fmode
, 0, NULL
, 0);
2043 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2044 psl
->sl_count
, psl
->sl_addr
, 0);
2045 sock_kfree_s(sk
, psl
, IP_SFLSIZE(psl
->sl_max
));
2047 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2049 pmc
->sflist
= newpsl
;
2050 pmc
->sfmode
= msf
->imsf_fmode
;
2055 err
= ip_mc_leave_group(sk
, &imr
);
2059 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2060 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2062 int err
, len
, count
, copycount
;
2063 struct ip_mreqn imr
;
2064 __be32 addr
= msf
->imsf_multiaddr
;
2065 struct ip_mc_socklist
*pmc
;
2066 struct in_device
*in_dev
;
2067 struct inet_sock
*inet
= inet_sk(sk
);
2068 struct ip_sf_socklist
*psl
;
2070 if (!ipv4_is_multicast(addr
))
2075 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2076 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2077 imr
.imr_ifindex
= 0;
2078 in_dev
= ip_mc_find_dev(&imr
);
2084 err
= -EADDRNOTAVAIL
;
2086 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2087 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2088 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2091 if (!pmc
) /* must have a prior join */
2093 msf
->imsf_fmode
= pmc
->sfmode
;
2100 count
= psl
->sl_count
;
2102 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2103 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2104 msf
->imsf_numsrc
= count
;
2105 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2106 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2110 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2118 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2119 struct group_filter __user
*optval
, int __user
*optlen
)
2121 int err
, i
, count
, copycount
;
2122 struct sockaddr_in
*psin
;
2124 struct ip_mc_socklist
*pmc
;
2125 struct inet_sock
*inet
= inet_sk(sk
);
2126 struct ip_sf_socklist
*psl
;
2128 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2129 if (psin
->sin_family
!= AF_INET
)
2131 addr
= psin
->sin_addr
.s_addr
;
2132 if (!ipv4_is_multicast(addr
))
2137 err
= -EADDRNOTAVAIL
;
2139 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2140 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2141 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2144 if (!pmc
) /* must have a prior join */
2146 gsf
->gf_fmode
= pmc
->sfmode
;
2149 count
= psl
? psl
->sl_count
: 0;
2150 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2151 gsf
->gf_numsrc
= count
;
2152 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2153 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2156 for (i
=0; i
<copycount
; i
++) {
2157 struct sockaddr_storage ss
;
2159 psin
= (struct sockaddr_in
*)&ss
;
2160 memset(&ss
, 0, sizeof(ss
));
2161 psin
->sin_family
= AF_INET
;
2162 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2163 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2173 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2175 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2177 struct inet_sock
*inet
= inet_sk(sk
);
2178 struct ip_mc_socklist
*pmc
;
2179 struct ip_sf_socklist
*psl
;
2182 if (!ipv4_is_multicast(loc_addr
))
2185 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2186 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2187 pmc
->multi
.imr_ifindex
== dif
)
2194 return pmc
->sfmode
== MCAST_EXCLUDE
;
2196 for (i
=0; i
<psl
->sl_count
; i
++) {
2197 if (psl
->sl_addr
[i
] == rmt_addr
)
2200 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2202 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2208 * A socket is closing.
2211 void ip_mc_drop_socket(struct sock
*sk
)
2213 struct inet_sock
*inet
= inet_sk(sk
);
2214 struct ip_mc_socklist
*iml
;
2216 if (inet
->mc_list
== NULL
)
2220 while ((iml
= inet
->mc_list
) != NULL
) {
2221 struct in_device
*in_dev
;
2222 inet
->mc_list
= iml
->next
;
2224 in_dev
= inetdev_by_index(&init_net
, iml
->multi
.imr_ifindex
);
2225 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2226 if (in_dev
!= NULL
) {
2227 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2230 sock_kfree_s(sk
, iml
, sizeof(*iml
));
2235 int ip_check_mc(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u16 proto
)
2237 struct ip_mc_list
*im
;
2238 struct ip_sf_list
*psf
;
2241 read_lock(&in_dev
->mc_list_lock
);
2242 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
2243 if (im
->multiaddr
== mc_addr
)
2246 if (im
&& proto
== IPPROTO_IGMP
) {
2250 for (psf
=im
->sources
; psf
; psf
=psf
->sf_next
) {
2251 if (psf
->sf_inaddr
== src_addr
)
2255 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2256 psf
->sf_count
[MCAST_EXCLUDE
] !=
2257 im
->sfcount
[MCAST_EXCLUDE
];
2259 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2261 rv
= 1; /* unspecified source; tentatively allow */
2263 read_unlock(&in_dev
->mc_list_lock
);
2267 #if defined(CONFIG_PROC_FS)
2268 struct igmp_mc_iter_state
{
2269 struct net_device
*dev
;
2270 struct in_device
*in_dev
;
2273 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2275 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2277 struct ip_mc_list
*im
= NULL
;
2278 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2280 state
->in_dev
= NULL
;
2281 for_each_netdev(&init_net
, state
->dev
) {
2282 struct in_device
*in_dev
;
2283 in_dev
= in_dev_get(state
->dev
);
2286 read_lock(&in_dev
->mc_list_lock
);
2287 im
= in_dev
->mc_list
;
2289 state
->in_dev
= in_dev
;
2292 read_unlock(&in_dev
->mc_list_lock
);
2298 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2300 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2303 if (likely(state
->in_dev
!= NULL
)) {
2304 read_unlock(&state
->in_dev
->mc_list_lock
);
2305 in_dev_put(state
->in_dev
);
2307 state
->dev
= next_net_device(state
->dev
);
2309 state
->in_dev
= NULL
;
2312 state
->in_dev
= in_dev_get(state
->dev
);
2315 read_lock(&state
->in_dev
->mc_list_lock
);
2316 im
= state
->in_dev
->mc_list
;
2321 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2323 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2325 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2327 return pos
? NULL
: im
;
2330 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2331 __acquires(dev_base_lock
)
2333 read_lock(&dev_base_lock
);
2334 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2337 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2339 struct ip_mc_list
*im
;
2340 if (v
== SEQ_START_TOKEN
)
2341 im
= igmp_mc_get_first(seq
);
2343 im
= igmp_mc_get_next(seq
, v
);
2348 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2349 __releases(dev_base_lock
)
2351 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2352 if (likely(state
->in_dev
!= NULL
)) {
2353 read_unlock(&state
->in_dev
->mc_list_lock
);
2354 in_dev_put(state
->in_dev
);
2355 state
->in_dev
= NULL
;
2358 read_unlock(&dev_base_lock
);
2361 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2363 if (v
== SEQ_START_TOKEN
)
2365 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2367 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2368 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2370 #ifdef CONFIG_IP_MULTICAST
2371 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2372 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2378 if (state
->in_dev
->mc_list
== im
) {
2379 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2380 state
->dev
->ifindex
, state
->dev
->name
, state
->dev
->mc_count
, querier
);
2384 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2385 im
->multiaddr
, im
->users
,
2386 im
->tm_running
, im
->tm_running
?
2387 jiffies_to_clock_t(im
->timer
.expires
-jiffies
) : 0,
2393 static const struct seq_operations igmp_mc_seq_ops
= {
2394 .start
= igmp_mc_seq_start
,
2395 .next
= igmp_mc_seq_next
,
2396 .stop
= igmp_mc_seq_stop
,
2397 .show
= igmp_mc_seq_show
,
2400 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2402 return seq_open_private(file
, &igmp_mc_seq_ops
,
2403 sizeof(struct igmp_mc_iter_state
));
2406 static const struct file_operations igmp_mc_seq_fops
= {
2407 .owner
= THIS_MODULE
,
2408 .open
= igmp_mc_seq_open
,
2410 .llseek
= seq_lseek
,
2411 .release
= seq_release_private
,
2414 struct igmp_mcf_iter_state
{
2415 struct net_device
*dev
;
2416 struct in_device
*idev
;
2417 struct ip_mc_list
*im
;
2420 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2422 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2424 struct ip_sf_list
*psf
= NULL
;
2425 struct ip_mc_list
*im
= NULL
;
2426 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2430 for_each_netdev(&init_net
, state
->dev
) {
2431 struct in_device
*idev
;
2432 idev
= in_dev_get(state
->dev
);
2433 if (unlikely(idev
== NULL
))
2435 read_lock(&idev
->mc_list_lock
);
2437 if (likely(im
!= NULL
)) {
2438 spin_lock_bh(&im
->lock
);
2440 if (likely(psf
!= NULL
)) {
2445 spin_unlock_bh(&im
->lock
);
2447 read_unlock(&idev
->mc_list_lock
);
2453 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2455 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2459 spin_unlock_bh(&state
->im
->lock
);
2460 state
->im
= state
->im
->next
;
2461 while (!state
->im
) {
2462 if (likely(state
->idev
!= NULL
)) {
2463 read_unlock(&state
->idev
->mc_list_lock
);
2464 in_dev_put(state
->idev
);
2466 state
->dev
= next_net_device(state
->dev
);
2471 state
->idev
= in_dev_get(state
->dev
);
2474 read_lock(&state
->idev
->mc_list_lock
);
2475 state
->im
= state
->idev
->mc_list
;
2479 spin_lock_bh(&state
->im
->lock
);
2480 psf
= state
->im
->sources
;
2486 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2488 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2490 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2492 return pos
? NULL
: psf
;
2495 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2497 read_lock(&dev_base_lock
);
2498 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2501 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2503 struct ip_sf_list
*psf
;
2504 if (v
== SEQ_START_TOKEN
)
2505 psf
= igmp_mcf_get_first(seq
);
2507 psf
= igmp_mcf_get_next(seq
, v
);
2512 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2514 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2515 if (likely(state
->im
!= NULL
)) {
2516 spin_unlock_bh(&state
->im
->lock
);
2519 if (likely(state
->idev
!= NULL
)) {
2520 read_unlock(&state
->idev
->mc_list_lock
);
2521 in_dev_put(state
->idev
);
2525 read_unlock(&dev_base_lock
);
2528 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2530 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2531 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2533 if (v
== SEQ_START_TOKEN
) {
2536 "%10s %10s %6s %6s\n", "Idx",
2538 "SRC", "INC", "EXC");
2542 "0x%08x %6lu %6lu\n",
2543 state
->dev
->ifindex
, state
->dev
->name
,
2544 ntohl(state
->im
->multiaddr
),
2545 ntohl(psf
->sf_inaddr
),
2546 psf
->sf_count
[MCAST_INCLUDE
],
2547 psf
->sf_count
[MCAST_EXCLUDE
]);
2552 static const struct seq_operations igmp_mcf_seq_ops
= {
2553 .start
= igmp_mcf_seq_start
,
2554 .next
= igmp_mcf_seq_next
,
2555 .stop
= igmp_mcf_seq_stop
,
2556 .show
= igmp_mcf_seq_show
,
2559 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2561 return seq_open_private(file
, &igmp_mcf_seq_ops
,
2562 sizeof(struct igmp_mcf_iter_state
));
2565 static const struct file_operations igmp_mcf_seq_fops
= {
2566 .owner
= THIS_MODULE
,
2567 .open
= igmp_mcf_seq_open
,
2569 .llseek
= seq_lseek
,
2570 .release
= seq_release_private
,
2573 int __init
igmp_mc_proc_init(void)
2575 proc_net_fops_create(&init_net
, "igmp", S_IRUGO
, &igmp_mc_seq_fops
);
2576 proc_net_fops_create(&init_net
, "mcfilter", S_IRUGO
, &igmp_mcf_seq_fops
);
2581 EXPORT_SYMBOL(ip_mc_dec_group
);
2582 EXPORT_SYMBOL(ip_mc_inc_group
);
2583 EXPORT_SYMBOL(ip_mc_join_group
);
2584 EXPORT_SYMBOL(ip_mc_rejoin_group
);