• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <asm/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 
unsolicited_report_interval(struct in_device * in_dev)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 
ip_ma_put(struct ip_mc_list * im)175 static void ip_ma_put(struct ip_mc_list *im)
176 {
177 	if (atomic_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 
igmp_stop_timer(struct ip_mc_list * im)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 		atomic_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 */
igmp_start_timer(struct ip_mc_list * im,int max_delay)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 		atomic_inc(&im->refcnt);
218 }
219 
igmp_gq_start_timer(struct in_device * in_dev)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 
igmp_ifc_start_timer(struct in_device * in_dev,int delay)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 
igmp_mod_timer(struct ip_mc_list * im,int max_delay)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 		atomic_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 
is_in(struct ip_mc_list * pmc,struct ip_sf_list * psf,int type,int gdeleted,int sdeleted)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
igmp_scount(struct ip_mc_list * pmc,int type,int gdeleted,int sdeleted)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 */
igmpv3_get_srcaddr(struct net_device * dev,const struct flowi4 * fl4)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 
igmpv3_newpack(struct net_device * dev,unsigned int mtu)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 
igmpv3_sendpack(struct sk_buff * skb)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 
grec_size(struct ip_mc_list * pmc,int type,int gdel,int sdel)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 
add_grhead(struct sk_buff * skb,struct ip_mc_list * pmc,int type,struct igmpv3_grec ** ppgr,unsigned int mtu)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 = (struct igmpv3_grec *)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 
add_grec(struct sk_buff * skb,struct ip_mc_list * pmc,int type,int gdeleted,int sdeleted)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 = (__be32 *)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 
igmpv3_send_report(struct in_device * in_dev,struct ip_mc_list * pmc)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  */
igmpv3_clear_zeros(struct ip_sf_list ** ppsf)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 
igmpv3_send_cr(struct in_device * in_dev)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 
igmp_send_report(struct in_device * in_dev,struct ip_mc_list * pmc,int type)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 = (struct igmphdr *)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 
igmp_gq_timer_expire(unsigned long data)783 static void igmp_gq_timer_expire(unsigned long data)
784 {
785 	struct in_device *in_dev = (struct in_device *)data;
786 
787 	in_dev->mr_gq_running = 0;
788 	igmpv3_send_report(in_dev, NULL);
789 	in_dev_put(in_dev);
790 }
791 
igmp_ifc_timer_expire(unsigned long data)792 static void igmp_ifc_timer_expire(unsigned long data)
793 {
794 	struct in_device *in_dev = (struct in_device *)data;
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 
igmp_ifc_event(struct in_device * in_dev)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 
igmp_timer_expire(unsigned long data)815 static void igmp_timer_expire(unsigned long data)
816 {
817 	struct ip_mc_list *im = (struct ip_mc_list *)data;
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 */
igmp_xmarksources(struct ip_mc_list * pmc,int nsrcs,__be32 * srcs)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 
igmp_marksources(struct ip_mc_list * pmc,int nsrcs,__be32 * srcs)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 */
igmp_heard_report(struct in_device * in_dev,__be32 group)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 */
igmp_heard_query(struct in_device * in_dev,struct sk_buff * skb,int len)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 */
igmp_rcv(struct sk_buff * skb)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 in_device *in_dev = __in_dev_get_rcu(skb->dev);
1039 	int len = skb->len;
1040 	bool dropped = true;
1041 
1042 	if (!in_dev)
1043 		goto drop;
1044 
1045 	if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1046 		goto drop;
1047 
1048 	if (skb_checksum_simple_validate(skb))
1049 		goto drop;
1050 
1051 	ih = igmp_hdr(skb);
1052 	switch (ih->type) {
1053 	case IGMP_HOST_MEMBERSHIP_QUERY:
1054 		dropped = igmp_heard_query(in_dev, skb, len);
1055 		break;
1056 	case IGMP_HOST_MEMBERSHIP_REPORT:
1057 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
1058 		/* Is it our report looped back? */
1059 		if (rt_is_output_route(skb_rtable(skb)))
1060 			break;
1061 		/* don't rely on MC router hearing unicast reports */
1062 		if (skb->pkt_type == PACKET_MULTICAST ||
1063 		    skb->pkt_type == PACKET_BROADCAST)
1064 			dropped = igmp_heard_report(in_dev, ih->group);
1065 		break;
1066 	case IGMP_PIM:
1067 #ifdef CONFIG_IP_PIMSM_V1
1068 		return pim_rcv_v1(skb);
1069 #endif
1070 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1071 	case IGMP_DVMRP:
1072 	case IGMP_TRACE:
1073 	case IGMP_HOST_LEAVE_MESSAGE:
1074 	case IGMP_MTRACE:
1075 	case IGMP_MTRACE_RESP:
1076 		break;
1077 	default:
1078 		break;
1079 	}
1080 
1081 drop:
1082 	if (dropped)
1083 		kfree_skb(skb);
1084 	else
1085 		consume_skb(skb);
1086 	return 0;
1087 }
1088 
1089 #endif
1090 
1091 
1092 /*
1093  *	Add a filter to a device
1094  */
1095 
ip_mc_filter_add(struct in_device * in_dev,__be32 addr)1096 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1097 {
1098 	char buf[MAX_ADDR_LEN];
1099 	struct net_device *dev = in_dev->dev;
1100 
1101 	/* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1102 	   We will get multicast token leakage, when IFF_MULTICAST
1103 	   is changed. This check should be done in ndo_set_rx_mode
1104 	   routine. Something sort of:
1105 	   if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1106 	   --ANK
1107 	   */
1108 	if (arp_mc_map(addr, buf, dev, 0) == 0)
1109 		dev_mc_add(dev, buf);
1110 }
1111 
1112 /*
1113  *	Remove a filter from a device
1114  */
1115 
ip_mc_filter_del(struct in_device * in_dev,__be32 addr)1116 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1117 {
1118 	char buf[MAX_ADDR_LEN];
1119 	struct net_device *dev = in_dev->dev;
1120 
1121 	if (arp_mc_map(addr, buf, dev, 0) == 0)
1122 		dev_mc_del(dev, buf);
1123 }
1124 
1125 #ifdef CONFIG_IP_MULTICAST
1126 /*
1127  * deleted ip_mc_list manipulation
1128  */
igmpv3_add_delrec(struct in_device * in_dev,struct ip_mc_list * im)1129 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1130 {
1131 	struct ip_mc_list *pmc;
1132 	struct net *net = dev_net(in_dev->dev);
1133 
1134 	/* this is an "ip_mc_list" for convenience; only the fields below
1135 	 * are actually used. In particular, the refcnt and users are not
1136 	 * used for management of the delete list. Using the same structure
1137 	 * for deleted items allows change reports to use common code with
1138 	 * non-deleted or query-response MCA's.
1139 	 */
1140 	pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1141 	if (!pmc)
1142 		return;
1143 	spin_lock_init(&pmc->lock);
1144 	spin_lock_bh(&im->lock);
1145 	pmc->interface = im->interface;
1146 	in_dev_hold(in_dev);
1147 	pmc->multiaddr = im->multiaddr;
1148 	pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1149 	pmc->sfmode = im->sfmode;
1150 	if (pmc->sfmode == MCAST_INCLUDE) {
1151 		struct ip_sf_list *psf;
1152 
1153 		pmc->tomb = im->tomb;
1154 		pmc->sources = im->sources;
1155 		im->tomb = im->sources = NULL;
1156 		for (psf = pmc->sources; psf; psf = psf->sf_next)
1157 			psf->sf_crcount = pmc->crcount;
1158 	}
1159 	spin_unlock_bh(&im->lock);
1160 
1161 	spin_lock_bh(&in_dev->mc_tomb_lock);
1162 	pmc->next = in_dev->mc_tomb;
1163 	in_dev->mc_tomb = pmc;
1164 	spin_unlock_bh(&in_dev->mc_tomb_lock);
1165 }
1166 
1167 /*
1168  * restore ip_mc_list deleted records
1169  */
igmpv3_del_delrec(struct in_device * in_dev,struct ip_mc_list * im)1170 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1171 {
1172 	struct ip_mc_list *pmc, *pmc_prev;
1173 	struct ip_sf_list *psf;
1174 	struct net *net = dev_net(in_dev->dev);
1175 	__be32 multiaddr = im->multiaddr;
1176 
1177 	spin_lock_bh(&in_dev->mc_tomb_lock);
1178 	pmc_prev = NULL;
1179 	for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1180 		if (pmc->multiaddr == multiaddr)
1181 			break;
1182 		pmc_prev = pmc;
1183 	}
1184 	if (pmc) {
1185 		if (pmc_prev)
1186 			pmc_prev->next = pmc->next;
1187 		else
1188 			in_dev->mc_tomb = pmc->next;
1189 	}
1190 	spin_unlock_bh(&in_dev->mc_tomb_lock);
1191 
1192 	spin_lock_bh(&im->lock);
1193 	if (pmc) {
1194 		im->interface = pmc->interface;
1195 		im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1196 		im->sfmode = pmc->sfmode;
1197 		if (pmc->sfmode == MCAST_INCLUDE) {
1198 			im->tomb = pmc->tomb;
1199 			im->sources = pmc->sources;
1200 			for (psf = im->sources; psf; psf = psf->sf_next)
1201 				psf->sf_crcount = im->crcount;
1202 		}
1203 		in_dev_put(pmc->interface);
1204 		kfree(pmc);
1205 	}
1206 	spin_unlock_bh(&im->lock);
1207 }
1208 
1209 /*
1210  * flush ip_mc_list deleted records
1211  */
igmpv3_clear_delrec(struct in_device * in_dev)1212 static void igmpv3_clear_delrec(struct in_device *in_dev)
1213 {
1214 	struct ip_mc_list *pmc, *nextpmc;
1215 
1216 	spin_lock_bh(&in_dev->mc_tomb_lock);
1217 	pmc = in_dev->mc_tomb;
1218 	in_dev->mc_tomb = NULL;
1219 	spin_unlock_bh(&in_dev->mc_tomb_lock);
1220 
1221 	for (; pmc; pmc = nextpmc) {
1222 		nextpmc = pmc->next;
1223 		ip_mc_clear_src(pmc);
1224 		in_dev_put(pmc->interface);
1225 		kfree(pmc);
1226 	}
1227 	/* clear dead sources, too */
1228 	rcu_read_lock();
1229 	for_each_pmc_rcu(in_dev, pmc) {
1230 		struct ip_sf_list *psf, *psf_next;
1231 
1232 		spin_lock_bh(&pmc->lock);
1233 		psf = pmc->tomb;
1234 		pmc->tomb = NULL;
1235 		spin_unlock_bh(&pmc->lock);
1236 		for (; psf; psf = psf_next) {
1237 			psf_next = psf->sf_next;
1238 			kfree(psf);
1239 		}
1240 	}
1241 	rcu_read_unlock();
1242 }
1243 #endif
1244 
igmp_group_dropped(struct ip_mc_list * im)1245 static void igmp_group_dropped(struct ip_mc_list *im)
1246 {
1247 	struct in_device *in_dev = im->interface;
1248 #ifdef CONFIG_IP_MULTICAST
1249 	struct net *net = dev_net(in_dev->dev);
1250 	int reporter;
1251 #endif
1252 
1253 	if (im->loaded) {
1254 		im->loaded = 0;
1255 		ip_mc_filter_del(in_dev, im->multiaddr);
1256 	}
1257 
1258 #ifdef CONFIG_IP_MULTICAST
1259 	if (im->multiaddr == IGMP_ALL_HOSTS)
1260 		return;
1261 	if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1262 		return;
1263 
1264 	reporter = im->reporter;
1265 	igmp_stop_timer(im);
1266 
1267 	if (!in_dev->dead) {
1268 		if (IGMP_V1_SEEN(in_dev))
1269 			return;
1270 		if (IGMP_V2_SEEN(in_dev)) {
1271 			if (reporter)
1272 				igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1273 			return;
1274 		}
1275 		/* IGMPv3 */
1276 		igmpv3_add_delrec(in_dev, im);
1277 
1278 		igmp_ifc_event(in_dev);
1279 	}
1280 #endif
1281 }
1282 
igmp_group_added(struct ip_mc_list * im)1283 static void igmp_group_added(struct ip_mc_list *im)
1284 {
1285 	struct in_device *in_dev = im->interface;
1286 #ifdef CONFIG_IP_MULTICAST
1287 	struct net *net = dev_net(in_dev->dev);
1288 #endif
1289 
1290 	if (im->loaded == 0) {
1291 		im->loaded = 1;
1292 		ip_mc_filter_add(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 	if (in_dev->dead)
1302 		return;
1303 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1304 		spin_lock_bh(&im->lock);
1305 		igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1306 		spin_unlock_bh(&im->lock);
1307 		return;
1308 	}
1309 	/* else, v3 */
1310 
1311 	im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1312 	igmp_ifc_event(in_dev);
1313 #endif
1314 }
1315 
1316 
1317 /*
1318  *	Multicast list managers
1319  */
1320 
ip_mc_hash(const struct ip_mc_list * im)1321 static u32 ip_mc_hash(const struct ip_mc_list *im)
1322 {
1323 	return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1324 }
1325 
ip_mc_hash_add(struct in_device * in_dev,struct ip_mc_list * im)1326 static void ip_mc_hash_add(struct in_device *in_dev,
1327 			   struct ip_mc_list *im)
1328 {
1329 	struct ip_mc_list __rcu **mc_hash;
1330 	u32 hash;
1331 
1332 	mc_hash = rtnl_dereference(in_dev->mc_hash);
1333 	if (mc_hash) {
1334 		hash = ip_mc_hash(im);
1335 		im->next_hash = mc_hash[hash];
1336 		rcu_assign_pointer(mc_hash[hash], im);
1337 		return;
1338 	}
1339 
1340 	/* do not use a hash table for small number of items */
1341 	if (in_dev->mc_count < 4)
1342 		return;
1343 
1344 	mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1345 			  GFP_KERNEL);
1346 	if (!mc_hash)
1347 		return;
1348 
1349 	for_each_pmc_rtnl(in_dev, im) {
1350 		hash = ip_mc_hash(im);
1351 		im->next_hash = mc_hash[hash];
1352 		RCU_INIT_POINTER(mc_hash[hash], im);
1353 	}
1354 
1355 	rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1356 }
1357 
ip_mc_hash_remove(struct in_device * in_dev,struct ip_mc_list * im)1358 static void ip_mc_hash_remove(struct in_device *in_dev,
1359 			      struct ip_mc_list *im)
1360 {
1361 	struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1362 	struct ip_mc_list *aux;
1363 
1364 	if (!mc_hash)
1365 		return;
1366 	mc_hash += ip_mc_hash(im);
1367 	while ((aux = rtnl_dereference(*mc_hash)) != im)
1368 		mc_hash = &aux->next_hash;
1369 	*mc_hash = im->next_hash;
1370 }
1371 
1372 
1373 /*
1374  *	A socket has joined a multicast group on device dev.
1375  */
1376 
ip_mc_inc_group(struct in_device * in_dev,__be32 addr)1377 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1378 {
1379 	struct ip_mc_list *im;
1380 #ifdef CONFIG_IP_MULTICAST
1381 	struct net *net = dev_net(in_dev->dev);
1382 #endif
1383 
1384 	ASSERT_RTNL();
1385 
1386 	for_each_pmc_rtnl(in_dev, im) {
1387 		if (im->multiaddr == addr) {
1388 			im->users++;
1389 			ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1390 			goto out;
1391 		}
1392 	}
1393 
1394 	im = kzalloc(sizeof(*im), GFP_KERNEL);
1395 	if (!im)
1396 		goto out;
1397 
1398 	im->users = 1;
1399 	im->interface = in_dev;
1400 	in_dev_hold(in_dev);
1401 	im->multiaddr = addr;
1402 	/* initial mode is (EX, empty) */
1403 	im->sfmode = MCAST_EXCLUDE;
1404 	im->sfcount[MCAST_EXCLUDE] = 1;
1405 	atomic_set(&im->refcnt, 1);
1406 	spin_lock_init(&im->lock);
1407 #ifdef CONFIG_IP_MULTICAST
1408 	setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
1409 	im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1410 #endif
1411 
1412 	im->next_rcu = in_dev->mc_list;
1413 	in_dev->mc_count++;
1414 	rcu_assign_pointer(in_dev->mc_list, im);
1415 
1416 	ip_mc_hash_add(in_dev, im);
1417 
1418 #ifdef CONFIG_IP_MULTICAST
1419 	igmpv3_del_delrec(in_dev, im);
1420 #endif
1421 	igmp_group_added(im);
1422 	if (!in_dev->dead)
1423 		ip_rt_multicast_event(in_dev);
1424 out:
1425 	return;
1426 }
1427 EXPORT_SYMBOL(ip_mc_inc_group);
1428 
ip_mc_check_iphdr(struct sk_buff * skb)1429 static int ip_mc_check_iphdr(struct sk_buff *skb)
1430 {
1431 	const struct iphdr *iph;
1432 	unsigned int len;
1433 	unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1434 
1435 	if (!pskb_may_pull(skb, offset))
1436 		return -EINVAL;
1437 
1438 	iph = ip_hdr(skb);
1439 
1440 	if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1441 		return -EINVAL;
1442 
1443 	offset += ip_hdrlen(skb) - sizeof(*iph);
1444 
1445 	if (!pskb_may_pull(skb, offset))
1446 		return -EINVAL;
1447 
1448 	iph = ip_hdr(skb);
1449 
1450 	if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1451 		return -EINVAL;
1452 
1453 	len = skb_network_offset(skb) + ntohs(iph->tot_len);
1454 	if (skb->len < len || len < offset)
1455 		return -EINVAL;
1456 
1457 	skb_set_transport_header(skb, offset);
1458 
1459 	return 0;
1460 }
1461 
ip_mc_check_igmp_reportv3(struct sk_buff * skb)1462 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1463 {
1464 	unsigned int len = skb_transport_offset(skb);
1465 
1466 	len += sizeof(struct igmpv3_report);
1467 
1468 	return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1469 }
1470 
ip_mc_check_igmp_query(struct sk_buff * skb)1471 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1472 {
1473 	unsigned int len = skb_transport_offset(skb);
1474 
1475 	len += sizeof(struct igmphdr);
1476 	if (skb->len < len)
1477 		return -EINVAL;
1478 
1479 	/* IGMPv{1,2}? */
1480 	if (skb->len != len) {
1481 		/* or IGMPv3? */
1482 		len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1483 		if (skb->len < len || !pskb_may_pull(skb, len))
1484 			return -EINVAL;
1485 	}
1486 
1487 	/* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1488 	 * all-systems destination addresses (224.0.0.1) for general queries
1489 	 */
1490 	if (!igmp_hdr(skb)->group &&
1491 	    ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1492 		return -EINVAL;
1493 
1494 	return 0;
1495 }
1496 
ip_mc_check_igmp_msg(struct sk_buff * skb)1497 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1498 {
1499 	switch (igmp_hdr(skb)->type) {
1500 	case IGMP_HOST_LEAVE_MESSAGE:
1501 	case IGMP_HOST_MEMBERSHIP_REPORT:
1502 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
1503 		/* fall through */
1504 		return 0;
1505 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1506 		return ip_mc_check_igmp_reportv3(skb);
1507 	case IGMP_HOST_MEMBERSHIP_QUERY:
1508 		return ip_mc_check_igmp_query(skb);
1509 	default:
1510 		return -ENOMSG;
1511 	}
1512 }
1513 
ip_mc_validate_checksum(struct sk_buff * skb)1514 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1515 {
1516 	return skb_checksum_simple_validate(skb);
1517 }
1518 
__ip_mc_check_igmp(struct sk_buff * skb,struct sk_buff ** skb_trimmed)1519 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1520 
1521 {
1522 	struct sk_buff *skb_chk;
1523 	unsigned int transport_len;
1524 	unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1525 	int ret = -EINVAL;
1526 
1527 	transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1528 
1529 	skb_chk = skb_checksum_trimmed(skb, transport_len,
1530 				       ip_mc_validate_checksum);
1531 	if (!skb_chk)
1532 		goto err;
1533 
1534 	if (!pskb_may_pull(skb_chk, len))
1535 		goto err;
1536 
1537 	ret = ip_mc_check_igmp_msg(skb_chk);
1538 	if (ret)
1539 		goto err;
1540 
1541 	if (skb_trimmed)
1542 		*skb_trimmed = skb_chk;
1543 	/* free now unneeded clone */
1544 	else if (skb_chk != skb)
1545 		kfree_skb(skb_chk);
1546 
1547 	ret = 0;
1548 
1549 err:
1550 	if (ret && skb_chk && skb_chk != skb)
1551 		kfree_skb(skb_chk);
1552 
1553 	return ret;
1554 }
1555 
1556 /**
1557  * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1558  * @skb: the skb to validate
1559  * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1560  *
1561  * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1562  * skb transport header accordingly and returns zero.
1563  *
1564  * -EINVAL: A broken packet was detected, i.e. it violates some internet
1565  *  standard
1566  * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1567  * -ENOMEM: A memory allocation failure happened.
1568  *
1569  * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1570  * to NULL): After parsing an IGMP packet successfully it will point to
1571  * an skb which has its tail aligned to the IP packet end. This might
1572  * either be the originally provided skb or a trimmed, cloned version if
1573  * the skb frame had data beyond the IP packet. A cloned skb allows us
1574  * to leave the original skb and its full frame unchanged (which might be
1575  * desirable for layer 2 frame jugglers).
1576  *
1577  * Caller needs to set the skb network header and free any returned skb if it
1578  * differs from the provided skb.
1579  */
ip_mc_check_igmp(struct sk_buff * skb,struct sk_buff ** skb_trimmed)1580 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1581 {
1582 	int ret = ip_mc_check_iphdr(skb);
1583 
1584 	if (ret < 0)
1585 		return ret;
1586 
1587 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1588 		return -ENOMSG;
1589 
1590 	return __ip_mc_check_igmp(skb, skb_trimmed);
1591 }
1592 EXPORT_SYMBOL(ip_mc_check_igmp);
1593 
1594 /*
1595  *	Resend IGMP JOIN report; used by netdev notifier.
1596  */
ip_mc_rejoin_groups(struct in_device * in_dev)1597 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1598 {
1599 #ifdef CONFIG_IP_MULTICAST
1600 	struct ip_mc_list *im;
1601 	int type;
1602 	struct net *net = dev_net(in_dev->dev);
1603 
1604 	ASSERT_RTNL();
1605 
1606 	for_each_pmc_rtnl(in_dev, im) {
1607 		if (im->multiaddr == IGMP_ALL_HOSTS)
1608 			continue;
1609 		if (ipv4_is_local_multicast(im->multiaddr) &&
1610 		    !net->ipv4.sysctl_igmp_llm_reports)
1611 			continue;
1612 
1613 		/* a failover is happening and switches
1614 		 * must be notified immediately
1615 		 */
1616 		if (IGMP_V1_SEEN(in_dev))
1617 			type = IGMP_HOST_MEMBERSHIP_REPORT;
1618 		else if (IGMP_V2_SEEN(in_dev))
1619 			type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1620 		else
1621 			type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1622 		igmp_send_report(in_dev, im, type);
1623 	}
1624 #endif
1625 }
1626 
1627 /*
1628  *	A socket has left a multicast group on device dev
1629  */
1630 
ip_mc_dec_group(struct in_device * in_dev,__be32 addr)1631 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1632 {
1633 	struct ip_mc_list *i;
1634 	struct ip_mc_list __rcu **ip;
1635 
1636 	ASSERT_RTNL();
1637 
1638 	for (ip = &in_dev->mc_list;
1639 	     (i = rtnl_dereference(*ip)) != NULL;
1640 	     ip = &i->next_rcu) {
1641 		if (i->multiaddr == addr) {
1642 			if (--i->users == 0) {
1643 				ip_mc_hash_remove(in_dev, i);
1644 				*ip = i->next_rcu;
1645 				in_dev->mc_count--;
1646 				igmp_group_dropped(i);
1647 				ip_mc_clear_src(i);
1648 
1649 				if (!in_dev->dead)
1650 					ip_rt_multicast_event(in_dev);
1651 
1652 				ip_ma_put(i);
1653 				return;
1654 			}
1655 			break;
1656 		}
1657 	}
1658 }
1659 EXPORT_SYMBOL(ip_mc_dec_group);
1660 
1661 /* Device changing type */
1662 
ip_mc_unmap(struct in_device * in_dev)1663 void ip_mc_unmap(struct in_device *in_dev)
1664 {
1665 	struct ip_mc_list *pmc;
1666 
1667 	ASSERT_RTNL();
1668 
1669 	for_each_pmc_rtnl(in_dev, pmc)
1670 		igmp_group_dropped(pmc);
1671 }
1672 
ip_mc_remap(struct in_device * in_dev)1673 void ip_mc_remap(struct in_device *in_dev)
1674 {
1675 	struct ip_mc_list *pmc;
1676 
1677 	ASSERT_RTNL();
1678 
1679 	for_each_pmc_rtnl(in_dev, pmc) {
1680 #ifdef CONFIG_IP_MULTICAST
1681 		igmpv3_del_delrec(in_dev, pmc);
1682 #endif
1683 		igmp_group_added(pmc);
1684 	}
1685 }
1686 
1687 /* Device going down */
1688 
ip_mc_down(struct in_device * in_dev)1689 void ip_mc_down(struct in_device *in_dev)
1690 {
1691 	struct ip_mc_list *pmc;
1692 
1693 	ASSERT_RTNL();
1694 
1695 	for_each_pmc_rtnl(in_dev, pmc)
1696 		igmp_group_dropped(pmc);
1697 
1698 #ifdef CONFIG_IP_MULTICAST
1699 	in_dev->mr_ifc_count = 0;
1700 	if (del_timer(&in_dev->mr_ifc_timer))
1701 		__in_dev_put(in_dev);
1702 	in_dev->mr_gq_running = 0;
1703 	if (del_timer(&in_dev->mr_gq_timer))
1704 		__in_dev_put(in_dev);
1705 #endif
1706 
1707 	ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1708 }
1709 
ip_mc_init_dev(struct in_device * in_dev)1710 void ip_mc_init_dev(struct in_device *in_dev)
1711 {
1712 #ifdef CONFIG_IP_MULTICAST
1713 	struct net *net = dev_net(in_dev->dev);
1714 #endif
1715 	ASSERT_RTNL();
1716 
1717 #ifdef CONFIG_IP_MULTICAST
1718 	setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1719 			(unsigned long)in_dev);
1720 	setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1721 			(unsigned long)in_dev);
1722 	in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1723 #endif
1724 
1725 	spin_lock_init(&in_dev->mc_tomb_lock);
1726 }
1727 
1728 /* Device going up */
1729 
ip_mc_up(struct in_device * in_dev)1730 void ip_mc_up(struct in_device *in_dev)
1731 {
1732 	struct ip_mc_list *pmc;
1733 #ifdef CONFIG_IP_MULTICAST
1734 	struct net *net = dev_net(in_dev->dev);
1735 #endif
1736 
1737 	ASSERT_RTNL();
1738 
1739 #ifdef CONFIG_IP_MULTICAST
1740 	in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1741 #endif
1742 	ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1743 
1744 	for_each_pmc_rtnl(in_dev, pmc) {
1745 #ifdef CONFIG_IP_MULTICAST
1746 		igmpv3_del_delrec(in_dev, pmc);
1747 #endif
1748 		igmp_group_added(pmc);
1749 	}
1750 }
1751 
1752 /*
1753  *	Device is about to be destroyed: clean up.
1754  */
1755 
ip_mc_destroy_dev(struct in_device * in_dev)1756 void ip_mc_destroy_dev(struct in_device *in_dev)
1757 {
1758 	struct ip_mc_list *i;
1759 
1760 	ASSERT_RTNL();
1761 
1762 	/* Deactivate timers */
1763 	ip_mc_down(in_dev);
1764 #ifdef CONFIG_IP_MULTICAST
1765 	igmpv3_clear_delrec(in_dev);
1766 #endif
1767 
1768 	while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1769 		in_dev->mc_list = i->next_rcu;
1770 		in_dev->mc_count--;
1771 		ip_ma_put(i);
1772 	}
1773 }
1774 
1775 /* RTNL is locked */
ip_mc_find_dev(struct net * net,struct ip_mreqn * imr)1776 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1777 {
1778 	struct net_device *dev = NULL;
1779 	struct in_device *idev = NULL;
1780 
1781 	if (imr->imr_ifindex) {
1782 		idev = inetdev_by_index(net, imr->imr_ifindex);
1783 		return idev;
1784 	}
1785 	if (imr->imr_address.s_addr) {
1786 		dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1787 		if (!dev)
1788 			return NULL;
1789 	}
1790 
1791 	if (!dev) {
1792 		struct rtable *rt = ip_route_output(net,
1793 						    imr->imr_multiaddr.s_addr,
1794 						    0, 0, 0);
1795 		if (!IS_ERR(rt)) {
1796 			dev = rt->dst.dev;
1797 			ip_rt_put(rt);
1798 		}
1799 	}
1800 	if (dev) {
1801 		imr->imr_ifindex = dev->ifindex;
1802 		idev = __in_dev_get_rtnl(dev);
1803 	}
1804 	return idev;
1805 }
1806 
1807 /*
1808  *	Join a socket to a group
1809  */
1810 
ip_mc_del1_src(struct ip_mc_list * pmc,int sfmode,__be32 * psfsrc)1811 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1812 	__be32 *psfsrc)
1813 {
1814 	struct ip_sf_list *psf, *psf_prev;
1815 	int rv = 0;
1816 
1817 	psf_prev = NULL;
1818 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
1819 		if (psf->sf_inaddr == *psfsrc)
1820 			break;
1821 		psf_prev = psf;
1822 	}
1823 	if (!psf || psf->sf_count[sfmode] == 0) {
1824 		/* source filter not found, or count wrong =>  bug */
1825 		return -ESRCH;
1826 	}
1827 	psf->sf_count[sfmode]--;
1828 	if (psf->sf_count[sfmode] == 0) {
1829 		ip_rt_multicast_event(pmc->interface);
1830 	}
1831 	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1832 #ifdef CONFIG_IP_MULTICAST
1833 		struct in_device *in_dev = pmc->interface;
1834 		struct net *net = dev_net(in_dev->dev);
1835 #endif
1836 
1837 		/* no more filters for this source */
1838 		if (psf_prev)
1839 			psf_prev->sf_next = psf->sf_next;
1840 		else
1841 			pmc->sources = psf->sf_next;
1842 #ifdef CONFIG_IP_MULTICAST
1843 		if (psf->sf_oldin &&
1844 		    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1845 			psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1846 			psf->sf_next = pmc->tomb;
1847 			pmc->tomb = psf;
1848 			rv = 1;
1849 		} else
1850 #endif
1851 			kfree(psf);
1852 	}
1853 	return rv;
1854 }
1855 
1856 #ifndef CONFIG_IP_MULTICAST
1857 #define igmp_ifc_event(x)	do { } while (0)
1858 #endif
1859 
ip_mc_del_src(struct in_device * in_dev,__be32 * pmca,int sfmode,int sfcount,__be32 * psfsrc,int delta)1860 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1861 			 int sfcount, __be32 *psfsrc, int delta)
1862 {
1863 	struct ip_mc_list *pmc;
1864 	int	changerec = 0;
1865 	int	i, err;
1866 
1867 	if (!in_dev)
1868 		return -ENODEV;
1869 	rcu_read_lock();
1870 	for_each_pmc_rcu(in_dev, pmc) {
1871 		if (*pmca == pmc->multiaddr)
1872 			break;
1873 	}
1874 	if (!pmc) {
1875 		/* MCA not found?? bug */
1876 		rcu_read_unlock();
1877 		return -ESRCH;
1878 	}
1879 	spin_lock_bh(&pmc->lock);
1880 	rcu_read_unlock();
1881 #ifdef CONFIG_IP_MULTICAST
1882 	sf_markstate(pmc);
1883 #endif
1884 	if (!delta) {
1885 		err = -EINVAL;
1886 		if (!pmc->sfcount[sfmode])
1887 			goto out_unlock;
1888 		pmc->sfcount[sfmode]--;
1889 	}
1890 	err = 0;
1891 	for (i = 0; i < sfcount; i++) {
1892 		int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1893 
1894 		changerec |= rv > 0;
1895 		if (!err && rv < 0)
1896 			err = rv;
1897 	}
1898 	if (pmc->sfmode == MCAST_EXCLUDE &&
1899 	    pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1900 	    pmc->sfcount[MCAST_INCLUDE]) {
1901 #ifdef CONFIG_IP_MULTICAST
1902 		struct ip_sf_list *psf;
1903 		struct net *net = dev_net(in_dev->dev);
1904 #endif
1905 
1906 		/* filter mode change */
1907 		pmc->sfmode = MCAST_INCLUDE;
1908 #ifdef CONFIG_IP_MULTICAST
1909 		pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1910 		in_dev->mr_ifc_count = pmc->crcount;
1911 		for (psf = pmc->sources; psf; psf = psf->sf_next)
1912 			psf->sf_crcount = 0;
1913 		igmp_ifc_event(pmc->interface);
1914 	} else if (sf_setstate(pmc) || changerec) {
1915 		igmp_ifc_event(pmc->interface);
1916 #endif
1917 	}
1918 out_unlock:
1919 	spin_unlock_bh(&pmc->lock);
1920 	return err;
1921 }
1922 
1923 /*
1924  * Add multicast single-source filter to the interface list
1925  */
ip_mc_add1_src(struct ip_mc_list * pmc,int sfmode,__be32 * psfsrc)1926 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1927 	__be32 *psfsrc)
1928 {
1929 	struct ip_sf_list *psf, *psf_prev;
1930 
1931 	psf_prev = NULL;
1932 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
1933 		if (psf->sf_inaddr == *psfsrc)
1934 			break;
1935 		psf_prev = psf;
1936 	}
1937 	if (!psf) {
1938 		psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1939 		if (!psf)
1940 			return -ENOBUFS;
1941 		psf->sf_inaddr = *psfsrc;
1942 		if (psf_prev) {
1943 			psf_prev->sf_next = psf;
1944 		} else
1945 			pmc->sources = psf;
1946 	}
1947 	psf->sf_count[sfmode]++;
1948 	if (psf->sf_count[sfmode] == 1) {
1949 		ip_rt_multicast_event(pmc->interface);
1950 	}
1951 	return 0;
1952 }
1953 
1954 #ifdef CONFIG_IP_MULTICAST
sf_markstate(struct ip_mc_list * pmc)1955 static void sf_markstate(struct ip_mc_list *pmc)
1956 {
1957 	struct ip_sf_list *psf;
1958 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1959 
1960 	for (psf = pmc->sources; psf; psf = psf->sf_next)
1961 		if (pmc->sfcount[MCAST_EXCLUDE]) {
1962 			psf->sf_oldin = mca_xcount ==
1963 				psf->sf_count[MCAST_EXCLUDE] &&
1964 				!psf->sf_count[MCAST_INCLUDE];
1965 		} else
1966 			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1967 }
1968 
sf_setstate(struct ip_mc_list * pmc)1969 static int sf_setstate(struct ip_mc_list *pmc)
1970 {
1971 	struct ip_sf_list *psf, *dpsf;
1972 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1973 	int qrv = pmc->interface->mr_qrv;
1974 	int new_in, rv;
1975 
1976 	rv = 0;
1977 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
1978 		if (pmc->sfcount[MCAST_EXCLUDE]) {
1979 			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1980 				!psf->sf_count[MCAST_INCLUDE];
1981 		} else
1982 			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1983 		if (new_in) {
1984 			if (!psf->sf_oldin) {
1985 				struct ip_sf_list *prev = NULL;
1986 
1987 				for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
1988 					if (dpsf->sf_inaddr == psf->sf_inaddr)
1989 						break;
1990 					prev = dpsf;
1991 				}
1992 				if (dpsf) {
1993 					if (prev)
1994 						prev->sf_next = dpsf->sf_next;
1995 					else
1996 						pmc->tomb = dpsf->sf_next;
1997 					kfree(dpsf);
1998 				}
1999 				psf->sf_crcount = qrv;
2000 				rv++;
2001 			}
2002 		} else if (psf->sf_oldin) {
2003 
2004 			psf->sf_crcount = 0;
2005 			/*
2006 			 * add or update "delete" records if an active filter
2007 			 * is now inactive
2008 			 */
2009 			for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2010 				if (dpsf->sf_inaddr == psf->sf_inaddr)
2011 					break;
2012 			if (!dpsf) {
2013 				dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2014 				if (!dpsf)
2015 					continue;
2016 				*dpsf = *psf;
2017 				/* pmc->lock held by callers */
2018 				dpsf->sf_next = pmc->tomb;
2019 				pmc->tomb = dpsf;
2020 			}
2021 			dpsf->sf_crcount = qrv;
2022 			rv++;
2023 		}
2024 	}
2025 	return rv;
2026 }
2027 #endif
2028 
2029 /*
2030  * Add multicast source filter list to the interface list
2031  */
ip_mc_add_src(struct in_device * in_dev,__be32 * pmca,int sfmode,int sfcount,__be32 * psfsrc,int delta)2032 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2033 			 int sfcount, __be32 *psfsrc, int delta)
2034 {
2035 	struct ip_mc_list *pmc;
2036 	int	isexclude;
2037 	int	i, err;
2038 
2039 	if (!in_dev)
2040 		return -ENODEV;
2041 	rcu_read_lock();
2042 	for_each_pmc_rcu(in_dev, pmc) {
2043 		if (*pmca == pmc->multiaddr)
2044 			break;
2045 	}
2046 	if (!pmc) {
2047 		/* MCA not found?? bug */
2048 		rcu_read_unlock();
2049 		return -ESRCH;
2050 	}
2051 	spin_lock_bh(&pmc->lock);
2052 	rcu_read_unlock();
2053 
2054 #ifdef CONFIG_IP_MULTICAST
2055 	sf_markstate(pmc);
2056 #endif
2057 	isexclude = pmc->sfmode == MCAST_EXCLUDE;
2058 	if (!delta)
2059 		pmc->sfcount[sfmode]++;
2060 	err = 0;
2061 	for (i = 0; i < sfcount; i++) {
2062 		err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2063 		if (err)
2064 			break;
2065 	}
2066 	if (err) {
2067 		int j;
2068 
2069 		if (!delta)
2070 			pmc->sfcount[sfmode]--;
2071 		for (j = 0; j < i; j++)
2072 			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2073 	} else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2074 #ifdef CONFIG_IP_MULTICAST
2075 		struct ip_sf_list *psf;
2076 		struct net *net = dev_net(pmc->interface->dev);
2077 		in_dev = pmc->interface;
2078 #endif
2079 
2080 		/* filter mode change */
2081 		if (pmc->sfcount[MCAST_EXCLUDE])
2082 			pmc->sfmode = MCAST_EXCLUDE;
2083 		else if (pmc->sfcount[MCAST_INCLUDE])
2084 			pmc->sfmode = MCAST_INCLUDE;
2085 #ifdef CONFIG_IP_MULTICAST
2086 		/* else no filters; keep old mode for reports */
2087 
2088 		pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2089 		in_dev->mr_ifc_count = pmc->crcount;
2090 		for (psf = pmc->sources; psf; psf = psf->sf_next)
2091 			psf->sf_crcount = 0;
2092 		igmp_ifc_event(in_dev);
2093 	} else if (sf_setstate(pmc)) {
2094 		igmp_ifc_event(in_dev);
2095 #endif
2096 	}
2097 	spin_unlock_bh(&pmc->lock);
2098 	return err;
2099 }
2100 
ip_mc_clear_src(struct ip_mc_list * pmc)2101 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2102 {
2103 	struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
2104 
2105 	spin_lock_bh(&pmc->lock);
2106 	tomb = pmc->tomb;
2107 	pmc->tomb = NULL;
2108 	sources = pmc->sources;
2109 	pmc->sources = NULL;
2110 	pmc->sfmode = MCAST_EXCLUDE;
2111 	pmc->sfcount[MCAST_INCLUDE] = 0;
2112 	pmc->sfcount[MCAST_EXCLUDE] = 1;
2113 	spin_unlock_bh(&pmc->lock);
2114 
2115 	for (psf = tomb; psf; psf = nextpsf) {
2116 		nextpsf = psf->sf_next;
2117 		kfree(psf);
2118 	}
2119 	for (psf = sources; psf; psf = nextpsf) {
2120 		nextpsf = psf->sf_next;
2121 		kfree(psf);
2122 	}
2123 }
2124 
2125 /* Join a multicast group
2126  */
2127 
ip_mc_join_group(struct sock * sk,struct ip_mreqn * imr)2128 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2129 {
2130 	__be32 addr = imr->imr_multiaddr.s_addr;
2131 	struct ip_mc_socklist *iml, *i;
2132 	struct in_device *in_dev;
2133 	struct inet_sock *inet = inet_sk(sk);
2134 	struct net *net = sock_net(sk);
2135 	int ifindex;
2136 	int count = 0;
2137 	int err;
2138 
2139 	ASSERT_RTNL();
2140 
2141 	if (!ipv4_is_multicast(addr))
2142 		return -EINVAL;
2143 
2144 	in_dev = ip_mc_find_dev(net, imr);
2145 
2146 	if (!in_dev) {
2147 		err = -ENODEV;
2148 		goto done;
2149 	}
2150 
2151 	err = -EADDRINUSE;
2152 	ifindex = imr->imr_ifindex;
2153 	for_each_pmc_rtnl(inet, i) {
2154 		if (i->multi.imr_multiaddr.s_addr == addr &&
2155 		    i->multi.imr_ifindex == ifindex)
2156 			goto done;
2157 		count++;
2158 	}
2159 	err = -ENOBUFS;
2160 	if (count >= net->ipv4.sysctl_igmp_max_memberships)
2161 		goto done;
2162 	iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2163 	if (!iml)
2164 		goto done;
2165 
2166 	memcpy(&iml->multi, imr, sizeof(*imr));
2167 	iml->next_rcu = inet->mc_list;
2168 	iml->sflist = NULL;
2169 	iml->sfmode = MCAST_EXCLUDE;
2170 	rcu_assign_pointer(inet->mc_list, iml);
2171 	ip_mc_inc_group(in_dev, addr);
2172 	err = 0;
2173 done:
2174 	return err;
2175 }
2176 EXPORT_SYMBOL(ip_mc_join_group);
2177 
ip_mc_leave_src(struct sock * sk,struct ip_mc_socklist * iml,struct in_device * in_dev)2178 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2179 			   struct in_device *in_dev)
2180 {
2181 	struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2182 	int err;
2183 
2184 	if (!psf) {
2185 		/* any-source empty exclude case */
2186 		return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2187 			iml->sfmode, 0, NULL, 0);
2188 	}
2189 	err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2190 			iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2191 	RCU_INIT_POINTER(iml->sflist, NULL);
2192 	/* decrease mem now to avoid the memleak warning */
2193 	atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2194 	kfree_rcu(psf, rcu);
2195 	return err;
2196 }
2197 
ip_mc_leave_group(struct sock * sk,struct ip_mreqn * imr)2198 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2199 {
2200 	struct inet_sock *inet = inet_sk(sk);
2201 	struct ip_mc_socklist *iml;
2202 	struct ip_mc_socklist __rcu **imlp;
2203 	struct in_device *in_dev;
2204 	struct net *net = sock_net(sk);
2205 	__be32 group = imr->imr_multiaddr.s_addr;
2206 	u32 ifindex;
2207 	int ret = -EADDRNOTAVAIL;
2208 
2209 	ASSERT_RTNL();
2210 
2211 	in_dev = ip_mc_find_dev(net, imr);
2212 	if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2213 		ret = -ENODEV;
2214 		goto out;
2215 	}
2216 	ifindex = imr->imr_ifindex;
2217 	for (imlp = &inet->mc_list;
2218 	     (iml = rtnl_dereference(*imlp)) != NULL;
2219 	     imlp = &iml->next_rcu) {
2220 		if (iml->multi.imr_multiaddr.s_addr != group)
2221 			continue;
2222 		if (ifindex) {
2223 			if (iml->multi.imr_ifindex != ifindex)
2224 				continue;
2225 		} else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2226 				iml->multi.imr_address.s_addr)
2227 			continue;
2228 
2229 		(void) ip_mc_leave_src(sk, iml, in_dev);
2230 
2231 		*imlp = iml->next_rcu;
2232 
2233 		if (in_dev)
2234 			ip_mc_dec_group(in_dev, group);
2235 
2236 		/* decrease mem now to avoid the memleak warning */
2237 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2238 		kfree_rcu(iml, rcu);
2239 		return 0;
2240 	}
2241 out:
2242 	return ret;
2243 }
2244 EXPORT_SYMBOL(ip_mc_leave_group);
2245 
ip_mc_source(int add,int omode,struct sock * sk,struct ip_mreq_source * mreqs,int ifindex)2246 int ip_mc_source(int add, int omode, struct sock *sk, struct
2247 	ip_mreq_source *mreqs, int ifindex)
2248 {
2249 	int err;
2250 	struct ip_mreqn imr;
2251 	__be32 addr = mreqs->imr_multiaddr;
2252 	struct ip_mc_socklist *pmc;
2253 	struct in_device *in_dev = NULL;
2254 	struct inet_sock *inet = inet_sk(sk);
2255 	struct ip_sf_socklist *psl;
2256 	struct net *net = sock_net(sk);
2257 	int leavegroup = 0;
2258 	int i, j, rv;
2259 
2260 	if (!ipv4_is_multicast(addr))
2261 		return -EINVAL;
2262 
2263 	ASSERT_RTNL();
2264 
2265 	imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2266 	imr.imr_address.s_addr = mreqs->imr_interface;
2267 	imr.imr_ifindex = ifindex;
2268 	in_dev = ip_mc_find_dev(net, &imr);
2269 
2270 	if (!in_dev) {
2271 		err = -ENODEV;
2272 		goto done;
2273 	}
2274 	err = -EADDRNOTAVAIL;
2275 
2276 	for_each_pmc_rtnl(inet, pmc) {
2277 		if ((pmc->multi.imr_multiaddr.s_addr ==
2278 		     imr.imr_multiaddr.s_addr) &&
2279 		    (pmc->multi.imr_ifindex == imr.imr_ifindex))
2280 			break;
2281 	}
2282 	if (!pmc) {		/* must have a prior join */
2283 		err = -EINVAL;
2284 		goto done;
2285 	}
2286 	/* if a source filter was set, must be the same mode as before */
2287 	if (pmc->sflist) {
2288 		if (pmc->sfmode != omode) {
2289 			err = -EINVAL;
2290 			goto done;
2291 		}
2292 	} else if (pmc->sfmode != omode) {
2293 		/* allow mode switches for empty-set filters */
2294 		ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2295 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2296 			NULL, 0);
2297 		pmc->sfmode = omode;
2298 	}
2299 
2300 	psl = rtnl_dereference(pmc->sflist);
2301 	if (!add) {
2302 		if (!psl)
2303 			goto done;	/* err = -EADDRNOTAVAIL */
2304 		rv = !0;
2305 		for (i = 0; i < psl->sl_count; i++) {
2306 			rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2307 				sizeof(__be32));
2308 			if (rv == 0)
2309 				break;
2310 		}
2311 		if (rv)		/* source not found */
2312 			goto done;	/* err = -EADDRNOTAVAIL */
2313 
2314 		/* special case - (INCLUDE, empty) == LEAVE_GROUP */
2315 		if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2316 			leavegroup = 1;
2317 			goto done;
2318 		}
2319 
2320 		/* update the interface filter */
2321 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2322 			&mreqs->imr_sourceaddr, 1);
2323 
2324 		for (j = i+1; j < psl->sl_count; j++)
2325 			psl->sl_addr[j-1] = psl->sl_addr[j];
2326 		psl->sl_count--;
2327 		err = 0;
2328 		goto done;
2329 	}
2330 	/* else, add a new source to the filter */
2331 
2332 	if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2333 		err = -ENOBUFS;
2334 		goto done;
2335 	}
2336 	if (!psl || psl->sl_count == psl->sl_max) {
2337 		struct ip_sf_socklist *newpsl;
2338 		int count = IP_SFBLOCK;
2339 
2340 		if (psl)
2341 			count += psl->sl_max;
2342 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2343 		if (!newpsl) {
2344 			err = -ENOBUFS;
2345 			goto done;
2346 		}
2347 		newpsl->sl_max = count;
2348 		newpsl->sl_count = count - IP_SFBLOCK;
2349 		if (psl) {
2350 			for (i = 0; i < psl->sl_count; i++)
2351 				newpsl->sl_addr[i] = psl->sl_addr[i];
2352 			/* decrease mem now to avoid the memleak warning */
2353 			atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2354 			kfree_rcu(psl, rcu);
2355 		}
2356 		rcu_assign_pointer(pmc->sflist, newpsl);
2357 		psl = newpsl;
2358 	}
2359 	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
2360 	for (i = 0; i < psl->sl_count; i++) {
2361 		rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2362 			sizeof(__be32));
2363 		if (rv == 0)
2364 			break;
2365 	}
2366 	if (rv == 0)		/* address already there is an error */
2367 		goto done;
2368 	for (j = psl->sl_count-1; j >= i; j--)
2369 		psl->sl_addr[j+1] = psl->sl_addr[j];
2370 	psl->sl_addr[i] = mreqs->imr_sourceaddr;
2371 	psl->sl_count++;
2372 	err = 0;
2373 	/* update the interface list */
2374 	ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2375 		&mreqs->imr_sourceaddr, 1);
2376 done:
2377 	if (leavegroup)
2378 		err = ip_mc_leave_group(sk, &imr);
2379 	return err;
2380 }
2381 
ip_mc_msfilter(struct sock * sk,struct ip_msfilter * msf,int ifindex)2382 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2383 {
2384 	int err = 0;
2385 	struct ip_mreqn	imr;
2386 	__be32 addr = msf->imsf_multiaddr;
2387 	struct ip_mc_socklist *pmc;
2388 	struct in_device *in_dev;
2389 	struct inet_sock *inet = inet_sk(sk);
2390 	struct ip_sf_socklist *newpsl, *psl;
2391 	struct net *net = sock_net(sk);
2392 	int leavegroup = 0;
2393 
2394 	if (!ipv4_is_multicast(addr))
2395 		return -EINVAL;
2396 	if (msf->imsf_fmode != MCAST_INCLUDE &&
2397 	    msf->imsf_fmode != MCAST_EXCLUDE)
2398 		return -EINVAL;
2399 
2400 	ASSERT_RTNL();
2401 
2402 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2403 	imr.imr_address.s_addr = msf->imsf_interface;
2404 	imr.imr_ifindex = ifindex;
2405 	in_dev = ip_mc_find_dev(net, &imr);
2406 
2407 	if (!in_dev) {
2408 		err = -ENODEV;
2409 		goto done;
2410 	}
2411 
2412 	/* special case - (INCLUDE, empty) == LEAVE_GROUP */
2413 	if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2414 		leavegroup = 1;
2415 		goto done;
2416 	}
2417 
2418 	for_each_pmc_rtnl(inet, pmc) {
2419 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2420 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
2421 			break;
2422 	}
2423 	if (!pmc) {		/* must have a prior join */
2424 		err = -EINVAL;
2425 		goto done;
2426 	}
2427 	if (msf->imsf_numsrc) {
2428 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2429 							   GFP_KERNEL);
2430 		if (!newpsl) {
2431 			err = -ENOBUFS;
2432 			goto done;
2433 		}
2434 		newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2435 		memcpy(newpsl->sl_addr, msf->imsf_slist,
2436 			msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2437 		err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2438 			msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2439 		if (err) {
2440 			sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2441 			goto done;
2442 		}
2443 	} else {
2444 		newpsl = NULL;
2445 		(void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2446 				     msf->imsf_fmode, 0, NULL, 0);
2447 	}
2448 	psl = rtnl_dereference(pmc->sflist);
2449 	if (psl) {
2450 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2451 			psl->sl_count, psl->sl_addr, 0);
2452 		/* decrease mem now to avoid the memleak warning */
2453 		atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2454 		kfree_rcu(psl, rcu);
2455 	} else
2456 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2457 			0, NULL, 0);
2458 	rcu_assign_pointer(pmc->sflist, newpsl);
2459 	pmc->sfmode = msf->imsf_fmode;
2460 	err = 0;
2461 done:
2462 	if (leavegroup)
2463 		err = ip_mc_leave_group(sk, &imr);
2464 	return err;
2465 }
2466 
ip_mc_msfget(struct sock * sk,struct ip_msfilter * msf,struct ip_msfilter __user * optval,int __user * optlen)2467 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2468 	struct ip_msfilter __user *optval, int __user *optlen)
2469 {
2470 	int err, len, count, copycount;
2471 	struct ip_mreqn	imr;
2472 	__be32 addr = msf->imsf_multiaddr;
2473 	struct ip_mc_socklist *pmc;
2474 	struct in_device *in_dev;
2475 	struct inet_sock *inet = inet_sk(sk);
2476 	struct ip_sf_socklist *psl;
2477 	struct net *net = sock_net(sk);
2478 
2479 	ASSERT_RTNL();
2480 
2481 	if (!ipv4_is_multicast(addr))
2482 		return -EINVAL;
2483 
2484 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2485 	imr.imr_address.s_addr = msf->imsf_interface;
2486 	imr.imr_ifindex = 0;
2487 	in_dev = ip_mc_find_dev(net, &imr);
2488 
2489 	if (!in_dev) {
2490 		err = -ENODEV;
2491 		goto done;
2492 	}
2493 	err = -EADDRNOTAVAIL;
2494 
2495 	for_each_pmc_rtnl(inet, pmc) {
2496 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2497 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
2498 			break;
2499 	}
2500 	if (!pmc)		/* must have a prior join */
2501 		goto done;
2502 	msf->imsf_fmode = pmc->sfmode;
2503 	psl = rtnl_dereference(pmc->sflist);
2504 	if (!psl) {
2505 		len = 0;
2506 		count = 0;
2507 	} else {
2508 		count = psl->sl_count;
2509 	}
2510 	copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2511 	len = copycount * sizeof(psl->sl_addr[0]);
2512 	msf->imsf_numsrc = count;
2513 	if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2514 	    copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2515 		return -EFAULT;
2516 	}
2517 	if (len &&
2518 	    copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2519 		return -EFAULT;
2520 	return 0;
2521 done:
2522 	return err;
2523 }
2524 
ip_mc_gsfget(struct sock * sk,struct group_filter * gsf,struct group_filter __user * optval,int __user * optlen)2525 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2526 	struct group_filter __user *optval, int __user *optlen)
2527 {
2528 	int err, i, count, copycount;
2529 	struct sockaddr_in *psin;
2530 	__be32 addr;
2531 	struct ip_mc_socklist *pmc;
2532 	struct inet_sock *inet = inet_sk(sk);
2533 	struct ip_sf_socklist *psl;
2534 
2535 	ASSERT_RTNL();
2536 
2537 	psin = (struct sockaddr_in *)&gsf->gf_group;
2538 	if (psin->sin_family != AF_INET)
2539 		return -EINVAL;
2540 	addr = psin->sin_addr.s_addr;
2541 	if (!ipv4_is_multicast(addr))
2542 		return -EINVAL;
2543 
2544 	err = -EADDRNOTAVAIL;
2545 
2546 	for_each_pmc_rtnl(inet, pmc) {
2547 		if (pmc->multi.imr_multiaddr.s_addr == addr &&
2548 		    pmc->multi.imr_ifindex == gsf->gf_interface)
2549 			break;
2550 	}
2551 	if (!pmc)		/* must have a prior join */
2552 		goto done;
2553 	gsf->gf_fmode = pmc->sfmode;
2554 	psl = rtnl_dereference(pmc->sflist);
2555 	count = psl ? psl->sl_count : 0;
2556 	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2557 	gsf->gf_numsrc = count;
2558 	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2559 	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2560 		return -EFAULT;
2561 	}
2562 	for (i = 0; i < copycount; i++) {
2563 		struct sockaddr_storage ss;
2564 
2565 		psin = (struct sockaddr_in *)&ss;
2566 		memset(&ss, 0, sizeof(ss));
2567 		psin->sin_family = AF_INET;
2568 		psin->sin_addr.s_addr = psl->sl_addr[i];
2569 		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2570 			return -EFAULT;
2571 	}
2572 	return 0;
2573 done:
2574 	return err;
2575 }
2576 
2577 /*
2578  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2579  */
ip_mc_sf_allow(struct sock * sk,__be32 loc_addr,__be32 rmt_addr,int dif)2580 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2581 {
2582 	struct inet_sock *inet = inet_sk(sk);
2583 	struct ip_mc_socklist *pmc;
2584 	struct ip_sf_socklist *psl;
2585 	int i;
2586 	int ret;
2587 
2588 	ret = 1;
2589 	if (!ipv4_is_multicast(loc_addr))
2590 		goto out;
2591 
2592 	rcu_read_lock();
2593 	for_each_pmc_rcu(inet, pmc) {
2594 		if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2595 		    pmc->multi.imr_ifindex == dif)
2596 			break;
2597 	}
2598 	ret = inet->mc_all;
2599 	if (!pmc)
2600 		goto unlock;
2601 	psl = rcu_dereference(pmc->sflist);
2602 	ret = (pmc->sfmode == MCAST_EXCLUDE);
2603 	if (!psl)
2604 		goto unlock;
2605 
2606 	for (i = 0; i < psl->sl_count; i++) {
2607 		if (psl->sl_addr[i] == rmt_addr)
2608 			break;
2609 	}
2610 	ret = 0;
2611 	if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2612 		goto unlock;
2613 	if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2614 		goto unlock;
2615 	ret = 1;
2616 unlock:
2617 	rcu_read_unlock();
2618 out:
2619 	return ret;
2620 }
2621 
2622 /*
2623  *	A socket is closing.
2624  */
2625 
ip_mc_drop_socket(struct sock * sk)2626 void ip_mc_drop_socket(struct sock *sk)
2627 {
2628 	struct inet_sock *inet = inet_sk(sk);
2629 	struct ip_mc_socklist *iml;
2630 	struct net *net = sock_net(sk);
2631 
2632 	if (!inet->mc_list)
2633 		return;
2634 
2635 	rtnl_lock();
2636 	while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2637 		struct in_device *in_dev;
2638 
2639 		inet->mc_list = iml->next_rcu;
2640 		in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2641 		(void) ip_mc_leave_src(sk, iml, in_dev);
2642 		if (in_dev)
2643 			ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2644 		/* decrease mem now to avoid the memleak warning */
2645 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2646 		kfree_rcu(iml, rcu);
2647 	}
2648 	rtnl_unlock();
2649 }
2650 
2651 /* called with rcu_read_lock() */
ip_check_mc_rcu(struct in_device * in_dev,__be32 mc_addr,__be32 src_addr,u8 proto)2652 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2653 {
2654 	struct ip_mc_list *im;
2655 	struct ip_mc_list __rcu **mc_hash;
2656 	struct ip_sf_list *psf;
2657 	int rv = 0;
2658 
2659 	mc_hash = rcu_dereference(in_dev->mc_hash);
2660 	if (mc_hash) {
2661 		u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2662 
2663 		for (im = rcu_dereference(mc_hash[hash]);
2664 		     im != NULL;
2665 		     im = rcu_dereference(im->next_hash)) {
2666 			if (im->multiaddr == mc_addr)
2667 				break;
2668 		}
2669 	} else {
2670 		for_each_pmc_rcu(in_dev, im) {
2671 			if (im->multiaddr == mc_addr)
2672 				break;
2673 		}
2674 	}
2675 	if (im && proto == IPPROTO_IGMP) {
2676 		rv = 1;
2677 	} else if (im) {
2678 		if (src_addr) {
2679 			for (psf = im->sources; psf; psf = psf->sf_next) {
2680 				if (psf->sf_inaddr == src_addr)
2681 					break;
2682 			}
2683 			if (psf)
2684 				rv = psf->sf_count[MCAST_INCLUDE] ||
2685 					psf->sf_count[MCAST_EXCLUDE] !=
2686 					im->sfcount[MCAST_EXCLUDE];
2687 			else
2688 				rv = im->sfcount[MCAST_EXCLUDE] != 0;
2689 		} else
2690 			rv = 1; /* unspecified source; tentatively allow */
2691 	}
2692 	return rv;
2693 }
2694 
2695 #if defined(CONFIG_PROC_FS)
2696 struct igmp_mc_iter_state {
2697 	struct seq_net_private p;
2698 	struct net_device *dev;
2699 	struct in_device *in_dev;
2700 };
2701 
2702 #define	igmp_mc_seq_private(seq)	((struct igmp_mc_iter_state *)(seq)->private)
2703 
igmp_mc_get_first(struct seq_file * seq)2704 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2705 {
2706 	struct net *net = seq_file_net(seq);
2707 	struct ip_mc_list *im = NULL;
2708 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2709 
2710 	state->in_dev = NULL;
2711 	for_each_netdev_rcu(net, state->dev) {
2712 		struct in_device *in_dev;
2713 
2714 		in_dev = __in_dev_get_rcu(state->dev);
2715 		if (!in_dev)
2716 			continue;
2717 		im = rcu_dereference(in_dev->mc_list);
2718 		if (im) {
2719 			state->in_dev = in_dev;
2720 			break;
2721 		}
2722 	}
2723 	return im;
2724 }
2725 
igmp_mc_get_next(struct seq_file * seq,struct ip_mc_list * im)2726 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2727 {
2728 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2729 
2730 	im = rcu_dereference(im->next_rcu);
2731 	while (!im) {
2732 		state->dev = next_net_device_rcu(state->dev);
2733 		if (!state->dev) {
2734 			state->in_dev = NULL;
2735 			break;
2736 		}
2737 		state->in_dev = __in_dev_get_rcu(state->dev);
2738 		if (!state->in_dev)
2739 			continue;
2740 		im = rcu_dereference(state->in_dev->mc_list);
2741 	}
2742 	return im;
2743 }
2744 
igmp_mc_get_idx(struct seq_file * seq,loff_t pos)2745 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2746 {
2747 	struct ip_mc_list *im = igmp_mc_get_first(seq);
2748 	if (im)
2749 		while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2750 			--pos;
2751 	return pos ? NULL : im;
2752 }
2753 
igmp_mc_seq_start(struct seq_file * seq,loff_t * pos)2754 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2755 	__acquires(rcu)
2756 {
2757 	rcu_read_lock();
2758 	return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2759 }
2760 
igmp_mc_seq_next(struct seq_file * seq,void * v,loff_t * pos)2761 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2762 {
2763 	struct ip_mc_list *im;
2764 	if (v == SEQ_START_TOKEN)
2765 		im = igmp_mc_get_first(seq);
2766 	else
2767 		im = igmp_mc_get_next(seq, v);
2768 	++*pos;
2769 	return im;
2770 }
2771 
igmp_mc_seq_stop(struct seq_file * seq,void * v)2772 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2773 	__releases(rcu)
2774 {
2775 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2776 
2777 	state->in_dev = NULL;
2778 	state->dev = NULL;
2779 	rcu_read_unlock();
2780 }
2781 
igmp_mc_seq_show(struct seq_file * seq,void * v)2782 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2783 {
2784 	if (v == SEQ_START_TOKEN)
2785 		seq_puts(seq,
2786 			 "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2787 	else {
2788 		struct ip_mc_list *im = (struct ip_mc_list *)v;
2789 		struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2790 		char   *querier;
2791 		long delta;
2792 
2793 #ifdef CONFIG_IP_MULTICAST
2794 		querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2795 			  IGMP_V2_SEEN(state->in_dev) ? "V2" :
2796 			  "V3";
2797 #else
2798 		querier = "NONE";
2799 #endif
2800 
2801 		if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2802 			seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2803 				   state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2804 		}
2805 
2806 		delta = im->timer.expires - jiffies;
2807 		seq_printf(seq,
2808 			   "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2809 			   im->multiaddr, im->users,
2810 			   im->tm_running,
2811 			   im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2812 			   im->reporter);
2813 	}
2814 	return 0;
2815 }
2816 
2817 static const struct seq_operations igmp_mc_seq_ops = {
2818 	.start	=	igmp_mc_seq_start,
2819 	.next	=	igmp_mc_seq_next,
2820 	.stop	=	igmp_mc_seq_stop,
2821 	.show	=	igmp_mc_seq_show,
2822 };
2823 
igmp_mc_seq_open(struct inode * inode,struct file * file)2824 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2825 {
2826 	return seq_open_net(inode, file, &igmp_mc_seq_ops,
2827 			sizeof(struct igmp_mc_iter_state));
2828 }
2829 
2830 static const struct file_operations igmp_mc_seq_fops = {
2831 	.owner		=	THIS_MODULE,
2832 	.open		=	igmp_mc_seq_open,
2833 	.read		=	seq_read,
2834 	.llseek		=	seq_lseek,
2835 	.release	=	seq_release_net,
2836 };
2837 
2838 struct igmp_mcf_iter_state {
2839 	struct seq_net_private p;
2840 	struct net_device *dev;
2841 	struct in_device *idev;
2842 	struct ip_mc_list *im;
2843 };
2844 
2845 #define igmp_mcf_seq_private(seq)	((struct igmp_mcf_iter_state *)(seq)->private)
2846 
igmp_mcf_get_first(struct seq_file * seq)2847 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2848 {
2849 	struct net *net = seq_file_net(seq);
2850 	struct ip_sf_list *psf = NULL;
2851 	struct ip_mc_list *im = NULL;
2852 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2853 
2854 	state->idev = NULL;
2855 	state->im = NULL;
2856 	for_each_netdev_rcu(net, state->dev) {
2857 		struct in_device *idev;
2858 		idev = __in_dev_get_rcu(state->dev);
2859 		if (unlikely(!idev))
2860 			continue;
2861 		im = rcu_dereference(idev->mc_list);
2862 		if (likely(im)) {
2863 			spin_lock_bh(&im->lock);
2864 			psf = im->sources;
2865 			if (likely(psf)) {
2866 				state->im = im;
2867 				state->idev = idev;
2868 				break;
2869 			}
2870 			spin_unlock_bh(&im->lock);
2871 		}
2872 	}
2873 	return psf;
2874 }
2875 
igmp_mcf_get_next(struct seq_file * seq,struct ip_sf_list * psf)2876 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2877 {
2878 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2879 
2880 	psf = psf->sf_next;
2881 	while (!psf) {
2882 		spin_unlock_bh(&state->im->lock);
2883 		state->im = state->im->next;
2884 		while (!state->im) {
2885 			state->dev = next_net_device_rcu(state->dev);
2886 			if (!state->dev) {
2887 				state->idev = NULL;
2888 				goto out;
2889 			}
2890 			state->idev = __in_dev_get_rcu(state->dev);
2891 			if (!state->idev)
2892 				continue;
2893 			state->im = rcu_dereference(state->idev->mc_list);
2894 		}
2895 		if (!state->im)
2896 			break;
2897 		spin_lock_bh(&state->im->lock);
2898 		psf = state->im->sources;
2899 	}
2900 out:
2901 	return psf;
2902 }
2903 
igmp_mcf_get_idx(struct seq_file * seq,loff_t pos)2904 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2905 {
2906 	struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2907 	if (psf)
2908 		while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2909 			--pos;
2910 	return pos ? NULL : psf;
2911 }
2912 
igmp_mcf_seq_start(struct seq_file * seq,loff_t * pos)2913 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2914 	__acquires(rcu)
2915 {
2916 	rcu_read_lock();
2917 	return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2918 }
2919 
igmp_mcf_seq_next(struct seq_file * seq,void * v,loff_t * pos)2920 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2921 {
2922 	struct ip_sf_list *psf;
2923 	if (v == SEQ_START_TOKEN)
2924 		psf = igmp_mcf_get_first(seq);
2925 	else
2926 		psf = igmp_mcf_get_next(seq, v);
2927 	++*pos;
2928 	return psf;
2929 }
2930 
igmp_mcf_seq_stop(struct seq_file * seq,void * v)2931 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2932 	__releases(rcu)
2933 {
2934 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2935 	if (likely(state->im)) {
2936 		spin_unlock_bh(&state->im->lock);
2937 		state->im = NULL;
2938 	}
2939 	state->idev = NULL;
2940 	state->dev = NULL;
2941 	rcu_read_unlock();
2942 }
2943 
igmp_mcf_seq_show(struct seq_file * seq,void * v)2944 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2945 {
2946 	struct ip_sf_list *psf = (struct ip_sf_list *)v;
2947 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2948 
2949 	if (v == SEQ_START_TOKEN) {
2950 		seq_puts(seq, "Idx Device        MCA        SRC    INC    EXC\n");
2951 	} else {
2952 		seq_printf(seq,
2953 			   "%3d %6.6s 0x%08x "
2954 			   "0x%08x %6lu %6lu\n",
2955 			   state->dev->ifindex, state->dev->name,
2956 			   ntohl(state->im->multiaddr),
2957 			   ntohl(psf->sf_inaddr),
2958 			   psf->sf_count[MCAST_INCLUDE],
2959 			   psf->sf_count[MCAST_EXCLUDE]);
2960 	}
2961 	return 0;
2962 }
2963 
2964 static const struct seq_operations igmp_mcf_seq_ops = {
2965 	.start	=	igmp_mcf_seq_start,
2966 	.next	=	igmp_mcf_seq_next,
2967 	.stop	=	igmp_mcf_seq_stop,
2968 	.show	=	igmp_mcf_seq_show,
2969 };
2970 
igmp_mcf_seq_open(struct inode * inode,struct file * file)2971 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2972 {
2973 	return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2974 			sizeof(struct igmp_mcf_iter_state));
2975 }
2976 
2977 static const struct file_operations igmp_mcf_seq_fops = {
2978 	.owner		=	THIS_MODULE,
2979 	.open		=	igmp_mcf_seq_open,
2980 	.read		=	seq_read,
2981 	.llseek		=	seq_lseek,
2982 	.release	=	seq_release_net,
2983 };
2984 
igmp_net_init(struct net * net)2985 static int __net_init igmp_net_init(struct net *net)
2986 {
2987 	struct proc_dir_entry *pde;
2988 	int err;
2989 
2990 	pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
2991 	if (!pde)
2992 		goto out_igmp;
2993 	pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2994 			  &igmp_mcf_seq_fops);
2995 	if (!pde)
2996 		goto out_mcfilter;
2997 	err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2998 				   SOCK_DGRAM, 0, net);
2999 	if (err < 0) {
3000 		pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3001 		       err);
3002 		goto out_sock;
3003 	}
3004 
3005 	return 0;
3006 
3007 out_sock:
3008 	remove_proc_entry("mcfilter", net->proc_net);
3009 out_mcfilter:
3010 	remove_proc_entry("igmp", net->proc_net);
3011 out_igmp:
3012 	return -ENOMEM;
3013 }
3014 
igmp_net_exit(struct net * net)3015 static void __net_exit igmp_net_exit(struct net *net)
3016 {
3017 	remove_proc_entry("mcfilter", net->proc_net);
3018 	remove_proc_entry("igmp", net->proc_net);
3019 	inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3020 }
3021 
3022 static struct pernet_operations igmp_net_ops = {
3023 	.init = igmp_net_init,
3024 	.exit = igmp_net_exit,
3025 };
3026 #endif
3027 
igmp_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)3028 static int igmp_netdev_event(struct notifier_block *this,
3029 			     unsigned long event, void *ptr)
3030 {
3031 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3032 	struct in_device *in_dev;
3033 
3034 	switch (event) {
3035 	case NETDEV_RESEND_IGMP:
3036 		in_dev = __in_dev_get_rtnl(dev);
3037 		if (in_dev)
3038 			ip_mc_rejoin_groups(in_dev);
3039 		break;
3040 	default:
3041 		break;
3042 	}
3043 	return NOTIFY_DONE;
3044 }
3045 
3046 static struct notifier_block igmp_notifier = {
3047 	.notifier_call = igmp_netdev_event,
3048 };
3049 
igmp_mc_init(void)3050 int __init igmp_mc_init(void)
3051 {
3052 #if defined(CONFIG_PROC_FS)
3053 	int err;
3054 
3055 	err = register_pernet_subsys(&igmp_net_ops);
3056 	if (err)
3057 		return err;
3058 	err = register_netdevice_notifier(&igmp_notifier);
3059 	if (err)
3060 		goto reg_notif_fail;
3061 	return 0;
3062 
3063 reg_notif_fail:
3064 	unregister_pernet_subsys(&igmp_net_ops);
3065 	return err;
3066 #else
3067 	return register_netdevice_notifier(&igmp_notifier);
3068 #endif
3069 }
3070