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