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