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