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