xref: /linux/net/ipv4/igmp.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1*1da177e4SLinus Torvalds /*
2*1da177e4SLinus Torvalds  *	Linux NET3:	Internet Group Management Protocol  [IGMP]
3*1da177e4SLinus Torvalds  *
4*1da177e4SLinus Torvalds  *	This code implements the IGMP protocol as defined in RFC1112. There has
5*1da177e4SLinus Torvalds  *	been a further revision of this protocol since which is now supported.
6*1da177e4SLinus Torvalds  *
7*1da177e4SLinus Torvalds  *	If you have trouble with this module be careful what gcc you have used,
8*1da177e4SLinus Torvalds  *	the older version didn't come out right using gcc 2.5.8, the newer one
9*1da177e4SLinus Torvalds  *	seems to fall out with gcc 2.6.2.
10*1da177e4SLinus Torvalds  *
11*1da177e4SLinus Torvalds  *	Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
12*1da177e4SLinus Torvalds  *
13*1da177e4SLinus Torvalds  *	Authors:
14*1da177e4SLinus Torvalds  *		Alan Cox <Alan.Cox@linux.org>
15*1da177e4SLinus Torvalds  *
16*1da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
17*1da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
18*1da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
19*1da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
20*1da177e4SLinus Torvalds  *
21*1da177e4SLinus Torvalds  *	Fixes:
22*1da177e4SLinus Torvalds  *
23*1da177e4SLinus Torvalds  *		Alan Cox	:	Added lots of __inline__ to optimise
24*1da177e4SLinus Torvalds  *					the memory usage of all the tiny little
25*1da177e4SLinus Torvalds  *					functions.
26*1da177e4SLinus Torvalds  *		Alan Cox	:	Dumped the header building experiment.
27*1da177e4SLinus Torvalds  *		Alan Cox	:	Minor tweaks ready for multicast routing
28*1da177e4SLinus Torvalds  *					and extended IGMP protocol.
29*1da177e4SLinus Torvalds  *		Alan Cox	:	Removed a load of inline directives. Gcc 2.5.8
30*1da177e4SLinus Torvalds  *					writes utterly bogus code otherwise (sigh)
31*1da177e4SLinus Torvalds  *					fixed IGMP loopback to behave in the manner
32*1da177e4SLinus Torvalds  *					desired by mrouted, fixed the fact it has been
33*1da177e4SLinus Torvalds  *					broken since 1.3.6 and cleaned up a few minor
34*1da177e4SLinus Torvalds  *					points.
35*1da177e4SLinus Torvalds  *
36*1da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Tried to revise IGMP to Version 2
37*1da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
38*1da177e4SLinus Torvalds  *					The enhancements are mainly based on Steve Deering's
39*1da177e4SLinus Torvalds  * 					ipmulti-3.5 source code.
40*1da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added the igmp_get_mrouter_info and
41*1da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		igmp_set_mrouter_info to keep track of
42*1da177e4SLinus Torvalds  *					the mrouted version on that device.
43*1da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added the max_resp_time parameter to
44*1da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		igmp_heard_query(). Using this parameter
45*1da177e4SLinus Torvalds  *					to identify the multicast router version
46*1da177e4SLinus Torvalds  *					and do what the IGMP version 2 specified.
47*1da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added a timer to revert to IGMP V2 router
48*1da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		if the specified time expired.
49*1da177e4SLinus Torvalds  *		Alan Cox	:	Stop IGMP from 0.0.0.0 being accepted.
50*1da177e4SLinus Torvalds  *		Alan Cox	:	Use GFP_ATOMIC in the right places.
51*1da177e4SLinus Torvalds  *		Christian Daudt :	igmp timer wasn't set for local group
52*1da177e4SLinus Torvalds  *					memberships but was being deleted,
53*1da177e4SLinus Torvalds  *					which caused a "del_timer() called
54*1da177e4SLinus Torvalds  *					from %p with timer not initialized\n"
55*1da177e4SLinus Torvalds  *					message (960131).
56*1da177e4SLinus Torvalds  *		Christian Daudt :	removed del_timer from
57*1da177e4SLinus Torvalds  *					igmp_timer_expire function (960205).
58*1da177e4SLinus Torvalds  *             Christian Daudt :       igmp_heard_report now only calls
59*1da177e4SLinus Torvalds  *                                     igmp_timer_expire if tm->running is
60*1da177e4SLinus Torvalds  *                                     true (960216).
61*1da177e4SLinus Torvalds  *		Malcolm Beattie :	ttl comparison wrong in igmp_rcv made
62*1da177e4SLinus Torvalds  *					igmp_heard_query never trigger. Expiry
63*1da177e4SLinus Torvalds  *					miscalculation fixed in igmp_heard_query
64*1da177e4SLinus Torvalds  *					and random() made to return unsigned to
65*1da177e4SLinus Torvalds  *					prevent negative expiry times.
66*1da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Wrong group leaving behaviour, backport
67*1da177e4SLinus Torvalds  *					fix from pending 2.1.x patches.
68*1da177e4SLinus Torvalds  *		Alan Cox:		Forget to enable FDDI support earlier.
69*1da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Fixed leaving groups on device down.
70*1da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Accordance to igmp-v2-06 draft.
71*1da177e4SLinus Torvalds  *		David L Stevens:	IGMPv3 support, with help from
72*1da177e4SLinus Torvalds  *					Vinay Kulkarni
73*1da177e4SLinus Torvalds  */
74*1da177e4SLinus Torvalds 
75*1da177e4SLinus Torvalds #include <linux/config.h>
76*1da177e4SLinus Torvalds #include <linux/module.h>
77*1da177e4SLinus Torvalds #include <asm/uaccess.h>
78*1da177e4SLinus Torvalds #include <asm/system.h>
79*1da177e4SLinus Torvalds #include <linux/types.h>
80*1da177e4SLinus Torvalds #include <linux/kernel.h>
81*1da177e4SLinus Torvalds #include <linux/jiffies.h>
82*1da177e4SLinus Torvalds #include <linux/string.h>
83*1da177e4SLinus Torvalds #include <linux/socket.h>
84*1da177e4SLinus Torvalds #include <linux/sockios.h>
85*1da177e4SLinus Torvalds #include <linux/in.h>
86*1da177e4SLinus Torvalds #include <linux/inet.h>
87*1da177e4SLinus Torvalds #include <linux/netdevice.h>
88*1da177e4SLinus Torvalds #include <linux/skbuff.h>
89*1da177e4SLinus Torvalds #include <linux/inetdevice.h>
90*1da177e4SLinus Torvalds #include <linux/igmp.h>
91*1da177e4SLinus Torvalds #include <linux/if_arp.h>
92*1da177e4SLinus Torvalds #include <linux/rtnetlink.h>
93*1da177e4SLinus Torvalds #include <linux/times.h>
94*1da177e4SLinus Torvalds #include <net/ip.h>
95*1da177e4SLinus Torvalds #include <net/protocol.h>
96*1da177e4SLinus Torvalds #include <net/route.h>
97*1da177e4SLinus Torvalds #include <net/sock.h>
98*1da177e4SLinus Torvalds #include <net/checksum.h>
99*1da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
100*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
101*1da177e4SLinus Torvalds #include <linux/mroute.h>
102*1da177e4SLinus Torvalds #endif
103*1da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
104*1da177e4SLinus Torvalds #include <linux/proc_fs.h>
105*1da177e4SLinus Torvalds #include <linux/seq_file.h>
106*1da177e4SLinus Torvalds #endif
107*1da177e4SLinus Torvalds 
108*1da177e4SLinus Torvalds #define IP_MAX_MEMBERSHIPS	20
109*1da177e4SLinus Torvalds #define IP_MAX_MSF		10
110*1da177e4SLinus Torvalds 
111*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
112*1da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */
113*1da177e4SLinus Torvalds 
114*1da177e4SLinus Torvalds #define IGMP_V1_Router_Present_Timeout		(400*HZ)
115*1da177e4SLinus Torvalds #define IGMP_V2_Router_Present_Timeout		(400*HZ)
116*1da177e4SLinus Torvalds #define IGMP_Unsolicited_Report_Interval	(10*HZ)
117*1da177e4SLinus Torvalds #define IGMP_Query_Response_Interval		(10*HZ)
118*1da177e4SLinus Torvalds #define IGMP_Unsolicited_Report_Count		2
119*1da177e4SLinus Torvalds 
120*1da177e4SLinus Torvalds 
121*1da177e4SLinus Torvalds #define IGMP_Initial_Report_Delay		(1)
122*1da177e4SLinus Torvalds 
123*1da177e4SLinus Torvalds /* IGMP_Initial_Report_Delay is not from IGMP specs!
124*1da177e4SLinus Torvalds  * IGMP specs require to report membership immediately after
125*1da177e4SLinus Torvalds  * joining a group, but we delay the first report by a
126*1da177e4SLinus Torvalds  * small interval. It seems more natural and still does not
127*1da177e4SLinus Torvalds  * contradict to specs provided this delay is small enough.
128*1da177e4SLinus Torvalds  */
129*1da177e4SLinus Torvalds 
130*1da177e4SLinus Torvalds #define IGMP_V1_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 1 || \
131*1da177e4SLinus Torvalds 		(in_dev)->cnf.force_igmp_version == 1 || \
132*1da177e4SLinus Torvalds 		((in_dev)->mr_v1_seen && \
133*1da177e4SLinus Torvalds 		time_before(jiffies, (in_dev)->mr_v1_seen)))
134*1da177e4SLinus Torvalds #define IGMP_V2_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 2 || \
135*1da177e4SLinus Torvalds 		(in_dev)->cnf.force_igmp_version == 2 || \
136*1da177e4SLinus Torvalds 		((in_dev)->mr_v2_seen && \
137*1da177e4SLinus Torvalds 		time_before(jiffies, (in_dev)->mr_v2_seen)))
138*1da177e4SLinus Torvalds 
139*1da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
140*1da177e4SLinus Torvalds static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr);
141*1da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev);
142*1da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc);
143*1da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc);
144*1da177e4SLinus Torvalds #endif
145*1da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc);
146*1da177e4SLinus Torvalds static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
147*1da177e4SLinus Torvalds 			 int sfcount, __u32 *psfsrc, int delta);
148*1da177e4SLinus Torvalds 
149*1da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im)
150*1da177e4SLinus Torvalds {
151*1da177e4SLinus Torvalds 	if (atomic_dec_and_test(&im->refcnt)) {
152*1da177e4SLinus Torvalds 		in_dev_put(im->interface);
153*1da177e4SLinus Torvalds 		kfree(im);
154*1da177e4SLinus Torvalds 	}
155*1da177e4SLinus Torvalds }
156*1da177e4SLinus Torvalds 
157*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
158*1da177e4SLinus Torvalds 
159*1da177e4SLinus Torvalds /*
160*1da177e4SLinus Torvalds  *	Timer management
161*1da177e4SLinus Torvalds  */
162*1da177e4SLinus Torvalds 
163*1da177e4SLinus Torvalds static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
164*1da177e4SLinus Torvalds {
165*1da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
166*1da177e4SLinus Torvalds 	if (del_timer(&im->timer))
167*1da177e4SLinus Torvalds 		atomic_dec(&im->refcnt);
168*1da177e4SLinus Torvalds 	im->tm_running=0;
169*1da177e4SLinus Torvalds 	im->reporter = 0;
170*1da177e4SLinus Torvalds 	im->unsolicit_count = 0;
171*1da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
172*1da177e4SLinus Torvalds }
173*1da177e4SLinus Torvalds 
174*1da177e4SLinus Torvalds /* It must be called with locked im->lock */
175*1da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
176*1da177e4SLinus Torvalds {
177*1da177e4SLinus Torvalds 	int tv=net_random() % max_delay;
178*1da177e4SLinus Torvalds 
179*1da177e4SLinus Torvalds 	im->tm_running=1;
180*1da177e4SLinus Torvalds 	if (!mod_timer(&im->timer, jiffies+tv+2))
181*1da177e4SLinus Torvalds 		atomic_inc(&im->refcnt);
182*1da177e4SLinus Torvalds }
183*1da177e4SLinus Torvalds 
184*1da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev)
185*1da177e4SLinus Torvalds {
186*1da177e4SLinus Torvalds 	int tv = net_random() % in_dev->mr_maxdelay;
187*1da177e4SLinus Torvalds 
188*1da177e4SLinus Torvalds 	in_dev->mr_gq_running = 1;
189*1da177e4SLinus Torvalds 	if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
190*1da177e4SLinus Torvalds 		in_dev_hold(in_dev);
191*1da177e4SLinus Torvalds }
192*1da177e4SLinus Torvalds 
193*1da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
194*1da177e4SLinus Torvalds {
195*1da177e4SLinus Torvalds 	int tv = net_random() % delay;
196*1da177e4SLinus Torvalds 
197*1da177e4SLinus Torvalds 	if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
198*1da177e4SLinus Torvalds 		in_dev_hold(in_dev);
199*1da177e4SLinus Torvalds }
200*1da177e4SLinus Torvalds 
201*1da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
202*1da177e4SLinus Torvalds {
203*1da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
204*1da177e4SLinus Torvalds 	im->unsolicit_count = 0;
205*1da177e4SLinus Torvalds 	if (del_timer(&im->timer)) {
206*1da177e4SLinus Torvalds 		if ((long)(im->timer.expires-jiffies) < max_delay) {
207*1da177e4SLinus Torvalds 			add_timer(&im->timer);
208*1da177e4SLinus Torvalds 			im->tm_running=1;
209*1da177e4SLinus Torvalds 			spin_unlock_bh(&im->lock);
210*1da177e4SLinus Torvalds 			return;
211*1da177e4SLinus Torvalds 		}
212*1da177e4SLinus Torvalds 		atomic_dec(&im->refcnt);
213*1da177e4SLinus Torvalds 	}
214*1da177e4SLinus Torvalds 	igmp_start_timer(im, max_delay);
215*1da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
216*1da177e4SLinus Torvalds }
217*1da177e4SLinus Torvalds 
218*1da177e4SLinus Torvalds 
219*1da177e4SLinus Torvalds /*
220*1da177e4SLinus Torvalds  *	Send an IGMP report.
221*1da177e4SLinus Torvalds  */
222*1da177e4SLinus Torvalds 
223*1da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
224*1da177e4SLinus Torvalds 
225*1da177e4SLinus Torvalds 
226*1da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
227*1da177e4SLinus Torvalds 	int gdeleted, int sdeleted)
228*1da177e4SLinus Torvalds {
229*1da177e4SLinus Torvalds 	switch (type) {
230*1da177e4SLinus Torvalds 	case IGMPV3_MODE_IS_INCLUDE:
231*1da177e4SLinus Torvalds 	case IGMPV3_MODE_IS_EXCLUDE:
232*1da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
233*1da177e4SLinus Torvalds 			return 0;
234*1da177e4SLinus Torvalds 		return !(pmc->gsquery && !psf->sf_gsresp);
235*1da177e4SLinus Torvalds 	case IGMPV3_CHANGE_TO_INCLUDE:
236*1da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
237*1da177e4SLinus Torvalds 			return 0;
238*1da177e4SLinus Torvalds 		return psf->sf_count[MCAST_INCLUDE] != 0;
239*1da177e4SLinus Torvalds 	case IGMPV3_CHANGE_TO_EXCLUDE:
240*1da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
241*1da177e4SLinus Torvalds 			return 0;
242*1da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
243*1da177e4SLinus Torvalds 		    psf->sf_count[MCAST_INCLUDE])
244*1da177e4SLinus Torvalds 			return 0;
245*1da177e4SLinus Torvalds 		return pmc->sfcount[MCAST_EXCLUDE] ==
246*1da177e4SLinus Torvalds 			psf->sf_count[MCAST_EXCLUDE];
247*1da177e4SLinus Torvalds 	case IGMPV3_ALLOW_NEW_SOURCES:
248*1da177e4SLinus Torvalds 		if (gdeleted || !psf->sf_crcount)
249*1da177e4SLinus Torvalds 			return 0;
250*1da177e4SLinus Torvalds 		return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
251*1da177e4SLinus Torvalds 	case IGMPV3_BLOCK_OLD_SOURCES:
252*1da177e4SLinus Torvalds 		if (pmc->sfmode == MCAST_INCLUDE)
253*1da177e4SLinus Torvalds 			return gdeleted || (psf->sf_crcount && sdeleted);
254*1da177e4SLinus Torvalds 		return psf->sf_crcount && !gdeleted && !sdeleted;
255*1da177e4SLinus Torvalds 	}
256*1da177e4SLinus Torvalds 	return 0;
257*1da177e4SLinus Torvalds }
258*1da177e4SLinus Torvalds 
259*1da177e4SLinus Torvalds static int
260*1da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
261*1da177e4SLinus Torvalds {
262*1da177e4SLinus Torvalds 	struct ip_sf_list *psf;
263*1da177e4SLinus Torvalds 	int scount = 0;
264*1da177e4SLinus Torvalds 
265*1da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
266*1da177e4SLinus Torvalds 		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
267*1da177e4SLinus Torvalds 			continue;
268*1da177e4SLinus Torvalds 		scount++;
269*1da177e4SLinus Torvalds 	}
270*1da177e4SLinus Torvalds 	return scount;
271*1da177e4SLinus Torvalds }
272*1da177e4SLinus Torvalds 
273*1da177e4SLinus Torvalds static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
274*1da177e4SLinus Torvalds {
275*1da177e4SLinus Torvalds 	struct sk_buff *skb;
276*1da177e4SLinus Torvalds 	struct rtable *rt;
277*1da177e4SLinus Torvalds 	struct iphdr *pip;
278*1da177e4SLinus Torvalds 	struct igmpv3_report *pig;
279*1da177e4SLinus Torvalds 
280*1da177e4SLinus Torvalds 	skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
281*1da177e4SLinus Torvalds 	if (skb == NULL)
282*1da177e4SLinus Torvalds 		return NULL;
283*1da177e4SLinus Torvalds 
284*1da177e4SLinus Torvalds 	{
285*1da177e4SLinus Torvalds 		struct flowi fl = { .oif = dev->ifindex,
286*1da177e4SLinus Torvalds 				    .nl_u = { .ip4_u = {
287*1da177e4SLinus Torvalds 				    .daddr = IGMPV3_ALL_MCR } },
288*1da177e4SLinus Torvalds 				    .proto = IPPROTO_IGMP };
289*1da177e4SLinus Torvalds 		if (ip_route_output_key(&rt, &fl)) {
290*1da177e4SLinus Torvalds 			kfree_skb(skb);
291*1da177e4SLinus Torvalds 			return NULL;
292*1da177e4SLinus Torvalds 		}
293*1da177e4SLinus Torvalds 	}
294*1da177e4SLinus Torvalds 	if (rt->rt_src == 0) {
295*1da177e4SLinus Torvalds 		kfree_skb(skb);
296*1da177e4SLinus Torvalds 		ip_rt_put(rt);
297*1da177e4SLinus Torvalds 		return NULL;
298*1da177e4SLinus Torvalds 	}
299*1da177e4SLinus Torvalds 
300*1da177e4SLinus Torvalds 	skb->dst = &rt->u.dst;
301*1da177e4SLinus Torvalds 	skb->dev = dev;
302*1da177e4SLinus Torvalds 
303*1da177e4SLinus Torvalds 	skb_reserve(skb, LL_RESERVED_SPACE(dev));
304*1da177e4SLinus Torvalds 
305*1da177e4SLinus Torvalds 	skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
306*1da177e4SLinus Torvalds 
307*1da177e4SLinus Torvalds 	pip->version  = 4;
308*1da177e4SLinus Torvalds 	pip->ihl      = (sizeof(struct iphdr)+4)>>2;
309*1da177e4SLinus Torvalds 	pip->tos      = 0xc0;
310*1da177e4SLinus Torvalds 	pip->frag_off = htons(IP_DF);
311*1da177e4SLinus Torvalds 	pip->ttl      = 1;
312*1da177e4SLinus Torvalds 	pip->daddr    = rt->rt_dst;
313*1da177e4SLinus Torvalds 	pip->saddr    = rt->rt_src;
314*1da177e4SLinus Torvalds 	pip->protocol = IPPROTO_IGMP;
315*1da177e4SLinus Torvalds 	pip->tot_len  = 0;	/* filled in later */
316*1da177e4SLinus Torvalds 	ip_select_ident(pip, &rt->u.dst, NULL);
317*1da177e4SLinus Torvalds 	((u8*)&pip[1])[0] = IPOPT_RA;
318*1da177e4SLinus Torvalds 	((u8*)&pip[1])[1] = 4;
319*1da177e4SLinus Torvalds 	((u8*)&pip[1])[2] = 0;
320*1da177e4SLinus Torvalds 	((u8*)&pip[1])[3] = 0;
321*1da177e4SLinus Torvalds 
322*1da177e4SLinus Torvalds 	pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
323*1da177e4SLinus Torvalds 	skb->h.igmph = (struct igmphdr *)pig;
324*1da177e4SLinus Torvalds 	pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
325*1da177e4SLinus Torvalds 	pig->resv1 = 0;
326*1da177e4SLinus Torvalds 	pig->csum = 0;
327*1da177e4SLinus Torvalds 	pig->resv2 = 0;
328*1da177e4SLinus Torvalds 	pig->ngrec = 0;
329*1da177e4SLinus Torvalds 	return skb;
330*1da177e4SLinus Torvalds }
331*1da177e4SLinus Torvalds 
332*1da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb)
333*1da177e4SLinus Torvalds {
334*1da177e4SLinus Torvalds 	struct iphdr *pip = skb->nh.iph;
335*1da177e4SLinus Torvalds 	struct igmphdr *pig = skb->h.igmph;
336*1da177e4SLinus Torvalds 	int iplen, igmplen;
337*1da177e4SLinus Torvalds 
338*1da177e4SLinus Torvalds 	iplen = skb->tail - (unsigned char *)skb->nh.iph;
339*1da177e4SLinus Torvalds 	pip->tot_len = htons(iplen);
340*1da177e4SLinus Torvalds 	ip_send_check(pip);
341*1da177e4SLinus Torvalds 
342*1da177e4SLinus Torvalds 	igmplen = skb->tail - (unsigned char *)skb->h.igmph;
343*1da177e4SLinus Torvalds 	pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
344*1da177e4SLinus Torvalds 
345*1da177e4SLinus Torvalds 	return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
346*1da177e4SLinus Torvalds 		       dst_output);
347*1da177e4SLinus Torvalds }
348*1da177e4SLinus Torvalds 
349*1da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
350*1da177e4SLinus Torvalds {
351*1da177e4SLinus Torvalds 	return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
352*1da177e4SLinus Torvalds }
353*1da177e4SLinus Torvalds 
354*1da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
355*1da177e4SLinus Torvalds 	int type, struct igmpv3_grec **ppgr)
356*1da177e4SLinus Torvalds {
357*1da177e4SLinus Torvalds 	struct net_device *dev = pmc->interface->dev;
358*1da177e4SLinus Torvalds 	struct igmpv3_report *pih;
359*1da177e4SLinus Torvalds 	struct igmpv3_grec *pgr;
360*1da177e4SLinus Torvalds 
361*1da177e4SLinus Torvalds 	if (!skb)
362*1da177e4SLinus Torvalds 		skb = igmpv3_newpack(dev, dev->mtu);
363*1da177e4SLinus Torvalds 	if (!skb)
364*1da177e4SLinus Torvalds 		return NULL;
365*1da177e4SLinus Torvalds 	pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
366*1da177e4SLinus Torvalds 	pgr->grec_type = type;
367*1da177e4SLinus Torvalds 	pgr->grec_auxwords = 0;
368*1da177e4SLinus Torvalds 	pgr->grec_nsrcs = 0;
369*1da177e4SLinus Torvalds 	pgr->grec_mca = pmc->multiaddr;
370*1da177e4SLinus Torvalds 	pih = (struct igmpv3_report *)skb->h.igmph;
371*1da177e4SLinus Torvalds 	pih->ngrec = htons(ntohs(pih->ngrec)+1);
372*1da177e4SLinus Torvalds 	*ppgr = pgr;
373*1da177e4SLinus Torvalds 	return skb;
374*1da177e4SLinus Torvalds }
375*1da177e4SLinus Torvalds 
376*1da177e4SLinus Torvalds #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
377*1da177e4SLinus Torvalds 	skb_tailroom(skb)) : 0)
378*1da177e4SLinus Torvalds 
379*1da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
380*1da177e4SLinus Torvalds 	int type, int gdeleted, int sdeleted)
381*1da177e4SLinus Torvalds {
382*1da177e4SLinus Torvalds 	struct net_device *dev = pmc->interface->dev;
383*1da177e4SLinus Torvalds 	struct igmpv3_report *pih;
384*1da177e4SLinus Torvalds 	struct igmpv3_grec *pgr = NULL;
385*1da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
386*1da177e4SLinus Torvalds 	int scount, first, isquery, truncate;
387*1da177e4SLinus Torvalds 
388*1da177e4SLinus Torvalds 	if (pmc->multiaddr == IGMP_ALL_HOSTS)
389*1da177e4SLinus Torvalds 		return skb;
390*1da177e4SLinus Torvalds 
391*1da177e4SLinus Torvalds 	isquery = type == IGMPV3_MODE_IS_INCLUDE ||
392*1da177e4SLinus Torvalds 		  type == IGMPV3_MODE_IS_EXCLUDE;
393*1da177e4SLinus Torvalds 	truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
394*1da177e4SLinus Torvalds 		    type == IGMPV3_CHANGE_TO_EXCLUDE;
395*1da177e4SLinus Torvalds 
396*1da177e4SLinus Torvalds 	psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
397*1da177e4SLinus Torvalds 
398*1da177e4SLinus Torvalds 	if (!*psf_list) {
399*1da177e4SLinus Torvalds 		if (type == IGMPV3_ALLOW_NEW_SOURCES ||
400*1da177e4SLinus Torvalds 		    type == IGMPV3_BLOCK_OLD_SOURCES)
401*1da177e4SLinus Torvalds 			return skb;
402*1da177e4SLinus Torvalds 		if (pmc->crcount || isquery) {
403*1da177e4SLinus Torvalds 			/* make sure we have room for group header and at
404*1da177e4SLinus Torvalds 			 * least one source.
405*1da177e4SLinus Torvalds 			 */
406*1da177e4SLinus Torvalds 			if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)+
407*1da177e4SLinus Torvalds 			    sizeof(__u32)) {
408*1da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
409*1da177e4SLinus Torvalds 				skb = NULL; /* add_grhead will get a new one */
410*1da177e4SLinus Torvalds 			}
411*1da177e4SLinus Torvalds 			skb = add_grhead(skb, pmc, type, &pgr);
412*1da177e4SLinus Torvalds 		}
413*1da177e4SLinus Torvalds 		return skb;
414*1da177e4SLinus Torvalds 	}
415*1da177e4SLinus Torvalds 	pih = skb ? (struct igmpv3_report *)skb->h.igmph : NULL;
416*1da177e4SLinus Torvalds 
417*1da177e4SLinus Torvalds 	/* EX and TO_EX get a fresh packet, if needed */
418*1da177e4SLinus Torvalds 	if (truncate) {
419*1da177e4SLinus Torvalds 		if (pih && pih->ngrec &&
420*1da177e4SLinus Torvalds 		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
421*1da177e4SLinus Torvalds 			if (skb)
422*1da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
423*1da177e4SLinus Torvalds 			skb = igmpv3_newpack(dev, dev->mtu);
424*1da177e4SLinus Torvalds 		}
425*1da177e4SLinus Torvalds 	}
426*1da177e4SLinus Torvalds 	first = 1;
427*1da177e4SLinus Torvalds 	scount = 0;
428*1da177e4SLinus Torvalds 	psf_prev = NULL;
429*1da177e4SLinus Torvalds 	for (psf=*psf_list; psf; psf=psf_next) {
430*1da177e4SLinus Torvalds 		u32 *psrc;
431*1da177e4SLinus Torvalds 
432*1da177e4SLinus Torvalds 		psf_next = psf->sf_next;
433*1da177e4SLinus Torvalds 
434*1da177e4SLinus Torvalds 		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
435*1da177e4SLinus Torvalds 			psf_prev = psf;
436*1da177e4SLinus Torvalds 			continue;
437*1da177e4SLinus Torvalds 		}
438*1da177e4SLinus Torvalds 
439*1da177e4SLinus Torvalds 		/* clear marks on query responses */
440*1da177e4SLinus Torvalds 		if (isquery)
441*1da177e4SLinus Torvalds 			psf->sf_gsresp = 0;
442*1da177e4SLinus Torvalds 
443*1da177e4SLinus Torvalds 		if (AVAILABLE(skb) < sizeof(u32) +
444*1da177e4SLinus Torvalds 		    first*sizeof(struct igmpv3_grec)) {
445*1da177e4SLinus Torvalds 			if (truncate && !first)
446*1da177e4SLinus Torvalds 				break;	 /* truncate these */
447*1da177e4SLinus Torvalds 			if (pgr)
448*1da177e4SLinus Torvalds 				pgr->grec_nsrcs = htons(scount);
449*1da177e4SLinus Torvalds 			if (skb)
450*1da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
451*1da177e4SLinus Torvalds 			skb = igmpv3_newpack(dev, dev->mtu);
452*1da177e4SLinus Torvalds 			first = 1;
453*1da177e4SLinus Torvalds 			scount = 0;
454*1da177e4SLinus Torvalds 		}
455*1da177e4SLinus Torvalds 		if (first) {
456*1da177e4SLinus Torvalds 			skb = add_grhead(skb, pmc, type, &pgr);
457*1da177e4SLinus Torvalds 			first = 0;
458*1da177e4SLinus Torvalds 		}
459*1da177e4SLinus Torvalds 		psrc = (u32 *)skb_put(skb, sizeof(u32));
460*1da177e4SLinus Torvalds 		*psrc = psf->sf_inaddr;
461*1da177e4SLinus Torvalds 		scount++;
462*1da177e4SLinus Torvalds 		if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
463*1da177e4SLinus Torvalds 		     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
464*1da177e4SLinus Torvalds 			psf->sf_crcount--;
465*1da177e4SLinus Torvalds 			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
466*1da177e4SLinus Torvalds 				if (psf_prev)
467*1da177e4SLinus Torvalds 					psf_prev->sf_next = psf->sf_next;
468*1da177e4SLinus Torvalds 				else
469*1da177e4SLinus Torvalds 					*psf_list = psf->sf_next;
470*1da177e4SLinus Torvalds 				kfree(psf);
471*1da177e4SLinus Torvalds 				continue;
472*1da177e4SLinus Torvalds 			}
473*1da177e4SLinus Torvalds 		}
474*1da177e4SLinus Torvalds 		psf_prev = psf;
475*1da177e4SLinus Torvalds 	}
476*1da177e4SLinus Torvalds 	if (pgr)
477*1da177e4SLinus Torvalds 		pgr->grec_nsrcs = htons(scount);
478*1da177e4SLinus Torvalds 
479*1da177e4SLinus Torvalds 	if (isquery)
480*1da177e4SLinus Torvalds 		pmc->gsquery = 0;	/* clear query state on report */
481*1da177e4SLinus Torvalds 	return skb;
482*1da177e4SLinus Torvalds }
483*1da177e4SLinus Torvalds 
484*1da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
485*1da177e4SLinus Torvalds {
486*1da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
487*1da177e4SLinus Torvalds 	int type;
488*1da177e4SLinus Torvalds 
489*1da177e4SLinus Torvalds 	if (!pmc) {
490*1da177e4SLinus Torvalds 		read_lock(&in_dev->mc_list_lock);
491*1da177e4SLinus Torvalds 		for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
492*1da177e4SLinus Torvalds 			if (pmc->multiaddr == IGMP_ALL_HOSTS)
493*1da177e4SLinus Torvalds 				continue;
494*1da177e4SLinus Torvalds 			spin_lock_bh(&pmc->lock);
495*1da177e4SLinus Torvalds 			if (pmc->sfcount[MCAST_EXCLUDE])
496*1da177e4SLinus Torvalds 				type = IGMPV3_MODE_IS_EXCLUDE;
497*1da177e4SLinus Torvalds 			else
498*1da177e4SLinus Torvalds 				type = IGMPV3_MODE_IS_INCLUDE;
499*1da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 0, 0);
500*1da177e4SLinus Torvalds 			spin_unlock_bh(&pmc->lock);
501*1da177e4SLinus Torvalds 		}
502*1da177e4SLinus Torvalds 		read_unlock(&in_dev->mc_list_lock);
503*1da177e4SLinus Torvalds 	} else {
504*1da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
505*1da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE])
506*1da177e4SLinus Torvalds 			type = IGMPV3_MODE_IS_EXCLUDE;
507*1da177e4SLinus Torvalds 		else
508*1da177e4SLinus Torvalds 			type = IGMPV3_MODE_IS_INCLUDE;
509*1da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, type, 0, 0);
510*1da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
511*1da177e4SLinus Torvalds 	}
512*1da177e4SLinus Torvalds 	if (!skb)
513*1da177e4SLinus Torvalds 		return 0;
514*1da177e4SLinus Torvalds 	return igmpv3_sendpack(skb);
515*1da177e4SLinus Torvalds }
516*1da177e4SLinus Torvalds 
517*1da177e4SLinus Torvalds /*
518*1da177e4SLinus Torvalds  * remove zero-count source records from a source filter list
519*1da177e4SLinus Torvalds  */
520*1da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
521*1da177e4SLinus Torvalds {
522*1da177e4SLinus Torvalds 	struct ip_sf_list *psf_prev, *psf_next, *psf;
523*1da177e4SLinus Torvalds 
524*1da177e4SLinus Torvalds 	psf_prev = NULL;
525*1da177e4SLinus Torvalds 	for (psf=*ppsf; psf; psf = psf_next) {
526*1da177e4SLinus Torvalds 		psf_next = psf->sf_next;
527*1da177e4SLinus Torvalds 		if (psf->sf_crcount == 0) {
528*1da177e4SLinus Torvalds 			if (psf_prev)
529*1da177e4SLinus Torvalds 				psf_prev->sf_next = psf->sf_next;
530*1da177e4SLinus Torvalds 			else
531*1da177e4SLinus Torvalds 				*ppsf = psf->sf_next;
532*1da177e4SLinus Torvalds 			kfree(psf);
533*1da177e4SLinus Torvalds 		} else
534*1da177e4SLinus Torvalds 			psf_prev = psf;
535*1da177e4SLinus Torvalds 	}
536*1da177e4SLinus Torvalds }
537*1da177e4SLinus Torvalds 
538*1da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev)
539*1da177e4SLinus Torvalds {
540*1da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
541*1da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
542*1da177e4SLinus Torvalds 	int type, dtype;
543*1da177e4SLinus Torvalds 
544*1da177e4SLinus Torvalds 	read_lock(&in_dev->mc_list_lock);
545*1da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
546*1da177e4SLinus Torvalds 
547*1da177e4SLinus Torvalds 	/* deleted MCA's */
548*1da177e4SLinus Torvalds 	pmc_prev = NULL;
549*1da177e4SLinus Torvalds 	for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
550*1da177e4SLinus Torvalds 		pmc_next = pmc->next;
551*1da177e4SLinus Torvalds 		if (pmc->sfmode == MCAST_INCLUDE) {
552*1da177e4SLinus Torvalds 			type = IGMPV3_BLOCK_OLD_SOURCES;
553*1da177e4SLinus Torvalds 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
554*1da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 1, 0);
555*1da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, dtype, 1, 1);
556*1da177e4SLinus Torvalds 		}
557*1da177e4SLinus Torvalds 		if (pmc->crcount) {
558*1da177e4SLinus Torvalds 			pmc->crcount--;
559*1da177e4SLinus Torvalds 			if (pmc->sfmode == MCAST_EXCLUDE) {
560*1da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_INCLUDE;
561*1da177e4SLinus Torvalds 				skb = add_grec(skb, pmc, type, 1, 0);
562*1da177e4SLinus Torvalds 			}
563*1da177e4SLinus Torvalds 			if (pmc->crcount == 0) {
564*1da177e4SLinus Torvalds 				igmpv3_clear_zeros(&pmc->tomb);
565*1da177e4SLinus Torvalds 				igmpv3_clear_zeros(&pmc->sources);
566*1da177e4SLinus Torvalds 			}
567*1da177e4SLinus Torvalds 		}
568*1da177e4SLinus Torvalds 		if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
569*1da177e4SLinus Torvalds 			if (pmc_prev)
570*1da177e4SLinus Torvalds 				pmc_prev->next = pmc_next;
571*1da177e4SLinus Torvalds 			else
572*1da177e4SLinus Torvalds 				in_dev->mc_tomb = pmc_next;
573*1da177e4SLinus Torvalds 			in_dev_put(pmc->interface);
574*1da177e4SLinus Torvalds 			kfree(pmc);
575*1da177e4SLinus Torvalds 		} else
576*1da177e4SLinus Torvalds 			pmc_prev = pmc;
577*1da177e4SLinus Torvalds 	}
578*1da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
579*1da177e4SLinus Torvalds 
580*1da177e4SLinus Torvalds 	/* change recs */
581*1da177e4SLinus Torvalds 	for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
582*1da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
583*1da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
584*1da177e4SLinus Torvalds 			type = IGMPV3_BLOCK_OLD_SOURCES;
585*1da177e4SLinus Torvalds 			dtype = IGMPV3_ALLOW_NEW_SOURCES;
586*1da177e4SLinus Torvalds 		} else {
587*1da177e4SLinus Torvalds 			type = IGMPV3_ALLOW_NEW_SOURCES;
588*1da177e4SLinus Torvalds 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
589*1da177e4SLinus Torvalds 		}
590*1da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, type, 0, 0);
591*1da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, dtype, 0, 1);	/* deleted sources */
592*1da177e4SLinus Torvalds 
593*1da177e4SLinus Torvalds 		/* filter mode changes */
594*1da177e4SLinus Torvalds 		if (pmc->crcount) {
595*1da177e4SLinus Torvalds 			pmc->crcount--;
596*1da177e4SLinus Torvalds 			if (pmc->sfmode == MCAST_EXCLUDE)
597*1da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_EXCLUDE;
598*1da177e4SLinus Torvalds 			else
599*1da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_INCLUDE;
600*1da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 0, 0);
601*1da177e4SLinus Torvalds 		}
602*1da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
603*1da177e4SLinus Torvalds 	}
604*1da177e4SLinus Torvalds 	read_unlock(&in_dev->mc_list_lock);
605*1da177e4SLinus Torvalds 
606*1da177e4SLinus Torvalds 	if (!skb)
607*1da177e4SLinus Torvalds 		return;
608*1da177e4SLinus Torvalds 	(void) igmpv3_sendpack(skb);
609*1da177e4SLinus Torvalds }
610*1da177e4SLinus Torvalds 
611*1da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
612*1da177e4SLinus Torvalds 	int type)
613*1da177e4SLinus Torvalds {
614*1da177e4SLinus Torvalds 	struct sk_buff *skb;
615*1da177e4SLinus Torvalds 	struct iphdr *iph;
616*1da177e4SLinus Torvalds 	struct igmphdr *ih;
617*1da177e4SLinus Torvalds 	struct rtable *rt;
618*1da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
619*1da177e4SLinus Torvalds 	u32	group = pmc ? pmc->multiaddr : 0;
620*1da177e4SLinus Torvalds 	u32	dst;
621*1da177e4SLinus Torvalds 
622*1da177e4SLinus Torvalds 	if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
623*1da177e4SLinus Torvalds 		return igmpv3_send_report(in_dev, pmc);
624*1da177e4SLinus Torvalds 	else if (type == IGMP_HOST_LEAVE_MESSAGE)
625*1da177e4SLinus Torvalds 		dst = IGMP_ALL_ROUTER;
626*1da177e4SLinus Torvalds 	else
627*1da177e4SLinus Torvalds 		dst = group;
628*1da177e4SLinus Torvalds 
629*1da177e4SLinus Torvalds 	{
630*1da177e4SLinus Torvalds 		struct flowi fl = { .oif = dev->ifindex,
631*1da177e4SLinus Torvalds 				    .nl_u = { .ip4_u = { .daddr = dst } },
632*1da177e4SLinus Torvalds 				    .proto = IPPROTO_IGMP };
633*1da177e4SLinus Torvalds 		if (ip_route_output_key(&rt, &fl))
634*1da177e4SLinus Torvalds 			return -1;
635*1da177e4SLinus Torvalds 	}
636*1da177e4SLinus Torvalds 	if (rt->rt_src == 0) {
637*1da177e4SLinus Torvalds 		ip_rt_put(rt);
638*1da177e4SLinus Torvalds 		return -1;
639*1da177e4SLinus Torvalds 	}
640*1da177e4SLinus Torvalds 
641*1da177e4SLinus Torvalds 	skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
642*1da177e4SLinus Torvalds 	if (skb == NULL) {
643*1da177e4SLinus Torvalds 		ip_rt_put(rt);
644*1da177e4SLinus Torvalds 		return -1;
645*1da177e4SLinus Torvalds 	}
646*1da177e4SLinus Torvalds 
647*1da177e4SLinus Torvalds 	skb->dst = &rt->u.dst;
648*1da177e4SLinus Torvalds 
649*1da177e4SLinus Torvalds 	skb_reserve(skb, LL_RESERVED_SPACE(dev));
650*1da177e4SLinus Torvalds 
651*1da177e4SLinus Torvalds 	skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
652*1da177e4SLinus Torvalds 
653*1da177e4SLinus Torvalds 	iph->version  = 4;
654*1da177e4SLinus Torvalds 	iph->ihl      = (sizeof(struct iphdr)+4)>>2;
655*1da177e4SLinus Torvalds 	iph->tos      = 0xc0;
656*1da177e4SLinus Torvalds 	iph->frag_off = htons(IP_DF);
657*1da177e4SLinus Torvalds 	iph->ttl      = 1;
658*1da177e4SLinus Torvalds 	iph->daddr    = dst;
659*1da177e4SLinus Torvalds 	iph->saddr    = rt->rt_src;
660*1da177e4SLinus Torvalds 	iph->protocol = IPPROTO_IGMP;
661*1da177e4SLinus Torvalds 	iph->tot_len  = htons(IGMP_SIZE);
662*1da177e4SLinus Torvalds 	ip_select_ident(iph, &rt->u.dst, NULL);
663*1da177e4SLinus Torvalds 	((u8*)&iph[1])[0] = IPOPT_RA;
664*1da177e4SLinus Torvalds 	((u8*)&iph[1])[1] = 4;
665*1da177e4SLinus Torvalds 	((u8*)&iph[1])[2] = 0;
666*1da177e4SLinus Torvalds 	((u8*)&iph[1])[3] = 0;
667*1da177e4SLinus Torvalds 	ip_send_check(iph);
668*1da177e4SLinus Torvalds 
669*1da177e4SLinus Torvalds 	ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
670*1da177e4SLinus Torvalds 	ih->type=type;
671*1da177e4SLinus Torvalds 	ih->code=0;
672*1da177e4SLinus Torvalds 	ih->csum=0;
673*1da177e4SLinus Torvalds 	ih->group=group;
674*1da177e4SLinus Torvalds 	ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
675*1da177e4SLinus Torvalds 
676*1da177e4SLinus Torvalds 	return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
677*1da177e4SLinus Torvalds 		       dst_output);
678*1da177e4SLinus Torvalds }
679*1da177e4SLinus Torvalds 
680*1da177e4SLinus Torvalds static void igmp_gq_timer_expire(unsigned long data)
681*1da177e4SLinus Torvalds {
682*1da177e4SLinus Torvalds 	struct in_device *in_dev = (struct in_device *)data;
683*1da177e4SLinus Torvalds 
684*1da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
685*1da177e4SLinus Torvalds 	igmpv3_send_report(in_dev, NULL);
686*1da177e4SLinus Torvalds 	__in_dev_put(in_dev);
687*1da177e4SLinus Torvalds }
688*1da177e4SLinus Torvalds 
689*1da177e4SLinus Torvalds static void igmp_ifc_timer_expire(unsigned long data)
690*1da177e4SLinus Torvalds {
691*1da177e4SLinus Torvalds 	struct in_device *in_dev = (struct in_device *)data;
692*1da177e4SLinus Torvalds 
693*1da177e4SLinus Torvalds 	igmpv3_send_cr(in_dev);
694*1da177e4SLinus Torvalds 	if (in_dev->mr_ifc_count) {
695*1da177e4SLinus Torvalds 		in_dev->mr_ifc_count--;
696*1da177e4SLinus Torvalds 		igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
697*1da177e4SLinus Torvalds 	}
698*1da177e4SLinus Torvalds 	__in_dev_put(in_dev);
699*1da177e4SLinus Torvalds }
700*1da177e4SLinus Torvalds 
701*1da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev)
702*1da177e4SLinus Torvalds {
703*1da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
704*1da177e4SLinus Torvalds 		return;
705*1da177e4SLinus Torvalds 	in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
706*1da177e4SLinus Torvalds 		IGMP_Unsolicited_Report_Count;
707*1da177e4SLinus Torvalds 	igmp_ifc_start_timer(in_dev, 1);
708*1da177e4SLinus Torvalds }
709*1da177e4SLinus Torvalds 
710*1da177e4SLinus Torvalds 
711*1da177e4SLinus Torvalds static void igmp_timer_expire(unsigned long data)
712*1da177e4SLinus Torvalds {
713*1da177e4SLinus Torvalds 	struct ip_mc_list *im=(struct ip_mc_list *)data;
714*1da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
715*1da177e4SLinus Torvalds 
716*1da177e4SLinus Torvalds 	spin_lock(&im->lock);
717*1da177e4SLinus Torvalds 	im->tm_running=0;
718*1da177e4SLinus Torvalds 
719*1da177e4SLinus Torvalds 	if (im->unsolicit_count) {
720*1da177e4SLinus Torvalds 		im->unsolicit_count--;
721*1da177e4SLinus Torvalds 		igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
722*1da177e4SLinus Torvalds 	}
723*1da177e4SLinus Torvalds 	im->reporter = 1;
724*1da177e4SLinus Torvalds 	spin_unlock(&im->lock);
725*1da177e4SLinus Torvalds 
726*1da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev))
727*1da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
728*1da177e4SLinus Torvalds 	else if (IGMP_V2_SEEN(in_dev))
729*1da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
730*1da177e4SLinus Torvalds 	else
731*1da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
732*1da177e4SLinus Torvalds 
733*1da177e4SLinus Torvalds 	ip_ma_put(im);
734*1da177e4SLinus Torvalds }
735*1da177e4SLinus Torvalds 
736*1da177e4SLinus Torvalds static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
737*1da177e4SLinus Torvalds {
738*1da177e4SLinus Torvalds 	struct ip_sf_list *psf;
739*1da177e4SLinus Torvalds 	int i, scount;
740*1da177e4SLinus Torvalds 
741*1da177e4SLinus Torvalds 	scount = 0;
742*1da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
743*1da177e4SLinus Torvalds 		if (scount == nsrcs)
744*1da177e4SLinus Torvalds 			break;
745*1da177e4SLinus Torvalds 		for (i=0; i<nsrcs; i++)
746*1da177e4SLinus Torvalds 			if (srcs[i] == psf->sf_inaddr) {
747*1da177e4SLinus Torvalds 				psf->sf_gsresp = 1;
748*1da177e4SLinus Torvalds 				scount++;
749*1da177e4SLinus Torvalds 				break;
750*1da177e4SLinus Torvalds 			}
751*1da177e4SLinus Torvalds 	}
752*1da177e4SLinus Torvalds }
753*1da177e4SLinus Torvalds 
754*1da177e4SLinus Torvalds static void igmp_heard_report(struct in_device *in_dev, u32 group)
755*1da177e4SLinus Torvalds {
756*1da177e4SLinus Torvalds 	struct ip_mc_list *im;
757*1da177e4SLinus Torvalds 
758*1da177e4SLinus Torvalds 	/* Timers are only set for non-local groups */
759*1da177e4SLinus Torvalds 
760*1da177e4SLinus Torvalds 	if (group == IGMP_ALL_HOSTS)
761*1da177e4SLinus Torvalds 		return;
762*1da177e4SLinus Torvalds 
763*1da177e4SLinus Torvalds 	read_lock(&in_dev->mc_list_lock);
764*1da177e4SLinus Torvalds 	for (im=in_dev->mc_list; im!=NULL; im=im->next) {
765*1da177e4SLinus Torvalds 		if (im->multiaddr == group) {
766*1da177e4SLinus Torvalds 			igmp_stop_timer(im);
767*1da177e4SLinus Torvalds 			break;
768*1da177e4SLinus Torvalds 		}
769*1da177e4SLinus Torvalds 	}
770*1da177e4SLinus Torvalds 	read_unlock(&in_dev->mc_list_lock);
771*1da177e4SLinus Torvalds }
772*1da177e4SLinus Torvalds 
773*1da177e4SLinus Torvalds static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
774*1da177e4SLinus Torvalds 	int len)
775*1da177e4SLinus Torvalds {
776*1da177e4SLinus Torvalds 	struct igmphdr 		*ih = skb->h.igmph;
777*1da177e4SLinus Torvalds 	struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
778*1da177e4SLinus Torvalds 	struct ip_mc_list	*im;
779*1da177e4SLinus Torvalds 	u32			group = ih->group;
780*1da177e4SLinus Torvalds 	int			max_delay;
781*1da177e4SLinus Torvalds 	int			mark = 0;
782*1da177e4SLinus Torvalds 
783*1da177e4SLinus Torvalds 
784*1da177e4SLinus Torvalds 	if (len == 8) {
785*1da177e4SLinus Torvalds 		if (ih->code == 0) {
786*1da177e4SLinus Torvalds 			/* Alas, old v1 router presents here. */
787*1da177e4SLinus Torvalds 
788*1da177e4SLinus Torvalds 			max_delay = IGMP_Query_Response_Interval;
789*1da177e4SLinus Torvalds 			in_dev->mr_v1_seen = jiffies +
790*1da177e4SLinus Torvalds 				IGMP_V1_Router_Present_Timeout;
791*1da177e4SLinus Torvalds 			group = 0;
792*1da177e4SLinus Torvalds 		} else {
793*1da177e4SLinus Torvalds 			/* v2 router present */
794*1da177e4SLinus Torvalds 			max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
795*1da177e4SLinus Torvalds 			in_dev->mr_v2_seen = jiffies +
796*1da177e4SLinus Torvalds 				IGMP_V2_Router_Present_Timeout;
797*1da177e4SLinus Torvalds 		}
798*1da177e4SLinus Torvalds 		/* cancel the interface change timer */
799*1da177e4SLinus Torvalds 		in_dev->mr_ifc_count = 0;
800*1da177e4SLinus Torvalds 		if (del_timer(&in_dev->mr_ifc_timer))
801*1da177e4SLinus Torvalds 			__in_dev_put(in_dev);
802*1da177e4SLinus Torvalds 		/* clear deleted report items */
803*1da177e4SLinus Torvalds 		igmpv3_clear_delrec(in_dev);
804*1da177e4SLinus Torvalds 	} else if (len < 12) {
805*1da177e4SLinus Torvalds 		return;	/* ignore bogus packet; freed by caller */
806*1da177e4SLinus Torvalds 	} else { /* v3 */
807*1da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
808*1da177e4SLinus Torvalds 			return;
809*1da177e4SLinus Torvalds 
810*1da177e4SLinus Torvalds 		ih3 = (struct igmpv3_query *) skb->h.raw;
811*1da177e4SLinus Torvalds 		if (ih3->nsrcs) {
812*1da177e4SLinus Torvalds 			if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
813*1da177e4SLinus Torvalds 					   + ntohs(ih3->nsrcs)*sizeof(__u32)))
814*1da177e4SLinus Torvalds 				return;
815*1da177e4SLinus Torvalds 			ih3 = (struct igmpv3_query *) skb->h.raw;
816*1da177e4SLinus Torvalds 		}
817*1da177e4SLinus Torvalds 
818*1da177e4SLinus Torvalds 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
819*1da177e4SLinus Torvalds 		if (!max_delay)
820*1da177e4SLinus Torvalds 			max_delay = 1;	/* can't mod w/ 0 */
821*1da177e4SLinus Torvalds 		in_dev->mr_maxdelay = max_delay;
822*1da177e4SLinus Torvalds 		if (ih3->qrv)
823*1da177e4SLinus Torvalds 			in_dev->mr_qrv = ih3->qrv;
824*1da177e4SLinus Torvalds 		if (!group) { /* general query */
825*1da177e4SLinus Torvalds 			if (ih3->nsrcs)
826*1da177e4SLinus Torvalds 				return;	/* no sources allowed */
827*1da177e4SLinus Torvalds 			igmp_gq_start_timer(in_dev);
828*1da177e4SLinus Torvalds 			return;
829*1da177e4SLinus Torvalds 		}
830*1da177e4SLinus Torvalds 		/* mark sources to include, if group & source-specific */
831*1da177e4SLinus Torvalds 		mark = ih3->nsrcs != 0;
832*1da177e4SLinus Torvalds 	}
833*1da177e4SLinus Torvalds 
834*1da177e4SLinus Torvalds 	/*
835*1da177e4SLinus Torvalds 	 * - Start the timers in all of our membership records
836*1da177e4SLinus Torvalds 	 *   that the query applies to for the interface on
837*1da177e4SLinus Torvalds 	 *   which the query arrived excl. those that belong
838*1da177e4SLinus Torvalds 	 *   to a "local" group (224.0.0.X)
839*1da177e4SLinus Torvalds 	 * - For timers already running check if they need to
840*1da177e4SLinus Torvalds 	 *   be reset.
841*1da177e4SLinus Torvalds 	 * - Use the igmp->igmp_code field as the maximum
842*1da177e4SLinus Torvalds 	 *   delay possible
843*1da177e4SLinus Torvalds 	 */
844*1da177e4SLinus Torvalds 	read_lock(&in_dev->mc_list_lock);
845*1da177e4SLinus Torvalds 	for (im=in_dev->mc_list; im!=NULL; im=im->next) {
846*1da177e4SLinus Torvalds 		if (group && group != im->multiaddr)
847*1da177e4SLinus Torvalds 			continue;
848*1da177e4SLinus Torvalds 		if (im->multiaddr == IGMP_ALL_HOSTS)
849*1da177e4SLinus Torvalds 			continue;
850*1da177e4SLinus Torvalds 		spin_lock_bh(&im->lock);
851*1da177e4SLinus Torvalds 		if (im->tm_running)
852*1da177e4SLinus Torvalds 			im->gsquery = im->gsquery && mark;
853*1da177e4SLinus Torvalds 		else
854*1da177e4SLinus Torvalds 			im->gsquery = mark;
855*1da177e4SLinus Torvalds 		if (im->gsquery)
856*1da177e4SLinus Torvalds 			igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
857*1da177e4SLinus Torvalds 		spin_unlock_bh(&im->lock);
858*1da177e4SLinus Torvalds 		igmp_mod_timer(im, max_delay);
859*1da177e4SLinus Torvalds 	}
860*1da177e4SLinus Torvalds 	read_unlock(&in_dev->mc_list_lock);
861*1da177e4SLinus Torvalds }
862*1da177e4SLinus Torvalds 
863*1da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb)
864*1da177e4SLinus Torvalds {
865*1da177e4SLinus Torvalds 	/* This basically follows the spec line by line -- see RFC1112 */
866*1da177e4SLinus Torvalds 	struct igmphdr *ih;
867*1da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(skb->dev);
868*1da177e4SLinus Torvalds 	int len = skb->len;
869*1da177e4SLinus Torvalds 
870*1da177e4SLinus Torvalds 	if (in_dev==NULL) {
871*1da177e4SLinus Torvalds 		kfree_skb(skb);
872*1da177e4SLinus Torvalds 		return 0;
873*1da177e4SLinus Torvalds 	}
874*1da177e4SLinus Torvalds 
875*1da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct igmphdr)) ||
876*1da177e4SLinus Torvalds 	    (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
877*1da177e4SLinus Torvalds 		in_dev_put(in_dev);
878*1da177e4SLinus Torvalds 		kfree_skb(skb);
879*1da177e4SLinus Torvalds 		return 0;
880*1da177e4SLinus Torvalds 	}
881*1da177e4SLinus Torvalds 
882*1da177e4SLinus Torvalds 	ih = skb->h.igmph;
883*1da177e4SLinus Torvalds 	switch (ih->type) {
884*1da177e4SLinus Torvalds 	case IGMP_HOST_MEMBERSHIP_QUERY:
885*1da177e4SLinus Torvalds 		igmp_heard_query(in_dev, skb, len);
886*1da177e4SLinus Torvalds 		break;
887*1da177e4SLinus Torvalds 	case IGMP_HOST_MEMBERSHIP_REPORT:
888*1da177e4SLinus Torvalds 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
889*1da177e4SLinus Torvalds 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
890*1da177e4SLinus Torvalds 		/* Is it our report looped back? */
891*1da177e4SLinus Torvalds 		if (((struct rtable*)skb->dst)->fl.iif == 0)
892*1da177e4SLinus Torvalds 			break;
893*1da177e4SLinus Torvalds 		igmp_heard_report(in_dev, ih->group);
894*1da177e4SLinus Torvalds 		break;
895*1da177e4SLinus Torvalds 	case IGMP_PIM:
896*1da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1
897*1da177e4SLinus Torvalds 		in_dev_put(in_dev);
898*1da177e4SLinus Torvalds 		return pim_rcv_v1(skb);
899*1da177e4SLinus Torvalds #endif
900*1da177e4SLinus Torvalds 	case IGMP_DVMRP:
901*1da177e4SLinus Torvalds 	case IGMP_TRACE:
902*1da177e4SLinus Torvalds 	case IGMP_HOST_LEAVE_MESSAGE:
903*1da177e4SLinus Torvalds 	case IGMP_MTRACE:
904*1da177e4SLinus Torvalds 	case IGMP_MTRACE_RESP:
905*1da177e4SLinus Torvalds 		break;
906*1da177e4SLinus Torvalds 	default:
907*1da177e4SLinus Torvalds 		NETDEBUG(printk(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type));
908*1da177e4SLinus Torvalds 	}
909*1da177e4SLinus Torvalds 	in_dev_put(in_dev);
910*1da177e4SLinus Torvalds 	kfree_skb(skb);
911*1da177e4SLinus Torvalds 	return 0;
912*1da177e4SLinus Torvalds }
913*1da177e4SLinus Torvalds 
914*1da177e4SLinus Torvalds #endif
915*1da177e4SLinus Torvalds 
916*1da177e4SLinus Torvalds 
917*1da177e4SLinus Torvalds /*
918*1da177e4SLinus Torvalds  *	Add a filter to a device
919*1da177e4SLinus Torvalds  */
920*1da177e4SLinus Torvalds 
921*1da177e4SLinus Torvalds static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
922*1da177e4SLinus Torvalds {
923*1da177e4SLinus Torvalds 	char buf[MAX_ADDR_LEN];
924*1da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
925*1da177e4SLinus Torvalds 
926*1da177e4SLinus Torvalds 	/* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
927*1da177e4SLinus Torvalds 	   We will get multicast token leakage, when IFF_MULTICAST
928*1da177e4SLinus Torvalds 	   is changed. This check should be done in dev->set_multicast_list
929*1da177e4SLinus Torvalds 	   routine. Something sort of:
930*1da177e4SLinus Torvalds 	   if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
931*1da177e4SLinus Torvalds 	   --ANK
932*1da177e4SLinus Torvalds 	   */
933*1da177e4SLinus Torvalds 	if (arp_mc_map(addr, buf, dev, 0) == 0)
934*1da177e4SLinus Torvalds 		dev_mc_add(dev,buf,dev->addr_len,0);
935*1da177e4SLinus Torvalds }
936*1da177e4SLinus Torvalds 
937*1da177e4SLinus Torvalds /*
938*1da177e4SLinus Torvalds  *	Remove a filter from a device
939*1da177e4SLinus Torvalds  */
940*1da177e4SLinus Torvalds 
941*1da177e4SLinus Torvalds static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
942*1da177e4SLinus Torvalds {
943*1da177e4SLinus Torvalds 	char buf[MAX_ADDR_LEN];
944*1da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
945*1da177e4SLinus Torvalds 
946*1da177e4SLinus Torvalds 	if (arp_mc_map(addr, buf, dev, 0) == 0)
947*1da177e4SLinus Torvalds 		dev_mc_delete(dev,buf,dev->addr_len,0);
948*1da177e4SLinus Torvalds }
949*1da177e4SLinus Torvalds 
950*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
951*1da177e4SLinus Torvalds /*
952*1da177e4SLinus Torvalds  * deleted ip_mc_list manipulation
953*1da177e4SLinus Torvalds  */
954*1da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
955*1da177e4SLinus Torvalds {
956*1da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
957*1da177e4SLinus Torvalds 
958*1da177e4SLinus Torvalds 	/* this is an "ip_mc_list" for convenience; only the fields below
959*1da177e4SLinus Torvalds 	 * are actually used. In particular, the refcnt and users are not
960*1da177e4SLinus Torvalds 	 * used for management of the delete list. Using the same structure
961*1da177e4SLinus Torvalds 	 * for deleted items allows change reports to use common code with
962*1da177e4SLinus Torvalds 	 * non-deleted or query-response MCA's.
963*1da177e4SLinus Torvalds 	 */
964*1da177e4SLinus Torvalds 	pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
965*1da177e4SLinus Torvalds 	if (!pmc)
966*1da177e4SLinus Torvalds 		return;
967*1da177e4SLinus Torvalds 	memset(pmc, 0, sizeof(*pmc));
968*1da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
969*1da177e4SLinus Torvalds 	pmc->interface = im->interface;
970*1da177e4SLinus Torvalds 	in_dev_hold(in_dev);
971*1da177e4SLinus Torvalds 	pmc->multiaddr = im->multiaddr;
972*1da177e4SLinus Torvalds 	pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
973*1da177e4SLinus Torvalds 		IGMP_Unsolicited_Report_Count;
974*1da177e4SLinus Torvalds 	pmc->sfmode = im->sfmode;
975*1da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_INCLUDE) {
976*1da177e4SLinus Torvalds 		struct ip_sf_list *psf;
977*1da177e4SLinus Torvalds 
978*1da177e4SLinus Torvalds 		pmc->tomb = im->tomb;
979*1da177e4SLinus Torvalds 		pmc->sources = im->sources;
980*1da177e4SLinus Torvalds 		im->tomb = im->sources = NULL;
981*1da177e4SLinus Torvalds 		for (psf=pmc->sources; psf; psf=psf->sf_next)
982*1da177e4SLinus Torvalds 			psf->sf_crcount = pmc->crcount;
983*1da177e4SLinus Torvalds 	}
984*1da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
985*1da177e4SLinus Torvalds 
986*1da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
987*1da177e4SLinus Torvalds 	pmc->next = in_dev->mc_tomb;
988*1da177e4SLinus Torvalds 	in_dev->mc_tomb = pmc;
989*1da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
990*1da177e4SLinus Torvalds }
991*1da177e4SLinus Torvalds 
992*1da177e4SLinus Torvalds static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
993*1da177e4SLinus Torvalds {
994*1da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *pmc_prev;
995*1da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_next;
996*1da177e4SLinus Torvalds 
997*1da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
998*1da177e4SLinus Torvalds 	pmc_prev = NULL;
999*1da177e4SLinus Torvalds 	for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1000*1da177e4SLinus Torvalds 		if (pmc->multiaddr == multiaddr)
1001*1da177e4SLinus Torvalds 			break;
1002*1da177e4SLinus Torvalds 		pmc_prev = pmc;
1003*1da177e4SLinus Torvalds 	}
1004*1da177e4SLinus Torvalds 	if (pmc) {
1005*1da177e4SLinus Torvalds 		if (pmc_prev)
1006*1da177e4SLinus Torvalds 			pmc_prev->next = pmc->next;
1007*1da177e4SLinus Torvalds 		else
1008*1da177e4SLinus Torvalds 			in_dev->mc_tomb = pmc->next;
1009*1da177e4SLinus Torvalds 	}
1010*1da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
1011*1da177e4SLinus Torvalds 	if (pmc) {
1012*1da177e4SLinus Torvalds 		for (psf=pmc->tomb; psf; psf=psf_next) {
1013*1da177e4SLinus Torvalds 			psf_next = psf->sf_next;
1014*1da177e4SLinus Torvalds 			kfree(psf);
1015*1da177e4SLinus Torvalds 		}
1016*1da177e4SLinus Torvalds 		in_dev_put(pmc->interface);
1017*1da177e4SLinus Torvalds 		kfree(pmc);
1018*1da177e4SLinus Torvalds 	}
1019*1da177e4SLinus Torvalds }
1020*1da177e4SLinus Torvalds 
1021*1da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev)
1022*1da177e4SLinus Torvalds {
1023*1da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *nextpmc;
1024*1da177e4SLinus Torvalds 
1025*1da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
1026*1da177e4SLinus Torvalds 	pmc = in_dev->mc_tomb;
1027*1da177e4SLinus Torvalds 	in_dev->mc_tomb = NULL;
1028*1da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
1029*1da177e4SLinus Torvalds 
1030*1da177e4SLinus Torvalds 	for (; pmc; pmc = nextpmc) {
1031*1da177e4SLinus Torvalds 		nextpmc = pmc->next;
1032*1da177e4SLinus Torvalds 		ip_mc_clear_src(pmc);
1033*1da177e4SLinus Torvalds 		in_dev_put(pmc->interface);
1034*1da177e4SLinus Torvalds 		kfree(pmc);
1035*1da177e4SLinus Torvalds 	}
1036*1da177e4SLinus Torvalds 	/* clear dead sources, too */
1037*1da177e4SLinus Torvalds 	read_lock(&in_dev->mc_list_lock);
1038*1da177e4SLinus Torvalds 	for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1039*1da177e4SLinus Torvalds 		struct ip_sf_list *psf, *psf_next;
1040*1da177e4SLinus Torvalds 
1041*1da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
1042*1da177e4SLinus Torvalds 		psf = pmc->tomb;
1043*1da177e4SLinus Torvalds 		pmc->tomb = NULL;
1044*1da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
1045*1da177e4SLinus Torvalds 		for (; psf; psf=psf_next) {
1046*1da177e4SLinus Torvalds 			psf_next = psf->sf_next;
1047*1da177e4SLinus Torvalds 			kfree(psf);
1048*1da177e4SLinus Torvalds 		}
1049*1da177e4SLinus Torvalds 	}
1050*1da177e4SLinus Torvalds 	read_unlock(&in_dev->mc_list_lock);
1051*1da177e4SLinus Torvalds }
1052*1da177e4SLinus Torvalds #endif
1053*1da177e4SLinus Torvalds 
1054*1da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im)
1055*1da177e4SLinus Torvalds {
1056*1da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
1057*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1058*1da177e4SLinus Torvalds 	int reporter;
1059*1da177e4SLinus Torvalds #endif
1060*1da177e4SLinus Torvalds 
1061*1da177e4SLinus Torvalds 	if (im->loaded) {
1062*1da177e4SLinus Torvalds 		im->loaded = 0;
1063*1da177e4SLinus Torvalds 		ip_mc_filter_del(in_dev, im->multiaddr);
1064*1da177e4SLinus Torvalds 	}
1065*1da177e4SLinus Torvalds 
1066*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1067*1da177e4SLinus Torvalds 	if (im->multiaddr == IGMP_ALL_HOSTS)
1068*1da177e4SLinus Torvalds 		return;
1069*1da177e4SLinus Torvalds 
1070*1da177e4SLinus Torvalds 	reporter = im->reporter;
1071*1da177e4SLinus Torvalds 	igmp_stop_timer(im);
1072*1da177e4SLinus Torvalds 
1073*1da177e4SLinus Torvalds 	if (!in_dev->dead) {
1074*1da177e4SLinus Torvalds 		if (IGMP_V1_SEEN(in_dev))
1075*1da177e4SLinus Torvalds 			goto done;
1076*1da177e4SLinus Torvalds 		if (IGMP_V2_SEEN(in_dev)) {
1077*1da177e4SLinus Torvalds 			if (reporter)
1078*1da177e4SLinus Torvalds 				igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1079*1da177e4SLinus Torvalds 			goto done;
1080*1da177e4SLinus Torvalds 		}
1081*1da177e4SLinus Torvalds 		/* IGMPv3 */
1082*1da177e4SLinus Torvalds 		igmpv3_add_delrec(in_dev, im);
1083*1da177e4SLinus Torvalds 
1084*1da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
1085*1da177e4SLinus Torvalds 	}
1086*1da177e4SLinus Torvalds done:
1087*1da177e4SLinus Torvalds #endif
1088*1da177e4SLinus Torvalds 	ip_mc_clear_src(im);
1089*1da177e4SLinus Torvalds }
1090*1da177e4SLinus Torvalds 
1091*1da177e4SLinus Torvalds static void igmp_group_added(struct ip_mc_list *im)
1092*1da177e4SLinus Torvalds {
1093*1da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
1094*1da177e4SLinus Torvalds 
1095*1da177e4SLinus Torvalds 	if (im->loaded == 0) {
1096*1da177e4SLinus Torvalds 		im->loaded = 1;
1097*1da177e4SLinus Torvalds 		ip_mc_filter_add(in_dev, im->multiaddr);
1098*1da177e4SLinus Torvalds 	}
1099*1da177e4SLinus Torvalds 
1100*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1101*1da177e4SLinus Torvalds 	if (im->multiaddr == IGMP_ALL_HOSTS)
1102*1da177e4SLinus Torvalds 		return;
1103*1da177e4SLinus Torvalds 
1104*1da177e4SLinus Torvalds 	if (in_dev->dead)
1105*1da177e4SLinus Torvalds 		return;
1106*1da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1107*1da177e4SLinus Torvalds 		spin_lock_bh(&im->lock);
1108*1da177e4SLinus Torvalds 		igmp_start_timer(im, IGMP_Initial_Report_Delay);
1109*1da177e4SLinus Torvalds 		spin_unlock_bh(&im->lock);
1110*1da177e4SLinus Torvalds 		return;
1111*1da177e4SLinus Torvalds 	}
1112*1da177e4SLinus Torvalds 	/* else, v3 */
1113*1da177e4SLinus Torvalds 
1114*1da177e4SLinus Torvalds 	im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1115*1da177e4SLinus Torvalds 		IGMP_Unsolicited_Report_Count;
1116*1da177e4SLinus Torvalds 	igmp_ifc_event(in_dev);
1117*1da177e4SLinus Torvalds #endif
1118*1da177e4SLinus Torvalds }
1119*1da177e4SLinus Torvalds 
1120*1da177e4SLinus Torvalds 
1121*1da177e4SLinus Torvalds /*
1122*1da177e4SLinus Torvalds  *	Multicast list managers
1123*1da177e4SLinus Torvalds  */
1124*1da177e4SLinus Torvalds 
1125*1da177e4SLinus Torvalds 
1126*1da177e4SLinus Torvalds /*
1127*1da177e4SLinus Torvalds  *	A socket has joined a multicast group on device dev.
1128*1da177e4SLinus Torvalds  */
1129*1da177e4SLinus Torvalds 
1130*1da177e4SLinus Torvalds void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1131*1da177e4SLinus Torvalds {
1132*1da177e4SLinus Torvalds 	struct ip_mc_list *im;
1133*1da177e4SLinus Torvalds 
1134*1da177e4SLinus Torvalds 	ASSERT_RTNL();
1135*1da177e4SLinus Torvalds 
1136*1da177e4SLinus Torvalds 	for (im=in_dev->mc_list; im; im=im->next) {
1137*1da177e4SLinus Torvalds 		if (im->multiaddr == addr) {
1138*1da177e4SLinus Torvalds 			im->users++;
1139*1da177e4SLinus Torvalds 			ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1140*1da177e4SLinus Torvalds 			goto out;
1141*1da177e4SLinus Torvalds 		}
1142*1da177e4SLinus Torvalds 	}
1143*1da177e4SLinus Torvalds 
1144*1da177e4SLinus Torvalds 	im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1145*1da177e4SLinus Torvalds 	if (!im)
1146*1da177e4SLinus Torvalds 		goto out;
1147*1da177e4SLinus Torvalds 
1148*1da177e4SLinus Torvalds 	im->users=1;
1149*1da177e4SLinus Torvalds 	im->interface=in_dev;
1150*1da177e4SLinus Torvalds 	in_dev_hold(in_dev);
1151*1da177e4SLinus Torvalds 	im->multiaddr=addr;
1152*1da177e4SLinus Torvalds 	/* initial mode is (EX, empty) */
1153*1da177e4SLinus Torvalds 	im->sfmode = MCAST_EXCLUDE;
1154*1da177e4SLinus Torvalds 	im->sfcount[MCAST_INCLUDE] = 0;
1155*1da177e4SLinus Torvalds 	im->sfcount[MCAST_EXCLUDE] = 1;
1156*1da177e4SLinus Torvalds 	im->sources = NULL;
1157*1da177e4SLinus Torvalds 	im->tomb = NULL;
1158*1da177e4SLinus Torvalds 	im->crcount = 0;
1159*1da177e4SLinus Torvalds 	atomic_set(&im->refcnt, 1);
1160*1da177e4SLinus Torvalds 	spin_lock_init(&im->lock);
1161*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1162*1da177e4SLinus Torvalds 	im->tm_running=0;
1163*1da177e4SLinus Torvalds 	init_timer(&im->timer);
1164*1da177e4SLinus Torvalds 	im->timer.data=(unsigned long)im;
1165*1da177e4SLinus Torvalds 	im->timer.function=&igmp_timer_expire;
1166*1da177e4SLinus Torvalds 	im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1167*1da177e4SLinus Torvalds 	im->reporter = 0;
1168*1da177e4SLinus Torvalds 	im->gsquery = 0;
1169*1da177e4SLinus Torvalds #endif
1170*1da177e4SLinus Torvalds 	im->loaded = 0;
1171*1da177e4SLinus Torvalds 	write_lock_bh(&in_dev->mc_list_lock);
1172*1da177e4SLinus Torvalds 	im->next=in_dev->mc_list;
1173*1da177e4SLinus Torvalds 	in_dev->mc_list=im;
1174*1da177e4SLinus Torvalds 	write_unlock_bh(&in_dev->mc_list_lock);
1175*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1176*1da177e4SLinus Torvalds 	igmpv3_del_delrec(in_dev, im->multiaddr);
1177*1da177e4SLinus Torvalds #endif
1178*1da177e4SLinus Torvalds 	igmp_group_added(im);
1179*1da177e4SLinus Torvalds 	if (!in_dev->dead)
1180*1da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
1181*1da177e4SLinus Torvalds out:
1182*1da177e4SLinus Torvalds 	return;
1183*1da177e4SLinus Torvalds }
1184*1da177e4SLinus Torvalds 
1185*1da177e4SLinus Torvalds /*
1186*1da177e4SLinus Torvalds  *	A socket has left a multicast group on device dev
1187*1da177e4SLinus Torvalds  */
1188*1da177e4SLinus Torvalds 
1189*1da177e4SLinus Torvalds void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1190*1da177e4SLinus Torvalds {
1191*1da177e4SLinus Torvalds 	struct ip_mc_list *i, **ip;
1192*1da177e4SLinus Torvalds 
1193*1da177e4SLinus Torvalds 	ASSERT_RTNL();
1194*1da177e4SLinus Torvalds 
1195*1da177e4SLinus Torvalds 	for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1196*1da177e4SLinus Torvalds 		if (i->multiaddr==addr) {
1197*1da177e4SLinus Torvalds 			if (--i->users == 0) {
1198*1da177e4SLinus Torvalds 				write_lock_bh(&in_dev->mc_list_lock);
1199*1da177e4SLinus Torvalds 				*ip = i->next;
1200*1da177e4SLinus Torvalds 				write_unlock_bh(&in_dev->mc_list_lock);
1201*1da177e4SLinus Torvalds 				igmp_group_dropped(i);
1202*1da177e4SLinus Torvalds 
1203*1da177e4SLinus Torvalds 				if (!in_dev->dead)
1204*1da177e4SLinus Torvalds 					ip_rt_multicast_event(in_dev);
1205*1da177e4SLinus Torvalds 
1206*1da177e4SLinus Torvalds 				ip_ma_put(i);
1207*1da177e4SLinus Torvalds 				return;
1208*1da177e4SLinus Torvalds 			}
1209*1da177e4SLinus Torvalds 			break;
1210*1da177e4SLinus Torvalds 		}
1211*1da177e4SLinus Torvalds 	}
1212*1da177e4SLinus Torvalds }
1213*1da177e4SLinus Torvalds 
1214*1da177e4SLinus Torvalds /* Device going down */
1215*1da177e4SLinus Torvalds 
1216*1da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev)
1217*1da177e4SLinus Torvalds {
1218*1da177e4SLinus Torvalds 	struct ip_mc_list *i;
1219*1da177e4SLinus Torvalds 
1220*1da177e4SLinus Torvalds 	ASSERT_RTNL();
1221*1da177e4SLinus Torvalds 
1222*1da177e4SLinus Torvalds 	for (i=in_dev->mc_list; i; i=i->next)
1223*1da177e4SLinus Torvalds 		igmp_group_dropped(i);
1224*1da177e4SLinus Torvalds 
1225*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1226*1da177e4SLinus Torvalds 	in_dev->mr_ifc_count = 0;
1227*1da177e4SLinus Torvalds 	if (del_timer(&in_dev->mr_ifc_timer))
1228*1da177e4SLinus Torvalds 		__in_dev_put(in_dev);
1229*1da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
1230*1da177e4SLinus Torvalds 	if (del_timer(&in_dev->mr_gq_timer))
1231*1da177e4SLinus Torvalds 		__in_dev_put(in_dev);
1232*1da177e4SLinus Torvalds 	igmpv3_clear_delrec(in_dev);
1233*1da177e4SLinus Torvalds #endif
1234*1da177e4SLinus Torvalds 
1235*1da177e4SLinus Torvalds 	ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1236*1da177e4SLinus Torvalds }
1237*1da177e4SLinus Torvalds 
1238*1da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev)
1239*1da177e4SLinus Torvalds {
1240*1da177e4SLinus Torvalds 	ASSERT_RTNL();
1241*1da177e4SLinus Torvalds 
1242*1da177e4SLinus Torvalds 	in_dev->mc_tomb = NULL;
1243*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1244*1da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
1245*1da177e4SLinus Torvalds 	init_timer(&in_dev->mr_gq_timer);
1246*1da177e4SLinus Torvalds 	in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1247*1da177e4SLinus Torvalds 	in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1248*1da177e4SLinus Torvalds 	in_dev->mr_ifc_count = 0;
1249*1da177e4SLinus Torvalds 	init_timer(&in_dev->mr_ifc_timer);
1250*1da177e4SLinus Torvalds 	in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1251*1da177e4SLinus Torvalds 	in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1252*1da177e4SLinus Torvalds 	in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1253*1da177e4SLinus Torvalds #endif
1254*1da177e4SLinus Torvalds 
1255*1da177e4SLinus Torvalds 	rwlock_init(&in_dev->mc_list_lock);
1256*1da177e4SLinus Torvalds 	spin_lock_init(&in_dev->mc_tomb_lock);
1257*1da177e4SLinus Torvalds }
1258*1da177e4SLinus Torvalds 
1259*1da177e4SLinus Torvalds /* Device going up */
1260*1da177e4SLinus Torvalds 
1261*1da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev)
1262*1da177e4SLinus Torvalds {
1263*1da177e4SLinus Torvalds 	struct ip_mc_list *i;
1264*1da177e4SLinus Torvalds 
1265*1da177e4SLinus Torvalds 	ASSERT_RTNL();
1266*1da177e4SLinus Torvalds 
1267*1da177e4SLinus Torvalds 	ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1268*1da177e4SLinus Torvalds 
1269*1da177e4SLinus Torvalds 	for (i=in_dev->mc_list; i; i=i->next)
1270*1da177e4SLinus Torvalds 		igmp_group_added(i);
1271*1da177e4SLinus Torvalds }
1272*1da177e4SLinus Torvalds 
1273*1da177e4SLinus Torvalds /*
1274*1da177e4SLinus Torvalds  *	Device is about to be destroyed: clean up.
1275*1da177e4SLinus Torvalds  */
1276*1da177e4SLinus Torvalds 
1277*1da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev)
1278*1da177e4SLinus Torvalds {
1279*1da177e4SLinus Torvalds 	struct ip_mc_list *i;
1280*1da177e4SLinus Torvalds 
1281*1da177e4SLinus Torvalds 	ASSERT_RTNL();
1282*1da177e4SLinus Torvalds 
1283*1da177e4SLinus Torvalds 	/* Deactivate timers */
1284*1da177e4SLinus Torvalds 	ip_mc_down(in_dev);
1285*1da177e4SLinus Torvalds 
1286*1da177e4SLinus Torvalds 	write_lock_bh(&in_dev->mc_list_lock);
1287*1da177e4SLinus Torvalds 	while ((i = in_dev->mc_list) != NULL) {
1288*1da177e4SLinus Torvalds 		in_dev->mc_list = i->next;
1289*1da177e4SLinus Torvalds 		write_unlock_bh(&in_dev->mc_list_lock);
1290*1da177e4SLinus Torvalds 
1291*1da177e4SLinus Torvalds 		igmp_group_dropped(i);
1292*1da177e4SLinus Torvalds 		ip_ma_put(i);
1293*1da177e4SLinus Torvalds 
1294*1da177e4SLinus Torvalds 		write_lock_bh(&in_dev->mc_list_lock);
1295*1da177e4SLinus Torvalds 	}
1296*1da177e4SLinus Torvalds 	write_unlock_bh(&in_dev->mc_list_lock);
1297*1da177e4SLinus Torvalds }
1298*1da177e4SLinus Torvalds 
1299*1da177e4SLinus Torvalds static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1300*1da177e4SLinus Torvalds {
1301*1da177e4SLinus Torvalds 	struct flowi fl = { .nl_u = { .ip4_u =
1302*1da177e4SLinus Torvalds 				      { .daddr = imr->imr_multiaddr.s_addr } } };
1303*1da177e4SLinus Torvalds 	struct rtable *rt;
1304*1da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1305*1da177e4SLinus Torvalds 	struct in_device *idev = NULL;
1306*1da177e4SLinus Torvalds 
1307*1da177e4SLinus Torvalds 	if (imr->imr_ifindex) {
1308*1da177e4SLinus Torvalds 		idev = inetdev_by_index(imr->imr_ifindex);
1309*1da177e4SLinus Torvalds 		if (idev)
1310*1da177e4SLinus Torvalds 			__in_dev_put(idev);
1311*1da177e4SLinus Torvalds 		return idev;
1312*1da177e4SLinus Torvalds 	}
1313*1da177e4SLinus Torvalds 	if (imr->imr_address.s_addr) {
1314*1da177e4SLinus Torvalds 		dev = ip_dev_find(imr->imr_address.s_addr);
1315*1da177e4SLinus Torvalds 		if (!dev)
1316*1da177e4SLinus Torvalds 			return NULL;
1317*1da177e4SLinus Torvalds 		__dev_put(dev);
1318*1da177e4SLinus Torvalds 	}
1319*1da177e4SLinus Torvalds 
1320*1da177e4SLinus Torvalds 	if (!dev && !ip_route_output_key(&rt, &fl)) {
1321*1da177e4SLinus Torvalds 		dev = rt->u.dst.dev;
1322*1da177e4SLinus Torvalds 		ip_rt_put(rt);
1323*1da177e4SLinus Torvalds 	}
1324*1da177e4SLinus Torvalds 	if (dev) {
1325*1da177e4SLinus Torvalds 		imr->imr_ifindex = dev->ifindex;
1326*1da177e4SLinus Torvalds 		idev = __in_dev_get(dev);
1327*1da177e4SLinus Torvalds 	}
1328*1da177e4SLinus Torvalds 	return idev;
1329*1da177e4SLinus Torvalds }
1330*1da177e4SLinus Torvalds 
1331*1da177e4SLinus Torvalds /*
1332*1da177e4SLinus Torvalds  *	Join a socket to a group
1333*1da177e4SLinus Torvalds  */
1334*1da177e4SLinus Torvalds int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1335*1da177e4SLinus Torvalds int sysctl_igmp_max_msf = IP_MAX_MSF;
1336*1da177e4SLinus Torvalds 
1337*1da177e4SLinus Torvalds 
1338*1da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1339*1da177e4SLinus Torvalds 	__u32 *psfsrc)
1340*1da177e4SLinus Torvalds {
1341*1da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_prev;
1342*1da177e4SLinus Torvalds 	int rv = 0;
1343*1da177e4SLinus Torvalds 
1344*1da177e4SLinus Torvalds 	psf_prev = NULL;
1345*1da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
1346*1da177e4SLinus Torvalds 		if (psf->sf_inaddr == *psfsrc)
1347*1da177e4SLinus Torvalds 			break;
1348*1da177e4SLinus Torvalds 		psf_prev = psf;
1349*1da177e4SLinus Torvalds 	}
1350*1da177e4SLinus Torvalds 	if (!psf || psf->sf_count[sfmode] == 0) {
1351*1da177e4SLinus Torvalds 		/* source filter not found, or count wrong =>  bug */
1352*1da177e4SLinus Torvalds 		return -ESRCH;
1353*1da177e4SLinus Torvalds 	}
1354*1da177e4SLinus Torvalds 	psf->sf_count[sfmode]--;
1355*1da177e4SLinus Torvalds 	if (psf->sf_count[sfmode] == 0) {
1356*1da177e4SLinus Torvalds 		ip_rt_multicast_event(pmc->interface);
1357*1da177e4SLinus Torvalds 	}
1358*1da177e4SLinus Torvalds 	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1359*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1360*1da177e4SLinus Torvalds 		struct in_device *in_dev = pmc->interface;
1361*1da177e4SLinus Torvalds #endif
1362*1da177e4SLinus Torvalds 
1363*1da177e4SLinus Torvalds 		/* no more filters for this source */
1364*1da177e4SLinus Torvalds 		if (psf_prev)
1365*1da177e4SLinus Torvalds 			psf_prev->sf_next = psf->sf_next;
1366*1da177e4SLinus Torvalds 		else
1367*1da177e4SLinus Torvalds 			pmc->sources = psf->sf_next;
1368*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1369*1da177e4SLinus Torvalds 		if (psf->sf_oldin &&
1370*1da177e4SLinus Torvalds 		    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1371*1da177e4SLinus Torvalds 			psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1372*1da177e4SLinus Torvalds 				IGMP_Unsolicited_Report_Count;
1373*1da177e4SLinus Torvalds 			psf->sf_next = pmc->tomb;
1374*1da177e4SLinus Torvalds 			pmc->tomb = psf;
1375*1da177e4SLinus Torvalds 			rv = 1;
1376*1da177e4SLinus Torvalds 		} else
1377*1da177e4SLinus Torvalds #endif
1378*1da177e4SLinus Torvalds 			kfree(psf);
1379*1da177e4SLinus Torvalds 	}
1380*1da177e4SLinus Torvalds 	return rv;
1381*1da177e4SLinus Torvalds }
1382*1da177e4SLinus Torvalds 
1383*1da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST
1384*1da177e4SLinus Torvalds #define igmp_ifc_event(x)	do { } while (0)
1385*1da177e4SLinus Torvalds #endif
1386*1da177e4SLinus Torvalds 
1387*1da177e4SLinus Torvalds static int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1388*1da177e4SLinus Torvalds 			 int sfcount, __u32 *psfsrc, int delta)
1389*1da177e4SLinus Torvalds {
1390*1da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
1391*1da177e4SLinus Torvalds 	int	changerec = 0;
1392*1da177e4SLinus Torvalds 	int	i, err;
1393*1da177e4SLinus Torvalds 
1394*1da177e4SLinus Torvalds 	if (!in_dev)
1395*1da177e4SLinus Torvalds 		return -ENODEV;
1396*1da177e4SLinus Torvalds 	read_lock(&in_dev->mc_list_lock);
1397*1da177e4SLinus Torvalds 	for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1398*1da177e4SLinus Torvalds 		if (*pmca == pmc->multiaddr)
1399*1da177e4SLinus Torvalds 			break;
1400*1da177e4SLinus Torvalds 	}
1401*1da177e4SLinus Torvalds 	if (!pmc) {
1402*1da177e4SLinus Torvalds 		/* MCA not found?? bug */
1403*1da177e4SLinus Torvalds 		read_unlock(&in_dev->mc_list_lock);
1404*1da177e4SLinus Torvalds 		return -ESRCH;
1405*1da177e4SLinus Torvalds 	}
1406*1da177e4SLinus Torvalds 	spin_lock_bh(&pmc->lock);
1407*1da177e4SLinus Torvalds 	read_unlock(&in_dev->mc_list_lock);
1408*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1409*1da177e4SLinus Torvalds 	sf_markstate(pmc);
1410*1da177e4SLinus Torvalds #endif
1411*1da177e4SLinus Torvalds 	if (!delta) {
1412*1da177e4SLinus Torvalds 		err = -EINVAL;
1413*1da177e4SLinus Torvalds 		if (!pmc->sfcount[sfmode])
1414*1da177e4SLinus Torvalds 			goto out_unlock;
1415*1da177e4SLinus Torvalds 		pmc->sfcount[sfmode]--;
1416*1da177e4SLinus Torvalds 	}
1417*1da177e4SLinus Torvalds 	err = 0;
1418*1da177e4SLinus Torvalds 	for (i=0; i<sfcount; i++) {
1419*1da177e4SLinus Torvalds 		int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1420*1da177e4SLinus Torvalds 
1421*1da177e4SLinus Torvalds 		changerec |= rv > 0;
1422*1da177e4SLinus Torvalds 		if (!err && rv < 0)
1423*1da177e4SLinus Torvalds 			err = rv;
1424*1da177e4SLinus Torvalds 	}
1425*1da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_EXCLUDE &&
1426*1da177e4SLinus Torvalds 	    pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1427*1da177e4SLinus Torvalds 	    pmc->sfcount[MCAST_INCLUDE]) {
1428*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1429*1da177e4SLinus Torvalds 		struct ip_sf_list *psf;
1430*1da177e4SLinus Torvalds #endif
1431*1da177e4SLinus Torvalds 
1432*1da177e4SLinus Torvalds 		/* filter mode change */
1433*1da177e4SLinus Torvalds 		pmc->sfmode = MCAST_INCLUDE;
1434*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1435*1da177e4SLinus Torvalds 		pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1436*1da177e4SLinus Torvalds 			IGMP_Unsolicited_Report_Count;
1437*1da177e4SLinus Torvalds 		in_dev->mr_ifc_count = pmc->crcount;
1438*1da177e4SLinus Torvalds 		for (psf=pmc->sources; psf; psf = psf->sf_next)
1439*1da177e4SLinus Torvalds 			psf->sf_crcount = 0;
1440*1da177e4SLinus Torvalds 		igmp_ifc_event(pmc->interface);
1441*1da177e4SLinus Torvalds 	} else if (sf_setstate(pmc) || changerec) {
1442*1da177e4SLinus Torvalds 		igmp_ifc_event(pmc->interface);
1443*1da177e4SLinus Torvalds #endif
1444*1da177e4SLinus Torvalds 	}
1445*1da177e4SLinus Torvalds out_unlock:
1446*1da177e4SLinus Torvalds 	spin_unlock_bh(&pmc->lock);
1447*1da177e4SLinus Torvalds 	return err;
1448*1da177e4SLinus Torvalds }
1449*1da177e4SLinus Torvalds 
1450*1da177e4SLinus Torvalds /*
1451*1da177e4SLinus Torvalds  * Add multicast single-source filter to the interface list
1452*1da177e4SLinus Torvalds  */
1453*1da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1454*1da177e4SLinus Torvalds 	__u32 *psfsrc, int delta)
1455*1da177e4SLinus Torvalds {
1456*1da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_prev;
1457*1da177e4SLinus Torvalds 
1458*1da177e4SLinus Torvalds 	psf_prev = NULL;
1459*1da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
1460*1da177e4SLinus Torvalds 		if (psf->sf_inaddr == *psfsrc)
1461*1da177e4SLinus Torvalds 			break;
1462*1da177e4SLinus Torvalds 		psf_prev = psf;
1463*1da177e4SLinus Torvalds 	}
1464*1da177e4SLinus Torvalds 	if (!psf) {
1465*1da177e4SLinus Torvalds 		psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1466*1da177e4SLinus Torvalds 		if (!psf)
1467*1da177e4SLinus Torvalds 			return -ENOBUFS;
1468*1da177e4SLinus Torvalds 		memset(psf, 0, sizeof(*psf));
1469*1da177e4SLinus Torvalds 		psf->sf_inaddr = *psfsrc;
1470*1da177e4SLinus Torvalds 		if (psf_prev) {
1471*1da177e4SLinus Torvalds 			psf_prev->sf_next = psf;
1472*1da177e4SLinus Torvalds 		} else
1473*1da177e4SLinus Torvalds 			pmc->sources = psf;
1474*1da177e4SLinus Torvalds 	}
1475*1da177e4SLinus Torvalds 	psf->sf_count[sfmode]++;
1476*1da177e4SLinus Torvalds 	if (psf->sf_count[sfmode] == 1) {
1477*1da177e4SLinus Torvalds 		ip_rt_multicast_event(pmc->interface);
1478*1da177e4SLinus Torvalds 	}
1479*1da177e4SLinus Torvalds 	return 0;
1480*1da177e4SLinus Torvalds }
1481*1da177e4SLinus Torvalds 
1482*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1483*1da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc)
1484*1da177e4SLinus Torvalds {
1485*1da177e4SLinus Torvalds 	struct ip_sf_list *psf;
1486*1da177e4SLinus Torvalds 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1487*1da177e4SLinus Torvalds 
1488*1da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next)
1489*1da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
1490*1da177e4SLinus Torvalds 			psf->sf_oldin = mca_xcount ==
1491*1da177e4SLinus Torvalds 				psf->sf_count[MCAST_EXCLUDE] &&
1492*1da177e4SLinus Torvalds 				!psf->sf_count[MCAST_INCLUDE];
1493*1da177e4SLinus Torvalds 		} else
1494*1da177e4SLinus Torvalds 			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1495*1da177e4SLinus Torvalds }
1496*1da177e4SLinus Torvalds 
1497*1da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc)
1498*1da177e4SLinus Torvalds {
1499*1da177e4SLinus Torvalds 	struct ip_sf_list *psf;
1500*1da177e4SLinus Torvalds 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1501*1da177e4SLinus Torvalds 	int qrv = pmc->interface->mr_qrv;
1502*1da177e4SLinus Torvalds 	int new_in, rv;
1503*1da177e4SLinus Torvalds 
1504*1da177e4SLinus Torvalds 	rv = 0;
1505*1da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
1506*1da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
1507*1da177e4SLinus Torvalds 			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1508*1da177e4SLinus Torvalds 				!psf->sf_count[MCAST_INCLUDE];
1509*1da177e4SLinus Torvalds 		} else
1510*1da177e4SLinus Torvalds 			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1511*1da177e4SLinus Torvalds 		if (new_in != psf->sf_oldin) {
1512*1da177e4SLinus Torvalds 			psf->sf_crcount = qrv;
1513*1da177e4SLinus Torvalds 			rv++;
1514*1da177e4SLinus Torvalds 		}
1515*1da177e4SLinus Torvalds 	}
1516*1da177e4SLinus Torvalds 	return rv;
1517*1da177e4SLinus Torvalds }
1518*1da177e4SLinus Torvalds #endif
1519*1da177e4SLinus Torvalds 
1520*1da177e4SLinus Torvalds /*
1521*1da177e4SLinus Torvalds  * Add multicast source filter list to the interface list
1522*1da177e4SLinus Torvalds  */
1523*1da177e4SLinus Torvalds static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1524*1da177e4SLinus Torvalds 			 int sfcount, __u32 *psfsrc, int delta)
1525*1da177e4SLinus Torvalds {
1526*1da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
1527*1da177e4SLinus Torvalds 	int	isexclude;
1528*1da177e4SLinus Torvalds 	int	i, err;
1529*1da177e4SLinus Torvalds 
1530*1da177e4SLinus Torvalds 	if (!in_dev)
1531*1da177e4SLinus Torvalds 		return -ENODEV;
1532*1da177e4SLinus Torvalds 	read_lock(&in_dev->mc_list_lock);
1533*1da177e4SLinus Torvalds 	for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1534*1da177e4SLinus Torvalds 		if (*pmca == pmc->multiaddr)
1535*1da177e4SLinus Torvalds 			break;
1536*1da177e4SLinus Torvalds 	}
1537*1da177e4SLinus Torvalds 	if (!pmc) {
1538*1da177e4SLinus Torvalds 		/* MCA not found?? bug */
1539*1da177e4SLinus Torvalds 		read_unlock(&in_dev->mc_list_lock);
1540*1da177e4SLinus Torvalds 		return -ESRCH;
1541*1da177e4SLinus Torvalds 	}
1542*1da177e4SLinus Torvalds 	spin_lock_bh(&pmc->lock);
1543*1da177e4SLinus Torvalds 	read_unlock(&in_dev->mc_list_lock);
1544*1da177e4SLinus Torvalds 
1545*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1546*1da177e4SLinus Torvalds 	sf_markstate(pmc);
1547*1da177e4SLinus Torvalds #endif
1548*1da177e4SLinus Torvalds 	isexclude = pmc->sfmode == MCAST_EXCLUDE;
1549*1da177e4SLinus Torvalds 	if (!delta)
1550*1da177e4SLinus Torvalds 		pmc->sfcount[sfmode]++;
1551*1da177e4SLinus Torvalds 	err = 0;
1552*1da177e4SLinus Torvalds 	for (i=0; i<sfcount; i++) {
1553*1da177e4SLinus Torvalds 		err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1554*1da177e4SLinus Torvalds 		if (err)
1555*1da177e4SLinus Torvalds 			break;
1556*1da177e4SLinus Torvalds 	}
1557*1da177e4SLinus Torvalds 	if (err) {
1558*1da177e4SLinus Torvalds 		int j;
1559*1da177e4SLinus Torvalds 
1560*1da177e4SLinus Torvalds 		pmc->sfcount[sfmode]--;
1561*1da177e4SLinus Torvalds 		for (j=0; j<i; j++)
1562*1da177e4SLinus Torvalds 			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1563*1da177e4SLinus Torvalds 	} else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1564*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1565*1da177e4SLinus Torvalds 		struct in_device *in_dev = pmc->interface;
1566*1da177e4SLinus Torvalds 		struct ip_sf_list *psf;
1567*1da177e4SLinus Torvalds #endif
1568*1da177e4SLinus Torvalds 
1569*1da177e4SLinus Torvalds 		/* filter mode change */
1570*1da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE])
1571*1da177e4SLinus Torvalds 			pmc->sfmode = MCAST_EXCLUDE;
1572*1da177e4SLinus Torvalds 		else if (pmc->sfcount[MCAST_INCLUDE])
1573*1da177e4SLinus Torvalds 			pmc->sfmode = MCAST_INCLUDE;
1574*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1575*1da177e4SLinus Torvalds 		/* else no filters; keep old mode for reports */
1576*1da177e4SLinus Torvalds 
1577*1da177e4SLinus Torvalds 		pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1578*1da177e4SLinus Torvalds 			IGMP_Unsolicited_Report_Count;
1579*1da177e4SLinus Torvalds 		in_dev->mr_ifc_count = pmc->crcount;
1580*1da177e4SLinus Torvalds 		for (psf=pmc->sources; psf; psf = psf->sf_next)
1581*1da177e4SLinus Torvalds 			psf->sf_crcount = 0;
1582*1da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
1583*1da177e4SLinus Torvalds 	} else if (sf_setstate(pmc)) {
1584*1da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
1585*1da177e4SLinus Torvalds #endif
1586*1da177e4SLinus Torvalds 	}
1587*1da177e4SLinus Torvalds 	spin_unlock_bh(&pmc->lock);
1588*1da177e4SLinus Torvalds 	return err;
1589*1da177e4SLinus Torvalds }
1590*1da177e4SLinus Torvalds 
1591*1da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc)
1592*1da177e4SLinus Torvalds {
1593*1da177e4SLinus Torvalds 	struct ip_sf_list *psf, *nextpsf;
1594*1da177e4SLinus Torvalds 
1595*1da177e4SLinus Torvalds 	for (psf=pmc->tomb; psf; psf=nextpsf) {
1596*1da177e4SLinus Torvalds 		nextpsf = psf->sf_next;
1597*1da177e4SLinus Torvalds 		kfree(psf);
1598*1da177e4SLinus Torvalds 	}
1599*1da177e4SLinus Torvalds 	pmc->tomb = NULL;
1600*1da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=nextpsf) {
1601*1da177e4SLinus Torvalds 		nextpsf = psf->sf_next;
1602*1da177e4SLinus Torvalds 		kfree(psf);
1603*1da177e4SLinus Torvalds 	}
1604*1da177e4SLinus Torvalds 	pmc->sources = NULL;
1605*1da177e4SLinus Torvalds 	pmc->sfmode = MCAST_EXCLUDE;
1606*1da177e4SLinus Torvalds 	pmc->sfcount[MCAST_EXCLUDE] = 0;
1607*1da177e4SLinus Torvalds 	pmc->sfcount[MCAST_EXCLUDE] = 1;
1608*1da177e4SLinus Torvalds }
1609*1da177e4SLinus Torvalds 
1610*1da177e4SLinus Torvalds 
1611*1da177e4SLinus Torvalds /*
1612*1da177e4SLinus Torvalds  * Join a multicast group
1613*1da177e4SLinus Torvalds  */
1614*1da177e4SLinus Torvalds int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1615*1da177e4SLinus Torvalds {
1616*1da177e4SLinus Torvalds 	int err;
1617*1da177e4SLinus Torvalds 	u32 addr = imr->imr_multiaddr.s_addr;
1618*1da177e4SLinus Torvalds 	struct ip_mc_socklist *iml, *i;
1619*1da177e4SLinus Torvalds 	struct in_device *in_dev;
1620*1da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1621*1da177e4SLinus Torvalds 	int count = 0;
1622*1da177e4SLinus Torvalds 
1623*1da177e4SLinus Torvalds 	if (!MULTICAST(addr))
1624*1da177e4SLinus Torvalds 		return -EINVAL;
1625*1da177e4SLinus Torvalds 
1626*1da177e4SLinus Torvalds 	rtnl_shlock();
1627*1da177e4SLinus Torvalds 
1628*1da177e4SLinus Torvalds 	in_dev = ip_mc_find_dev(imr);
1629*1da177e4SLinus Torvalds 
1630*1da177e4SLinus Torvalds 	if (!in_dev) {
1631*1da177e4SLinus Torvalds 		iml = NULL;
1632*1da177e4SLinus Torvalds 		err = -ENODEV;
1633*1da177e4SLinus Torvalds 		goto done;
1634*1da177e4SLinus Torvalds 	}
1635*1da177e4SLinus Torvalds 
1636*1da177e4SLinus Torvalds 	iml = (struct ip_mc_socklist *)sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1637*1da177e4SLinus Torvalds 
1638*1da177e4SLinus Torvalds 	err = -EADDRINUSE;
1639*1da177e4SLinus Torvalds 	for (i = inet->mc_list; i; i = i->next) {
1640*1da177e4SLinus Torvalds 		if (memcmp(&i->multi, imr, sizeof(*imr)) == 0) {
1641*1da177e4SLinus Torvalds 			/* New style additions are reference counted */
1642*1da177e4SLinus Torvalds 			if (imr->imr_address.s_addr == 0) {
1643*1da177e4SLinus Torvalds 				i->count++;
1644*1da177e4SLinus Torvalds 				err = 0;
1645*1da177e4SLinus Torvalds 			}
1646*1da177e4SLinus Torvalds 			goto done;
1647*1da177e4SLinus Torvalds 		}
1648*1da177e4SLinus Torvalds 		count++;
1649*1da177e4SLinus Torvalds 	}
1650*1da177e4SLinus Torvalds 	err = -ENOBUFS;
1651*1da177e4SLinus Torvalds 	if (iml == NULL || count >= sysctl_igmp_max_memberships)
1652*1da177e4SLinus Torvalds 		goto done;
1653*1da177e4SLinus Torvalds 	memcpy(&iml->multi, imr, sizeof(*imr));
1654*1da177e4SLinus Torvalds 	iml->next = inet->mc_list;
1655*1da177e4SLinus Torvalds 	iml->count = 1;
1656*1da177e4SLinus Torvalds 	iml->sflist = NULL;
1657*1da177e4SLinus Torvalds 	iml->sfmode = MCAST_EXCLUDE;
1658*1da177e4SLinus Torvalds 	inet->mc_list = iml;
1659*1da177e4SLinus Torvalds 	ip_mc_inc_group(in_dev, addr);
1660*1da177e4SLinus Torvalds 	iml = NULL;
1661*1da177e4SLinus Torvalds 	err = 0;
1662*1da177e4SLinus Torvalds 
1663*1da177e4SLinus Torvalds done:
1664*1da177e4SLinus Torvalds 	rtnl_shunlock();
1665*1da177e4SLinus Torvalds 	if (iml)
1666*1da177e4SLinus Torvalds 		sock_kfree_s(sk, iml, sizeof(*iml));
1667*1da177e4SLinus Torvalds 	return err;
1668*1da177e4SLinus Torvalds }
1669*1da177e4SLinus Torvalds 
1670*1da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1671*1da177e4SLinus Torvalds 			   struct in_device *in_dev)
1672*1da177e4SLinus Torvalds {
1673*1da177e4SLinus Torvalds 	int err;
1674*1da177e4SLinus Torvalds 
1675*1da177e4SLinus Torvalds 	if (iml->sflist == 0) {
1676*1da177e4SLinus Torvalds 		/* any-source empty exclude case */
1677*1da177e4SLinus Torvalds 		return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1678*1da177e4SLinus Torvalds 			iml->sfmode, 0, NULL, 0);
1679*1da177e4SLinus Torvalds 	}
1680*1da177e4SLinus Torvalds 	err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1681*1da177e4SLinus Torvalds 			iml->sfmode, iml->sflist->sl_count,
1682*1da177e4SLinus Torvalds 			iml->sflist->sl_addr, 0);
1683*1da177e4SLinus Torvalds 	sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1684*1da177e4SLinus Torvalds 	iml->sflist = NULL;
1685*1da177e4SLinus Torvalds 	return err;
1686*1da177e4SLinus Torvalds }
1687*1da177e4SLinus Torvalds 
1688*1da177e4SLinus Torvalds /*
1689*1da177e4SLinus Torvalds  *	Ask a socket to leave a group.
1690*1da177e4SLinus Torvalds  */
1691*1da177e4SLinus Torvalds 
1692*1da177e4SLinus Torvalds int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1693*1da177e4SLinus Torvalds {
1694*1da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1695*1da177e4SLinus Torvalds 	struct ip_mc_socklist *iml, **imlp;
1696*1da177e4SLinus Torvalds 
1697*1da177e4SLinus Torvalds 	rtnl_lock();
1698*1da177e4SLinus Torvalds 	for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1699*1da177e4SLinus Torvalds 		if (iml->multi.imr_multiaddr.s_addr==imr->imr_multiaddr.s_addr &&
1700*1da177e4SLinus Torvalds 		    iml->multi.imr_address.s_addr==imr->imr_address.s_addr &&
1701*1da177e4SLinus Torvalds 		    (!imr->imr_ifindex || iml->multi.imr_ifindex==imr->imr_ifindex)) {
1702*1da177e4SLinus Torvalds 			struct in_device *in_dev;
1703*1da177e4SLinus Torvalds 
1704*1da177e4SLinus Torvalds 			in_dev = inetdev_by_index(iml->multi.imr_ifindex);
1705*1da177e4SLinus Torvalds 			if (in_dev)
1706*1da177e4SLinus Torvalds 				(void) ip_mc_leave_src(sk, iml, in_dev);
1707*1da177e4SLinus Torvalds 			if (--iml->count) {
1708*1da177e4SLinus Torvalds 				rtnl_unlock();
1709*1da177e4SLinus Torvalds 				if (in_dev)
1710*1da177e4SLinus Torvalds 					in_dev_put(in_dev);
1711*1da177e4SLinus Torvalds 				return 0;
1712*1da177e4SLinus Torvalds 			}
1713*1da177e4SLinus Torvalds 
1714*1da177e4SLinus Torvalds 			*imlp = iml->next;
1715*1da177e4SLinus Torvalds 
1716*1da177e4SLinus Torvalds 			if (in_dev) {
1717*1da177e4SLinus Torvalds 				ip_mc_dec_group(in_dev, imr->imr_multiaddr.s_addr);
1718*1da177e4SLinus Torvalds 				in_dev_put(in_dev);
1719*1da177e4SLinus Torvalds 			}
1720*1da177e4SLinus Torvalds 			rtnl_unlock();
1721*1da177e4SLinus Torvalds 			sock_kfree_s(sk, iml, sizeof(*iml));
1722*1da177e4SLinus Torvalds 			return 0;
1723*1da177e4SLinus Torvalds 		}
1724*1da177e4SLinus Torvalds 	}
1725*1da177e4SLinus Torvalds 	rtnl_unlock();
1726*1da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
1727*1da177e4SLinus Torvalds }
1728*1da177e4SLinus Torvalds 
1729*1da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct
1730*1da177e4SLinus Torvalds 	ip_mreq_source *mreqs, int ifindex)
1731*1da177e4SLinus Torvalds {
1732*1da177e4SLinus Torvalds 	int err;
1733*1da177e4SLinus Torvalds 	struct ip_mreqn imr;
1734*1da177e4SLinus Torvalds 	u32 addr = mreqs->imr_multiaddr;
1735*1da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
1736*1da177e4SLinus Torvalds 	struct in_device *in_dev = NULL;
1737*1da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1738*1da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
1739*1da177e4SLinus Torvalds 	int i, j, rv;
1740*1da177e4SLinus Torvalds 
1741*1da177e4SLinus Torvalds 	if (!MULTICAST(addr))
1742*1da177e4SLinus Torvalds 		return -EINVAL;
1743*1da177e4SLinus Torvalds 
1744*1da177e4SLinus Torvalds 	rtnl_shlock();
1745*1da177e4SLinus Torvalds 
1746*1da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1747*1da177e4SLinus Torvalds 	imr.imr_address.s_addr = mreqs->imr_interface;
1748*1da177e4SLinus Torvalds 	imr.imr_ifindex = ifindex;
1749*1da177e4SLinus Torvalds 	in_dev = ip_mc_find_dev(&imr);
1750*1da177e4SLinus Torvalds 
1751*1da177e4SLinus Torvalds 	if (!in_dev) {
1752*1da177e4SLinus Torvalds 		err = -ENODEV;
1753*1da177e4SLinus Torvalds 		goto done;
1754*1da177e4SLinus Torvalds 	}
1755*1da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
1756*1da177e4SLinus Torvalds 
1757*1da177e4SLinus Torvalds 	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1758*1da177e4SLinus Torvalds 		if (memcmp(&pmc->multi, mreqs, 2*sizeof(__u32)) == 0)
1759*1da177e4SLinus Torvalds 			break;
1760*1da177e4SLinus Torvalds 	}
1761*1da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
1762*1da177e4SLinus Torvalds 		goto done;
1763*1da177e4SLinus Torvalds 	/* if a source filter was set, must be the same mode as before */
1764*1da177e4SLinus Torvalds 	if (pmc->sflist) {
1765*1da177e4SLinus Torvalds 		if (pmc->sfmode != omode)
1766*1da177e4SLinus Torvalds 			goto done;
1767*1da177e4SLinus Torvalds 	} else if (pmc->sfmode != omode) {
1768*1da177e4SLinus Torvalds 		/* allow mode switches for empty-set filters */
1769*1da177e4SLinus Torvalds 		ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1770*1da177e4SLinus Torvalds 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1771*1da177e4SLinus Torvalds 			NULL, 0);
1772*1da177e4SLinus Torvalds 		pmc->sfmode = omode;
1773*1da177e4SLinus Torvalds 	}
1774*1da177e4SLinus Torvalds 
1775*1da177e4SLinus Torvalds 	psl = pmc->sflist;
1776*1da177e4SLinus Torvalds 	if (!add) {
1777*1da177e4SLinus Torvalds 		if (!psl)
1778*1da177e4SLinus Torvalds 			goto done;
1779*1da177e4SLinus Torvalds 		rv = !0;
1780*1da177e4SLinus Torvalds 		for (i=0; i<psl->sl_count; i++) {
1781*1da177e4SLinus Torvalds 			rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1782*1da177e4SLinus Torvalds 				sizeof(__u32));
1783*1da177e4SLinus Torvalds 			if (rv == 0)
1784*1da177e4SLinus Torvalds 				break;
1785*1da177e4SLinus Torvalds 		}
1786*1da177e4SLinus Torvalds 		if (rv)		/* source not found */
1787*1da177e4SLinus Torvalds 			goto done;
1788*1da177e4SLinus Torvalds 
1789*1da177e4SLinus Torvalds 		/* update the interface filter */
1790*1da177e4SLinus Torvalds 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1791*1da177e4SLinus Torvalds 			&mreqs->imr_sourceaddr, 1);
1792*1da177e4SLinus Torvalds 
1793*1da177e4SLinus Torvalds 		for (j=i+1; j<psl->sl_count; j++)
1794*1da177e4SLinus Torvalds 			psl->sl_addr[j-1] = psl->sl_addr[j];
1795*1da177e4SLinus Torvalds 		psl->sl_count--;
1796*1da177e4SLinus Torvalds 		err = 0;
1797*1da177e4SLinus Torvalds 		goto done;
1798*1da177e4SLinus Torvalds 	}
1799*1da177e4SLinus Torvalds 	/* else, add a new source to the filter */
1800*1da177e4SLinus Torvalds 
1801*1da177e4SLinus Torvalds 	if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1802*1da177e4SLinus Torvalds 		err = -ENOBUFS;
1803*1da177e4SLinus Torvalds 		goto done;
1804*1da177e4SLinus Torvalds 	}
1805*1da177e4SLinus Torvalds 	if (!psl || psl->sl_count == psl->sl_max) {
1806*1da177e4SLinus Torvalds 		struct ip_sf_socklist *newpsl;
1807*1da177e4SLinus Torvalds 		int count = IP_SFBLOCK;
1808*1da177e4SLinus Torvalds 
1809*1da177e4SLinus Torvalds 		if (psl)
1810*1da177e4SLinus Torvalds 			count += psl->sl_max;
1811*1da177e4SLinus Torvalds 		newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1812*1da177e4SLinus Torvalds 			IP_SFLSIZE(count), GFP_KERNEL);
1813*1da177e4SLinus Torvalds 		if (!newpsl) {
1814*1da177e4SLinus Torvalds 			err = -ENOBUFS;
1815*1da177e4SLinus Torvalds 			goto done;
1816*1da177e4SLinus Torvalds 		}
1817*1da177e4SLinus Torvalds 		newpsl->sl_max = count;
1818*1da177e4SLinus Torvalds 		newpsl->sl_count = count - IP_SFBLOCK;
1819*1da177e4SLinus Torvalds 		if (psl) {
1820*1da177e4SLinus Torvalds 			for (i=0; i<psl->sl_count; i++)
1821*1da177e4SLinus Torvalds 				newpsl->sl_addr[i] = psl->sl_addr[i];
1822*1da177e4SLinus Torvalds 			sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1823*1da177e4SLinus Torvalds 		}
1824*1da177e4SLinus Torvalds 		pmc->sflist = psl = newpsl;
1825*1da177e4SLinus Torvalds 	}
1826*1da177e4SLinus Torvalds 	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
1827*1da177e4SLinus Torvalds 	for (i=0; i<psl->sl_count; i++) {
1828*1da177e4SLinus Torvalds 		rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1829*1da177e4SLinus Torvalds 			sizeof(__u32));
1830*1da177e4SLinus Torvalds 		if (rv == 0)
1831*1da177e4SLinus Torvalds 			break;
1832*1da177e4SLinus Torvalds 	}
1833*1da177e4SLinus Torvalds 	if (rv == 0)		/* address already there is an error */
1834*1da177e4SLinus Torvalds 		goto done;
1835*1da177e4SLinus Torvalds 	for (j=psl->sl_count-1; j>=i; j--)
1836*1da177e4SLinus Torvalds 		psl->sl_addr[j+1] = psl->sl_addr[j];
1837*1da177e4SLinus Torvalds 	psl->sl_addr[i] = mreqs->imr_sourceaddr;
1838*1da177e4SLinus Torvalds 	psl->sl_count++;
1839*1da177e4SLinus Torvalds 	err = 0;
1840*1da177e4SLinus Torvalds 	/* update the interface list */
1841*1da177e4SLinus Torvalds 	ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1842*1da177e4SLinus Torvalds 		&mreqs->imr_sourceaddr, 1);
1843*1da177e4SLinus Torvalds done:
1844*1da177e4SLinus Torvalds 	rtnl_shunlock();
1845*1da177e4SLinus Torvalds 	return err;
1846*1da177e4SLinus Torvalds }
1847*1da177e4SLinus Torvalds 
1848*1da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1849*1da177e4SLinus Torvalds {
1850*1da177e4SLinus Torvalds 	int err;
1851*1da177e4SLinus Torvalds 	struct ip_mreqn	imr;
1852*1da177e4SLinus Torvalds 	u32 addr = msf->imsf_multiaddr;
1853*1da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
1854*1da177e4SLinus Torvalds 	struct in_device *in_dev;
1855*1da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1856*1da177e4SLinus Torvalds 	struct ip_sf_socklist *newpsl, *psl;
1857*1da177e4SLinus Torvalds 
1858*1da177e4SLinus Torvalds 	if (!MULTICAST(addr))
1859*1da177e4SLinus Torvalds 		return -EINVAL;
1860*1da177e4SLinus Torvalds 	if (msf->imsf_fmode != MCAST_INCLUDE &&
1861*1da177e4SLinus Torvalds 	    msf->imsf_fmode != MCAST_EXCLUDE)
1862*1da177e4SLinus Torvalds 		return -EINVAL;
1863*1da177e4SLinus Torvalds 
1864*1da177e4SLinus Torvalds 	rtnl_shlock();
1865*1da177e4SLinus Torvalds 
1866*1da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1867*1da177e4SLinus Torvalds 	imr.imr_address.s_addr = msf->imsf_interface;
1868*1da177e4SLinus Torvalds 	imr.imr_ifindex = ifindex;
1869*1da177e4SLinus Torvalds 	in_dev = ip_mc_find_dev(&imr);
1870*1da177e4SLinus Torvalds 
1871*1da177e4SLinus Torvalds 	if (!in_dev) {
1872*1da177e4SLinus Torvalds 		err = -ENODEV;
1873*1da177e4SLinus Torvalds 		goto done;
1874*1da177e4SLinus Torvalds 	}
1875*1da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
1876*1da177e4SLinus Torvalds 
1877*1da177e4SLinus Torvalds 	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1878*1da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1879*1da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
1880*1da177e4SLinus Torvalds 			break;
1881*1da177e4SLinus Torvalds 	}
1882*1da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
1883*1da177e4SLinus Torvalds 		goto done;
1884*1da177e4SLinus Torvalds 	if (msf->imsf_numsrc) {
1885*1da177e4SLinus Torvalds 		newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1886*1da177e4SLinus Torvalds 				IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1887*1da177e4SLinus Torvalds 		if (!newpsl) {
1888*1da177e4SLinus Torvalds 			err = -ENOBUFS;
1889*1da177e4SLinus Torvalds 			goto done;
1890*1da177e4SLinus Torvalds 		}
1891*1da177e4SLinus Torvalds 		newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1892*1da177e4SLinus Torvalds 		memcpy(newpsl->sl_addr, msf->imsf_slist,
1893*1da177e4SLinus Torvalds 			msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1894*1da177e4SLinus Torvalds 		err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1895*1da177e4SLinus Torvalds 			msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1896*1da177e4SLinus Torvalds 		if (err) {
1897*1da177e4SLinus Torvalds 			sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1898*1da177e4SLinus Torvalds 			goto done;
1899*1da177e4SLinus Torvalds 		}
1900*1da177e4SLinus Torvalds 	} else
1901*1da177e4SLinus Torvalds 		newpsl = NULL;
1902*1da177e4SLinus Torvalds 	psl = pmc->sflist;
1903*1da177e4SLinus Torvalds 	if (psl) {
1904*1da177e4SLinus Torvalds 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1905*1da177e4SLinus Torvalds 			psl->sl_count, psl->sl_addr, 0);
1906*1da177e4SLinus Torvalds 		sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1907*1da177e4SLinus Torvalds 	} else
1908*1da177e4SLinus Torvalds 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1909*1da177e4SLinus Torvalds 			0, NULL, 0);
1910*1da177e4SLinus Torvalds 	pmc->sflist = newpsl;
1911*1da177e4SLinus Torvalds 	pmc->sfmode = msf->imsf_fmode;
1912*1da177e4SLinus Torvalds done:
1913*1da177e4SLinus Torvalds 	rtnl_shunlock();
1914*1da177e4SLinus Torvalds 	return err;
1915*1da177e4SLinus Torvalds }
1916*1da177e4SLinus Torvalds 
1917*1da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1918*1da177e4SLinus Torvalds 	struct ip_msfilter __user *optval, int __user *optlen)
1919*1da177e4SLinus Torvalds {
1920*1da177e4SLinus Torvalds 	int err, len, count, copycount;
1921*1da177e4SLinus Torvalds 	struct ip_mreqn	imr;
1922*1da177e4SLinus Torvalds 	u32 addr = msf->imsf_multiaddr;
1923*1da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
1924*1da177e4SLinus Torvalds 	struct in_device *in_dev;
1925*1da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1926*1da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
1927*1da177e4SLinus Torvalds 
1928*1da177e4SLinus Torvalds 	if (!MULTICAST(addr))
1929*1da177e4SLinus Torvalds 		return -EINVAL;
1930*1da177e4SLinus Torvalds 
1931*1da177e4SLinus Torvalds 	rtnl_shlock();
1932*1da177e4SLinus Torvalds 
1933*1da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1934*1da177e4SLinus Torvalds 	imr.imr_address.s_addr = msf->imsf_interface;
1935*1da177e4SLinus Torvalds 	imr.imr_ifindex = 0;
1936*1da177e4SLinus Torvalds 	in_dev = ip_mc_find_dev(&imr);
1937*1da177e4SLinus Torvalds 
1938*1da177e4SLinus Torvalds 	if (!in_dev) {
1939*1da177e4SLinus Torvalds 		err = -ENODEV;
1940*1da177e4SLinus Torvalds 		goto done;
1941*1da177e4SLinus Torvalds 	}
1942*1da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
1943*1da177e4SLinus Torvalds 
1944*1da177e4SLinus Torvalds 	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1945*1da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1946*1da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
1947*1da177e4SLinus Torvalds 			break;
1948*1da177e4SLinus Torvalds 	}
1949*1da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
1950*1da177e4SLinus Torvalds 		goto done;
1951*1da177e4SLinus Torvalds 	msf->imsf_fmode = pmc->sfmode;
1952*1da177e4SLinus Torvalds 	psl = pmc->sflist;
1953*1da177e4SLinus Torvalds 	rtnl_shunlock();
1954*1da177e4SLinus Torvalds 	if (!psl) {
1955*1da177e4SLinus Torvalds 		len = 0;
1956*1da177e4SLinus Torvalds 		count = 0;
1957*1da177e4SLinus Torvalds 	} else {
1958*1da177e4SLinus Torvalds 		count = psl->sl_count;
1959*1da177e4SLinus Torvalds 	}
1960*1da177e4SLinus Torvalds 	copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1961*1da177e4SLinus Torvalds 	len = copycount * sizeof(psl->sl_addr[0]);
1962*1da177e4SLinus Torvalds 	msf->imsf_numsrc = count;
1963*1da177e4SLinus Torvalds 	if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1964*1da177e4SLinus Torvalds 	    copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
1965*1da177e4SLinus Torvalds 		return -EFAULT;
1966*1da177e4SLinus Torvalds 	}
1967*1da177e4SLinus Torvalds 	if (len &&
1968*1da177e4SLinus Torvalds 	    copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
1969*1da177e4SLinus Torvalds 		return -EFAULT;
1970*1da177e4SLinus Torvalds 	return 0;
1971*1da177e4SLinus Torvalds done:
1972*1da177e4SLinus Torvalds 	rtnl_shunlock();
1973*1da177e4SLinus Torvalds 	return err;
1974*1da177e4SLinus Torvalds }
1975*1da177e4SLinus Torvalds 
1976*1da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
1977*1da177e4SLinus Torvalds 	struct group_filter __user *optval, int __user *optlen)
1978*1da177e4SLinus Torvalds {
1979*1da177e4SLinus Torvalds 	int err, i, count, copycount;
1980*1da177e4SLinus Torvalds 	struct sockaddr_in *psin;
1981*1da177e4SLinus Torvalds 	u32 addr;
1982*1da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
1983*1da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1984*1da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
1985*1da177e4SLinus Torvalds 
1986*1da177e4SLinus Torvalds 	psin = (struct sockaddr_in *)&gsf->gf_group;
1987*1da177e4SLinus Torvalds 	if (psin->sin_family != AF_INET)
1988*1da177e4SLinus Torvalds 		return -EINVAL;
1989*1da177e4SLinus Torvalds 	addr = psin->sin_addr.s_addr;
1990*1da177e4SLinus Torvalds 	if (!MULTICAST(addr))
1991*1da177e4SLinus Torvalds 		return -EINVAL;
1992*1da177e4SLinus Torvalds 
1993*1da177e4SLinus Torvalds 	rtnl_shlock();
1994*1da177e4SLinus Torvalds 
1995*1da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
1996*1da177e4SLinus Torvalds 
1997*1da177e4SLinus Torvalds 	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1998*1da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == addr &&
1999*1da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == gsf->gf_interface)
2000*1da177e4SLinus Torvalds 			break;
2001*1da177e4SLinus Torvalds 	}
2002*1da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
2003*1da177e4SLinus Torvalds 		goto done;
2004*1da177e4SLinus Torvalds 	gsf->gf_fmode = pmc->sfmode;
2005*1da177e4SLinus Torvalds 	psl = pmc->sflist;
2006*1da177e4SLinus Torvalds 	rtnl_shunlock();
2007*1da177e4SLinus Torvalds 	count = psl ? psl->sl_count : 0;
2008*1da177e4SLinus Torvalds 	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2009*1da177e4SLinus Torvalds 	gsf->gf_numsrc = count;
2010*1da177e4SLinus Torvalds 	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2011*1da177e4SLinus Torvalds 	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2012*1da177e4SLinus Torvalds 		return -EFAULT;
2013*1da177e4SLinus Torvalds 	}
2014*1da177e4SLinus Torvalds 	for (i=0; i<copycount; i++) {
2015*1da177e4SLinus Torvalds 		struct sockaddr_in *psin;
2016*1da177e4SLinus Torvalds 		struct sockaddr_storage ss;
2017*1da177e4SLinus Torvalds 
2018*1da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&ss;
2019*1da177e4SLinus Torvalds 		memset(&ss, 0, sizeof(ss));
2020*1da177e4SLinus Torvalds 		psin->sin_family = AF_INET;
2021*1da177e4SLinus Torvalds 		psin->sin_addr.s_addr = psl->sl_addr[i];
2022*1da177e4SLinus Torvalds 		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2023*1da177e4SLinus Torvalds 			return -EFAULT;
2024*1da177e4SLinus Torvalds 	}
2025*1da177e4SLinus Torvalds 	return 0;
2026*1da177e4SLinus Torvalds done:
2027*1da177e4SLinus Torvalds 	rtnl_shunlock();
2028*1da177e4SLinus Torvalds 	return err;
2029*1da177e4SLinus Torvalds }
2030*1da177e4SLinus Torvalds 
2031*1da177e4SLinus Torvalds /*
2032*1da177e4SLinus Torvalds  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2033*1da177e4SLinus Torvalds  */
2034*1da177e4SLinus Torvalds int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2035*1da177e4SLinus Torvalds {
2036*1da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2037*1da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
2038*1da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
2039*1da177e4SLinus Torvalds 	int i;
2040*1da177e4SLinus Torvalds 
2041*1da177e4SLinus Torvalds 	if (!MULTICAST(loc_addr))
2042*1da177e4SLinus Torvalds 		return 1;
2043*1da177e4SLinus Torvalds 
2044*1da177e4SLinus Torvalds 	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2045*1da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2046*1da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == dif)
2047*1da177e4SLinus Torvalds 			break;
2048*1da177e4SLinus Torvalds 	}
2049*1da177e4SLinus Torvalds 	if (!pmc)
2050*1da177e4SLinus Torvalds 		return 1;
2051*1da177e4SLinus Torvalds 	psl = pmc->sflist;
2052*1da177e4SLinus Torvalds 	if (!psl)
2053*1da177e4SLinus Torvalds 		return pmc->sfmode == MCAST_EXCLUDE;
2054*1da177e4SLinus Torvalds 
2055*1da177e4SLinus Torvalds 	for (i=0; i<psl->sl_count; i++) {
2056*1da177e4SLinus Torvalds 		if (psl->sl_addr[i] == rmt_addr)
2057*1da177e4SLinus Torvalds 			break;
2058*1da177e4SLinus Torvalds 	}
2059*1da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2060*1da177e4SLinus Torvalds 		return 0;
2061*1da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2062*1da177e4SLinus Torvalds 		return 0;
2063*1da177e4SLinus Torvalds 	return 1;
2064*1da177e4SLinus Torvalds }
2065*1da177e4SLinus Torvalds 
2066*1da177e4SLinus Torvalds /*
2067*1da177e4SLinus Torvalds  *	A socket is closing.
2068*1da177e4SLinus Torvalds  */
2069*1da177e4SLinus Torvalds 
2070*1da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk)
2071*1da177e4SLinus Torvalds {
2072*1da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2073*1da177e4SLinus Torvalds 	struct ip_mc_socklist *iml;
2074*1da177e4SLinus Torvalds 
2075*1da177e4SLinus Torvalds 	if (inet->mc_list == NULL)
2076*1da177e4SLinus Torvalds 		return;
2077*1da177e4SLinus Torvalds 
2078*1da177e4SLinus Torvalds 	rtnl_lock();
2079*1da177e4SLinus Torvalds 	while ((iml = inet->mc_list) != NULL) {
2080*1da177e4SLinus Torvalds 		struct in_device *in_dev;
2081*1da177e4SLinus Torvalds 		inet->mc_list = iml->next;
2082*1da177e4SLinus Torvalds 
2083*1da177e4SLinus Torvalds 		if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2084*1da177e4SLinus Torvalds 			(void) ip_mc_leave_src(sk, iml, in_dev);
2085*1da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2086*1da177e4SLinus Torvalds 			in_dev_put(in_dev);
2087*1da177e4SLinus Torvalds 		}
2088*1da177e4SLinus Torvalds 		sock_kfree_s(sk, iml, sizeof(*iml));
2089*1da177e4SLinus Torvalds 
2090*1da177e4SLinus Torvalds 	}
2091*1da177e4SLinus Torvalds 	rtnl_unlock();
2092*1da177e4SLinus Torvalds }
2093*1da177e4SLinus Torvalds 
2094*1da177e4SLinus Torvalds int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2095*1da177e4SLinus Torvalds {
2096*1da177e4SLinus Torvalds 	struct ip_mc_list *im;
2097*1da177e4SLinus Torvalds 	struct ip_sf_list *psf;
2098*1da177e4SLinus Torvalds 	int rv = 0;
2099*1da177e4SLinus Torvalds 
2100*1da177e4SLinus Torvalds 	read_lock(&in_dev->mc_list_lock);
2101*1da177e4SLinus Torvalds 	for (im=in_dev->mc_list; im; im=im->next) {
2102*1da177e4SLinus Torvalds 		if (im->multiaddr == mc_addr)
2103*1da177e4SLinus Torvalds 			break;
2104*1da177e4SLinus Torvalds 	}
2105*1da177e4SLinus Torvalds 	if (im && proto == IPPROTO_IGMP) {
2106*1da177e4SLinus Torvalds 		rv = 1;
2107*1da177e4SLinus Torvalds 	} else if (im) {
2108*1da177e4SLinus Torvalds 		if (src_addr) {
2109*1da177e4SLinus Torvalds 			for (psf=im->sources; psf; psf=psf->sf_next) {
2110*1da177e4SLinus Torvalds 				if (psf->sf_inaddr == src_addr)
2111*1da177e4SLinus Torvalds 					break;
2112*1da177e4SLinus Torvalds 			}
2113*1da177e4SLinus Torvalds 			if (psf)
2114*1da177e4SLinus Torvalds 				rv = psf->sf_count[MCAST_INCLUDE] ||
2115*1da177e4SLinus Torvalds 					psf->sf_count[MCAST_EXCLUDE] !=
2116*1da177e4SLinus Torvalds 					im->sfcount[MCAST_EXCLUDE];
2117*1da177e4SLinus Torvalds 			else
2118*1da177e4SLinus Torvalds 				rv = im->sfcount[MCAST_EXCLUDE] != 0;
2119*1da177e4SLinus Torvalds 		} else
2120*1da177e4SLinus Torvalds 			rv = 1; /* unspecified source; tentatively allow */
2121*1da177e4SLinus Torvalds 	}
2122*1da177e4SLinus Torvalds 	read_unlock(&in_dev->mc_list_lock);
2123*1da177e4SLinus Torvalds 	return rv;
2124*1da177e4SLinus Torvalds }
2125*1da177e4SLinus Torvalds 
2126*1da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS)
2127*1da177e4SLinus Torvalds struct igmp_mc_iter_state {
2128*1da177e4SLinus Torvalds 	struct net_device *dev;
2129*1da177e4SLinus Torvalds 	struct in_device *in_dev;
2130*1da177e4SLinus Torvalds };
2131*1da177e4SLinus Torvalds 
2132*1da177e4SLinus Torvalds #define	igmp_mc_seq_private(seq)	((struct igmp_mc_iter_state *)(seq)->private)
2133*1da177e4SLinus Torvalds 
2134*1da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2135*1da177e4SLinus Torvalds {
2136*1da177e4SLinus Torvalds 	struct ip_mc_list *im = NULL;
2137*1da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2138*1da177e4SLinus Torvalds 
2139*1da177e4SLinus Torvalds 	for (state->dev = dev_base, state->in_dev = NULL;
2140*1da177e4SLinus Torvalds 	     state->dev;
2141*1da177e4SLinus Torvalds 	     state->dev = state->dev->next) {
2142*1da177e4SLinus Torvalds 		struct in_device *in_dev;
2143*1da177e4SLinus Torvalds 		in_dev = in_dev_get(state->dev);
2144*1da177e4SLinus Torvalds 		if (!in_dev)
2145*1da177e4SLinus Torvalds 			continue;
2146*1da177e4SLinus Torvalds 		read_lock(&in_dev->mc_list_lock);
2147*1da177e4SLinus Torvalds 		im = in_dev->mc_list;
2148*1da177e4SLinus Torvalds 		if (im) {
2149*1da177e4SLinus Torvalds 			state->in_dev = in_dev;
2150*1da177e4SLinus Torvalds 			break;
2151*1da177e4SLinus Torvalds 		}
2152*1da177e4SLinus Torvalds 		read_unlock(&in_dev->mc_list_lock);
2153*1da177e4SLinus Torvalds 		in_dev_put(in_dev);
2154*1da177e4SLinus Torvalds 	}
2155*1da177e4SLinus Torvalds 	return im;
2156*1da177e4SLinus Torvalds }
2157*1da177e4SLinus Torvalds 
2158*1da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2159*1da177e4SLinus Torvalds {
2160*1da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2161*1da177e4SLinus Torvalds 	im = im->next;
2162*1da177e4SLinus Torvalds 	while (!im) {
2163*1da177e4SLinus Torvalds 		if (likely(state->in_dev != NULL)) {
2164*1da177e4SLinus Torvalds 			read_unlock(&state->in_dev->mc_list_lock);
2165*1da177e4SLinus Torvalds 			in_dev_put(state->in_dev);
2166*1da177e4SLinus Torvalds 		}
2167*1da177e4SLinus Torvalds 		state->dev = state->dev->next;
2168*1da177e4SLinus Torvalds 		if (!state->dev) {
2169*1da177e4SLinus Torvalds 			state->in_dev = NULL;
2170*1da177e4SLinus Torvalds 			break;
2171*1da177e4SLinus Torvalds 		}
2172*1da177e4SLinus Torvalds 		state->in_dev = in_dev_get(state->dev);
2173*1da177e4SLinus Torvalds 		if (!state->in_dev)
2174*1da177e4SLinus Torvalds 			continue;
2175*1da177e4SLinus Torvalds 		read_lock(&state->in_dev->mc_list_lock);
2176*1da177e4SLinus Torvalds 		im = state->in_dev->mc_list;
2177*1da177e4SLinus Torvalds 	}
2178*1da177e4SLinus Torvalds 	return im;
2179*1da177e4SLinus Torvalds }
2180*1da177e4SLinus Torvalds 
2181*1da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2182*1da177e4SLinus Torvalds {
2183*1da177e4SLinus Torvalds 	struct ip_mc_list *im = igmp_mc_get_first(seq);
2184*1da177e4SLinus Torvalds 	if (im)
2185*1da177e4SLinus Torvalds 		while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2186*1da177e4SLinus Torvalds 			--pos;
2187*1da177e4SLinus Torvalds 	return pos ? NULL : im;
2188*1da177e4SLinus Torvalds }
2189*1da177e4SLinus Torvalds 
2190*1da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2191*1da177e4SLinus Torvalds {
2192*1da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
2193*1da177e4SLinus Torvalds 	return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2194*1da177e4SLinus Torvalds }
2195*1da177e4SLinus Torvalds 
2196*1da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2197*1da177e4SLinus Torvalds {
2198*1da177e4SLinus Torvalds 	struct ip_mc_list *im;
2199*1da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2200*1da177e4SLinus Torvalds 		im = igmp_mc_get_first(seq);
2201*1da177e4SLinus Torvalds 	else
2202*1da177e4SLinus Torvalds 		im = igmp_mc_get_next(seq, v);
2203*1da177e4SLinus Torvalds 	++*pos;
2204*1da177e4SLinus Torvalds 	return im;
2205*1da177e4SLinus Torvalds }
2206*1da177e4SLinus Torvalds 
2207*1da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2208*1da177e4SLinus Torvalds {
2209*1da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2210*1da177e4SLinus Torvalds 	if (likely(state->in_dev != NULL)) {
2211*1da177e4SLinus Torvalds 		read_unlock(&state->in_dev->mc_list_lock);
2212*1da177e4SLinus Torvalds 		in_dev_put(state->in_dev);
2213*1da177e4SLinus Torvalds 		state->in_dev = NULL;
2214*1da177e4SLinus Torvalds 	}
2215*1da177e4SLinus Torvalds 	state->dev = NULL;
2216*1da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
2217*1da177e4SLinus Torvalds }
2218*1da177e4SLinus Torvalds 
2219*1da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2220*1da177e4SLinus Torvalds {
2221*1da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2222*1da177e4SLinus Torvalds 		seq_puts(seq,
2223*1da177e4SLinus Torvalds 			 "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2224*1da177e4SLinus Torvalds 	else {
2225*1da177e4SLinus Torvalds 		struct ip_mc_list *im = (struct ip_mc_list *)v;
2226*1da177e4SLinus Torvalds 		struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2227*1da177e4SLinus Torvalds 		char   *querier;
2228*1da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
2229*1da177e4SLinus Torvalds 		querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2230*1da177e4SLinus Torvalds 			  IGMP_V2_SEEN(state->in_dev) ? "V2" :
2231*1da177e4SLinus Torvalds 			  "V3";
2232*1da177e4SLinus Torvalds #else
2233*1da177e4SLinus Torvalds 		querier = "NONE";
2234*1da177e4SLinus Torvalds #endif
2235*1da177e4SLinus Torvalds 
2236*1da177e4SLinus Torvalds 		if (state->in_dev->mc_list == im) {
2237*1da177e4SLinus Torvalds 			seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2238*1da177e4SLinus Torvalds 				   state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2239*1da177e4SLinus Torvalds 		}
2240*1da177e4SLinus Torvalds 
2241*1da177e4SLinus Torvalds 		seq_printf(seq,
2242*1da177e4SLinus Torvalds 			   "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2243*1da177e4SLinus Torvalds 			   im->multiaddr, im->users,
2244*1da177e4SLinus Torvalds 			   im->tm_running, im->tm_running ?
2245*1da177e4SLinus Torvalds 			   jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2246*1da177e4SLinus Torvalds 			   im->reporter);
2247*1da177e4SLinus Torvalds 	}
2248*1da177e4SLinus Torvalds 	return 0;
2249*1da177e4SLinus Torvalds }
2250*1da177e4SLinus Torvalds 
2251*1da177e4SLinus Torvalds static struct seq_operations igmp_mc_seq_ops = {
2252*1da177e4SLinus Torvalds 	.start	=	igmp_mc_seq_start,
2253*1da177e4SLinus Torvalds 	.next	=	igmp_mc_seq_next,
2254*1da177e4SLinus Torvalds 	.stop	=	igmp_mc_seq_stop,
2255*1da177e4SLinus Torvalds 	.show	=	igmp_mc_seq_show,
2256*1da177e4SLinus Torvalds };
2257*1da177e4SLinus Torvalds 
2258*1da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2259*1da177e4SLinus Torvalds {
2260*1da177e4SLinus Torvalds 	struct seq_file *seq;
2261*1da177e4SLinus Torvalds 	int rc = -ENOMEM;
2262*1da177e4SLinus Torvalds 	struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2263*1da177e4SLinus Torvalds 
2264*1da177e4SLinus Torvalds 	if (!s)
2265*1da177e4SLinus Torvalds 		goto out;
2266*1da177e4SLinus Torvalds 	rc = seq_open(file, &igmp_mc_seq_ops);
2267*1da177e4SLinus Torvalds 	if (rc)
2268*1da177e4SLinus Torvalds 		goto out_kfree;
2269*1da177e4SLinus Torvalds 
2270*1da177e4SLinus Torvalds 	seq = file->private_data;
2271*1da177e4SLinus Torvalds 	seq->private = s;
2272*1da177e4SLinus Torvalds 	memset(s, 0, sizeof(*s));
2273*1da177e4SLinus Torvalds out:
2274*1da177e4SLinus Torvalds 	return rc;
2275*1da177e4SLinus Torvalds out_kfree:
2276*1da177e4SLinus Torvalds 	kfree(s);
2277*1da177e4SLinus Torvalds 	goto out;
2278*1da177e4SLinus Torvalds }
2279*1da177e4SLinus Torvalds 
2280*1da177e4SLinus Torvalds static struct file_operations igmp_mc_seq_fops = {
2281*1da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
2282*1da177e4SLinus Torvalds 	.open		=	igmp_mc_seq_open,
2283*1da177e4SLinus Torvalds 	.read		=	seq_read,
2284*1da177e4SLinus Torvalds 	.llseek		=	seq_lseek,
2285*1da177e4SLinus Torvalds 	.release	=	seq_release_private,
2286*1da177e4SLinus Torvalds };
2287*1da177e4SLinus Torvalds 
2288*1da177e4SLinus Torvalds struct igmp_mcf_iter_state {
2289*1da177e4SLinus Torvalds 	struct net_device *dev;
2290*1da177e4SLinus Torvalds 	struct in_device *idev;
2291*1da177e4SLinus Torvalds 	struct ip_mc_list *im;
2292*1da177e4SLinus Torvalds };
2293*1da177e4SLinus Torvalds 
2294*1da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq)	((struct igmp_mcf_iter_state *)(seq)->private)
2295*1da177e4SLinus Torvalds 
2296*1da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2297*1da177e4SLinus Torvalds {
2298*1da177e4SLinus Torvalds 	struct ip_sf_list *psf = NULL;
2299*1da177e4SLinus Torvalds 	struct ip_mc_list *im = NULL;
2300*1da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2301*1da177e4SLinus Torvalds 
2302*1da177e4SLinus Torvalds 	for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2303*1da177e4SLinus Torvalds 	     state->dev;
2304*1da177e4SLinus Torvalds 	     state->dev = state->dev->next) {
2305*1da177e4SLinus Torvalds 		struct in_device *idev;
2306*1da177e4SLinus Torvalds 		idev = in_dev_get(state->dev);
2307*1da177e4SLinus Torvalds 		if (unlikely(idev == NULL))
2308*1da177e4SLinus Torvalds 			continue;
2309*1da177e4SLinus Torvalds 		read_lock(&idev->mc_list_lock);
2310*1da177e4SLinus Torvalds 		im = idev->mc_list;
2311*1da177e4SLinus Torvalds 		if (likely(im != NULL)) {
2312*1da177e4SLinus Torvalds 			spin_lock_bh(&im->lock);
2313*1da177e4SLinus Torvalds 			psf = im->sources;
2314*1da177e4SLinus Torvalds 			if (likely(psf != NULL)) {
2315*1da177e4SLinus Torvalds 				state->im = im;
2316*1da177e4SLinus Torvalds 				state->idev = idev;
2317*1da177e4SLinus Torvalds 				break;
2318*1da177e4SLinus Torvalds 			}
2319*1da177e4SLinus Torvalds 			spin_unlock_bh(&im->lock);
2320*1da177e4SLinus Torvalds 		}
2321*1da177e4SLinus Torvalds 		read_unlock(&idev->mc_list_lock);
2322*1da177e4SLinus Torvalds 		in_dev_put(idev);
2323*1da177e4SLinus Torvalds 	}
2324*1da177e4SLinus Torvalds 	return psf;
2325*1da177e4SLinus Torvalds }
2326*1da177e4SLinus Torvalds 
2327*1da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2328*1da177e4SLinus Torvalds {
2329*1da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2330*1da177e4SLinus Torvalds 
2331*1da177e4SLinus Torvalds 	psf = psf->sf_next;
2332*1da177e4SLinus Torvalds 	while (!psf) {
2333*1da177e4SLinus Torvalds 		spin_unlock_bh(&state->im->lock);
2334*1da177e4SLinus Torvalds 		state->im = state->im->next;
2335*1da177e4SLinus Torvalds 		while (!state->im) {
2336*1da177e4SLinus Torvalds 			if (likely(state->idev != NULL)) {
2337*1da177e4SLinus Torvalds 				read_unlock(&state->idev->mc_list_lock);
2338*1da177e4SLinus Torvalds 				in_dev_put(state->idev);
2339*1da177e4SLinus Torvalds 			}
2340*1da177e4SLinus Torvalds 			state->dev = state->dev->next;
2341*1da177e4SLinus Torvalds 			if (!state->dev) {
2342*1da177e4SLinus Torvalds 				state->idev = NULL;
2343*1da177e4SLinus Torvalds 				goto out;
2344*1da177e4SLinus Torvalds 			}
2345*1da177e4SLinus Torvalds 			state->idev = in_dev_get(state->dev);
2346*1da177e4SLinus Torvalds 			if (!state->idev)
2347*1da177e4SLinus Torvalds 				continue;
2348*1da177e4SLinus Torvalds 			read_lock(&state->idev->mc_list_lock);
2349*1da177e4SLinus Torvalds 			state->im = state->idev->mc_list;
2350*1da177e4SLinus Torvalds 		}
2351*1da177e4SLinus Torvalds 		if (!state->im)
2352*1da177e4SLinus Torvalds 			break;
2353*1da177e4SLinus Torvalds 		spin_lock_bh(&state->im->lock);
2354*1da177e4SLinus Torvalds 		psf = state->im->sources;
2355*1da177e4SLinus Torvalds 	}
2356*1da177e4SLinus Torvalds out:
2357*1da177e4SLinus Torvalds 	return psf;
2358*1da177e4SLinus Torvalds }
2359*1da177e4SLinus Torvalds 
2360*1da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2361*1da177e4SLinus Torvalds {
2362*1da177e4SLinus Torvalds 	struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2363*1da177e4SLinus Torvalds 	if (psf)
2364*1da177e4SLinus Torvalds 		while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2365*1da177e4SLinus Torvalds 			--pos;
2366*1da177e4SLinus Torvalds 	return pos ? NULL : psf;
2367*1da177e4SLinus Torvalds }
2368*1da177e4SLinus Torvalds 
2369*1da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2370*1da177e4SLinus Torvalds {
2371*1da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
2372*1da177e4SLinus Torvalds 	return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2373*1da177e4SLinus Torvalds }
2374*1da177e4SLinus Torvalds 
2375*1da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2376*1da177e4SLinus Torvalds {
2377*1da177e4SLinus Torvalds 	struct ip_sf_list *psf;
2378*1da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2379*1da177e4SLinus Torvalds 		psf = igmp_mcf_get_first(seq);
2380*1da177e4SLinus Torvalds 	else
2381*1da177e4SLinus Torvalds 		psf = igmp_mcf_get_next(seq, v);
2382*1da177e4SLinus Torvalds 	++*pos;
2383*1da177e4SLinus Torvalds 	return psf;
2384*1da177e4SLinus Torvalds }
2385*1da177e4SLinus Torvalds 
2386*1da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2387*1da177e4SLinus Torvalds {
2388*1da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2389*1da177e4SLinus Torvalds 	if (likely(state->im != NULL)) {
2390*1da177e4SLinus Torvalds 		spin_unlock_bh(&state->im->lock);
2391*1da177e4SLinus Torvalds 		state->im = NULL;
2392*1da177e4SLinus Torvalds 	}
2393*1da177e4SLinus Torvalds 	if (likely(state->idev != NULL)) {
2394*1da177e4SLinus Torvalds 		read_unlock(&state->idev->mc_list_lock);
2395*1da177e4SLinus Torvalds 		in_dev_put(state->idev);
2396*1da177e4SLinus Torvalds 		state->idev = NULL;
2397*1da177e4SLinus Torvalds 	}
2398*1da177e4SLinus Torvalds 	state->dev = NULL;
2399*1da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
2400*1da177e4SLinus Torvalds }
2401*1da177e4SLinus Torvalds 
2402*1da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2403*1da177e4SLinus Torvalds {
2404*1da177e4SLinus Torvalds 	struct ip_sf_list *psf = (struct ip_sf_list *)v;
2405*1da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2406*1da177e4SLinus Torvalds 
2407*1da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2408*1da177e4SLinus Torvalds 		seq_printf(seq,
2409*1da177e4SLinus Torvalds 			   "%3s %6s "
2410*1da177e4SLinus Torvalds 			   "%10s %10s %6s %6s\n", "Idx",
2411*1da177e4SLinus Torvalds 			   "Device", "MCA",
2412*1da177e4SLinus Torvalds 			   "SRC", "INC", "EXC");
2413*1da177e4SLinus Torvalds 	} else {
2414*1da177e4SLinus Torvalds 		seq_printf(seq,
2415*1da177e4SLinus Torvalds 			   "%3d %6.6s 0x%08x "
2416*1da177e4SLinus Torvalds 			   "0x%08x %6lu %6lu\n",
2417*1da177e4SLinus Torvalds 			   state->dev->ifindex, state->dev->name,
2418*1da177e4SLinus Torvalds 			   ntohl(state->im->multiaddr),
2419*1da177e4SLinus Torvalds 			   ntohl(psf->sf_inaddr),
2420*1da177e4SLinus Torvalds 			   psf->sf_count[MCAST_INCLUDE],
2421*1da177e4SLinus Torvalds 			   psf->sf_count[MCAST_EXCLUDE]);
2422*1da177e4SLinus Torvalds 	}
2423*1da177e4SLinus Torvalds 	return 0;
2424*1da177e4SLinus Torvalds }
2425*1da177e4SLinus Torvalds 
2426*1da177e4SLinus Torvalds static struct seq_operations igmp_mcf_seq_ops = {
2427*1da177e4SLinus Torvalds 	.start	=	igmp_mcf_seq_start,
2428*1da177e4SLinus Torvalds 	.next	=	igmp_mcf_seq_next,
2429*1da177e4SLinus Torvalds 	.stop	=	igmp_mcf_seq_stop,
2430*1da177e4SLinus Torvalds 	.show	=	igmp_mcf_seq_show,
2431*1da177e4SLinus Torvalds };
2432*1da177e4SLinus Torvalds 
2433*1da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2434*1da177e4SLinus Torvalds {
2435*1da177e4SLinus Torvalds 	struct seq_file *seq;
2436*1da177e4SLinus Torvalds 	int rc = -ENOMEM;
2437*1da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2438*1da177e4SLinus Torvalds 
2439*1da177e4SLinus Torvalds 	if (!s)
2440*1da177e4SLinus Torvalds 		goto out;
2441*1da177e4SLinus Torvalds 	rc = seq_open(file, &igmp_mcf_seq_ops);
2442*1da177e4SLinus Torvalds 	if (rc)
2443*1da177e4SLinus Torvalds 		goto out_kfree;
2444*1da177e4SLinus Torvalds 
2445*1da177e4SLinus Torvalds 	seq = file->private_data;
2446*1da177e4SLinus Torvalds 	seq->private = s;
2447*1da177e4SLinus Torvalds 	memset(s, 0, sizeof(*s));
2448*1da177e4SLinus Torvalds out:
2449*1da177e4SLinus Torvalds 	return rc;
2450*1da177e4SLinus Torvalds out_kfree:
2451*1da177e4SLinus Torvalds 	kfree(s);
2452*1da177e4SLinus Torvalds 	goto out;
2453*1da177e4SLinus Torvalds }
2454*1da177e4SLinus Torvalds 
2455*1da177e4SLinus Torvalds static struct file_operations igmp_mcf_seq_fops = {
2456*1da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
2457*1da177e4SLinus Torvalds 	.open		=	igmp_mcf_seq_open,
2458*1da177e4SLinus Torvalds 	.read		=	seq_read,
2459*1da177e4SLinus Torvalds 	.llseek		=	seq_lseek,
2460*1da177e4SLinus Torvalds 	.release	=	seq_release_private,
2461*1da177e4SLinus Torvalds };
2462*1da177e4SLinus Torvalds 
2463*1da177e4SLinus Torvalds int __init igmp_mc_proc_init(void)
2464*1da177e4SLinus Torvalds {
2465*1da177e4SLinus Torvalds 	proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2466*1da177e4SLinus Torvalds 	proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2467*1da177e4SLinus Torvalds 	return 0;
2468*1da177e4SLinus Torvalds }
2469*1da177e4SLinus Torvalds #endif
2470*1da177e4SLinus Torvalds 
2471*1da177e4SLinus Torvalds EXPORT_SYMBOL(ip_mc_dec_group);
2472*1da177e4SLinus Torvalds EXPORT_SYMBOL(ip_mc_inc_group);
2473*1da177e4SLinus Torvalds EXPORT_SYMBOL(ip_mc_join_group);
2474