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