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