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