xref: /openbmc/linux/net/ipv4/igmp.c (revision 8730046c)
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@lxorguk.ukuu.org.uk>
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 <linux/slab.h>
75 #include <linux/uaccess.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 #include <linux/pkt_sched.h>
92 
93 #include <net/net_namespace.h>
94 #include <net/arp.h>
95 #include <net/ip.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
98 #include <net/sock.h>
99 #include <net/checksum.h>
100 #include <net/inet_common.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
104 #endif
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
108 #endif
109 
110 #ifdef CONFIG_IP_MULTICAST
111 /* Parameter names and values are taken from igmp-v2-06 draft */
112 
113 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT		(400*HZ)
114 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT		(400*HZ)
115 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL	(10*HZ)
116 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL	(1*HZ)
117 #define IGMP_QUERY_RESPONSE_INTERVAL		(10*HZ)
118 #define IGMP_QUERY_ROBUSTNESS_VARIABLE		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 int unsolicited_report_interval(struct in_device *in_dev)
142 {
143 	int interval_ms, interval_jiffies;
144 
145 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
146 		interval_ms = IN_DEV_CONF_GET(
147 			in_dev,
148 			IGMPV2_UNSOLICITED_REPORT_INTERVAL);
149 	else /* v3 */
150 		interval_ms = IN_DEV_CONF_GET(
151 			in_dev,
152 			IGMPV3_UNSOLICITED_REPORT_INTERVAL);
153 
154 	interval_jiffies = msecs_to_jiffies(interval_ms);
155 
156 	/* _timer functions can't handle a delay of 0 jiffies so ensure
157 	 *  we always return a positive value.
158 	 */
159 	if (interval_jiffies <= 0)
160 		interval_jiffies = 1;
161 	return interval_jiffies;
162 }
163 
164 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
165 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
166 static void igmpv3_clear_delrec(struct in_device *in_dev);
167 static int sf_setstate(struct ip_mc_list *pmc);
168 static void sf_markstate(struct ip_mc_list *pmc);
169 #endif
170 static void ip_mc_clear_src(struct ip_mc_list *pmc);
171 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
172 			 int sfcount, __be32 *psfsrc, int delta);
173 
174 static void ip_ma_put(struct ip_mc_list *im)
175 {
176 	if (atomic_dec_and_test(&im->refcnt)) {
177 		in_dev_put(im->interface);
178 		kfree_rcu(im, rcu);
179 	}
180 }
181 
182 #define for_each_pmc_rcu(in_dev, pmc)				\
183 	for (pmc = rcu_dereference(in_dev->mc_list);		\
184 	     pmc != NULL;					\
185 	     pmc = rcu_dereference(pmc->next_rcu))
186 
187 #define for_each_pmc_rtnl(in_dev, pmc)				\
188 	for (pmc = rtnl_dereference(in_dev->mc_list);		\
189 	     pmc != NULL;					\
190 	     pmc = rtnl_dereference(pmc->next_rcu))
191 
192 #ifdef CONFIG_IP_MULTICAST
193 
194 /*
195  *	Timer management
196  */
197 
198 static void igmp_stop_timer(struct ip_mc_list *im)
199 {
200 	spin_lock_bh(&im->lock);
201 	if (del_timer(&im->timer))
202 		atomic_dec(&im->refcnt);
203 	im->tm_running = 0;
204 	im->reporter = 0;
205 	im->unsolicit_count = 0;
206 	spin_unlock_bh(&im->lock);
207 }
208 
209 /* It must be called with locked im->lock */
210 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
211 {
212 	int tv = prandom_u32() % max_delay;
213 
214 	im->tm_running = 1;
215 	if (!mod_timer(&im->timer, jiffies+tv+2))
216 		atomic_inc(&im->refcnt);
217 }
218 
219 static void igmp_gq_start_timer(struct in_device *in_dev)
220 {
221 	int tv = prandom_u32() % in_dev->mr_maxdelay;
222 	unsigned long exp = jiffies + tv + 2;
223 
224 	if (in_dev->mr_gq_running &&
225 	    time_after_eq(exp, (in_dev->mr_gq_timer).expires))
226 		return;
227 
228 	in_dev->mr_gq_running = 1;
229 	if (!mod_timer(&in_dev->mr_gq_timer, exp))
230 		in_dev_hold(in_dev);
231 }
232 
233 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
234 {
235 	int tv = prandom_u32() % delay;
236 
237 	if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
238 		in_dev_hold(in_dev);
239 }
240 
241 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
242 {
243 	spin_lock_bh(&im->lock);
244 	im->unsolicit_count = 0;
245 	if (del_timer(&im->timer)) {
246 		if ((long)(im->timer.expires-jiffies) < max_delay) {
247 			add_timer(&im->timer);
248 			im->tm_running = 1;
249 			spin_unlock_bh(&im->lock);
250 			return;
251 		}
252 		atomic_dec(&im->refcnt);
253 	}
254 	igmp_start_timer(im, max_delay);
255 	spin_unlock_bh(&im->lock);
256 }
257 
258 
259 /*
260  *	Send an IGMP report.
261  */
262 
263 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
264 
265 
266 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
267 	int gdeleted, int sdeleted)
268 {
269 	switch (type) {
270 	case IGMPV3_MODE_IS_INCLUDE:
271 	case IGMPV3_MODE_IS_EXCLUDE:
272 		if (gdeleted || sdeleted)
273 			return 0;
274 		if (!(pmc->gsquery && !psf->sf_gsresp)) {
275 			if (pmc->sfmode == MCAST_INCLUDE)
276 				return 1;
277 			/* don't include if this source is excluded
278 			 * in all filters
279 			 */
280 			if (psf->sf_count[MCAST_INCLUDE])
281 				return type == IGMPV3_MODE_IS_INCLUDE;
282 			return pmc->sfcount[MCAST_EXCLUDE] ==
283 				psf->sf_count[MCAST_EXCLUDE];
284 		}
285 		return 0;
286 	case IGMPV3_CHANGE_TO_INCLUDE:
287 		if (gdeleted || sdeleted)
288 			return 0;
289 		return psf->sf_count[MCAST_INCLUDE] != 0;
290 	case IGMPV3_CHANGE_TO_EXCLUDE:
291 		if (gdeleted || sdeleted)
292 			return 0;
293 		if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
294 		    psf->sf_count[MCAST_INCLUDE])
295 			return 0;
296 		return pmc->sfcount[MCAST_EXCLUDE] ==
297 			psf->sf_count[MCAST_EXCLUDE];
298 	case IGMPV3_ALLOW_NEW_SOURCES:
299 		if (gdeleted || !psf->sf_crcount)
300 			return 0;
301 		return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
302 	case IGMPV3_BLOCK_OLD_SOURCES:
303 		if (pmc->sfmode == MCAST_INCLUDE)
304 			return gdeleted || (psf->sf_crcount && sdeleted);
305 		return psf->sf_crcount && !gdeleted && !sdeleted;
306 	}
307 	return 0;
308 }
309 
310 static int
311 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
312 {
313 	struct ip_sf_list *psf;
314 	int scount = 0;
315 
316 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
317 		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
318 			continue;
319 		scount++;
320 	}
321 	return scount;
322 }
323 
324 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
325 {
326 	struct sk_buff *skb;
327 	struct rtable *rt;
328 	struct iphdr *pip;
329 	struct igmpv3_report *pig;
330 	struct net *net = dev_net(dev);
331 	struct flowi4 fl4;
332 	int hlen = LL_RESERVED_SPACE(dev);
333 	int tlen = dev->needed_tailroom;
334 	unsigned int size = mtu;
335 
336 	while (1) {
337 		skb = alloc_skb(size + hlen + tlen,
338 				GFP_ATOMIC | __GFP_NOWARN);
339 		if (skb)
340 			break;
341 		size >>= 1;
342 		if (size < 256)
343 			return NULL;
344 	}
345 	skb->priority = TC_PRIO_CONTROL;
346 
347 	rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
348 				   0, 0,
349 				   IPPROTO_IGMP, 0, dev->ifindex);
350 	if (IS_ERR(rt)) {
351 		kfree_skb(skb);
352 		return NULL;
353 	}
354 
355 	skb_dst_set(skb, &rt->dst);
356 	skb->dev = dev;
357 
358 	skb_reserve(skb, hlen);
359 	skb_tailroom_reserve(skb, mtu, tlen);
360 
361 	skb_reset_network_header(skb);
362 	pip = ip_hdr(skb);
363 	skb_put(skb, sizeof(struct iphdr) + 4);
364 
365 	pip->version  = 4;
366 	pip->ihl      = (sizeof(struct iphdr)+4)>>2;
367 	pip->tos      = 0xc0;
368 	pip->frag_off = htons(IP_DF);
369 	pip->ttl      = 1;
370 	pip->daddr    = fl4.daddr;
371 	pip->saddr    = fl4.saddr;
372 	pip->protocol = IPPROTO_IGMP;
373 	pip->tot_len  = 0;	/* filled in later */
374 	ip_select_ident(net, skb, NULL);
375 	((u8 *)&pip[1])[0] = IPOPT_RA;
376 	((u8 *)&pip[1])[1] = 4;
377 	((u8 *)&pip[1])[2] = 0;
378 	((u8 *)&pip[1])[3] = 0;
379 
380 	skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
381 	skb_put(skb, sizeof(*pig));
382 	pig = igmpv3_report_hdr(skb);
383 	pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
384 	pig->resv1 = 0;
385 	pig->csum = 0;
386 	pig->resv2 = 0;
387 	pig->ngrec = 0;
388 	return skb;
389 }
390 
391 static int igmpv3_sendpack(struct sk_buff *skb)
392 {
393 	struct igmphdr *pig = igmp_hdr(skb);
394 	const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
395 
396 	pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
397 
398 	return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
399 }
400 
401 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
402 {
403 	return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
404 }
405 
406 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
407 	int type, struct igmpv3_grec **ppgr)
408 {
409 	struct net_device *dev = pmc->interface->dev;
410 	struct igmpv3_report *pih;
411 	struct igmpv3_grec *pgr;
412 
413 	if (!skb)
414 		skb = igmpv3_newpack(dev, dev->mtu);
415 	if (!skb)
416 		return NULL;
417 	pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
418 	pgr->grec_type = type;
419 	pgr->grec_auxwords = 0;
420 	pgr->grec_nsrcs = 0;
421 	pgr->grec_mca = pmc->multiaddr;
422 	pih = igmpv3_report_hdr(skb);
423 	pih->ngrec = htons(ntohs(pih->ngrec)+1);
424 	*ppgr = pgr;
425 	return skb;
426 }
427 
428 #define AVAILABLE(skb)	((skb) ? skb_availroom(skb) : 0)
429 
430 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
431 	int type, int gdeleted, int sdeleted)
432 {
433 	struct net_device *dev = pmc->interface->dev;
434 	struct net *net = dev_net(dev);
435 	struct igmpv3_report *pih;
436 	struct igmpv3_grec *pgr = NULL;
437 	struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
438 	int scount, stotal, first, isquery, truncate;
439 
440 	if (pmc->multiaddr == IGMP_ALL_HOSTS)
441 		return skb;
442 	if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
443 		return skb;
444 
445 	isquery = type == IGMPV3_MODE_IS_INCLUDE ||
446 		  type == IGMPV3_MODE_IS_EXCLUDE;
447 	truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
448 		    type == IGMPV3_CHANGE_TO_EXCLUDE;
449 
450 	stotal = scount = 0;
451 
452 	psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
453 
454 	if (!*psf_list)
455 		goto empty_source;
456 
457 	pih = skb ? igmpv3_report_hdr(skb) : NULL;
458 
459 	/* EX and TO_EX get a fresh packet, if needed */
460 	if (truncate) {
461 		if (pih && pih->ngrec &&
462 		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
463 			if (skb)
464 				igmpv3_sendpack(skb);
465 			skb = igmpv3_newpack(dev, dev->mtu);
466 		}
467 	}
468 	first = 1;
469 	psf_prev = NULL;
470 	for (psf = *psf_list; psf; psf = psf_next) {
471 		__be32 *psrc;
472 
473 		psf_next = psf->sf_next;
474 
475 		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
476 			psf_prev = psf;
477 			continue;
478 		}
479 
480 		/* Based on RFC3376 5.1. Should not send source-list change
481 		 * records when there is a filter mode change.
482 		 */
483 		if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
484 		     (!gdeleted && pmc->crcount)) &&
485 		    (type == IGMPV3_ALLOW_NEW_SOURCES ||
486 		     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
487 			goto decrease_sf_crcount;
488 
489 		/* clear marks on query responses */
490 		if (isquery)
491 			psf->sf_gsresp = 0;
492 
493 		if (AVAILABLE(skb) < sizeof(__be32) +
494 		    first*sizeof(struct igmpv3_grec)) {
495 			if (truncate && !first)
496 				break;	 /* truncate these */
497 			if (pgr)
498 				pgr->grec_nsrcs = htons(scount);
499 			if (skb)
500 				igmpv3_sendpack(skb);
501 			skb = igmpv3_newpack(dev, dev->mtu);
502 			first = 1;
503 			scount = 0;
504 		}
505 		if (first) {
506 			skb = add_grhead(skb, pmc, type, &pgr);
507 			first = 0;
508 		}
509 		if (!skb)
510 			return NULL;
511 		psrc = (__be32 *)skb_put(skb, sizeof(__be32));
512 		*psrc = psf->sf_inaddr;
513 		scount++; stotal++;
514 		if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
515 		     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
516 decrease_sf_crcount:
517 			psf->sf_crcount--;
518 			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
519 				if (psf_prev)
520 					psf_prev->sf_next = psf->sf_next;
521 				else
522 					*psf_list = psf->sf_next;
523 				kfree(psf);
524 				continue;
525 			}
526 		}
527 		psf_prev = psf;
528 	}
529 
530 empty_source:
531 	if (!stotal) {
532 		if (type == IGMPV3_ALLOW_NEW_SOURCES ||
533 		    type == IGMPV3_BLOCK_OLD_SOURCES)
534 			return skb;
535 		if (pmc->crcount || isquery) {
536 			/* make sure we have room for group header */
537 			if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
538 				igmpv3_sendpack(skb);
539 				skb = NULL; /* add_grhead will get a new one */
540 			}
541 			skb = add_grhead(skb, pmc, type, &pgr);
542 		}
543 	}
544 	if (pgr)
545 		pgr->grec_nsrcs = htons(scount);
546 
547 	if (isquery)
548 		pmc->gsquery = 0;	/* clear query state on report */
549 	return skb;
550 }
551 
552 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
553 {
554 	struct sk_buff *skb = NULL;
555 	struct net *net = dev_net(in_dev->dev);
556 	int type;
557 
558 	if (!pmc) {
559 		rcu_read_lock();
560 		for_each_pmc_rcu(in_dev, pmc) {
561 			if (pmc->multiaddr == IGMP_ALL_HOSTS)
562 				continue;
563 			if (ipv4_is_local_multicast(pmc->multiaddr) &&
564 			     !net->ipv4.sysctl_igmp_llm_reports)
565 				continue;
566 			spin_lock_bh(&pmc->lock);
567 			if (pmc->sfcount[MCAST_EXCLUDE])
568 				type = IGMPV3_MODE_IS_EXCLUDE;
569 			else
570 				type = IGMPV3_MODE_IS_INCLUDE;
571 			skb = add_grec(skb, pmc, type, 0, 0);
572 			spin_unlock_bh(&pmc->lock);
573 		}
574 		rcu_read_unlock();
575 	} else {
576 		spin_lock_bh(&pmc->lock);
577 		if (pmc->sfcount[MCAST_EXCLUDE])
578 			type = IGMPV3_MODE_IS_EXCLUDE;
579 		else
580 			type = IGMPV3_MODE_IS_INCLUDE;
581 		skb = add_grec(skb, pmc, type, 0, 0);
582 		spin_unlock_bh(&pmc->lock);
583 	}
584 	if (!skb)
585 		return 0;
586 	return igmpv3_sendpack(skb);
587 }
588 
589 /*
590  * remove zero-count source records from a source filter list
591  */
592 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
593 {
594 	struct ip_sf_list *psf_prev, *psf_next, *psf;
595 
596 	psf_prev = NULL;
597 	for (psf = *ppsf; psf; psf = psf_next) {
598 		psf_next = psf->sf_next;
599 		if (psf->sf_crcount == 0) {
600 			if (psf_prev)
601 				psf_prev->sf_next = psf->sf_next;
602 			else
603 				*ppsf = psf->sf_next;
604 			kfree(psf);
605 		} else
606 			psf_prev = psf;
607 	}
608 }
609 
610 static void igmpv3_send_cr(struct in_device *in_dev)
611 {
612 	struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
613 	struct sk_buff *skb = NULL;
614 	int type, dtype;
615 
616 	rcu_read_lock();
617 	spin_lock_bh(&in_dev->mc_tomb_lock);
618 
619 	/* deleted MCA's */
620 	pmc_prev = NULL;
621 	for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
622 		pmc_next = pmc->next;
623 		if (pmc->sfmode == MCAST_INCLUDE) {
624 			type = IGMPV3_BLOCK_OLD_SOURCES;
625 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
626 			skb = add_grec(skb, pmc, type, 1, 0);
627 			skb = add_grec(skb, pmc, dtype, 1, 1);
628 		}
629 		if (pmc->crcount) {
630 			if (pmc->sfmode == MCAST_EXCLUDE) {
631 				type = IGMPV3_CHANGE_TO_INCLUDE;
632 				skb = add_grec(skb, pmc, type, 1, 0);
633 			}
634 			pmc->crcount--;
635 			if (pmc->crcount == 0) {
636 				igmpv3_clear_zeros(&pmc->tomb);
637 				igmpv3_clear_zeros(&pmc->sources);
638 			}
639 		}
640 		if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
641 			if (pmc_prev)
642 				pmc_prev->next = pmc_next;
643 			else
644 				in_dev->mc_tomb = pmc_next;
645 			in_dev_put(pmc->interface);
646 			kfree(pmc);
647 		} else
648 			pmc_prev = pmc;
649 	}
650 	spin_unlock_bh(&in_dev->mc_tomb_lock);
651 
652 	/* change recs */
653 	for_each_pmc_rcu(in_dev, pmc) {
654 		spin_lock_bh(&pmc->lock);
655 		if (pmc->sfcount[MCAST_EXCLUDE]) {
656 			type = IGMPV3_BLOCK_OLD_SOURCES;
657 			dtype = IGMPV3_ALLOW_NEW_SOURCES;
658 		} else {
659 			type = IGMPV3_ALLOW_NEW_SOURCES;
660 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
661 		}
662 		skb = add_grec(skb, pmc, type, 0, 0);
663 		skb = add_grec(skb, pmc, dtype, 0, 1);	/* deleted sources */
664 
665 		/* filter mode changes */
666 		if (pmc->crcount) {
667 			if (pmc->sfmode == MCAST_EXCLUDE)
668 				type = IGMPV3_CHANGE_TO_EXCLUDE;
669 			else
670 				type = IGMPV3_CHANGE_TO_INCLUDE;
671 			skb = add_grec(skb, pmc, type, 0, 0);
672 			pmc->crcount--;
673 		}
674 		spin_unlock_bh(&pmc->lock);
675 	}
676 	rcu_read_unlock();
677 
678 	if (!skb)
679 		return;
680 	(void) igmpv3_sendpack(skb);
681 }
682 
683 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
684 	int type)
685 {
686 	struct sk_buff *skb;
687 	struct iphdr *iph;
688 	struct igmphdr *ih;
689 	struct rtable *rt;
690 	struct net_device *dev = in_dev->dev;
691 	struct net *net = dev_net(dev);
692 	__be32	group = pmc ? pmc->multiaddr : 0;
693 	struct flowi4 fl4;
694 	__be32	dst;
695 	int hlen, tlen;
696 
697 	if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
698 		return igmpv3_send_report(in_dev, pmc);
699 
700 	if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
701 		return 0;
702 
703 	if (type == IGMP_HOST_LEAVE_MESSAGE)
704 		dst = IGMP_ALL_ROUTER;
705 	else
706 		dst = group;
707 
708 	rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
709 				   0, 0,
710 				   IPPROTO_IGMP, 0, dev->ifindex);
711 	if (IS_ERR(rt))
712 		return -1;
713 
714 	hlen = LL_RESERVED_SPACE(dev);
715 	tlen = dev->needed_tailroom;
716 	skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
717 	if (!skb) {
718 		ip_rt_put(rt);
719 		return -1;
720 	}
721 	skb->priority = TC_PRIO_CONTROL;
722 
723 	skb_dst_set(skb, &rt->dst);
724 
725 	skb_reserve(skb, hlen);
726 
727 	skb_reset_network_header(skb);
728 	iph = ip_hdr(skb);
729 	skb_put(skb, sizeof(struct iphdr) + 4);
730 
731 	iph->version  = 4;
732 	iph->ihl      = (sizeof(struct iphdr)+4)>>2;
733 	iph->tos      = 0xc0;
734 	iph->frag_off = htons(IP_DF);
735 	iph->ttl      = 1;
736 	iph->daddr    = dst;
737 	iph->saddr    = fl4.saddr;
738 	iph->protocol = IPPROTO_IGMP;
739 	ip_select_ident(net, skb, NULL);
740 	((u8 *)&iph[1])[0] = IPOPT_RA;
741 	((u8 *)&iph[1])[1] = 4;
742 	((u8 *)&iph[1])[2] = 0;
743 	((u8 *)&iph[1])[3] = 0;
744 
745 	ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
746 	ih->type = type;
747 	ih->code = 0;
748 	ih->csum = 0;
749 	ih->group = group;
750 	ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
751 
752 	return ip_local_out(net, skb->sk, skb);
753 }
754 
755 static void igmp_gq_timer_expire(unsigned long data)
756 {
757 	struct in_device *in_dev = (struct in_device *)data;
758 
759 	in_dev->mr_gq_running = 0;
760 	igmpv3_send_report(in_dev, NULL);
761 	in_dev_put(in_dev);
762 }
763 
764 static void igmp_ifc_timer_expire(unsigned long data)
765 {
766 	struct in_device *in_dev = (struct in_device *)data;
767 
768 	igmpv3_send_cr(in_dev);
769 	if (in_dev->mr_ifc_count) {
770 		in_dev->mr_ifc_count--;
771 		igmp_ifc_start_timer(in_dev,
772 				     unsolicited_report_interval(in_dev));
773 	}
774 	in_dev_put(in_dev);
775 }
776 
777 static void igmp_ifc_event(struct in_device *in_dev)
778 {
779 	struct net *net = dev_net(in_dev->dev);
780 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
781 		return;
782 	in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
783 	igmp_ifc_start_timer(in_dev, 1);
784 }
785 
786 
787 static void igmp_timer_expire(unsigned long data)
788 {
789 	struct ip_mc_list *im = (struct ip_mc_list *)data;
790 	struct in_device *in_dev = im->interface;
791 
792 	spin_lock(&im->lock);
793 	im->tm_running = 0;
794 
795 	if (im->unsolicit_count) {
796 		im->unsolicit_count--;
797 		igmp_start_timer(im, unsolicited_report_interval(in_dev));
798 	}
799 	im->reporter = 1;
800 	spin_unlock(&im->lock);
801 
802 	if (IGMP_V1_SEEN(in_dev))
803 		igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
804 	else if (IGMP_V2_SEEN(in_dev))
805 		igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
806 	else
807 		igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
808 
809 	ip_ma_put(im);
810 }
811 
812 /* mark EXCLUDE-mode sources */
813 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
814 {
815 	struct ip_sf_list *psf;
816 	int i, scount;
817 
818 	scount = 0;
819 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
820 		if (scount == nsrcs)
821 			break;
822 		for (i = 0; i < nsrcs; i++) {
823 			/* skip inactive filters */
824 			if (psf->sf_count[MCAST_INCLUDE] ||
825 			    pmc->sfcount[MCAST_EXCLUDE] !=
826 			    psf->sf_count[MCAST_EXCLUDE])
827 				break;
828 			if (srcs[i] == psf->sf_inaddr) {
829 				scount++;
830 				break;
831 			}
832 		}
833 	}
834 	pmc->gsquery = 0;
835 	if (scount == nsrcs)	/* all sources excluded */
836 		return 0;
837 	return 1;
838 }
839 
840 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
841 {
842 	struct ip_sf_list *psf;
843 	int i, scount;
844 
845 	if (pmc->sfmode == MCAST_EXCLUDE)
846 		return igmp_xmarksources(pmc, nsrcs, srcs);
847 
848 	/* mark INCLUDE-mode sources */
849 	scount = 0;
850 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
851 		if (scount == nsrcs)
852 			break;
853 		for (i = 0; i < nsrcs; i++)
854 			if (srcs[i] == psf->sf_inaddr) {
855 				psf->sf_gsresp = 1;
856 				scount++;
857 				break;
858 			}
859 	}
860 	if (!scount) {
861 		pmc->gsquery = 0;
862 		return 0;
863 	}
864 	pmc->gsquery = 1;
865 	return 1;
866 }
867 
868 /* return true if packet was dropped */
869 static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
870 {
871 	struct ip_mc_list *im;
872 	struct net *net = dev_net(in_dev->dev);
873 
874 	/* Timers are only set for non-local groups */
875 
876 	if (group == IGMP_ALL_HOSTS)
877 		return false;
878 	if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
879 		return false;
880 
881 	rcu_read_lock();
882 	for_each_pmc_rcu(in_dev, im) {
883 		if (im->multiaddr == group) {
884 			igmp_stop_timer(im);
885 			break;
886 		}
887 	}
888 	rcu_read_unlock();
889 	return false;
890 }
891 
892 /* return true if packet was dropped */
893 static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
894 	int len)
895 {
896 	struct igmphdr 		*ih = igmp_hdr(skb);
897 	struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
898 	struct ip_mc_list	*im;
899 	__be32			group = ih->group;
900 	int			max_delay;
901 	int			mark = 0;
902 	struct net		*net = dev_net(in_dev->dev);
903 
904 
905 	if (len == 8) {
906 		if (ih->code == 0) {
907 			/* Alas, old v1 router presents here. */
908 
909 			max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
910 			in_dev->mr_v1_seen = jiffies +
911 				IGMP_V1_ROUTER_PRESENT_TIMEOUT;
912 			group = 0;
913 		} else {
914 			/* v2 router present */
915 			max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
916 			in_dev->mr_v2_seen = jiffies +
917 				IGMP_V2_ROUTER_PRESENT_TIMEOUT;
918 		}
919 		/* cancel the interface change timer */
920 		in_dev->mr_ifc_count = 0;
921 		if (del_timer(&in_dev->mr_ifc_timer))
922 			__in_dev_put(in_dev);
923 		/* clear deleted report items */
924 		igmpv3_clear_delrec(in_dev);
925 	} else if (len < 12) {
926 		return true;	/* ignore bogus packet; freed by caller */
927 	} else if (IGMP_V1_SEEN(in_dev)) {
928 		/* This is a v3 query with v1 queriers present */
929 		max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
930 		group = 0;
931 	} else if (IGMP_V2_SEEN(in_dev)) {
932 		/* this is a v3 query with v2 queriers present;
933 		 * Interpretation of the max_delay code is problematic here.
934 		 * A real v2 host would use ih_code directly, while v3 has a
935 		 * different encoding. We use the v3 encoding as more likely
936 		 * to be intended in a v3 query.
937 		 */
938 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
939 		if (!max_delay)
940 			max_delay = 1;	/* can't mod w/ 0 */
941 	} else { /* v3 */
942 		if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
943 			return true;
944 
945 		ih3 = igmpv3_query_hdr(skb);
946 		if (ih3->nsrcs) {
947 			if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
948 					   + ntohs(ih3->nsrcs)*sizeof(__be32)))
949 				return true;
950 			ih3 = igmpv3_query_hdr(skb);
951 		}
952 
953 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
954 		if (!max_delay)
955 			max_delay = 1;	/* can't mod w/ 0 */
956 		in_dev->mr_maxdelay = max_delay;
957 		if (ih3->qrv)
958 			in_dev->mr_qrv = ih3->qrv;
959 		if (!group) { /* general query */
960 			if (ih3->nsrcs)
961 				return true;	/* no sources allowed */
962 			igmp_gq_start_timer(in_dev);
963 			return false;
964 		}
965 		/* mark sources to include, if group & source-specific */
966 		mark = ih3->nsrcs != 0;
967 	}
968 
969 	/*
970 	 * - Start the timers in all of our membership records
971 	 *   that the query applies to for the interface on
972 	 *   which the query arrived excl. those that belong
973 	 *   to a "local" group (224.0.0.X)
974 	 * - For timers already running check if they need to
975 	 *   be reset.
976 	 * - Use the igmp->igmp_code field as the maximum
977 	 *   delay possible
978 	 */
979 	rcu_read_lock();
980 	for_each_pmc_rcu(in_dev, im) {
981 		int changed;
982 
983 		if (group && group != im->multiaddr)
984 			continue;
985 		if (im->multiaddr == IGMP_ALL_HOSTS)
986 			continue;
987 		if (ipv4_is_local_multicast(im->multiaddr) &&
988 		    !net->ipv4.sysctl_igmp_llm_reports)
989 			continue;
990 		spin_lock_bh(&im->lock);
991 		if (im->tm_running)
992 			im->gsquery = im->gsquery && mark;
993 		else
994 			im->gsquery = mark;
995 		changed = !im->gsquery ||
996 			igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
997 		spin_unlock_bh(&im->lock);
998 		if (changed)
999 			igmp_mod_timer(im, max_delay);
1000 	}
1001 	rcu_read_unlock();
1002 	return false;
1003 }
1004 
1005 /* called in rcu_read_lock() section */
1006 int igmp_rcv(struct sk_buff *skb)
1007 {
1008 	/* This basically follows the spec line by line -- see RFC1112 */
1009 	struct igmphdr *ih;
1010 	struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
1011 	int len = skb->len;
1012 	bool dropped = true;
1013 
1014 	if (!in_dev)
1015 		goto drop;
1016 
1017 	if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1018 		goto drop;
1019 
1020 	if (skb_checksum_simple_validate(skb))
1021 		goto drop;
1022 
1023 	ih = igmp_hdr(skb);
1024 	switch (ih->type) {
1025 	case IGMP_HOST_MEMBERSHIP_QUERY:
1026 		dropped = igmp_heard_query(in_dev, skb, len);
1027 		break;
1028 	case IGMP_HOST_MEMBERSHIP_REPORT:
1029 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
1030 		/* Is it our report looped back? */
1031 		if (rt_is_output_route(skb_rtable(skb)))
1032 			break;
1033 		/* don't rely on MC router hearing unicast reports */
1034 		if (skb->pkt_type == PACKET_MULTICAST ||
1035 		    skb->pkt_type == PACKET_BROADCAST)
1036 			dropped = igmp_heard_report(in_dev, ih->group);
1037 		break;
1038 	case IGMP_PIM:
1039 #ifdef CONFIG_IP_PIMSM_V1
1040 		return pim_rcv_v1(skb);
1041 #endif
1042 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1043 	case IGMP_DVMRP:
1044 	case IGMP_TRACE:
1045 	case IGMP_HOST_LEAVE_MESSAGE:
1046 	case IGMP_MTRACE:
1047 	case IGMP_MTRACE_RESP:
1048 		break;
1049 	default:
1050 		break;
1051 	}
1052 
1053 drop:
1054 	if (dropped)
1055 		kfree_skb(skb);
1056 	else
1057 		consume_skb(skb);
1058 	return 0;
1059 }
1060 
1061 #endif
1062 
1063 
1064 /*
1065  *	Add a filter to a device
1066  */
1067 
1068 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1069 {
1070 	char buf[MAX_ADDR_LEN];
1071 	struct net_device *dev = in_dev->dev;
1072 
1073 	/* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1074 	   We will get multicast token leakage, when IFF_MULTICAST
1075 	   is changed. This check should be done in ndo_set_rx_mode
1076 	   routine. Something sort of:
1077 	   if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1078 	   --ANK
1079 	   */
1080 	if (arp_mc_map(addr, buf, dev, 0) == 0)
1081 		dev_mc_add(dev, buf);
1082 }
1083 
1084 /*
1085  *	Remove a filter from a device
1086  */
1087 
1088 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1089 {
1090 	char buf[MAX_ADDR_LEN];
1091 	struct net_device *dev = in_dev->dev;
1092 
1093 	if (arp_mc_map(addr, buf, dev, 0) == 0)
1094 		dev_mc_del(dev, buf);
1095 }
1096 
1097 #ifdef CONFIG_IP_MULTICAST
1098 /*
1099  * deleted ip_mc_list manipulation
1100  */
1101 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1102 {
1103 	struct ip_mc_list *pmc;
1104 	struct net *net = dev_net(in_dev->dev);
1105 
1106 	/* this is an "ip_mc_list" for convenience; only the fields below
1107 	 * are actually used. In particular, the refcnt and users are not
1108 	 * used for management of the delete list. Using the same structure
1109 	 * for deleted items allows change reports to use common code with
1110 	 * non-deleted or query-response MCA's.
1111 	 */
1112 	pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1113 	if (!pmc)
1114 		return;
1115 	spin_lock_bh(&im->lock);
1116 	pmc->interface = im->interface;
1117 	in_dev_hold(in_dev);
1118 	pmc->multiaddr = im->multiaddr;
1119 	pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1120 	pmc->sfmode = im->sfmode;
1121 	if (pmc->sfmode == MCAST_INCLUDE) {
1122 		struct ip_sf_list *psf;
1123 
1124 		pmc->tomb = im->tomb;
1125 		pmc->sources = im->sources;
1126 		im->tomb = im->sources = NULL;
1127 		for (psf = pmc->sources; psf; psf = psf->sf_next)
1128 			psf->sf_crcount = pmc->crcount;
1129 	}
1130 	spin_unlock_bh(&im->lock);
1131 
1132 	spin_lock_bh(&in_dev->mc_tomb_lock);
1133 	pmc->next = in_dev->mc_tomb;
1134 	in_dev->mc_tomb = pmc;
1135 	spin_unlock_bh(&in_dev->mc_tomb_lock);
1136 }
1137 
1138 /*
1139  * restore ip_mc_list deleted records
1140  */
1141 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1142 {
1143 	struct ip_mc_list *pmc, *pmc_prev;
1144 	struct ip_sf_list *psf;
1145 	struct net *net = dev_net(in_dev->dev);
1146 	__be32 multiaddr = im->multiaddr;
1147 
1148 	spin_lock_bh(&in_dev->mc_tomb_lock);
1149 	pmc_prev = NULL;
1150 	for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1151 		if (pmc->multiaddr == multiaddr)
1152 			break;
1153 		pmc_prev = pmc;
1154 	}
1155 	if (pmc) {
1156 		if (pmc_prev)
1157 			pmc_prev->next = pmc->next;
1158 		else
1159 			in_dev->mc_tomb = pmc->next;
1160 	}
1161 	spin_unlock_bh(&in_dev->mc_tomb_lock);
1162 
1163 	spin_lock_bh(&im->lock);
1164 	if (pmc) {
1165 		im->interface = pmc->interface;
1166 		im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1167 		im->sfmode = pmc->sfmode;
1168 		if (pmc->sfmode == MCAST_INCLUDE) {
1169 			im->tomb = pmc->tomb;
1170 			im->sources = pmc->sources;
1171 			for (psf = im->sources; psf; psf = psf->sf_next)
1172 				psf->sf_crcount = im->crcount;
1173 		}
1174 		in_dev_put(pmc->interface);
1175 	}
1176 	spin_unlock_bh(&im->lock);
1177 }
1178 
1179 /*
1180  * flush ip_mc_list deleted records
1181  */
1182 static void igmpv3_clear_delrec(struct in_device *in_dev)
1183 {
1184 	struct ip_mc_list *pmc, *nextpmc;
1185 
1186 	spin_lock_bh(&in_dev->mc_tomb_lock);
1187 	pmc = in_dev->mc_tomb;
1188 	in_dev->mc_tomb = NULL;
1189 	spin_unlock_bh(&in_dev->mc_tomb_lock);
1190 
1191 	for (; pmc; pmc = nextpmc) {
1192 		nextpmc = pmc->next;
1193 		ip_mc_clear_src(pmc);
1194 		in_dev_put(pmc->interface);
1195 		kfree(pmc);
1196 	}
1197 	/* clear dead sources, too */
1198 	rcu_read_lock();
1199 	for_each_pmc_rcu(in_dev, pmc) {
1200 		struct ip_sf_list *psf, *psf_next;
1201 
1202 		spin_lock_bh(&pmc->lock);
1203 		psf = pmc->tomb;
1204 		pmc->tomb = NULL;
1205 		spin_unlock_bh(&pmc->lock);
1206 		for (; psf; psf = psf_next) {
1207 			psf_next = psf->sf_next;
1208 			kfree(psf);
1209 		}
1210 	}
1211 	rcu_read_unlock();
1212 }
1213 #endif
1214 
1215 static void igmp_group_dropped(struct ip_mc_list *im)
1216 {
1217 	struct in_device *in_dev = im->interface;
1218 #ifdef CONFIG_IP_MULTICAST
1219 	struct net *net = dev_net(in_dev->dev);
1220 	int reporter;
1221 #endif
1222 
1223 	if (im->loaded) {
1224 		im->loaded = 0;
1225 		ip_mc_filter_del(in_dev, im->multiaddr);
1226 	}
1227 
1228 #ifdef CONFIG_IP_MULTICAST
1229 	if (im->multiaddr == IGMP_ALL_HOSTS)
1230 		return;
1231 	if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1232 		return;
1233 
1234 	reporter = im->reporter;
1235 	igmp_stop_timer(im);
1236 
1237 	if (!in_dev->dead) {
1238 		if (IGMP_V1_SEEN(in_dev))
1239 			return;
1240 		if (IGMP_V2_SEEN(in_dev)) {
1241 			if (reporter)
1242 				igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1243 			return;
1244 		}
1245 		/* IGMPv3 */
1246 		igmpv3_add_delrec(in_dev, im);
1247 
1248 		igmp_ifc_event(in_dev);
1249 	}
1250 #endif
1251 }
1252 
1253 static void igmp_group_added(struct ip_mc_list *im)
1254 {
1255 	struct in_device *in_dev = im->interface;
1256 #ifdef CONFIG_IP_MULTICAST
1257 	struct net *net = dev_net(in_dev->dev);
1258 #endif
1259 
1260 	if (im->loaded == 0) {
1261 		im->loaded = 1;
1262 		ip_mc_filter_add(in_dev, im->multiaddr);
1263 	}
1264 
1265 #ifdef CONFIG_IP_MULTICAST
1266 	if (im->multiaddr == IGMP_ALL_HOSTS)
1267 		return;
1268 	if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1269 		return;
1270 
1271 	if (in_dev->dead)
1272 		return;
1273 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1274 		spin_lock_bh(&im->lock);
1275 		igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1276 		spin_unlock_bh(&im->lock);
1277 		return;
1278 	}
1279 	/* else, v3 */
1280 
1281 	im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1282 	igmp_ifc_event(in_dev);
1283 #endif
1284 }
1285 
1286 
1287 /*
1288  *	Multicast list managers
1289  */
1290 
1291 static u32 ip_mc_hash(const struct ip_mc_list *im)
1292 {
1293 	return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1294 }
1295 
1296 static void ip_mc_hash_add(struct in_device *in_dev,
1297 			   struct ip_mc_list *im)
1298 {
1299 	struct ip_mc_list __rcu **mc_hash;
1300 	u32 hash;
1301 
1302 	mc_hash = rtnl_dereference(in_dev->mc_hash);
1303 	if (mc_hash) {
1304 		hash = ip_mc_hash(im);
1305 		im->next_hash = mc_hash[hash];
1306 		rcu_assign_pointer(mc_hash[hash], im);
1307 		return;
1308 	}
1309 
1310 	/* do not use a hash table for small number of items */
1311 	if (in_dev->mc_count < 4)
1312 		return;
1313 
1314 	mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1315 			  GFP_KERNEL);
1316 	if (!mc_hash)
1317 		return;
1318 
1319 	for_each_pmc_rtnl(in_dev, im) {
1320 		hash = ip_mc_hash(im);
1321 		im->next_hash = mc_hash[hash];
1322 		RCU_INIT_POINTER(mc_hash[hash], im);
1323 	}
1324 
1325 	rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1326 }
1327 
1328 static void ip_mc_hash_remove(struct in_device *in_dev,
1329 			      struct ip_mc_list *im)
1330 {
1331 	struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1332 	struct ip_mc_list *aux;
1333 
1334 	if (!mc_hash)
1335 		return;
1336 	mc_hash += ip_mc_hash(im);
1337 	while ((aux = rtnl_dereference(*mc_hash)) != im)
1338 		mc_hash = &aux->next_hash;
1339 	*mc_hash = im->next_hash;
1340 }
1341 
1342 
1343 /*
1344  *	A socket has joined a multicast group on device dev.
1345  */
1346 
1347 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1348 {
1349 	struct ip_mc_list *im;
1350 #ifdef CONFIG_IP_MULTICAST
1351 	struct net *net = dev_net(in_dev->dev);
1352 #endif
1353 
1354 	ASSERT_RTNL();
1355 
1356 	for_each_pmc_rtnl(in_dev, im) {
1357 		if (im->multiaddr == addr) {
1358 			im->users++;
1359 			ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1360 			goto out;
1361 		}
1362 	}
1363 
1364 	im = kzalloc(sizeof(*im), GFP_KERNEL);
1365 	if (!im)
1366 		goto out;
1367 
1368 	im->users = 1;
1369 	im->interface = in_dev;
1370 	in_dev_hold(in_dev);
1371 	im->multiaddr = addr;
1372 	/* initial mode is (EX, empty) */
1373 	im->sfmode = MCAST_EXCLUDE;
1374 	im->sfcount[MCAST_EXCLUDE] = 1;
1375 	atomic_set(&im->refcnt, 1);
1376 	spin_lock_init(&im->lock);
1377 #ifdef CONFIG_IP_MULTICAST
1378 	setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
1379 	im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1380 #endif
1381 
1382 	im->next_rcu = in_dev->mc_list;
1383 	in_dev->mc_count++;
1384 	rcu_assign_pointer(in_dev->mc_list, im);
1385 
1386 	ip_mc_hash_add(in_dev, im);
1387 
1388 #ifdef CONFIG_IP_MULTICAST
1389 	igmpv3_del_delrec(in_dev, im);
1390 #endif
1391 	igmp_group_added(im);
1392 	if (!in_dev->dead)
1393 		ip_rt_multicast_event(in_dev);
1394 out:
1395 	return;
1396 }
1397 EXPORT_SYMBOL(ip_mc_inc_group);
1398 
1399 static int ip_mc_check_iphdr(struct sk_buff *skb)
1400 {
1401 	const struct iphdr *iph;
1402 	unsigned int len;
1403 	unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1404 
1405 	if (!pskb_may_pull(skb, offset))
1406 		return -EINVAL;
1407 
1408 	iph = ip_hdr(skb);
1409 
1410 	if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1411 		return -EINVAL;
1412 
1413 	offset += ip_hdrlen(skb) - sizeof(*iph);
1414 
1415 	if (!pskb_may_pull(skb, offset))
1416 		return -EINVAL;
1417 
1418 	iph = ip_hdr(skb);
1419 
1420 	if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1421 		return -EINVAL;
1422 
1423 	len = skb_network_offset(skb) + ntohs(iph->tot_len);
1424 	if (skb->len < len || len < offset)
1425 		return -EINVAL;
1426 
1427 	skb_set_transport_header(skb, offset);
1428 
1429 	return 0;
1430 }
1431 
1432 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1433 {
1434 	unsigned int len = skb_transport_offset(skb);
1435 
1436 	len += sizeof(struct igmpv3_report);
1437 
1438 	return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1439 }
1440 
1441 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1442 {
1443 	unsigned int len = skb_transport_offset(skb);
1444 
1445 	len += sizeof(struct igmphdr);
1446 	if (skb->len < len)
1447 		return -EINVAL;
1448 
1449 	/* IGMPv{1,2}? */
1450 	if (skb->len != len) {
1451 		/* or IGMPv3? */
1452 		len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1453 		if (skb->len < len || !pskb_may_pull(skb, len))
1454 			return -EINVAL;
1455 	}
1456 
1457 	/* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1458 	 * all-systems destination addresses (224.0.0.1) for general queries
1459 	 */
1460 	if (!igmp_hdr(skb)->group &&
1461 	    ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1462 		return -EINVAL;
1463 
1464 	return 0;
1465 }
1466 
1467 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1468 {
1469 	switch (igmp_hdr(skb)->type) {
1470 	case IGMP_HOST_LEAVE_MESSAGE:
1471 	case IGMP_HOST_MEMBERSHIP_REPORT:
1472 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
1473 		/* fall through */
1474 		return 0;
1475 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1476 		return ip_mc_check_igmp_reportv3(skb);
1477 	case IGMP_HOST_MEMBERSHIP_QUERY:
1478 		return ip_mc_check_igmp_query(skb);
1479 	default:
1480 		return -ENOMSG;
1481 	}
1482 }
1483 
1484 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1485 {
1486 	return skb_checksum_simple_validate(skb);
1487 }
1488 
1489 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1490 
1491 {
1492 	struct sk_buff *skb_chk;
1493 	unsigned int transport_len;
1494 	unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1495 	int ret = -EINVAL;
1496 
1497 	transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1498 
1499 	skb_chk = skb_checksum_trimmed(skb, transport_len,
1500 				       ip_mc_validate_checksum);
1501 	if (!skb_chk)
1502 		goto err;
1503 
1504 	if (!pskb_may_pull(skb_chk, len))
1505 		goto err;
1506 
1507 	ret = ip_mc_check_igmp_msg(skb_chk);
1508 	if (ret)
1509 		goto err;
1510 
1511 	if (skb_trimmed)
1512 		*skb_trimmed = skb_chk;
1513 	/* free now unneeded clone */
1514 	else if (skb_chk != skb)
1515 		kfree_skb(skb_chk);
1516 
1517 	ret = 0;
1518 
1519 err:
1520 	if (ret && skb_chk && skb_chk != skb)
1521 		kfree_skb(skb_chk);
1522 
1523 	return ret;
1524 }
1525 
1526 /**
1527  * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1528  * @skb: the skb to validate
1529  * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1530  *
1531  * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1532  * skb transport header accordingly and returns zero.
1533  *
1534  * -EINVAL: A broken packet was detected, i.e. it violates some internet
1535  *  standard
1536  * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1537  * -ENOMEM: A memory allocation failure happened.
1538  *
1539  * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1540  * to NULL): After parsing an IGMP packet successfully it will point to
1541  * an skb which has its tail aligned to the IP packet end. This might
1542  * either be the originally provided skb or a trimmed, cloned version if
1543  * the skb frame had data beyond the IP packet. A cloned skb allows us
1544  * to leave the original skb and its full frame unchanged (which might be
1545  * desirable for layer 2 frame jugglers).
1546  *
1547  * Caller needs to set the skb network header and free any returned skb if it
1548  * differs from the provided skb.
1549  */
1550 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1551 {
1552 	int ret = ip_mc_check_iphdr(skb);
1553 
1554 	if (ret < 0)
1555 		return ret;
1556 
1557 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1558 		return -ENOMSG;
1559 
1560 	return __ip_mc_check_igmp(skb, skb_trimmed);
1561 }
1562 EXPORT_SYMBOL(ip_mc_check_igmp);
1563 
1564 /*
1565  *	Resend IGMP JOIN report; used by netdev notifier.
1566  */
1567 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1568 {
1569 #ifdef CONFIG_IP_MULTICAST
1570 	struct ip_mc_list *im;
1571 	int type;
1572 	struct net *net = dev_net(in_dev->dev);
1573 
1574 	ASSERT_RTNL();
1575 
1576 	for_each_pmc_rtnl(in_dev, im) {
1577 		if (im->multiaddr == IGMP_ALL_HOSTS)
1578 			continue;
1579 		if (ipv4_is_local_multicast(im->multiaddr) &&
1580 		    !net->ipv4.sysctl_igmp_llm_reports)
1581 			continue;
1582 
1583 		/* a failover is happening and switches
1584 		 * must be notified immediately
1585 		 */
1586 		if (IGMP_V1_SEEN(in_dev))
1587 			type = IGMP_HOST_MEMBERSHIP_REPORT;
1588 		else if (IGMP_V2_SEEN(in_dev))
1589 			type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1590 		else
1591 			type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1592 		igmp_send_report(in_dev, im, type);
1593 	}
1594 #endif
1595 }
1596 
1597 /*
1598  *	A socket has left a multicast group on device dev
1599  */
1600 
1601 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1602 {
1603 	struct ip_mc_list *i;
1604 	struct ip_mc_list __rcu **ip;
1605 
1606 	ASSERT_RTNL();
1607 
1608 	for (ip = &in_dev->mc_list;
1609 	     (i = rtnl_dereference(*ip)) != NULL;
1610 	     ip = &i->next_rcu) {
1611 		if (i->multiaddr == addr) {
1612 			if (--i->users == 0) {
1613 				ip_mc_hash_remove(in_dev, i);
1614 				*ip = i->next_rcu;
1615 				in_dev->mc_count--;
1616 				igmp_group_dropped(i);
1617 				ip_mc_clear_src(i);
1618 
1619 				if (!in_dev->dead)
1620 					ip_rt_multicast_event(in_dev);
1621 
1622 				ip_ma_put(i);
1623 				return;
1624 			}
1625 			break;
1626 		}
1627 	}
1628 }
1629 EXPORT_SYMBOL(ip_mc_dec_group);
1630 
1631 /* Device changing type */
1632 
1633 void ip_mc_unmap(struct in_device *in_dev)
1634 {
1635 	struct ip_mc_list *pmc;
1636 
1637 	ASSERT_RTNL();
1638 
1639 	for_each_pmc_rtnl(in_dev, pmc)
1640 		igmp_group_dropped(pmc);
1641 }
1642 
1643 void ip_mc_remap(struct in_device *in_dev)
1644 {
1645 	struct ip_mc_list *pmc;
1646 
1647 	ASSERT_RTNL();
1648 
1649 	for_each_pmc_rtnl(in_dev, pmc) {
1650 #ifdef CONFIG_IP_MULTICAST
1651 		igmpv3_del_delrec(in_dev, pmc);
1652 #endif
1653 		igmp_group_added(pmc);
1654 	}
1655 }
1656 
1657 /* Device going down */
1658 
1659 void ip_mc_down(struct in_device *in_dev)
1660 {
1661 	struct ip_mc_list *pmc;
1662 
1663 	ASSERT_RTNL();
1664 
1665 	for_each_pmc_rtnl(in_dev, pmc)
1666 		igmp_group_dropped(pmc);
1667 
1668 #ifdef CONFIG_IP_MULTICAST
1669 	in_dev->mr_ifc_count = 0;
1670 	if (del_timer(&in_dev->mr_ifc_timer))
1671 		__in_dev_put(in_dev);
1672 	in_dev->mr_gq_running = 0;
1673 	if (del_timer(&in_dev->mr_gq_timer))
1674 		__in_dev_put(in_dev);
1675 #endif
1676 
1677 	ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1678 }
1679 
1680 void ip_mc_init_dev(struct in_device *in_dev)
1681 {
1682 #ifdef CONFIG_IP_MULTICAST
1683 	struct net *net = dev_net(in_dev->dev);
1684 #endif
1685 	ASSERT_RTNL();
1686 
1687 #ifdef CONFIG_IP_MULTICAST
1688 	setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1689 			(unsigned long)in_dev);
1690 	setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1691 			(unsigned long)in_dev);
1692 	in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1693 #endif
1694 
1695 	spin_lock_init(&in_dev->mc_tomb_lock);
1696 }
1697 
1698 /* Device going up */
1699 
1700 void ip_mc_up(struct in_device *in_dev)
1701 {
1702 	struct ip_mc_list *pmc;
1703 #ifdef CONFIG_IP_MULTICAST
1704 	struct net *net = dev_net(in_dev->dev);
1705 #endif
1706 
1707 	ASSERT_RTNL();
1708 
1709 #ifdef CONFIG_IP_MULTICAST
1710 	in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1711 #endif
1712 	ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1713 
1714 	for_each_pmc_rtnl(in_dev, pmc) {
1715 #ifdef CONFIG_IP_MULTICAST
1716 		igmpv3_del_delrec(in_dev, pmc);
1717 #endif
1718 		igmp_group_added(pmc);
1719 	}
1720 }
1721 
1722 /*
1723  *	Device is about to be destroyed: clean up.
1724  */
1725 
1726 void ip_mc_destroy_dev(struct in_device *in_dev)
1727 {
1728 	struct ip_mc_list *i;
1729 
1730 	ASSERT_RTNL();
1731 
1732 	/* Deactivate timers */
1733 	ip_mc_down(in_dev);
1734 #ifdef CONFIG_IP_MULTICAST
1735 	igmpv3_clear_delrec(in_dev);
1736 #endif
1737 
1738 	while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1739 		in_dev->mc_list = i->next_rcu;
1740 		in_dev->mc_count--;
1741 		ip_ma_put(i);
1742 	}
1743 }
1744 
1745 /* RTNL is locked */
1746 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1747 {
1748 	struct net_device *dev = NULL;
1749 	struct in_device *idev = NULL;
1750 
1751 	if (imr->imr_ifindex) {
1752 		idev = inetdev_by_index(net, imr->imr_ifindex);
1753 		return idev;
1754 	}
1755 	if (imr->imr_address.s_addr) {
1756 		dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1757 		if (!dev)
1758 			return NULL;
1759 	}
1760 
1761 	if (!dev) {
1762 		struct rtable *rt = ip_route_output(net,
1763 						    imr->imr_multiaddr.s_addr,
1764 						    0, 0, 0);
1765 		if (!IS_ERR(rt)) {
1766 			dev = rt->dst.dev;
1767 			ip_rt_put(rt);
1768 		}
1769 	}
1770 	if (dev) {
1771 		imr->imr_ifindex = dev->ifindex;
1772 		idev = __in_dev_get_rtnl(dev);
1773 	}
1774 	return idev;
1775 }
1776 
1777 /*
1778  *	Join a socket to a group
1779  */
1780 
1781 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1782 	__be32 *psfsrc)
1783 {
1784 	struct ip_sf_list *psf, *psf_prev;
1785 	int rv = 0;
1786 
1787 	psf_prev = NULL;
1788 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
1789 		if (psf->sf_inaddr == *psfsrc)
1790 			break;
1791 		psf_prev = psf;
1792 	}
1793 	if (!psf || psf->sf_count[sfmode] == 0) {
1794 		/* source filter not found, or count wrong =>  bug */
1795 		return -ESRCH;
1796 	}
1797 	psf->sf_count[sfmode]--;
1798 	if (psf->sf_count[sfmode] == 0) {
1799 		ip_rt_multicast_event(pmc->interface);
1800 	}
1801 	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1802 #ifdef CONFIG_IP_MULTICAST
1803 		struct in_device *in_dev = pmc->interface;
1804 		struct net *net = dev_net(in_dev->dev);
1805 #endif
1806 
1807 		/* no more filters for this source */
1808 		if (psf_prev)
1809 			psf_prev->sf_next = psf->sf_next;
1810 		else
1811 			pmc->sources = psf->sf_next;
1812 #ifdef CONFIG_IP_MULTICAST
1813 		if (psf->sf_oldin &&
1814 		    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1815 			psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1816 			psf->sf_next = pmc->tomb;
1817 			pmc->tomb = psf;
1818 			rv = 1;
1819 		} else
1820 #endif
1821 			kfree(psf);
1822 	}
1823 	return rv;
1824 }
1825 
1826 #ifndef CONFIG_IP_MULTICAST
1827 #define igmp_ifc_event(x)	do { } while (0)
1828 #endif
1829 
1830 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1831 			 int sfcount, __be32 *psfsrc, int delta)
1832 {
1833 	struct ip_mc_list *pmc;
1834 	int	changerec = 0;
1835 	int	i, err;
1836 
1837 	if (!in_dev)
1838 		return -ENODEV;
1839 	rcu_read_lock();
1840 	for_each_pmc_rcu(in_dev, pmc) {
1841 		if (*pmca == pmc->multiaddr)
1842 			break;
1843 	}
1844 	if (!pmc) {
1845 		/* MCA not found?? bug */
1846 		rcu_read_unlock();
1847 		return -ESRCH;
1848 	}
1849 	spin_lock_bh(&pmc->lock);
1850 	rcu_read_unlock();
1851 #ifdef CONFIG_IP_MULTICAST
1852 	sf_markstate(pmc);
1853 #endif
1854 	if (!delta) {
1855 		err = -EINVAL;
1856 		if (!pmc->sfcount[sfmode])
1857 			goto out_unlock;
1858 		pmc->sfcount[sfmode]--;
1859 	}
1860 	err = 0;
1861 	for (i = 0; i < sfcount; i++) {
1862 		int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1863 
1864 		changerec |= rv > 0;
1865 		if (!err && rv < 0)
1866 			err = rv;
1867 	}
1868 	if (pmc->sfmode == MCAST_EXCLUDE &&
1869 	    pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1870 	    pmc->sfcount[MCAST_INCLUDE]) {
1871 #ifdef CONFIG_IP_MULTICAST
1872 		struct ip_sf_list *psf;
1873 		struct net *net = dev_net(in_dev->dev);
1874 #endif
1875 
1876 		/* filter mode change */
1877 		pmc->sfmode = MCAST_INCLUDE;
1878 #ifdef CONFIG_IP_MULTICAST
1879 		pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1880 		in_dev->mr_ifc_count = pmc->crcount;
1881 		for (psf = pmc->sources; psf; psf = psf->sf_next)
1882 			psf->sf_crcount = 0;
1883 		igmp_ifc_event(pmc->interface);
1884 	} else if (sf_setstate(pmc) || changerec) {
1885 		igmp_ifc_event(pmc->interface);
1886 #endif
1887 	}
1888 out_unlock:
1889 	spin_unlock_bh(&pmc->lock);
1890 	return err;
1891 }
1892 
1893 /*
1894  * Add multicast single-source filter to the interface list
1895  */
1896 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1897 	__be32 *psfsrc)
1898 {
1899 	struct ip_sf_list *psf, *psf_prev;
1900 
1901 	psf_prev = NULL;
1902 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
1903 		if (psf->sf_inaddr == *psfsrc)
1904 			break;
1905 		psf_prev = psf;
1906 	}
1907 	if (!psf) {
1908 		psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1909 		if (!psf)
1910 			return -ENOBUFS;
1911 		psf->sf_inaddr = *psfsrc;
1912 		if (psf_prev) {
1913 			psf_prev->sf_next = psf;
1914 		} else
1915 			pmc->sources = psf;
1916 	}
1917 	psf->sf_count[sfmode]++;
1918 	if (psf->sf_count[sfmode] == 1) {
1919 		ip_rt_multicast_event(pmc->interface);
1920 	}
1921 	return 0;
1922 }
1923 
1924 #ifdef CONFIG_IP_MULTICAST
1925 static void sf_markstate(struct ip_mc_list *pmc)
1926 {
1927 	struct ip_sf_list *psf;
1928 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1929 
1930 	for (psf = pmc->sources; psf; psf = psf->sf_next)
1931 		if (pmc->sfcount[MCAST_EXCLUDE]) {
1932 			psf->sf_oldin = mca_xcount ==
1933 				psf->sf_count[MCAST_EXCLUDE] &&
1934 				!psf->sf_count[MCAST_INCLUDE];
1935 		} else
1936 			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1937 }
1938 
1939 static int sf_setstate(struct ip_mc_list *pmc)
1940 {
1941 	struct ip_sf_list *psf, *dpsf;
1942 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1943 	int qrv = pmc->interface->mr_qrv;
1944 	int new_in, rv;
1945 
1946 	rv = 0;
1947 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
1948 		if (pmc->sfcount[MCAST_EXCLUDE]) {
1949 			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1950 				!psf->sf_count[MCAST_INCLUDE];
1951 		} else
1952 			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1953 		if (new_in) {
1954 			if (!psf->sf_oldin) {
1955 				struct ip_sf_list *prev = NULL;
1956 
1957 				for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
1958 					if (dpsf->sf_inaddr == psf->sf_inaddr)
1959 						break;
1960 					prev = dpsf;
1961 				}
1962 				if (dpsf) {
1963 					if (prev)
1964 						prev->sf_next = dpsf->sf_next;
1965 					else
1966 						pmc->tomb = dpsf->sf_next;
1967 					kfree(dpsf);
1968 				}
1969 				psf->sf_crcount = qrv;
1970 				rv++;
1971 			}
1972 		} else if (psf->sf_oldin) {
1973 
1974 			psf->sf_crcount = 0;
1975 			/*
1976 			 * add or update "delete" records if an active filter
1977 			 * is now inactive
1978 			 */
1979 			for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
1980 				if (dpsf->sf_inaddr == psf->sf_inaddr)
1981 					break;
1982 			if (!dpsf) {
1983 				dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1984 				if (!dpsf)
1985 					continue;
1986 				*dpsf = *psf;
1987 				/* pmc->lock held by callers */
1988 				dpsf->sf_next = pmc->tomb;
1989 				pmc->tomb = dpsf;
1990 			}
1991 			dpsf->sf_crcount = qrv;
1992 			rv++;
1993 		}
1994 	}
1995 	return rv;
1996 }
1997 #endif
1998 
1999 /*
2000  * Add multicast source filter list to the interface list
2001  */
2002 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2003 			 int sfcount, __be32 *psfsrc, int delta)
2004 {
2005 	struct ip_mc_list *pmc;
2006 	int	isexclude;
2007 	int	i, err;
2008 
2009 	if (!in_dev)
2010 		return -ENODEV;
2011 	rcu_read_lock();
2012 	for_each_pmc_rcu(in_dev, pmc) {
2013 		if (*pmca == pmc->multiaddr)
2014 			break;
2015 	}
2016 	if (!pmc) {
2017 		/* MCA not found?? bug */
2018 		rcu_read_unlock();
2019 		return -ESRCH;
2020 	}
2021 	spin_lock_bh(&pmc->lock);
2022 	rcu_read_unlock();
2023 
2024 #ifdef CONFIG_IP_MULTICAST
2025 	sf_markstate(pmc);
2026 #endif
2027 	isexclude = pmc->sfmode == MCAST_EXCLUDE;
2028 	if (!delta)
2029 		pmc->sfcount[sfmode]++;
2030 	err = 0;
2031 	for (i = 0; i < sfcount; i++) {
2032 		err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2033 		if (err)
2034 			break;
2035 	}
2036 	if (err) {
2037 		int j;
2038 
2039 		if (!delta)
2040 			pmc->sfcount[sfmode]--;
2041 		for (j = 0; j < i; j++)
2042 			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2043 	} else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2044 #ifdef CONFIG_IP_MULTICAST
2045 		struct ip_sf_list *psf;
2046 		struct net *net = dev_net(pmc->interface->dev);
2047 		in_dev = pmc->interface;
2048 #endif
2049 
2050 		/* filter mode change */
2051 		if (pmc->sfcount[MCAST_EXCLUDE])
2052 			pmc->sfmode = MCAST_EXCLUDE;
2053 		else if (pmc->sfcount[MCAST_INCLUDE])
2054 			pmc->sfmode = MCAST_INCLUDE;
2055 #ifdef CONFIG_IP_MULTICAST
2056 		/* else no filters; keep old mode for reports */
2057 
2058 		pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2059 		in_dev->mr_ifc_count = pmc->crcount;
2060 		for (psf = pmc->sources; psf; psf = psf->sf_next)
2061 			psf->sf_crcount = 0;
2062 		igmp_ifc_event(in_dev);
2063 	} else if (sf_setstate(pmc)) {
2064 		igmp_ifc_event(in_dev);
2065 #endif
2066 	}
2067 	spin_unlock_bh(&pmc->lock);
2068 	return err;
2069 }
2070 
2071 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2072 {
2073 	struct ip_sf_list *psf, *nextpsf;
2074 
2075 	for (psf = pmc->tomb; psf; psf = nextpsf) {
2076 		nextpsf = psf->sf_next;
2077 		kfree(psf);
2078 	}
2079 	pmc->tomb = NULL;
2080 	for (psf = pmc->sources; psf; psf = nextpsf) {
2081 		nextpsf = psf->sf_next;
2082 		kfree(psf);
2083 	}
2084 	pmc->sources = NULL;
2085 	pmc->sfmode = MCAST_EXCLUDE;
2086 	pmc->sfcount[MCAST_INCLUDE] = 0;
2087 	pmc->sfcount[MCAST_EXCLUDE] = 1;
2088 }
2089 
2090 /* Join a multicast group
2091  */
2092 
2093 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2094 {
2095 	__be32 addr = imr->imr_multiaddr.s_addr;
2096 	struct ip_mc_socklist *iml, *i;
2097 	struct in_device *in_dev;
2098 	struct inet_sock *inet = inet_sk(sk);
2099 	struct net *net = sock_net(sk);
2100 	int ifindex;
2101 	int count = 0;
2102 	int err;
2103 
2104 	ASSERT_RTNL();
2105 
2106 	if (!ipv4_is_multicast(addr))
2107 		return -EINVAL;
2108 
2109 	in_dev = ip_mc_find_dev(net, imr);
2110 
2111 	if (!in_dev) {
2112 		err = -ENODEV;
2113 		goto done;
2114 	}
2115 
2116 	err = -EADDRINUSE;
2117 	ifindex = imr->imr_ifindex;
2118 	for_each_pmc_rtnl(inet, i) {
2119 		if (i->multi.imr_multiaddr.s_addr == addr &&
2120 		    i->multi.imr_ifindex == ifindex)
2121 			goto done;
2122 		count++;
2123 	}
2124 	err = -ENOBUFS;
2125 	if (count >= net->ipv4.sysctl_igmp_max_memberships)
2126 		goto done;
2127 	iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2128 	if (!iml)
2129 		goto done;
2130 
2131 	memcpy(&iml->multi, imr, sizeof(*imr));
2132 	iml->next_rcu = inet->mc_list;
2133 	iml->sflist = NULL;
2134 	iml->sfmode = MCAST_EXCLUDE;
2135 	rcu_assign_pointer(inet->mc_list, iml);
2136 	ip_mc_inc_group(in_dev, addr);
2137 	err = 0;
2138 done:
2139 	return err;
2140 }
2141 EXPORT_SYMBOL(ip_mc_join_group);
2142 
2143 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2144 			   struct in_device *in_dev)
2145 {
2146 	struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2147 	int err;
2148 
2149 	if (!psf) {
2150 		/* any-source empty exclude case */
2151 		return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2152 			iml->sfmode, 0, NULL, 0);
2153 	}
2154 	err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2155 			iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2156 	RCU_INIT_POINTER(iml->sflist, NULL);
2157 	/* decrease mem now to avoid the memleak warning */
2158 	atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2159 	kfree_rcu(psf, rcu);
2160 	return err;
2161 }
2162 
2163 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2164 {
2165 	struct inet_sock *inet = inet_sk(sk);
2166 	struct ip_mc_socklist *iml;
2167 	struct ip_mc_socklist __rcu **imlp;
2168 	struct in_device *in_dev;
2169 	struct net *net = sock_net(sk);
2170 	__be32 group = imr->imr_multiaddr.s_addr;
2171 	u32 ifindex;
2172 	int ret = -EADDRNOTAVAIL;
2173 
2174 	ASSERT_RTNL();
2175 
2176 	in_dev = ip_mc_find_dev(net, imr);
2177 	if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2178 		ret = -ENODEV;
2179 		goto out;
2180 	}
2181 	ifindex = imr->imr_ifindex;
2182 	for (imlp = &inet->mc_list;
2183 	     (iml = rtnl_dereference(*imlp)) != NULL;
2184 	     imlp = &iml->next_rcu) {
2185 		if (iml->multi.imr_multiaddr.s_addr != group)
2186 			continue;
2187 		if (ifindex) {
2188 			if (iml->multi.imr_ifindex != ifindex)
2189 				continue;
2190 		} else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2191 				iml->multi.imr_address.s_addr)
2192 			continue;
2193 
2194 		(void) ip_mc_leave_src(sk, iml, in_dev);
2195 
2196 		*imlp = iml->next_rcu;
2197 
2198 		if (in_dev)
2199 			ip_mc_dec_group(in_dev, group);
2200 
2201 		/* decrease mem now to avoid the memleak warning */
2202 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2203 		kfree_rcu(iml, rcu);
2204 		return 0;
2205 	}
2206 out:
2207 	return ret;
2208 }
2209 EXPORT_SYMBOL(ip_mc_leave_group);
2210 
2211 int ip_mc_source(int add, int omode, struct sock *sk, struct
2212 	ip_mreq_source *mreqs, int ifindex)
2213 {
2214 	int err;
2215 	struct ip_mreqn imr;
2216 	__be32 addr = mreqs->imr_multiaddr;
2217 	struct ip_mc_socklist *pmc;
2218 	struct in_device *in_dev = NULL;
2219 	struct inet_sock *inet = inet_sk(sk);
2220 	struct ip_sf_socklist *psl;
2221 	struct net *net = sock_net(sk);
2222 	int leavegroup = 0;
2223 	int i, j, rv;
2224 
2225 	if (!ipv4_is_multicast(addr))
2226 		return -EINVAL;
2227 
2228 	ASSERT_RTNL();
2229 
2230 	imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2231 	imr.imr_address.s_addr = mreqs->imr_interface;
2232 	imr.imr_ifindex = ifindex;
2233 	in_dev = ip_mc_find_dev(net, &imr);
2234 
2235 	if (!in_dev) {
2236 		err = -ENODEV;
2237 		goto done;
2238 	}
2239 	err = -EADDRNOTAVAIL;
2240 
2241 	for_each_pmc_rtnl(inet, pmc) {
2242 		if ((pmc->multi.imr_multiaddr.s_addr ==
2243 		     imr.imr_multiaddr.s_addr) &&
2244 		    (pmc->multi.imr_ifindex == imr.imr_ifindex))
2245 			break;
2246 	}
2247 	if (!pmc) {		/* must have a prior join */
2248 		err = -EINVAL;
2249 		goto done;
2250 	}
2251 	/* if a source filter was set, must be the same mode as before */
2252 	if (pmc->sflist) {
2253 		if (pmc->sfmode != omode) {
2254 			err = -EINVAL;
2255 			goto done;
2256 		}
2257 	} else if (pmc->sfmode != omode) {
2258 		/* allow mode switches for empty-set filters */
2259 		ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2260 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2261 			NULL, 0);
2262 		pmc->sfmode = omode;
2263 	}
2264 
2265 	psl = rtnl_dereference(pmc->sflist);
2266 	if (!add) {
2267 		if (!psl)
2268 			goto done;	/* err = -EADDRNOTAVAIL */
2269 		rv = !0;
2270 		for (i = 0; i < psl->sl_count; i++) {
2271 			rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2272 				sizeof(__be32));
2273 			if (rv == 0)
2274 				break;
2275 		}
2276 		if (rv)		/* source not found */
2277 			goto done;	/* err = -EADDRNOTAVAIL */
2278 
2279 		/* special case - (INCLUDE, empty) == LEAVE_GROUP */
2280 		if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2281 			leavegroup = 1;
2282 			goto done;
2283 		}
2284 
2285 		/* update the interface filter */
2286 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2287 			&mreqs->imr_sourceaddr, 1);
2288 
2289 		for (j = i+1; j < psl->sl_count; j++)
2290 			psl->sl_addr[j-1] = psl->sl_addr[j];
2291 		psl->sl_count--;
2292 		err = 0;
2293 		goto done;
2294 	}
2295 	/* else, add a new source to the filter */
2296 
2297 	if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2298 		err = -ENOBUFS;
2299 		goto done;
2300 	}
2301 	if (!psl || psl->sl_count == psl->sl_max) {
2302 		struct ip_sf_socklist *newpsl;
2303 		int count = IP_SFBLOCK;
2304 
2305 		if (psl)
2306 			count += psl->sl_max;
2307 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2308 		if (!newpsl) {
2309 			err = -ENOBUFS;
2310 			goto done;
2311 		}
2312 		newpsl->sl_max = count;
2313 		newpsl->sl_count = count - IP_SFBLOCK;
2314 		if (psl) {
2315 			for (i = 0; i < psl->sl_count; i++)
2316 				newpsl->sl_addr[i] = psl->sl_addr[i];
2317 			/* decrease mem now to avoid the memleak warning */
2318 			atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2319 			kfree_rcu(psl, rcu);
2320 		}
2321 		rcu_assign_pointer(pmc->sflist, newpsl);
2322 		psl = newpsl;
2323 	}
2324 	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
2325 	for (i = 0; i < psl->sl_count; i++) {
2326 		rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2327 			sizeof(__be32));
2328 		if (rv == 0)
2329 			break;
2330 	}
2331 	if (rv == 0)		/* address already there is an error */
2332 		goto done;
2333 	for (j = psl->sl_count-1; j >= i; j--)
2334 		psl->sl_addr[j+1] = psl->sl_addr[j];
2335 	psl->sl_addr[i] = mreqs->imr_sourceaddr;
2336 	psl->sl_count++;
2337 	err = 0;
2338 	/* update the interface list */
2339 	ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2340 		&mreqs->imr_sourceaddr, 1);
2341 done:
2342 	if (leavegroup)
2343 		err = ip_mc_leave_group(sk, &imr);
2344 	return err;
2345 }
2346 
2347 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2348 {
2349 	int err = 0;
2350 	struct ip_mreqn	imr;
2351 	__be32 addr = msf->imsf_multiaddr;
2352 	struct ip_mc_socklist *pmc;
2353 	struct in_device *in_dev;
2354 	struct inet_sock *inet = inet_sk(sk);
2355 	struct ip_sf_socklist *newpsl, *psl;
2356 	struct net *net = sock_net(sk);
2357 	int leavegroup = 0;
2358 
2359 	if (!ipv4_is_multicast(addr))
2360 		return -EINVAL;
2361 	if (msf->imsf_fmode != MCAST_INCLUDE &&
2362 	    msf->imsf_fmode != MCAST_EXCLUDE)
2363 		return -EINVAL;
2364 
2365 	ASSERT_RTNL();
2366 
2367 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2368 	imr.imr_address.s_addr = msf->imsf_interface;
2369 	imr.imr_ifindex = ifindex;
2370 	in_dev = ip_mc_find_dev(net, &imr);
2371 
2372 	if (!in_dev) {
2373 		err = -ENODEV;
2374 		goto done;
2375 	}
2376 
2377 	/* special case - (INCLUDE, empty) == LEAVE_GROUP */
2378 	if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2379 		leavegroup = 1;
2380 		goto done;
2381 	}
2382 
2383 	for_each_pmc_rtnl(inet, pmc) {
2384 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2385 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
2386 			break;
2387 	}
2388 	if (!pmc) {		/* must have a prior join */
2389 		err = -EINVAL;
2390 		goto done;
2391 	}
2392 	if (msf->imsf_numsrc) {
2393 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2394 							   GFP_KERNEL);
2395 		if (!newpsl) {
2396 			err = -ENOBUFS;
2397 			goto done;
2398 		}
2399 		newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2400 		memcpy(newpsl->sl_addr, msf->imsf_slist,
2401 			msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2402 		err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2403 			msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2404 		if (err) {
2405 			sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2406 			goto done;
2407 		}
2408 	} else {
2409 		newpsl = NULL;
2410 		(void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2411 				     msf->imsf_fmode, 0, NULL, 0);
2412 	}
2413 	psl = rtnl_dereference(pmc->sflist);
2414 	if (psl) {
2415 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2416 			psl->sl_count, psl->sl_addr, 0);
2417 		/* decrease mem now to avoid the memleak warning */
2418 		atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2419 		kfree_rcu(psl, rcu);
2420 	} else
2421 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2422 			0, NULL, 0);
2423 	rcu_assign_pointer(pmc->sflist, newpsl);
2424 	pmc->sfmode = msf->imsf_fmode;
2425 	err = 0;
2426 done:
2427 	if (leavegroup)
2428 		err = ip_mc_leave_group(sk, &imr);
2429 	return err;
2430 }
2431 
2432 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2433 	struct ip_msfilter __user *optval, int __user *optlen)
2434 {
2435 	int err, len, count, copycount;
2436 	struct ip_mreqn	imr;
2437 	__be32 addr = msf->imsf_multiaddr;
2438 	struct ip_mc_socklist *pmc;
2439 	struct in_device *in_dev;
2440 	struct inet_sock *inet = inet_sk(sk);
2441 	struct ip_sf_socklist *psl;
2442 	struct net *net = sock_net(sk);
2443 
2444 	ASSERT_RTNL();
2445 
2446 	if (!ipv4_is_multicast(addr))
2447 		return -EINVAL;
2448 
2449 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2450 	imr.imr_address.s_addr = msf->imsf_interface;
2451 	imr.imr_ifindex = 0;
2452 	in_dev = ip_mc_find_dev(net, &imr);
2453 
2454 	if (!in_dev) {
2455 		err = -ENODEV;
2456 		goto done;
2457 	}
2458 	err = -EADDRNOTAVAIL;
2459 
2460 	for_each_pmc_rtnl(inet, pmc) {
2461 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2462 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
2463 			break;
2464 	}
2465 	if (!pmc)		/* must have a prior join */
2466 		goto done;
2467 	msf->imsf_fmode = pmc->sfmode;
2468 	psl = rtnl_dereference(pmc->sflist);
2469 	if (!psl) {
2470 		len = 0;
2471 		count = 0;
2472 	} else {
2473 		count = psl->sl_count;
2474 	}
2475 	copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2476 	len = copycount * sizeof(psl->sl_addr[0]);
2477 	msf->imsf_numsrc = count;
2478 	if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2479 	    copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2480 		return -EFAULT;
2481 	}
2482 	if (len &&
2483 	    copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2484 		return -EFAULT;
2485 	return 0;
2486 done:
2487 	return err;
2488 }
2489 
2490 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2491 	struct group_filter __user *optval, int __user *optlen)
2492 {
2493 	int err, i, count, copycount;
2494 	struct sockaddr_in *psin;
2495 	__be32 addr;
2496 	struct ip_mc_socklist *pmc;
2497 	struct inet_sock *inet = inet_sk(sk);
2498 	struct ip_sf_socklist *psl;
2499 
2500 	ASSERT_RTNL();
2501 
2502 	psin = (struct sockaddr_in *)&gsf->gf_group;
2503 	if (psin->sin_family != AF_INET)
2504 		return -EINVAL;
2505 	addr = psin->sin_addr.s_addr;
2506 	if (!ipv4_is_multicast(addr))
2507 		return -EINVAL;
2508 
2509 	err = -EADDRNOTAVAIL;
2510 
2511 	for_each_pmc_rtnl(inet, pmc) {
2512 		if (pmc->multi.imr_multiaddr.s_addr == addr &&
2513 		    pmc->multi.imr_ifindex == gsf->gf_interface)
2514 			break;
2515 	}
2516 	if (!pmc)		/* must have a prior join */
2517 		goto done;
2518 	gsf->gf_fmode = pmc->sfmode;
2519 	psl = rtnl_dereference(pmc->sflist);
2520 	count = psl ? psl->sl_count : 0;
2521 	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2522 	gsf->gf_numsrc = count;
2523 	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2524 	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2525 		return -EFAULT;
2526 	}
2527 	for (i = 0; i < copycount; i++) {
2528 		struct sockaddr_storage ss;
2529 
2530 		psin = (struct sockaddr_in *)&ss;
2531 		memset(&ss, 0, sizeof(ss));
2532 		psin->sin_family = AF_INET;
2533 		psin->sin_addr.s_addr = psl->sl_addr[i];
2534 		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2535 			return -EFAULT;
2536 	}
2537 	return 0;
2538 done:
2539 	return err;
2540 }
2541 
2542 /*
2543  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2544  */
2545 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2546 {
2547 	struct inet_sock *inet = inet_sk(sk);
2548 	struct ip_mc_socklist *pmc;
2549 	struct ip_sf_socklist *psl;
2550 	int i;
2551 	int ret;
2552 
2553 	ret = 1;
2554 	if (!ipv4_is_multicast(loc_addr))
2555 		goto out;
2556 
2557 	rcu_read_lock();
2558 	for_each_pmc_rcu(inet, pmc) {
2559 		if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2560 		    pmc->multi.imr_ifindex == dif)
2561 			break;
2562 	}
2563 	ret = inet->mc_all;
2564 	if (!pmc)
2565 		goto unlock;
2566 	psl = rcu_dereference(pmc->sflist);
2567 	ret = (pmc->sfmode == MCAST_EXCLUDE);
2568 	if (!psl)
2569 		goto unlock;
2570 
2571 	for (i = 0; i < psl->sl_count; i++) {
2572 		if (psl->sl_addr[i] == rmt_addr)
2573 			break;
2574 	}
2575 	ret = 0;
2576 	if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2577 		goto unlock;
2578 	if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2579 		goto unlock;
2580 	ret = 1;
2581 unlock:
2582 	rcu_read_unlock();
2583 out:
2584 	return ret;
2585 }
2586 
2587 /*
2588  *	A socket is closing.
2589  */
2590 
2591 void ip_mc_drop_socket(struct sock *sk)
2592 {
2593 	struct inet_sock *inet = inet_sk(sk);
2594 	struct ip_mc_socklist *iml;
2595 	struct net *net = sock_net(sk);
2596 
2597 	if (!inet->mc_list)
2598 		return;
2599 
2600 	rtnl_lock();
2601 	while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2602 		struct in_device *in_dev;
2603 
2604 		inet->mc_list = iml->next_rcu;
2605 		in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2606 		(void) ip_mc_leave_src(sk, iml, in_dev);
2607 		if (in_dev)
2608 			ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2609 		/* decrease mem now to avoid the memleak warning */
2610 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2611 		kfree_rcu(iml, rcu);
2612 	}
2613 	rtnl_unlock();
2614 }
2615 
2616 /* called with rcu_read_lock() */
2617 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2618 {
2619 	struct ip_mc_list *im;
2620 	struct ip_mc_list __rcu **mc_hash;
2621 	struct ip_sf_list *psf;
2622 	int rv = 0;
2623 
2624 	mc_hash = rcu_dereference(in_dev->mc_hash);
2625 	if (mc_hash) {
2626 		u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2627 
2628 		for (im = rcu_dereference(mc_hash[hash]);
2629 		     im != NULL;
2630 		     im = rcu_dereference(im->next_hash)) {
2631 			if (im->multiaddr == mc_addr)
2632 				break;
2633 		}
2634 	} else {
2635 		for_each_pmc_rcu(in_dev, im) {
2636 			if (im->multiaddr == mc_addr)
2637 				break;
2638 		}
2639 	}
2640 	if (im && proto == IPPROTO_IGMP) {
2641 		rv = 1;
2642 	} else if (im) {
2643 		if (src_addr) {
2644 			for (psf = im->sources; psf; psf = psf->sf_next) {
2645 				if (psf->sf_inaddr == src_addr)
2646 					break;
2647 			}
2648 			if (psf)
2649 				rv = psf->sf_count[MCAST_INCLUDE] ||
2650 					psf->sf_count[MCAST_EXCLUDE] !=
2651 					im->sfcount[MCAST_EXCLUDE];
2652 			else
2653 				rv = im->sfcount[MCAST_EXCLUDE] != 0;
2654 		} else
2655 			rv = 1; /* unspecified source; tentatively allow */
2656 	}
2657 	return rv;
2658 }
2659 
2660 #if defined(CONFIG_PROC_FS)
2661 struct igmp_mc_iter_state {
2662 	struct seq_net_private p;
2663 	struct net_device *dev;
2664 	struct in_device *in_dev;
2665 };
2666 
2667 #define	igmp_mc_seq_private(seq)	((struct igmp_mc_iter_state *)(seq)->private)
2668 
2669 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2670 {
2671 	struct net *net = seq_file_net(seq);
2672 	struct ip_mc_list *im = NULL;
2673 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2674 
2675 	state->in_dev = NULL;
2676 	for_each_netdev_rcu(net, state->dev) {
2677 		struct in_device *in_dev;
2678 
2679 		in_dev = __in_dev_get_rcu(state->dev);
2680 		if (!in_dev)
2681 			continue;
2682 		im = rcu_dereference(in_dev->mc_list);
2683 		if (im) {
2684 			state->in_dev = in_dev;
2685 			break;
2686 		}
2687 	}
2688 	return im;
2689 }
2690 
2691 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2692 {
2693 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2694 
2695 	im = rcu_dereference(im->next_rcu);
2696 	while (!im) {
2697 		state->dev = next_net_device_rcu(state->dev);
2698 		if (!state->dev) {
2699 			state->in_dev = NULL;
2700 			break;
2701 		}
2702 		state->in_dev = __in_dev_get_rcu(state->dev);
2703 		if (!state->in_dev)
2704 			continue;
2705 		im = rcu_dereference(state->in_dev->mc_list);
2706 	}
2707 	return im;
2708 }
2709 
2710 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2711 {
2712 	struct ip_mc_list *im = igmp_mc_get_first(seq);
2713 	if (im)
2714 		while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2715 			--pos;
2716 	return pos ? NULL : im;
2717 }
2718 
2719 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2720 	__acquires(rcu)
2721 {
2722 	rcu_read_lock();
2723 	return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2724 }
2725 
2726 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2727 {
2728 	struct ip_mc_list *im;
2729 	if (v == SEQ_START_TOKEN)
2730 		im = igmp_mc_get_first(seq);
2731 	else
2732 		im = igmp_mc_get_next(seq, v);
2733 	++*pos;
2734 	return im;
2735 }
2736 
2737 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2738 	__releases(rcu)
2739 {
2740 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2741 
2742 	state->in_dev = NULL;
2743 	state->dev = NULL;
2744 	rcu_read_unlock();
2745 }
2746 
2747 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2748 {
2749 	if (v == SEQ_START_TOKEN)
2750 		seq_puts(seq,
2751 			 "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2752 	else {
2753 		struct ip_mc_list *im = (struct ip_mc_list *)v;
2754 		struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2755 		char   *querier;
2756 		long delta;
2757 
2758 #ifdef CONFIG_IP_MULTICAST
2759 		querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2760 			  IGMP_V2_SEEN(state->in_dev) ? "V2" :
2761 			  "V3";
2762 #else
2763 		querier = "NONE";
2764 #endif
2765 
2766 		if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2767 			seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2768 				   state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2769 		}
2770 
2771 		delta = im->timer.expires - jiffies;
2772 		seq_printf(seq,
2773 			   "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2774 			   im->multiaddr, im->users,
2775 			   im->tm_running,
2776 			   im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2777 			   im->reporter);
2778 	}
2779 	return 0;
2780 }
2781 
2782 static const struct seq_operations igmp_mc_seq_ops = {
2783 	.start	=	igmp_mc_seq_start,
2784 	.next	=	igmp_mc_seq_next,
2785 	.stop	=	igmp_mc_seq_stop,
2786 	.show	=	igmp_mc_seq_show,
2787 };
2788 
2789 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2790 {
2791 	return seq_open_net(inode, file, &igmp_mc_seq_ops,
2792 			sizeof(struct igmp_mc_iter_state));
2793 }
2794 
2795 static const struct file_operations igmp_mc_seq_fops = {
2796 	.owner		=	THIS_MODULE,
2797 	.open		=	igmp_mc_seq_open,
2798 	.read		=	seq_read,
2799 	.llseek		=	seq_lseek,
2800 	.release	=	seq_release_net,
2801 };
2802 
2803 struct igmp_mcf_iter_state {
2804 	struct seq_net_private p;
2805 	struct net_device *dev;
2806 	struct in_device *idev;
2807 	struct ip_mc_list *im;
2808 };
2809 
2810 #define igmp_mcf_seq_private(seq)	((struct igmp_mcf_iter_state *)(seq)->private)
2811 
2812 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2813 {
2814 	struct net *net = seq_file_net(seq);
2815 	struct ip_sf_list *psf = NULL;
2816 	struct ip_mc_list *im = NULL;
2817 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2818 
2819 	state->idev = NULL;
2820 	state->im = NULL;
2821 	for_each_netdev_rcu(net, state->dev) {
2822 		struct in_device *idev;
2823 		idev = __in_dev_get_rcu(state->dev);
2824 		if (unlikely(!idev))
2825 			continue;
2826 		im = rcu_dereference(idev->mc_list);
2827 		if (likely(im)) {
2828 			spin_lock_bh(&im->lock);
2829 			psf = im->sources;
2830 			if (likely(psf)) {
2831 				state->im = im;
2832 				state->idev = idev;
2833 				break;
2834 			}
2835 			spin_unlock_bh(&im->lock);
2836 		}
2837 	}
2838 	return psf;
2839 }
2840 
2841 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2842 {
2843 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2844 
2845 	psf = psf->sf_next;
2846 	while (!psf) {
2847 		spin_unlock_bh(&state->im->lock);
2848 		state->im = state->im->next;
2849 		while (!state->im) {
2850 			state->dev = next_net_device_rcu(state->dev);
2851 			if (!state->dev) {
2852 				state->idev = NULL;
2853 				goto out;
2854 			}
2855 			state->idev = __in_dev_get_rcu(state->dev);
2856 			if (!state->idev)
2857 				continue;
2858 			state->im = rcu_dereference(state->idev->mc_list);
2859 		}
2860 		if (!state->im)
2861 			break;
2862 		spin_lock_bh(&state->im->lock);
2863 		psf = state->im->sources;
2864 	}
2865 out:
2866 	return psf;
2867 }
2868 
2869 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2870 {
2871 	struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2872 	if (psf)
2873 		while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2874 			--pos;
2875 	return pos ? NULL : psf;
2876 }
2877 
2878 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2879 	__acquires(rcu)
2880 {
2881 	rcu_read_lock();
2882 	return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2883 }
2884 
2885 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2886 {
2887 	struct ip_sf_list *psf;
2888 	if (v == SEQ_START_TOKEN)
2889 		psf = igmp_mcf_get_first(seq);
2890 	else
2891 		psf = igmp_mcf_get_next(seq, v);
2892 	++*pos;
2893 	return psf;
2894 }
2895 
2896 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2897 	__releases(rcu)
2898 {
2899 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2900 	if (likely(state->im)) {
2901 		spin_unlock_bh(&state->im->lock);
2902 		state->im = NULL;
2903 	}
2904 	state->idev = NULL;
2905 	state->dev = NULL;
2906 	rcu_read_unlock();
2907 }
2908 
2909 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2910 {
2911 	struct ip_sf_list *psf = (struct ip_sf_list *)v;
2912 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2913 
2914 	if (v == SEQ_START_TOKEN) {
2915 		seq_puts(seq, "Idx Device        MCA        SRC    INC    EXC\n");
2916 	} else {
2917 		seq_printf(seq,
2918 			   "%3d %6.6s 0x%08x "
2919 			   "0x%08x %6lu %6lu\n",
2920 			   state->dev->ifindex, state->dev->name,
2921 			   ntohl(state->im->multiaddr),
2922 			   ntohl(psf->sf_inaddr),
2923 			   psf->sf_count[MCAST_INCLUDE],
2924 			   psf->sf_count[MCAST_EXCLUDE]);
2925 	}
2926 	return 0;
2927 }
2928 
2929 static const struct seq_operations igmp_mcf_seq_ops = {
2930 	.start	=	igmp_mcf_seq_start,
2931 	.next	=	igmp_mcf_seq_next,
2932 	.stop	=	igmp_mcf_seq_stop,
2933 	.show	=	igmp_mcf_seq_show,
2934 };
2935 
2936 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2937 {
2938 	return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2939 			sizeof(struct igmp_mcf_iter_state));
2940 }
2941 
2942 static const struct file_operations igmp_mcf_seq_fops = {
2943 	.owner		=	THIS_MODULE,
2944 	.open		=	igmp_mcf_seq_open,
2945 	.read		=	seq_read,
2946 	.llseek		=	seq_lseek,
2947 	.release	=	seq_release_net,
2948 };
2949 
2950 static int __net_init igmp_net_init(struct net *net)
2951 {
2952 	struct proc_dir_entry *pde;
2953 	int err;
2954 
2955 	pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
2956 	if (!pde)
2957 		goto out_igmp;
2958 	pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2959 			  &igmp_mcf_seq_fops);
2960 	if (!pde)
2961 		goto out_mcfilter;
2962 	err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2963 				   SOCK_DGRAM, 0, net);
2964 	if (err < 0) {
2965 		pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2966 		       err);
2967 		goto out_sock;
2968 	}
2969 
2970 	/* Sysctl initialization */
2971 	net->ipv4.sysctl_igmp_max_memberships = 20;
2972 	net->ipv4.sysctl_igmp_max_msf = 10;
2973 	/* IGMP reports for link-local multicast groups are enabled by default */
2974 	net->ipv4.sysctl_igmp_llm_reports = 1;
2975 	net->ipv4.sysctl_igmp_qrv = 2;
2976 	return 0;
2977 
2978 out_sock:
2979 	remove_proc_entry("mcfilter", net->proc_net);
2980 out_mcfilter:
2981 	remove_proc_entry("igmp", net->proc_net);
2982 out_igmp:
2983 	return -ENOMEM;
2984 }
2985 
2986 static void __net_exit igmp_net_exit(struct net *net)
2987 {
2988 	remove_proc_entry("mcfilter", net->proc_net);
2989 	remove_proc_entry("igmp", net->proc_net);
2990 	inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
2991 }
2992 
2993 static struct pernet_operations igmp_net_ops = {
2994 	.init = igmp_net_init,
2995 	.exit = igmp_net_exit,
2996 };
2997 #endif
2998 
2999 static int igmp_netdev_event(struct notifier_block *this,
3000 			     unsigned long event, void *ptr)
3001 {
3002 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3003 	struct in_device *in_dev;
3004 
3005 	switch (event) {
3006 	case NETDEV_RESEND_IGMP:
3007 		in_dev = __in_dev_get_rtnl(dev);
3008 		if (in_dev)
3009 			ip_mc_rejoin_groups(in_dev);
3010 		break;
3011 	default:
3012 		break;
3013 	}
3014 	return NOTIFY_DONE;
3015 }
3016 
3017 static struct notifier_block igmp_notifier = {
3018 	.notifier_call = igmp_netdev_event,
3019 };
3020 
3021 int __init igmp_mc_init(void)
3022 {
3023 #if defined(CONFIG_PROC_FS)
3024 	int err;
3025 
3026 	err = register_pernet_subsys(&igmp_net_ops);
3027 	if (err)
3028 		return err;
3029 	err = register_netdevice_notifier(&igmp_notifier);
3030 	if (err)
3031 		goto reg_notif_fail;
3032 	return 0;
3033 
3034 reg_notif_fail:
3035 	unregister_pernet_subsys(&igmp_net_ops);
3036 	return err;
3037 #else
3038 	return register_netdevice_notifier(&igmp_notifier);
3039 #endif
3040 }
3041