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