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.
12 * Alan Cox <Alan.Cox@linux.org>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
73 #include <linux/module.h>
74 #include <asm/uaccess.h>
75 #include <asm/system.h>
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/jiffies.h>
79 #include <linux/string.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
83 #include <linux/inet.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/if_arp.h>
89 #include <linux/rtnetlink.h>
90 #include <linux/times.h>
92 #include <net/net_namespace.h>
95 #include <net/protocol.h>
96 #include <net/route.h>
98 #include <net/checksum.h>
99 #include <linux/netfilter_ipv4.h>
100 #ifdef CONFIG_IP_MROUTE
101 #include <linux/mroute.h>
103 #ifdef CONFIG_PROC_FS
104 #include <linux/proc_fs.h>
105 #include <linux/seq_file.h>
108 #define IP_MAX_MEMBERSHIPS 20
109 #define IP_MAX_MSF 10
111 #ifdef CONFIG_IP_MULTICAST
112 /* Parameter names and values are taken from igmp-v2-06 draft */
114 #define IGMP_V1_Router_Present_Timeout (400*HZ)
115 #define IGMP_V2_Router_Present_Timeout (400*HZ)
116 #define IGMP_Unsolicited_Report_Interval (10*HZ)
117 #define IGMP_Query_Response_Interval (10*HZ)
118 #define IGMP_Unsolicited_Report_Count 2
121 #define IGMP_Initial_Report_Delay (1)
123 /* IGMP_Initial_Report_Delay is not from IGMP specs!
124 * IGMP specs require to report membership immediately after
125 * joining a group, but we delay the first report by a
126 * small interval. It seems more natural and still does not
127 * contradict to specs provided this delay is small enough.
130 #define IGMP_V1_SEEN(in_dev) \
131 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
132 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
133 ((in_dev)->mr_v1_seen && \
134 time_before(jiffies, (in_dev)->mr_v1_seen)))
135 #define IGMP_V2_SEEN(in_dev) \
136 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
137 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
141 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
142 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
);
143 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
144 static int sf_setstate(struct ip_mc_list
*pmc
);
145 static void sf_markstate(struct ip_mc_list
*pmc
);
147 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
148 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
149 int sfcount
, __be32
*psfsrc
, int delta
);
151 static void ip_ma_put(struct ip_mc_list
*im
)
153 if (atomic_dec_and_test(&im
->refcnt
)) {
154 in_dev_put(im
->interface
);
159 #ifdef CONFIG_IP_MULTICAST
165 static __inline__
void igmp_stop_timer(struct ip_mc_list
*im
)
167 spin_lock_bh(&im
->lock
);
168 if (del_timer(&im
->timer
))
169 atomic_dec(&im
->refcnt
);
172 im
->unsolicit_count
= 0;
173 spin_unlock_bh(&im
->lock
);
176 /* It must be called with locked im->lock */
177 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
179 int tv
=net_random() % max_delay
;
182 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
183 atomic_inc(&im
->refcnt
);
186 static void igmp_gq_start_timer(struct in_device
*in_dev
)
188 int tv
= net_random() % in_dev
->mr_maxdelay
;
190 in_dev
->mr_gq_running
= 1;
191 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
195 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
197 int tv
= net_random() % delay
;
199 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
203 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
205 spin_lock_bh(&im
->lock
);
206 im
->unsolicit_count
= 0;
207 if (del_timer(&im
->timer
)) {
208 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
209 add_timer(&im
->timer
);
211 spin_unlock_bh(&im
->lock
);
214 atomic_dec(&im
->refcnt
);
216 igmp_start_timer(im
, max_delay
);
217 spin_unlock_bh(&im
->lock
);
222 * Send an IGMP report.
225 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
228 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
229 int gdeleted
, int sdeleted
)
232 case IGMPV3_MODE_IS_INCLUDE
:
233 case IGMPV3_MODE_IS_EXCLUDE
:
234 if (gdeleted
|| sdeleted
)
236 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
237 if (pmc
->sfmode
== MCAST_INCLUDE
)
239 /* don't include if this source is excluded
242 if (psf
->sf_count
[MCAST_INCLUDE
])
243 return type
== IGMPV3_MODE_IS_INCLUDE
;
244 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
245 psf
->sf_count
[MCAST_EXCLUDE
];
248 case IGMPV3_CHANGE_TO_INCLUDE
:
249 if (gdeleted
|| sdeleted
)
251 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
252 case IGMPV3_CHANGE_TO_EXCLUDE
:
253 if (gdeleted
|| sdeleted
)
255 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
256 psf
->sf_count
[MCAST_INCLUDE
])
258 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
259 psf
->sf_count
[MCAST_EXCLUDE
];
260 case IGMPV3_ALLOW_NEW_SOURCES
:
261 if (gdeleted
|| !psf
->sf_crcount
)
263 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
264 case IGMPV3_BLOCK_OLD_SOURCES
:
265 if (pmc
->sfmode
== MCAST_INCLUDE
)
266 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
267 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
273 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
275 struct ip_sf_list
*psf
;
278 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
279 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
286 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, int size
)
291 struct igmpv3_report
*pig
;
293 skb
= alloc_skb(size
+ LL_ALLOCATED_SPACE(dev
), GFP_ATOMIC
);
298 struct flowi fl
= { .oif
= dev
->ifindex
,
300 .daddr
= IGMPV3_ALL_MCR
} },
301 .proto
= IPPROTO_IGMP
};
302 if (ip_route_output_key(&init_net
, &rt
, &fl
)) {
307 if (rt
->rt_src
== 0) {
313 skb
->dst
= &rt
->u
.dst
;
316 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
318 skb_reset_network_header(skb
);
320 skb_put(skb
, sizeof(struct iphdr
) + 4);
323 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
325 pip
->frag_off
= htons(IP_DF
);
327 pip
->daddr
= rt
->rt_dst
;
328 pip
->saddr
= rt
->rt_src
;
329 pip
->protocol
= IPPROTO_IGMP
;
330 pip
->tot_len
= 0; /* filled in later */
331 ip_select_ident(pip
, &rt
->u
.dst
, NULL
);
332 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
333 ((u8
*)&pip
[1])[1] = 4;
334 ((u8
*)&pip
[1])[2] = 0;
335 ((u8
*)&pip
[1])[3] = 0;
337 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
338 skb_put(skb
, sizeof(*pig
));
339 pig
= igmpv3_report_hdr(skb
);
340 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
348 static int igmpv3_sendpack(struct sk_buff
*skb
)
350 struct igmphdr
*pig
= igmp_hdr(skb
);
351 const int igmplen
= skb
->tail
- skb
->transport_header
;
353 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
355 return ip_local_out(skb
);
358 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
360 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
,type
,gdel
,sdel
);
363 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
364 int type
, struct igmpv3_grec
**ppgr
)
366 struct net_device
*dev
= pmc
->interface
->dev
;
367 struct igmpv3_report
*pih
;
368 struct igmpv3_grec
*pgr
;
371 skb
= igmpv3_newpack(dev
, dev
->mtu
);
374 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
375 pgr
->grec_type
= type
;
376 pgr
->grec_auxwords
= 0;
378 pgr
->grec_mca
= pmc
->multiaddr
;
379 pih
= igmpv3_report_hdr(skb
);
380 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
385 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
386 skb_tailroom(skb)) : 0)
388 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
389 int type
, int gdeleted
, int sdeleted
)
391 struct net_device
*dev
= pmc
->interface
->dev
;
392 struct igmpv3_report
*pih
;
393 struct igmpv3_grec
*pgr
= NULL
;
394 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
395 int scount
, stotal
, first
, isquery
, truncate
;
397 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
400 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
401 type
== IGMPV3_MODE_IS_EXCLUDE
;
402 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
403 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
407 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
412 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
414 /* EX and TO_EX get a fresh packet, if needed */
416 if (pih
&& pih
->ngrec
&&
417 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
419 igmpv3_sendpack(skb
);
420 skb
= igmpv3_newpack(dev
, dev
->mtu
);
425 for (psf
=*psf_list
; psf
; psf
=psf_next
) {
428 psf_next
= psf
->sf_next
;
430 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
435 /* clear marks on query responses */
439 if (AVAILABLE(skb
) < sizeof(__be32
) +
440 first
*sizeof(struct igmpv3_grec
)) {
441 if (truncate
&& !first
)
442 break; /* truncate these */
444 pgr
->grec_nsrcs
= htons(scount
);
446 igmpv3_sendpack(skb
);
447 skb
= igmpv3_newpack(dev
, dev
->mtu
);
452 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
457 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
458 *psrc
= psf
->sf_inaddr
;
460 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
461 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
463 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
465 psf_prev
->sf_next
= psf
->sf_next
;
467 *psf_list
= psf
->sf_next
;
477 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
478 type
== IGMPV3_BLOCK_OLD_SOURCES
)
480 if (pmc
->crcount
|| isquery
) {
481 /* make sure we have room for group header */
482 if (skb
&& AVAILABLE(skb
)<sizeof(struct igmpv3_grec
)) {
483 igmpv3_sendpack(skb
);
484 skb
= NULL
; /* add_grhead will get a new one */
486 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
490 pgr
->grec_nsrcs
= htons(scount
);
493 pmc
->gsquery
= 0; /* clear query state on report */
497 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
499 struct sk_buff
*skb
= NULL
;
503 read_lock(&in_dev
->mc_list_lock
);
504 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
505 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
507 spin_lock_bh(&pmc
->lock
);
508 if (pmc
->sfcount
[MCAST_EXCLUDE
])
509 type
= IGMPV3_MODE_IS_EXCLUDE
;
511 type
= IGMPV3_MODE_IS_INCLUDE
;
512 skb
= add_grec(skb
, pmc
, type
, 0, 0);
513 spin_unlock_bh(&pmc
->lock
);
515 read_unlock(&in_dev
->mc_list_lock
);
517 spin_lock_bh(&pmc
->lock
);
518 if (pmc
->sfcount
[MCAST_EXCLUDE
])
519 type
= IGMPV3_MODE_IS_EXCLUDE
;
521 type
= IGMPV3_MODE_IS_INCLUDE
;
522 skb
= add_grec(skb
, pmc
, type
, 0, 0);
523 spin_unlock_bh(&pmc
->lock
);
527 return igmpv3_sendpack(skb
);
531 * remove zero-count source records from a source filter list
533 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
535 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
538 for (psf
=*ppsf
; psf
; psf
= psf_next
) {
539 psf_next
= psf
->sf_next
;
540 if (psf
->sf_crcount
== 0) {
542 psf_prev
->sf_next
= psf
->sf_next
;
544 *ppsf
= psf
->sf_next
;
551 static void igmpv3_send_cr(struct in_device
*in_dev
)
553 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
554 struct sk_buff
*skb
= NULL
;
557 read_lock(&in_dev
->mc_list_lock
);
558 spin_lock_bh(&in_dev
->mc_tomb_lock
);
562 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc_next
) {
563 pmc_next
= pmc
->next
;
564 if (pmc
->sfmode
== MCAST_INCLUDE
) {
565 type
= IGMPV3_BLOCK_OLD_SOURCES
;
566 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
567 skb
= add_grec(skb
, pmc
, type
, 1, 0);
568 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
571 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
572 type
= IGMPV3_CHANGE_TO_INCLUDE
;
573 skb
= add_grec(skb
, pmc
, type
, 1, 0);
576 if (pmc
->crcount
== 0) {
577 igmpv3_clear_zeros(&pmc
->tomb
);
578 igmpv3_clear_zeros(&pmc
->sources
);
581 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
583 pmc_prev
->next
= pmc_next
;
585 in_dev
->mc_tomb
= pmc_next
;
586 in_dev_put(pmc
->interface
);
591 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
594 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
595 spin_lock_bh(&pmc
->lock
);
596 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
597 type
= IGMPV3_BLOCK_OLD_SOURCES
;
598 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
600 type
= IGMPV3_ALLOW_NEW_SOURCES
;
601 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
603 skb
= add_grec(skb
, pmc
, type
, 0, 0);
604 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
606 /* filter mode changes */
608 if (pmc
->sfmode
== MCAST_EXCLUDE
)
609 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
611 type
= IGMPV3_CHANGE_TO_INCLUDE
;
612 skb
= add_grec(skb
, pmc
, type
, 0, 0);
615 spin_unlock_bh(&pmc
->lock
);
617 read_unlock(&in_dev
->mc_list_lock
);
621 (void) igmpv3_sendpack(skb
);
624 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
631 struct net_device
*dev
= in_dev
->dev
;
632 __be32 group
= pmc
? pmc
->multiaddr
: 0;
635 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
636 return igmpv3_send_report(in_dev
, pmc
);
637 else if (type
== IGMP_HOST_LEAVE_MESSAGE
)
638 dst
= IGMP_ALL_ROUTER
;
643 struct flowi fl
= { .oif
= dev
->ifindex
,
644 .nl_u
= { .ip4_u
= { .daddr
= dst
} },
645 .proto
= IPPROTO_IGMP
};
646 if (ip_route_output_key(&init_net
, &rt
, &fl
))
649 if (rt
->rt_src
== 0) {
654 skb
=alloc_skb(IGMP_SIZE
+LL_ALLOCATED_SPACE(dev
), GFP_ATOMIC
);
660 skb
->dst
= &rt
->u
.dst
;
662 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
664 skb_reset_network_header(skb
);
666 skb_put(skb
, sizeof(struct iphdr
) + 4);
669 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
671 iph
->frag_off
= htons(IP_DF
);
674 iph
->saddr
= rt
->rt_src
;
675 iph
->protocol
= IPPROTO_IGMP
;
676 ip_select_ident(iph
, &rt
->u
.dst
, NULL
);
677 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
678 ((u8
*)&iph
[1])[1] = 4;
679 ((u8
*)&iph
[1])[2] = 0;
680 ((u8
*)&iph
[1])[3] = 0;
682 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
687 ih
->csum
=ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
689 return ip_local_out(skb
);
692 static void igmp_gq_timer_expire(unsigned long data
)
694 struct in_device
*in_dev
= (struct in_device
*)data
;
696 in_dev
->mr_gq_running
= 0;
697 igmpv3_send_report(in_dev
, NULL
);
698 __in_dev_put(in_dev
);
701 static void igmp_ifc_timer_expire(unsigned long data
)
703 struct in_device
*in_dev
= (struct in_device
*)data
;
705 igmpv3_send_cr(in_dev
);
706 if (in_dev
->mr_ifc_count
) {
707 in_dev
->mr_ifc_count
--;
708 igmp_ifc_start_timer(in_dev
, IGMP_Unsolicited_Report_Interval
);
710 __in_dev_put(in_dev
);
713 static void igmp_ifc_event(struct in_device
*in_dev
)
715 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
717 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
718 IGMP_Unsolicited_Report_Count
;
719 igmp_ifc_start_timer(in_dev
, 1);
723 static void igmp_timer_expire(unsigned long data
)
725 struct ip_mc_list
*im
=(struct ip_mc_list
*)data
;
726 struct in_device
*in_dev
= im
->interface
;
728 spin_lock(&im
->lock
);
731 if (im
->unsolicit_count
) {
732 im
->unsolicit_count
--;
733 igmp_start_timer(im
, IGMP_Unsolicited_Report_Interval
);
736 spin_unlock(&im
->lock
);
738 if (IGMP_V1_SEEN(in_dev
))
739 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
740 else if (IGMP_V2_SEEN(in_dev
))
741 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
743 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
748 /* mark EXCLUDE-mode sources */
749 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
751 struct ip_sf_list
*psf
;
755 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
758 for (i
=0; i
<nsrcs
; i
++) {
759 /* skip inactive filters */
760 if (pmc
->sfcount
[MCAST_INCLUDE
] ||
761 pmc
->sfcount
[MCAST_EXCLUDE
] !=
762 psf
->sf_count
[MCAST_EXCLUDE
])
764 if (srcs
[i
] == psf
->sf_inaddr
) {
771 if (scount
== nsrcs
) /* all sources excluded */
776 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
778 struct ip_sf_list
*psf
;
781 if (pmc
->sfmode
== MCAST_EXCLUDE
)
782 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
784 /* mark INCLUDE-mode sources */
786 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
789 for (i
=0; i
<nsrcs
; i
++)
790 if (srcs
[i
] == psf
->sf_inaddr
) {
804 static void igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
806 struct ip_mc_list
*im
;
808 /* Timers are only set for non-local groups */
810 if (group
== IGMP_ALL_HOSTS
)
813 read_lock(&in_dev
->mc_list_lock
);
814 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
815 if (im
->multiaddr
== group
) {
820 read_unlock(&in_dev
->mc_list_lock
);
823 static void igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
826 struct igmphdr
*ih
= igmp_hdr(skb
);
827 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
828 struct ip_mc_list
*im
;
829 __be32 group
= ih
->group
;
836 /* Alas, old v1 router presents here. */
838 max_delay
= IGMP_Query_Response_Interval
;
839 in_dev
->mr_v1_seen
= jiffies
+
840 IGMP_V1_Router_Present_Timeout
;
843 /* v2 router present */
844 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
845 in_dev
->mr_v2_seen
= jiffies
+
846 IGMP_V2_Router_Present_Timeout
;
848 /* cancel the interface change timer */
849 in_dev
->mr_ifc_count
= 0;
850 if (del_timer(&in_dev
->mr_ifc_timer
))
851 __in_dev_put(in_dev
);
852 /* clear deleted report items */
853 igmpv3_clear_delrec(in_dev
);
854 } else if (len
< 12) {
855 return; /* ignore bogus packet; freed by caller */
857 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
860 ih3
= igmpv3_query_hdr(skb
);
862 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
863 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
865 ih3
= igmpv3_query_hdr(skb
);
868 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
870 max_delay
= 1; /* can't mod w/ 0 */
871 in_dev
->mr_maxdelay
= max_delay
;
873 in_dev
->mr_qrv
= ih3
->qrv
;
874 if (!group
) { /* general query */
876 return; /* no sources allowed */
877 igmp_gq_start_timer(in_dev
);
880 /* mark sources to include, if group & source-specific */
881 mark
= ih3
->nsrcs
!= 0;
885 * - Start the timers in all of our membership records
886 * that the query applies to for the interface on
887 * which the query arrived excl. those that belong
888 * to a "local" group (224.0.0.X)
889 * - For timers already running check if they need to
891 * - Use the igmp->igmp_code field as the maximum
894 read_lock(&in_dev
->mc_list_lock
);
895 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
898 if (group
&& group
!= im
->multiaddr
)
900 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
902 spin_lock_bh(&im
->lock
);
904 im
->gsquery
= im
->gsquery
&& mark
;
907 changed
= !im
->gsquery
||
908 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
909 spin_unlock_bh(&im
->lock
);
911 igmp_mod_timer(im
, max_delay
);
913 read_unlock(&in_dev
->mc_list_lock
);
916 int igmp_rcv(struct sk_buff
*skb
)
918 /* This basically follows the spec line by line -- see RFC1112 */
920 struct in_device
*in_dev
= in_dev_get(skb
->dev
);
926 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
929 switch (skb
->ip_summed
) {
930 case CHECKSUM_COMPLETE
:
931 if (!csum_fold(skb
->csum
))
936 if (__skb_checksum_complete(skb
))
942 case IGMP_HOST_MEMBERSHIP_QUERY
:
943 igmp_heard_query(in_dev
, skb
, len
);
945 case IGMP_HOST_MEMBERSHIP_REPORT
:
946 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
947 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
948 /* Is it our report looped back? */
949 if (skb
->rtable
->fl
.iif
== 0)
951 /* don't rely on MC router hearing unicast reports */
952 if (skb
->pkt_type
== PACKET_MULTICAST
||
953 skb
->pkt_type
== PACKET_BROADCAST
)
954 igmp_heard_report(in_dev
, ih
->group
);
957 #ifdef CONFIG_IP_PIMSM_V1
959 return pim_rcv_v1(skb
);
963 case IGMP_HOST_LEAVE_MESSAGE
:
965 case IGMP_MTRACE_RESP
:
982 * Add a filter to a device
985 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
987 char buf
[MAX_ADDR_LEN
];
988 struct net_device
*dev
= in_dev
->dev
;
990 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
991 We will get multicast token leakage, when IFF_MULTICAST
992 is changed. This check should be done in dev->set_multicast_list
993 routine. Something sort of:
994 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
997 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
998 dev_mc_add(dev
,buf
,dev
->addr_len
,0);
1002 * Remove a filter from a device
1005 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1007 char buf
[MAX_ADDR_LEN
];
1008 struct net_device
*dev
= in_dev
->dev
;
1010 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1011 dev_mc_delete(dev
,buf
,dev
->addr_len
,0);
1014 #ifdef CONFIG_IP_MULTICAST
1016 * deleted ip_mc_list manipulation
1018 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1020 struct ip_mc_list
*pmc
;
1022 /* this is an "ip_mc_list" for convenience; only the fields below
1023 * are actually used. In particular, the refcnt and users are not
1024 * used for management of the delete list. Using the same structure
1025 * for deleted items allows change reports to use common code with
1026 * non-deleted or query-response MCA's.
1028 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1031 spin_lock_bh(&im
->lock
);
1032 pmc
->interface
= im
->interface
;
1033 in_dev_hold(in_dev
);
1034 pmc
->multiaddr
= im
->multiaddr
;
1035 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1036 IGMP_Unsolicited_Report_Count
;
1037 pmc
->sfmode
= im
->sfmode
;
1038 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1039 struct ip_sf_list
*psf
;
1041 pmc
->tomb
= im
->tomb
;
1042 pmc
->sources
= im
->sources
;
1043 im
->tomb
= im
->sources
= NULL
;
1044 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1045 psf
->sf_crcount
= pmc
->crcount
;
1047 spin_unlock_bh(&im
->lock
);
1049 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1050 pmc
->next
= in_dev
->mc_tomb
;
1051 in_dev
->mc_tomb
= pmc
;
1052 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1055 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1057 struct ip_mc_list
*pmc
, *pmc_prev
;
1058 struct ip_sf_list
*psf
, *psf_next
;
1060 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1062 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
1063 if (pmc
->multiaddr
== multiaddr
)
1069 pmc_prev
->next
= pmc
->next
;
1071 in_dev
->mc_tomb
= pmc
->next
;
1073 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1075 for (psf
=pmc
->tomb
; psf
; psf
=psf_next
) {
1076 psf_next
= psf
->sf_next
;
1079 in_dev_put(pmc
->interface
);
1084 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1086 struct ip_mc_list
*pmc
, *nextpmc
;
1088 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1089 pmc
= in_dev
->mc_tomb
;
1090 in_dev
->mc_tomb
= NULL
;
1091 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1093 for (; pmc
; pmc
= nextpmc
) {
1094 nextpmc
= pmc
->next
;
1095 ip_mc_clear_src(pmc
);
1096 in_dev_put(pmc
->interface
);
1099 /* clear dead sources, too */
1100 read_lock(&in_dev
->mc_list_lock
);
1101 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1102 struct ip_sf_list
*psf
, *psf_next
;
1104 spin_lock_bh(&pmc
->lock
);
1107 spin_unlock_bh(&pmc
->lock
);
1108 for (; psf
; psf
=psf_next
) {
1109 psf_next
= psf
->sf_next
;
1113 read_unlock(&in_dev
->mc_list_lock
);
1117 static void igmp_group_dropped(struct ip_mc_list
*im
)
1119 struct in_device
*in_dev
= im
->interface
;
1120 #ifdef CONFIG_IP_MULTICAST
1126 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1129 #ifdef CONFIG_IP_MULTICAST
1130 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1133 reporter
= im
->reporter
;
1134 igmp_stop_timer(im
);
1136 if (!in_dev
->dead
) {
1137 if (IGMP_V1_SEEN(in_dev
))
1139 if (IGMP_V2_SEEN(in_dev
)) {
1141 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1145 igmpv3_add_delrec(in_dev
, im
);
1147 igmp_ifc_event(in_dev
);
1151 ip_mc_clear_src(im
);
1154 static void igmp_group_added(struct ip_mc_list
*im
)
1156 struct in_device
*in_dev
= im
->interface
;
1158 if (im
->loaded
== 0) {
1160 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1163 #ifdef CONFIG_IP_MULTICAST
1164 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1169 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1170 spin_lock_bh(&im
->lock
);
1171 igmp_start_timer(im
, IGMP_Initial_Report_Delay
);
1172 spin_unlock_bh(&im
->lock
);
1177 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1178 IGMP_Unsolicited_Report_Count
;
1179 igmp_ifc_event(in_dev
);
1185 * Multicast list managers
1190 * A socket has joined a multicast group on device dev.
1193 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1195 struct ip_mc_list
*im
;
1199 if (dev_net(in_dev
->dev
) != &init_net
)
1202 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
1203 if (im
->multiaddr
== addr
) {
1205 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1210 im
= kmalloc(sizeof(*im
), GFP_KERNEL
);
1215 im
->interface
=in_dev
;
1216 in_dev_hold(in_dev
);
1218 /* initial mode is (EX, empty) */
1219 im
->sfmode
= MCAST_EXCLUDE
;
1220 im
->sfcount
[MCAST_INCLUDE
] = 0;
1221 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1225 atomic_set(&im
->refcnt
, 1);
1226 spin_lock_init(&im
->lock
);
1227 #ifdef CONFIG_IP_MULTICAST
1229 setup_timer(&im
->timer
, &igmp_timer_expire
, (unsigned long)im
);
1230 im
->unsolicit_count
= IGMP_Unsolicited_Report_Count
;
1235 write_lock_bh(&in_dev
->mc_list_lock
);
1236 im
->next
=in_dev
->mc_list
;
1238 write_unlock_bh(&in_dev
->mc_list_lock
);
1239 #ifdef CONFIG_IP_MULTICAST
1240 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1242 igmp_group_added(im
);
1244 ip_rt_multicast_event(in_dev
);
1250 * Resend IGMP JOIN report; used for bonding.
1252 void ip_mc_rejoin_group(struct ip_mc_list
*im
)
1254 #ifdef CONFIG_IP_MULTICAST
1255 struct in_device
*in_dev
= im
->interface
;
1257 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1260 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1261 igmp_mod_timer(im
, IGMP_Initial_Report_Delay
);
1265 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1266 IGMP_Unsolicited_Report_Count
;
1267 igmp_ifc_event(in_dev
);
1272 * A socket has left a multicast group on device dev
1275 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1277 struct ip_mc_list
*i
, **ip
;
1281 if (dev_net(in_dev
->dev
) != &init_net
)
1284 for (ip
=&in_dev
->mc_list
; (i
=*ip
)!=NULL
; ip
=&i
->next
) {
1285 if (i
->multiaddr
==addr
) {
1286 if (--i
->users
== 0) {
1287 write_lock_bh(&in_dev
->mc_list_lock
);
1289 write_unlock_bh(&in_dev
->mc_list_lock
);
1290 igmp_group_dropped(i
);
1293 ip_rt_multicast_event(in_dev
);
1303 /* Device going down */
1305 void ip_mc_down(struct in_device
*in_dev
)
1307 struct ip_mc_list
*i
;
1311 if (dev_net(in_dev
->dev
) != &init_net
)
1314 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1315 igmp_group_dropped(i
);
1317 #ifdef CONFIG_IP_MULTICAST
1318 in_dev
->mr_ifc_count
= 0;
1319 if (del_timer(&in_dev
->mr_ifc_timer
))
1320 __in_dev_put(in_dev
);
1321 in_dev
->mr_gq_running
= 0;
1322 if (del_timer(&in_dev
->mr_gq_timer
))
1323 __in_dev_put(in_dev
);
1324 igmpv3_clear_delrec(in_dev
);
1327 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1330 void ip_mc_init_dev(struct in_device
*in_dev
)
1334 if (dev_net(in_dev
->dev
) != &init_net
)
1337 in_dev
->mc_tomb
= NULL
;
1338 #ifdef CONFIG_IP_MULTICAST
1339 in_dev
->mr_gq_running
= 0;
1340 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1341 (unsigned long)in_dev
);
1342 in_dev
->mr_ifc_count
= 0;
1343 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1344 (unsigned long)in_dev
);
1345 in_dev
->mr_qrv
= IGMP_Unsolicited_Report_Count
;
1348 rwlock_init(&in_dev
->mc_list_lock
);
1349 spin_lock_init(&in_dev
->mc_tomb_lock
);
1352 /* Device going up */
1354 void ip_mc_up(struct in_device
*in_dev
)
1356 struct ip_mc_list
*i
;
1360 if (dev_net(in_dev
->dev
) != &init_net
)
1363 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1365 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1366 igmp_group_added(i
);
1370 * Device is about to be destroyed: clean up.
1373 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1375 struct ip_mc_list
*i
;
1379 if (dev_net(in_dev
->dev
) != &init_net
)
1382 /* Deactivate timers */
1385 write_lock_bh(&in_dev
->mc_list_lock
);
1386 while ((i
= in_dev
->mc_list
) != NULL
) {
1387 in_dev
->mc_list
= i
->next
;
1388 write_unlock_bh(&in_dev
->mc_list_lock
);
1390 igmp_group_dropped(i
);
1393 write_lock_bh(&in_dev
->mc_list_lock
);
1395 write_unlock_bh(&in_dev
->mc_list_lock
);
1398 static struct in_device
* ip_mc_find_dev(struct ip_mreqn
*imr
)
1400 struct flowi fl
= { .nl_u
= { .ip4_u
=
1401 { .daddr
= imr
->imr_multiaddr
.s_addr
} } };
1403 struct net_device
*dev
= NULL
;
1404 struct in_device
*idev
= NULL
;
1406 if (imr
->imr_ifindex
) {
1407 idev
= inetdev_by_index(&init_net
, imr
->imr_ifindex
);
1412 if (imr
->imr_address
.s_addr
) {
1413 dev
= ip_dev_find(&init_net
, imr
->imr_address
.s_addr
);
1419 if (!dev
&& !ip_route_output_key(&init_net
, &rt
, &fl
)) {
1420 dev
= rt
->u
.dst
.dev
;
1424 imr
->imr_ifindex
= dev
->ifindex
;
1425 idev
= __in_dev_get_rtnl(dev
);
1431 * Join a socket to a group
1433 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1434 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1437 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1440 struct ip_sf_list
*psf
, *psf_prev
;
1444 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1445 if (psf
->sf_inaddr
== *psfsrc
)
1449 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1450 /* source filter not found, or count wrong => bug */
1453 psf
->sf_count
[sfmode
]--;
1454 if (psf
->sf_count
[sfmode
] == 0) {
1455 ip_rt_multicast_event(pmc
->interface
);
1457 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1458 #ifdef CONFIG_IP_MULTICAST
1459 struct in_device
*in_dev
= pmc
->interface
;
1462 /* no more filters for this source */
1464 psf_prev
->sf_next
= psf
->sf_next
;
1466 pmc
->sources
= psf
->sf_next
;
1467 #ifdef CONFIG_IP_MULTICAST
1468 if (psf
->sf_oldin
&&
1469 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1470 psf
->sf_crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1471 IGMP_Unsolicited_Report_Count
;
1472 psf
->sf_next
= pmc
->tomb
;
1482 #ifndef CONFIG_IP_MULTICAST
1483 #define igmp_ifc_event(x) do { } while (0)
1486 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1487 int sfcount
, __be32
*psfsrc
, int delta
)
1489 struct ip_mc_list
*pmc
;
1495 read_lock(&in_dev
->mc_list_lock
);
1496 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1497 if (*pmca
== pmc
->multiaddr
)
1501 /* MCA not found?? bug */
1502 read_unlock(&in_dev
->mc_list_lock
);
1505 spin_lock_bh(&pmc
->lock
);
1506 read_unlock(&in_dev
->mc_list_lock
);
1507 #ifdef CONFIG_IP_MULTICAST
1512 if (!pmc
->sfcount
[sfmode
])
1514 pmc
->sfcount
[sfmode
]--;
1517 for (i
=0; i
<sfcount
; i
++) {
1518 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1520 changerec
|= rv
> 0;
1524 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1525 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1526 pmc
->sfcount
[MCAST_INCLUDE
]) {
1527 #ifdef CONFIG_IP_MULTICAST
1528 struct ip_sf_list
*psf
;
1531 /* filter mode change */
1532 pmc
->sfmode
= MCAST_INCLUDE
;
1533 #ifdef CONFIG_IP_MULTICAST
1534 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1535 IGMP_Unsolicited_Report_Count
;
1536 in_dev
->mr_ifc_count
= pmc
->crcount
;
1537 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1538 psf
->sf_crcount
= 0;
1539 igmp_ifc_event(pmc
->interface
);
1540 } else if (sf_setstate(pmc
) || changerec
) {
1541 igmp_ifc_event(pmc
->interface
);
1545 spin_unlock_bh(&pmc
->lock
);
1550 * Add multicast single-source filter to the interface list
1552 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1553 __be32
*psfsrc
, int delta
)
1555 struct ip_sf_list
*psf
, *psf_prev
;
1558 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1559 if (psf
->sf_inaddr
== *psfsrc
)
1564 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1567 psf
->sf_inaddr
= *psfsrc
;
1569 psf_prev
->sf_next
= psf
;
1573 psf
->sf_count
[sfmode
]++;
1574 if (psf
->sf_count
[sfmode
] == 1) {
1575 ip_rt_multicast_event(pmc
->interface
);
1580 #ifdef CONFIG_IP_MULTICAST
1581 static void sf_markstate(struct ip_mc_list
*pmc
)
1583 struct ip_sf_list
*psf
;
1584 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1586 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1587 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1588 psf
->sf_oldin
= mca_xcount
==
1589 psf
->sf_count
[MCAST_EXCLUDE
] &&
1590 !psf
->sf_count
[MCAST_INCLUDE
];
1592 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1595 static int sf_setstate(struct ip_mc_list
*pmc
)
1597 struct ip_sf_list
*psf
, *dpsf
;
1598 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1599 int qrv
= pmc
->interface
->mr_qrv
;
1603 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1604 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1605 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1606 !psf
->sf_count
[MCAST_INCLUDE
];
1608 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1610 if (!psf
->sf_oldin
) {
1611 struct ip_sf_list
*prev
= NULL
;
1613 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
) {
1614 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1620 prev
->sf_next
= dpsf
->sf_next
;
1622 pmc
->tomb
= dpsf
->sf_next
;
1625 psf
->sf_crcount
= qrv
;
1628 } else if (psf
->sf_oldin
) {
1630 psf
->sf_crcount
= 0;
1632 * add or update "delete" records if an active filter
1635 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
)
1636 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1639 dpsf
= (struct ip_sf_list
*)
1640 kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1644 /* pmc->lock held by callers */
1645 dpsf
->sf_next
= pmc
->tomb
;
1648 dpsf
->sf_crcount
= qrv
;
1657 * Add multicast source filter list to the interface list
1659 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1660 int sfcount
, __be32
*psfsrc
, int delta
)
1662 struct ip_mc_list
*pmc
;
1668 read_lock(&in_dev
->mc_list_lock
);
1669 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1670 if (*pmca
== pmc
->multiaddr
)
1674 /* MCA not found?? bug */
1675 read_unlock(&in_dev
->mc_list_lock
);
1678 spin_lock_bh(&pmc
->lock
);
1679 read_unlock(&in_dev
->mc_list_lock
);
1681 #ifdef CONFIG_IP_MULTICAST
1684 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1686 pmc
->sfcount
[sfmode
]++;
1688 for (i
=0; i
<sfcount
; i
++) {
1689 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1696 pmc
->sfcount
[sfmode
]--;
1698 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1699 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1700 #ifdef CONFIG_IP_MULTICAST
1701 struct ip_sf_list
*psf
;
1702 in_dev
= pmc
->interface
;
1705 /* filter mode change */
1706 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1707 pmc
->sfmode
= MCAST_EXCLUDE
;
1708 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1709 pmc
->sfmode
= MCAST_INCLUDE
;
1710 #ifdef CONFIG_IP_MULTICAST
1711 /* else no filters; keep old mode for reports */
1713 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1714 IGMP_Unsolicited_Report_Count
;
1715 in_dev
->mr_ifc_count
= pmc
->crcount
;
1716 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1717 psf
->sf_crcount
= 0;
1718 igmp_ifc_event(in_dev
);
1719 } else if (sf_setstate(pmc
)) {
1720 igmp_ifc_event(in_dev
);
1723 spin_unlock_bh(&pmc
->lock
);
1727 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1729 struct ip_sf_list
*psf
, *nextpsf
;
1731 for (psf
=pmc
->tomb
; psf
; psf
=nextpsf
) {
1732 nextpsf
= psf
->sf_next
;
1736 for (psf
=pmc
->sources
; psf
; psf
=nextpsf
) {
1737 nextpsf
= psf
->sf_next
;
1740 pmc
->sources
= NULL
;
1741 pmc
->sfmode
= MCAST_EXCLUDE
;
1742 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1743 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1748 * Join a multicast group
1750 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1753 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
1754 struct ip_mc_socklist
*iml
=NULL
, *i
;
1755 struct in_device
*in_dev
;
1756 struct inet_sock
*inet
= inet_sk(sk
);
1760 if (!ipv4_is_multicast(addr
))
1763 if (sock_net(sk
) != &init_net
)
1764 return -EPROTONOSUPPORT
;
1768 in_dev
= ip_mc_find_dev(imr
);
1777 ifindex
= imr
->imr_ifindex
;
1778 for (i
= inet
->mc_list
; i
; i
= i
->next
) {
1779 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1780 i
->multi
.imr_ifindex
== ifindex
)
1785 if (count
>= sysctl_igmp_max_memberships
)
1787 iml
= sock_kmalloc(sk
,sizeof(*iml
),GFP_KERNEL
);
1791 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1792 iml
->next
= inet
->mc_list
;
1794 iml
->sfmode
= MCAST_EXCLUDE
;
1795 inet
->mc_list
= iml
;
1796 ip_mc_inc_group(in_dev
, addr
);
1803 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1804 struct in_device
*in_dev
)
1808 if (iml
->sflist
== NULL
) {
1809 /* any-source empty exclude case */
1810 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1811 iml
->sfmode
, 0, NULL
, 0);
1813 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1814 iml
->sfmode
, iml
->sflist
->sl_count
,
1815 iml
->sflist
->sl_addr
, 0);
1816 sock_kfree_s(sk
, iml
->sflist
, IP_SFLSIZE(iml
->sflist
->sl_max
));
1822 * Ask a socket to leave a group.
1825 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1827 struct inet_sock
*inet
= inet_sk(sk
);
1828 struct ip_mc_socklist
*iml
, **imlp
;
1829 struct in_device
*in_dev
;
1830 __be32 group
= imr
->imr_multiaddr
.s_addr
;
1832 int ret
= -EADDRNOTAVAIL
;
1834 if (sock_net(sk
) != &init_net
)
1835 return -EPROTONOSUPPORT
;
1838 in_dev
= ip_mc_find_dev(imr
);
1839 ifindex
= imr
->imr_ifindex
;
1840 for (imlp
= &inet
->mc_list
; (iml
= *imlp
) != NULL
; imlp
= &iml
->next
) {
1841 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
1844 if (iml
->multi
.imr_ifindex
!= ifindex
)
1846 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
1847 iml
->multi
.imr_address
.s_addr
)
1850 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1855 ip_mc_dec_group(in_dev
, group
);
1857 sock_kfree_s(sk
, iml
, sizeof(*iml
));
1866 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1867 ip_mreq_source
*mreqs
, int ifindex
)
1870 struct ip_mreqn imr
;
1871 __be32 addr
= mreqs
->imr_multiaddr
;
1872 struct ip_mc_socklist
*pmc
;
1873 struct in_device
*in_dev
= NULL
;
1874 struct inet_sock
*inet
= inet_sk(sk
);
1875 struct ip_sf_socklist
*psl
;
1879 if (!ipv4_is_multicast(addr
))
1882 if (sock_net(sk
) != &init_net
)
1883 return -EPROTONOSUPPORT
;
1887 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1888 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1889 imr
.imr_ifindex
= ifindex
;
1890 in_dev
= ip_mc_find_dev(&imr
);
1896 err
= -EADDRNOTAVAIL
;
1898 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1899 if (pmc
->multi
.imr_multiaddr
.s_addr
== imr
.imr_multiaddr
.s_addr
1900 && pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
1903 if (!pmc
) { /* must have a prior join */
1907 /* if a source filter was set, must be the same mode as before */
1909 if (pmc
->sfmode
!= omode
) {
1913 } else if (pmc
->sfmode
!= omode
) {
1914 /* allow mode switches for empty-set filters */
1915 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
1916 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
1918 pmc
->sfmode
= omode
;
1924 goto done
; /* err = -EADDRNOTAVAIL */
1926 for (i
=0; i
<psl
->sl_count
; i
++) {
1927 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1932 if (rv
) /* source not found */
1933 goto done
; /* err = -EADDRNOTAVAIL */
1935 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1936 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
1941 /* update the interface filter */
1942 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1943 &mreqs
->imr_sourceaddr
, 1);
1945 for (j
=i
+1; j
<psl
->sl_count
; j
++)
1946 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
1951 /* else, add a new source to the filter */
1953 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
1957 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
1958 struct ip_sf_socklist
*newpsl
;
1959 int count
= IP_SFBLOCK
;
1962 count
+= psl
->sl_max
;
1963 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
1968 newpsl
->sl_max
= count
;
1969 newpsl
->sl_count
= count
- IP_SFBLOCK
;
1971 for (i
=0; i
<psl
->sl_count
; i
++)
1972 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
1973 sock_kfree_s(sk
, psl
, IP_SFLSIZE(psl
->sl_max
));
1975 pmc
->sflist
= psl
= newpsl
;
1977 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
1978 for (i
=0; i
<psl
->sl_count
; i
++) {
1979 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1984 if (rv
== 0) /* address already there is an error */
1986 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
1987 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
1988 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
1991 /* update the interface list */
1992 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1993 &mreqs
->imr_sourceaddr
, 1);
1997 return ip_mc_leave_group(sk
, &imr
);
2001 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2004 struct ip_mreqn imr
;
2005 __be32 addr
= msf
->imsf_multiaddr
;
2006 struct ip_mc_socklist
*pmc
;
2007 struct in_device
*in_dev
;
2008 struct inet_sock
*inet
= inet_sk(sk
);
2009 struct ip_sf_socklist
*newpsl
, *psl
;
2012 if (!ipv4_is_multicast(addr
))
2014 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2015 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2018 if (sock_net(sk
) != &init_net
)
2019 return -EPROTONOSUPPORT
;
2023 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2024 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2025 imr
.imr_ifindex
= ifindex
;
2026 in_dev
= ip_mc_find_dev(&imr
);
2033 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2034 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2039 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2040 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2041 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2044 if (!pmc
) { /* must have a prior join */
2048 if (msf
->imsf_numsrc
) {
2049 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2055 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2056 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2057 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2058 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2059 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2061 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2066 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2067 msf
->imsf_fmode
, 0, NULL
, 0);
2071 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2072 psl
->sl_count
, psl
->sl_addr
, 0);
2073 sock_kfree_s(sk
, psl
, IP_SFLSIZE(psl
->sl_max
));
2075 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2077 pmc
->sflist
= newpsl
;
2078 pmc
->sfmode
= msf
->imsf_fmode
;
2083 err
= ip_mc_leave_group(sk
, &imr
);
2087 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2088 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2090 int err
, len
, count
, copycount
;
2091 struct ip_mreqn imr
;
2092 __be32 addr
= msf
->imsf_multiaddr
;
2093 struct ip_mc_socklist
*pmc
;
2094 struct in_device
*in_dev
;
2095 struct inet_sock
*inet
= inet_sk(sk
);
2096 struct ip_sf_socklist
*psl
;
2098 if (!ipv4_is_multicast(addr
))
2101 if (sock_net(sk
) != &init_net
)
2102 return -EPROTONOSUPPORT
;
2106 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2107 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2108 imr
.imr_ifindex
= 0;
2109 in_dev
= ip_mc_find_dev(&imr
);
2115 err
= -EADDRNOTAVAIL
;
2117 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2118 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2119 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2122 if (!pmc
) /* must have a prior join */
2124 msf
->imsf_fmode
= pmc
->sfmode
;
2131 count
= psl
->sl_count
;
2133 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2134 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2135 msf
->imsf_numsrc
= count
;
2136 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2137 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2141 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2149 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2150 struct group_filter __user
*optval
, int __user
*optlen
)
2152 int err
, i
, count
, copycount
;
2153 struct sockaddr_in
*psin
;
2155 struct ip_mc_socklist
*pmc
;
2156 struct inet_sock
*inet
= inet_sk(sk
);
2157 struct ip_sf_socklist
*psl
;
2159 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2160 if (psin
->sin_family
!= AF_INET
)
2162 addr
= psin
->sin_addr
.s_addr
;
2163 if (!ipv4_is_multicast(addr
))
2166 if (sock_net(sk
) != &init_net
)
2167 return -EPROTONOSUPPORT
;
2171 err
= -EADDRNOTAVAIL
;
2173 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2174 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2175 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2178 if (!pmc
) /* must have a prior join */
2180 gsf
->gf_fmode
= pmc
->sfmode
;
2183 count
= psl
? psl
->sl_count
: 0;
2184 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2185 gsf
->gf_numsrc
= count
;
2186 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2187 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2190 for (i
=0; i
<copycount
; i
++) {
2191 struct sockaddr_storage ss
;
2193 psin
= (struct sockaddr_in
*)&ss
;
2194 memset(&ss
, 0, sizeof(ss
));
2195 psin
->sin_family
= AF_INET
;
2196 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2197 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2207 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2209 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2211 struct inet_sock
*inet
= inet_sk(sk
);
2212 struct ip_mc_socklist
*pmc
;
2213 struct ip_sf_socklist
*psl
;
2216 if (!ipv4_is_multicast(loc_addr
))
2219 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2220 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2221 pmc
->multi
.imr_ifindex
== dif
)
2228 return pmc
->sfmode
== MCAST_EXCLUDE
;
2230 for (i
=0; i
<psl
->sl_count
; i
++) {
2231 if (psl
->sl_addr
[i
] == rmt_addr
)
2234 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2236 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2242 * A socket is closing.
2245 void ip_mc_drop_socket(struct sock
*sk
)
2247 struct inet_sock
*inet
= inet_sk(sk
);
2248 struct ip_mc_socklist
*iml
;
2250 if (inet
->mc_list
== NULL
)
2253 if (sock_net(sk
) != &init_net
)
2257 while ((iml
= inet
->mc_list
) != NULL
) {
2258 struct in_device
*in_dev
;
2259 inet
->mc_list
= iml
->next
;
2261 in_dev
= inetdev_by_index(&init_net
, iml
->multi
.imr_ifindex
);
2262 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2263 if (in_dev
!= NULL
) {
2264 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2267 sock_kfree_s(sk
, iml
, sizeof(*iml
));
2272 int ip_check_mc(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u16 proto
)
2274 struct ip_mc_list
*im
;
2275 struct ip_sf_list
*psf
;
2278 read_lock(&in_dev
->mc_list_lock
);
2279 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
2280 if (im
->multiaddr
== mc_addr
)
2283 if (im
&& proto
== IPPROTO_IGMP
) {
2287 for (psf
=im
->sources
; psf
; psf
=psf
->sf_next
) {
2288 if (psf
->sf_inaddr
== src_addr
)
2292 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2293 psf
->sf_count
[MCAST_EXCLUDE
] !=
2294 im
->sfcount
[MCAST_EXCLUDE
];
2296 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2298 rv
= 1; /* unspecified source; tentatively allow */
2300 read_unlock(&in_dev
->mc_list_lock
);
2304 #if defined(CONFIG_PROC_FS)
2305 struct igmp_mc_iter_state
{
2306 struct net_device
*dev
;
2307 struct in_device
*in_dev
;
2310 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2312 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2314 struct ip_mc_list
*im
= NULL
;
2315 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2317 state
->in_dev
= NULL
;
2318 for_each_netdev(&init_net
, state
->dev
) {
2319 struct in_device
*in_dev
;
2320 in_dev
= in_dev_get(state
->dev
);
2323 read_lock(&in_dev
->mc_list_lock
);
2324 im
= in_dev
->mc_list
;
2326 state
->in_dev
= in_dev
;
2329 read_unlock(&in_dev
->mc_list_lock
);
2335 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2337 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2340 if (likely(state
->in_dev
!= NULL
)) {
2341 read_unlock(&state
->in_dev
->mc_list_lock
);
2342 in_dev_put(state
->in_dev
);
2344 state
->dev
= next_net_device(state
->dev
);
2346 state
->in_dev
= NULL
;
2349 state
->in_dev
= in_dev_get(state
->dev
);
2352 read_lock(&state
->in_dev
->mc_list_lock
);
2353 im
= state
->in_dev
->mc_list
;
2358 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2360 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2362 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2364 return pos
? NULL
: im
;
2367 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2368 __acquires(dev_base_lock
)
2370 read_lock(&dev_base_lock
);
2371 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2374 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2376 struct ip_mc_list
*im
;
2377 if (v
== SEQ_START_TOKEN
)
2378 im
= igmp_mc_get_first(seq
);
2380 im
= igmp_mc_get_next(seq
, v
);
2385 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2386 __releases(dev_base_lock
)
2388 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2389 if (likely(state
->in_dev
!= NULL
)) {
2390 read_unlock(&state
->in_dev
->mc_list_lock
);
2391 in_dev_put(state
->in_dev
);
2392 state
->in_dev
= NULL
;
2395 read_unlock(&dev_base_lock
);
2398 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2400 if (v
== SEQ_START_TOKEN
)
2402 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2404 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2405 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2407 #ifdef CONFIG_IP_MULTICAST
2408 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2409 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2415 if (state
->in_dev
->mc_list
== im
) {
2416 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2417 state
->dev
->ifindex
, state
->dev
->name
, state
->dev
->mc_count
, querier
);
2421 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2422 im
->multiaddr
, im
->users
,
2423 im
->tm_running
, im
->tm_running
?
2424 jiffies_to_clock_t(im
->timer
.expires
-jiffies
) : 0,
2430 static const struct seq_operations igmp_mc_seq_ops
= {
2431 .start
= igmp_mc_seq_start
,
2432 .next
= igmp_mc_seq_next
,
2433 .stop
= igmp_mc_seq_stop
,
2434 .show
= igmp_mc_seq_show
,
2437 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2439 return seq_open_private(file
, &igmp_mc_seq_ops
,
2440 sizeof(struct igmp_mc_iter_state
));
2443 static const struct file_operations igmp_mc_seq_fops
= {
2444 .owner
= THIS_MODULE
,
2445 .open
= igmp_mc_seq_open
,
2447 .llseek
= seq_lseek
,
2448 .release
= seq_release_private
,
2451 struct igmp_mcf_iter_state
{
2452 struct net_device
*dev
;
2453 struct in_device
*idev
;
2454 struct ip_mc_list
*im
;
2457 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2459 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2461 struct ip_sf_list
*psf
= NULL
;
2462 struct ip_mc_list
*im
= NULL
;
2463 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2467 for_each_netdev(&init_net
, state
->dev
) {
2468 struct in_device
*idev
;
2469 idev
= in_dev_get(state
->dev
);
2470 if (unlikely(idev
== NULL
))
2472 read_lock(&idev
->mc_list_lock
);
2474 if (likely(im
!= NULL
)) {
2475 spin_lock_bh(&im
->lock
);
2477 if (likely(psf
!= NULL
)) {
2482 spin_unlock_bh(&im
->lock
);
2484 read_unlock(&idev
->mc_list_lock
);
2490 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2492 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2496 spin_unlock_bh(&state
->im
->lock
);
2497 state
->im
= state
->im
->next
;
2498 while (!state
->im
) {
2499 if (likely(state
->idev
!= NULL
)) {
2500 read_unlock(&state
->idev
->mc_list_lock
);
2501 in_dev_put(state
->idev
);
2503 state
->dev
= next_net_device(state
->dev
);
2508 state
->idev
= in_dev_get(state
->dev
);
2511 read_lock(&state
->idev
->mc_list_lock
);
2512 state
->im
= state
->idev
->mc_list
;
2516 spin_lock_bh(&state
->im
->lock
);
2517 psf
= state
->im
->sources
;
2523 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2525 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2527 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2529 return pos
? NULL
: psf
;
2532 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2534 read_lock(&dev_base_lock
);
2535 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2538 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2540 struct ip_sf_list
*psf
;
2541 if (v
== SEQ_START_TOKEN
)
2542 psf
= igmp_mcf_get_first(seq
);
2544 psf
= igmp_mcf_get_next(seq
, v
);
2549 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2551 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2552 if (likely(state
->im
!= NULL
)) {
2553 spin_unlock_bh(&state
->im
->lock
);
2556 if (likely(state
->idev
!= NULL
)) {
2557 read_unlock(&state
->idev
->mc_list_lock
);
2558 in_dev_put(state
->idev
);
2562 read_unlock(&dev_base_lock
);
2565 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2567 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2568 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2570 if (v
== SEQ_START_TOKEN
) {
2573 "%10s %10s %6s %6s\n", "Idx",
2575 "SRC", "INC", "EXC");
2579 "0x%08x %6lu %6lu\n",
2580 state
->dev
->ifindex
, state
->dev
->name
,
2581 ntohl(state
->im
->multiaddr
),
2582 ntohl(psf
->sf_inaddr
),
2583 psf
->sf_count
[MCAST_INCLUDE
],
2584 psf
->sf_count
[MCAST_EXCLUDE
]);
2589 static const struct seq_operations igmp_mcf_seq_ops
= {
2590 .start
= igmp_mcf_seq_start
,
2591 .next
= igmp_mcf_seq_next
,
2592 .stop
= igmp_mcf_seq_stop
,
2593 .show
= igmp_mcf_seq_show
,
2596 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2598 return seq_open_private(file
, &igmp_mcf_seq_ops
,
2599 sizeof(struct igmp_mcf_iter_state
));
2602 static const struct file_operations igmp_mcf_seq_fops
= {
2603 .owner
= THIS_MODULE
,
2604 .open
= igmp_mcf_seq_open
,
2606 .llseek
= seq_lseek
,
2607 .release
= seq_release_private
,
2610 int __init
igmp_mc_proc_init(void)
2612 proc_net_fops_create(&init_net
, "igmp", S_IRUGO
, &igmp_mc_seq_fops
);
2613 proc_net_fops_create(&init_net
, "mcfilter", S_IRUGO
, &igmp_mcf_seq_fops
);
2618 EXPORT_SYMBOL(ip_mc_dec_group
);
2619 EXPORT_SYMBOL(ip_mc_inc_group
);
2620 EXPORT_SYMBOL(ip_mc_join_group
);
2621 EXPORT_SYMBOL(ip_mc_rejoin_group
);