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