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