xref: /openbmc/linux/net/ipv4/igmp.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Linux NET3:	Internet Group Management Protocol  [IGMP]
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	This code implements the IGMP protocol as defined in RFC1112. There has
61da177e4SLinus Torvalds  *	been a further revision of this protocol since which is now supported.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	If you have trouble with this module be careful what gcc you have used,
91da177e4SLinus Torvalds  *	the older version didn't come out right using gcc 2.5.8, the newer one
101da177e4SLinus Torvalds  *	seems to fall out with gcc 2.6.2.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	Authors:
13113aa838SAlan Cox  *		Alan Cox <alan@lxorguk.ukuu.org.uk>
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	Fixes:
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *		Alan Cox	:	Added lots of __inline__ to optimise
181da177e4SLinus Torvalds  *					the memory usage of all the tiny little
191da177e4SLinus Torvalds  *					functions.
201da177e4SLinus Torvalds  *		Alan Cox	:	Dumped the header building experiment.
211da177e4SLinus Torvalds  *		Alan Cox	:	Minor tweaks ready for multicast routing
221da177e4SLinus Torvalds  *					and extended IGMP protocol.
231da177e4SLinus Torvalds  *		Alan Cox	:	Removed a load of inline directives. Gcc 2.5.8
241da177e4SLinus Torvalds  *					writes utterly bogus code otherwise (sigh)
251da177e4SLinus Torvalds  *					fixed IGMP loopback to behave in the manner
261da177e4SLinus Torvalds  *					desired by mrouted, fixed the fact it has been
271da177e4SLinus Torvalds  *					broken since 1.3.6 and cleaned up a few minor
281da177e4SLinus Torvalds  *					points.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Tried to revise IGMP to Version 2
311da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
321da177e4SLinus Torvalds  *					The enhancements are mainly based on Steve Deering's
331da177e4SLinus Torvalds  * 					ipmulti-3.5 source code.
341da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added the igmp_get_mrouter_info and
351da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		igmp_set_mrouter_info to keep track of
361da177e4SLinus Torvalds  *					the mrouted version on that device.
371da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added the max_resp_time parameter to
381da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		igmp_heard_query(). Using this parameter
391da177e4SLinus Torvalds  *					to identify the multicast router version
401da177e4SLinus Torvalds  *					and do what the IGMP version 2 specified.
411da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added a timer to revert to IGMP V2 router
421da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		if the specified time expired.
431da177e4SLinus Torvalds  *		Alan Cox	:	Stop IGMP from 0.0.0.0 being accepted.
441da177e4SLinus Torvalds  *		Alan Cox	:	Use GFP_ATOMIC in the right places.
451da177e4SLinus Torvalds  *		Christian Daudt :	igmp timer wasn't set for local group
461da177e4SLinus Torvalds  *					memberships but was being deleted,
471da177e4SLinus Torvalds  *					which caused a "del_timer() called
481da177e4SLinus Torvalds  *					from %p with timer not initialized\n"
491da177e4SLinus Torvalds  *					message (960131).
501da177e4SLinus Torvalds  *		Christian Daudt :	removed del_timer from
511da177e4SLinus Torvalds  *					igmp_timer_expire function (960205).
521da177e4SLinus Torvalds  *             Christian Daudt :       igmp_heard_report now only calls
531da177e4SLinus Torvalds  *                                     igmp_timer_expire if tm->running is
541da177e4SLinus Torvalds  *                                     true (960216).
551da177e4SLinus Torvalds  *		Malcolm Beattie :	ttl comparison wrong in igmp_rcv made
561da177e4SLinus Torvalds  *					igmp_heard_query never trigger. Expiry
571da177e4SLinus Torvalds  *					miscalculation fixed in igmp_heard_query
581da177e4SLinus Torvalds  *					and random() made to return unsigned to
591da177e4SLinus Torvalds  *					prevent negative expiry times.
601da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Wrong group leaving behaviour, backport
611da177e4SLinus Torvalds  *					fix from pending 2.1.x patches.
621da177e4SLinus Torvalds  *		Alan Cox:		Forget to enable FDDI support earlier.
631da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Fixed leaving groups on device down.
641da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Accordance to igmp-v2-06 draft.
651da177e4SLinus Torvalds  *		David L Stevens:	IGMPv3 support, with help from
661da177e4SLinus Torvalds  *					Vinay Kulkarni
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <linux/module.h>
705a0e3ad6STejun Heo #include <linux/slab.h>
717c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
721da177e4SLinus Torvalds #include <linux/types.h>
731da177e4SLinus Torvalds #include <linux/kernel.h>
741da177e4SLinus Torvalds #include <linux/jiffies.h>
751da177e4SLinus Torvalds #include <linux/string.h>
761da177e4SLinus Torvalds #include <linux/socket.h>
771da177e4SLinus Torvalds #include <linux/sockios.h>
781da177e4SLinus Torvalds #include <linux/in.h>
791da177e4SLinus Torvalds #include <linux/inet.h>
801da177e4SLinus Torvalds #include <linux/netdevice.h>
811da177e4SLinus Torvalds #include <linux/skbuff.h>
821da177e4SLinus Torvalds #include <linux/inetdevice.h>
831da177e4SLinus Torvalds #include <linux/igmp.h>
841da177e4SLinus Torvalds #include <linux/if_arp.h>
851da177e4SLinus Torvalds #include <linux/rtnetlink.h>
861da177e4SLinus Torvalds #include <linux/times.h>
879d4a0314SHannes Frederic Sowa #include <linux/pkt_sched.h>
88a46182b0SKevin Cernekee #include <linux/byteorder/generic.h>
8914c85021SArnaldo Carvalho de Melo 
90457c4cbcSEric W. Biederman #include <net/net_namespace.h>
9114c85021SArnaldo Carvalho de Melo #include <net/arp.h>
921da177e4SLinus Torvalds #include <net/ip.h>
931da177e4SLinus Torvalds #include <net/protocol.h>
941da177e4SLinus Torvalds #include <net/route.h>
951da177e4SLinus Torvalds #include <net/sock.h>
961da177e4SLinus Torvalds #include <net/checksum.h>
9793a714d6SMadhu Challa #include <net/inet_common.h>
981da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
991da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1001da177e4SLinus Torvalds #include <linux/mroute.h>
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
1031da177e4SLinus Torvalds #include <linux/proc_fs.h>
1041da177e4SLinus Torvalds #include <linux/seq_file.h>
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1081da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */
1091da177e4SLinus Torvalds 
110436f7c20SFabian Frederick #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL	(10*HZ)
111436f7c20SFabian Frederick #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL	(1*HZ)
112966c37f2SHangbin Liu #define IGMP_QUERY_INTERVAL			(125*HZ)
113436f7c20SFabian Frederick #define IGMP_QUERY_RESPONSE_INTERVAL		(10*HZ)
1141da177e4SLinus Torvalds 
115436f7c20SFabian Frederick #define IGMP_INITIAL_REPORT_DELAY		(1)
1161da177e4SLinus Torvalds 
117436f7c20SFabian Frederick /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
1181da177e4SLinus Torvalds  * IGMP specs require to report membership immediately after
1191da177e4SLinus Torvalds  * joining a group, but we delay the first report by a
1201da177e4SLinus Torvalds  * small interval. It seems more natural and still does not
1211da177e4SLinus Torvalds  * contradict to specs provided this delay is small enough.
1221da177e4SLinus Torvalds  */
1231da177e4SLinus Torvalds 
12442f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \
125c346dca1SYOSHIFUJI Hideaki 	(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
12642f811b8SHerbert Xu 	 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
1271da177e4SLinus Torvalds 	 ((in_dev)->mr_v1_seen && \
1281da177e4SLinus Torvalds 	  time_before(jiffies, (in_dev)->mr_v1_seen)))
12942f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \
130c346dca1SYOSHIFUJI Hideaki 	(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
13142f811b8SHerbert Xu 	 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
1321da177e4SLinus Torvalds 	 ((in_dev)->mr_v2_seen && \
1331da177e4SLinus Torvalds 	  time_before(jiffies, (in_dev)->mr_v2_seen)))
1341da177e4SLinus Torvalds 
135cab70040SWilliam Manley static int unsolicited_report_interval(struct in_device *in_dev)
136cab70040SWilliam Manley {
1372690048cSWilliam Manley 	int interval_ms, interval_jiffies;
1382690048cSWilliam Manley 
139cab70040SWilliam Manley 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
1402690048cSWilliam Manley 		interval_ms = IN_DEV_CONF_GET(
1412690048cSWilliam Manley 			in_dev,
1422690048cSWilliam Manley 			IGMPV2_UNSOLICITED_REPORT_INTERVAL);
143cab70040SWilliam Manley 	else /* v3 */
1442690048cSWilliam Manley 		interval_ms = IN_DEV_CONF_GET(
1452690048cSWilliam Manley 			in_dev,
1462690048cSWilliam Manley 			IGMPV3_UNSOLICITED_REPORT_INTERVAL);
1472690048cSWilliam Manley 
1482690048cSWilliam Manley 	interval_jiffies = msecs_to_jiffies(interval_ms);
1492690048cSWilliam Manley 
1502690048cSWilliam Manley 	/* _timer functions can't handle a delay of 0 jiffies so ensure
1512690048cSWilliam Manley 	 *  we always return a positive value.
1522690048cSWilliam Manley 	 */
1532690048cSWilliam Manley 	if (interval_jiffies <= 0)
1542690048cSWilliam Manley 		interval_jiffies = 1;
1552690048cSWilliam Manley 	return interval_jiffies;
156cab70040SWilliam Manley }
157cab70040SWilliam Manley 
1589fb20801SFlorian Fainelli static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
1599fb20801SFlorian Fainelli 			      gfp_t gfp);
16024803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
1611da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev);
1621da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc);
1631da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc);
1641da177e4SLinus Torvalds #endif
1651da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc);
1668f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1678f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im)
1701da177e4SLinus Torvalds {
1718851ab52SReshetova, Elena 	if (refcount_dec_and_test(&im->refcnt)) {
1721da177e4SLinus Torvalds 		in_dev_put(im->interface);
17342ea299dSLai Jiangshan 		kfree_rcu(im, rcu);
1741da177e4SLinus Torvalds 	}
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
177d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc)				\
178d9aa9380SDavid S. Miller 	for (pmc = rcu_dereference(in_dev->mc_list);		\
179d9aa9380SDavid S. Miller 	     pmc != NULL;					\
180d9aa9380SDavid S. Miller 	     pmc = rcu_dereference(pmc->next_rcu))
181d9aa9380SDavid S. Miller 
182d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc)				\
183d9aa9380SDavid S. Miller 	for (pmc = rtnl_dereference(in_dev->mc_list);		\
184d9aa9380SDavid S. Miller 	     pmc != NULL;					\
185d9aa9380SDavid S. Miller 	     pmc = rtnl_dereference(pmc->next_rcu))
186d9aa9380SDavid S. Miller 
1871da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  *	Timer management
1911da177e4SLinus Torvalds  */
1921da177e4SLinus Torvalds 
1931d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
1961da177e4SLinus Torvalds 	if (del_timer(&im->timer))
1978851ab52SReshetova, Elena 		refcount_dec(&im->refcnt);
1981da177e4SLinus Torvalds 	im->tm_running = 0;
1991da177e4SLinus Torvalds 	im->reporter = 0;
2001da177e4SLinus Torvalds 	im->unsolicit_count = 0;
2011da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /* It must be called with locked im->lock */
2051da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
2061da177e4SLinus Torvalds {
20763862b5bSAruna-Hewapathirane 	int tv = prandom_u32() % max_delay;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	im->tm_running = 1;
2101da177e4SLinus Torvalds 	if (!mod_timer(&im->timer, jiffies+tv+2))
2118851ab52SReshetova, Elena 		refcount_inc(&im->refcnt);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev)
2151da177e4SLinus Torvalds {
21663862b5bSAruna-Hewapathirane 	int tv = prandom_u32() % in_dev->mr_maxdelay;
2177ababb78SMichal Tesar 	unsigned long exp = jiffies + tv + 2;
2187ababb78SMichal Tesar 
2197ababb78SMichal Tesar 	if (in_dev->mr_gq_running &&
2207ababb78SMichal Tesar 	    time_after_eq(exp, (in_dev->mr_gq_timer).expires))
2217ababb78SMichal Tesar 		return;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	in_dev->mr_gq_running = 1;
2247ababb78SMichal Tesar 	if (!mod_timer(&in_dev->mr_gq_timer, exp))
2251da177e4SLinus Torvalds 		in_dev_hold(in_dev);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
2291da177e4SLinus Torvalds {
23063862b5bSAruna-Hewapathirane 	int tv = prandom_u32() % delay;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
2331da177e4SLinus Torvalds 		in_dev_hold(in_dev);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
2391da177e4SLinus Torvalds 	im->unsolicit_count = 0;
2401da177e4SLinus Torvalds 	if (del_timer(&im->timer)) {
2411da177e4SLinus Torvalds 		if ((long)(im->timer.expires-jiffies) < max_delay) {
2421da177e4SLinus Torvalds 			add_timer(&im->timer);
2431da177e4SLinus Torvalds 			im->tm_running = 1;
2441da177e4SLinus Torvalds 			spin_unlock_bh(&im->lock);
2451da177e4SLinus Torvalds 			return;
2461da177e4SLinus Torvalds 		}
2478851ab52SReshetova, Elena 		refcount_dec(&im->refcnt);
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 	igmp_start_timer(im, max_delay);
2501da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  *	Send an IGMP report.
2561da177e4SLinus Torvalds  */
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
2621da177e4SLinus Torvalds 	int gdeleted, int sdeleted)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	switch (type) {
2651da177e4SLinus Torvalds 	case IGMPV3_MODE_IS_INCLUDE:
2661da177e4SLinus Torvalds 	case IGMPV3_MODE_IS_EXCLUDE:
2671da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2681da177e4SLinus Torvalds 			return 0;
269ad12583fSDavid L Stevens 		if (!(pmc->gsquery && !psf->sf_gsresp)) {
270ad12583fSDavid L Stevens 			if (pmc->sfmode == MCAST_INCLUDE)
271ad12583fSDavid L Stevens 				return 1;
272ad12583fSDavid L Stevens 			/* don't include if this source is excluded
273ad12583fSDavid L Stevens 			 * in all filters
274ad12583fSDavid L Stevens 			 */
275ad12583fSDavid L Stevens 			if (psf->sf_count[MCAST_INCLUDE])
276ad12583fSDavid L Stevens 				return type == IGMPV3_MODE_IS_INCLUDE;
277ad12583fSDavid L Stevens 			return pmc->sfcount[MCAST_EXCLUDE] ==
278ad12583fSDavid L Stevens 				psf->sf_count[MCAST_EXCLUDE];
279ad12583fSDavid L Stevens 		}
280ad12583fSDavid L Stevens 		return 0;
2811da177e4SLinus Torvalds 	case IGMPV3_CHANGE_TO_INCLUDE:
2821da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2831da177e4SLinus Torvalds 			return 0;
2841da177e4SLinus Torvalds 		return psf->sf_count[MCAST_INCLUDE] != 0;
2851da177e4SLinus Torvalds 	case IGMPV3_CHANGE_TO_EXCLUDE:
2861da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2871da177e4SLinus Torvalds 			return 0;
2881da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
2891da177e4SLinus Torvalds 		    psf->sf_count[MCAST_INCLUDE])
2901da177e4SLinus Torvalds 			return 0;
2911da177e4SLinus Torvalds 		return pmc->sfcount[MCAST_EXCLUDE] ==
2921da177e4SLinus Torvalds 			psf->sf_count[MCAST_EXCLUDE];
2931da177e4SLinus Torvalds 	case IGMPV3_ALLOW_NEW_SOURCES:
2941da177e4SLinus Torvalds 		if (gdeleted || !psf->sf_crcount)
2951da177e4SLinus Torvalds 			return 0;
2961da177e4SLinus Torvalds 		return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
2971da177e4SLinus Torvalds 	case IGMPV3_BLOCK_OLD_SOURCES:
2981da177e4SLinus Torvalds 		if (pmc->sfmode == MCAST_INCLUDE)
2991da177e4SLinus Torvalds 			return gdeleted || (psf->sf_crcount && sdeleted);
3001da177e4SLinus Torvalds 		return psf->sf_crcount && !gdeleted && !sdeleted;
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds 	return 0;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds static int
3061da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	struct ip_sf_list *psf;
3091da177e4SLinus Torvalds 	int scount = 0;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
3121da177e4SLinus Torvalds 		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
3131da177e4SLinus Torvalds 			continue;
3141da177e4SLinus Torvalds 		scount++;
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 	return scount;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
319a46182b0SKevin Cernekee /* source address selection per RFC 3376 section 4.2.13 */
320a46182b0SKevin Cernekee static __be32 igmpv3_get_srcaddr(struct net_device *dev,
321a46182b0SKevin Cernekee 				 const struct flowi4 *fl4)
322a46182b0SKevin Cernekee {
323a46182b0SKevin Cernekee 	struct in_device *in_dev = __in_dev_get_rcu(dev);
324a46182b0SKevin Cernekee 
325a46182b0SKevin Cernekee 	if (!in_dev)
326a46182b0SKevin Cernekee 		return htonl(INADDR_ANY);
327a46182b0SKevin Cernekee 
328a46182b0SKevin Cernekee 	for_ifa(in_dev) {
329ad23b750SFelix Fietkau 		if (fl4->saddr == ifa->ifa_local)
330a46182b0SKevin Cernekee 			return fl4->saddr;
331a46182b0SKevin Cernekee 	} endfor_ifa(in_dev);
332a46182b0SKevin Cernekee 
333a46182b0SKevin Cernekee 	return htonl(INADDR_ANY);
334a46182b0SKevin Cernekee }
335a46182b0SKevin Cernekee 
3364c672e4bSDaniel Borkmann static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	struct sk_buff *skb;
3391da177e4SLinus Torvalds 	struct rtable *rt;
3401da177e4SLinus Torvalds 	struct iphdr *pip;
3411da177e4SLinus Torvalds 	struct igmpv3_report *pig;
342877acedcSDaniel Lezcano 	struct net *net = dev_net(dev);
34331e4543dSDavid S. Miller 	struct flowi4 fl4;
34466088243SHerbert Xu 	int hlen = LL_RESERVED_SPACE(dev);
34566088243SHerbert Xu 	int tlen = dev->needed_tailroom;
3464c672e4bSDaniel Borkmann 	unsigned int size = mtu;
3471da177e4SLinus Torvalds 
34857e1ab6eSEric Dumazet 	while (1) {
34966088243SHerbert Xu 		skb = alloc_skb(size + hlen + tlen,
35057e1ab6eSEric Dumazet 				GFP_ATOMIC | __GFP_NOWARN);
35157e1ab6eSEric Dumazet 		if (skb)
35257e1ab6eSEric Dumazet 			break;
35357e1ab6eSEric Dumazet 		size >>= 1;
35457e1ab6eSEric Dumazet 		if (size < 256)
3551da177e4SLinus Torvalds 			return NULL;
35657e1ab6eSEric Dumazet 	}
3579d4a0314SHannes Frederic Sowa 	skb->priority = TC_PRIO_CONTROL;
3581da177e4SLinus Torvalds 
35931e4543dSDavid S. Miller 	rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
36078fbfd8aSDavid S. Miller 				   0, 0,
36178fbfd8aSDavid S. Miller 				   IPPROTO_IGMP, 0, dev->ifindex);
362b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
3631da177e4SLinus Torvalds 		kfree_skb(skb);
3641da177e4SLinus Torvalds 		return NULL;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 
367d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
3681da177e4SLinus Torvalds 	skb->dev = dev;
3691da177e4SLinus Torvalds 
37066088243SHerbert Xu 	skb_reserve(skb, hlen);
3711837b2e2SBenjamin Poirier 	skb_tailroom_reserve(skb, mtu, tlen);
3721da177e4SLinus Torvalds 
3737e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
374eddc9ec5SArnaldo Carvalho de Melo 	pip = ip_hdr(skb);
3757e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + 4);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	pip->version  = 4;
3781da177e4SLinus Torvalds 	pip->ihl      = (sizeof(struct iphdr)+4)>>2;
3791da177e4SLinus Torvalds 	pip->tos      = 0xc0;
3801da177e4SLinus Torvalds 	pip->frag_off = htons(IP_DF);
3811da177e4SLinus Torvalds 	pip->ttl      = 1;
382492f64ceSDavid S. Miller 	pip->daddr    = fl4.daddr;
383e7aadb27SEric Dumazet 
384e7aadb27SEric Dumazet 	rcu_read_lock();
385a46182b0SKevin Cernekee 	pip->saddr    = igmpv3_get_srcaddr(dev, &fl4);
386e7aadb27SEric Dumazet 	rcu_read_unlock();
387e7aadb27SEric Dumazet 
3881da177e4SLinus Torvalds 	pip->protocol = IPPROTO_IGMP;
3891da177e4SLinus Torvalds 	pip->tot_len  = 0;	/* filled in later */
390b6a7719aSHannes Frederic Sowa 	ip_select_ident(net, skb, NULL);
3911da177e4SLinus Torvalds 	((u8 *)&pip[1])[0] = IPOPT_RA;
3921da177e4SLinus Torvalds 	((u8 *)&pip[1])[1] = 4;
3931da177e4SLinus Torvalds 	((u8 *)&pip[1])[2] = 0;
3941da177e4SLinus Torvalds 	((u8 *)&pip[1])[3] = 0;
3951da177e4SLinus Torvalds 
396b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
397d10ba34bSArnaldo Carvalho de Melo 	skb_put(skb, sizeof(*pig));
398d9edf9e2SArnaldo Carvalho de Melo 	pig = igmpv3_report_hdr(skb);
3991da177e4SLinus Torvalds 	pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
4001da177e4SLinus Torvalds 	pig->resv1 = 0;
4011da177e4SLinus Torvalds 	pig->csum = 0;
4021da177e4SLinus Torvalds 	pig->resv2 = 0;
4031da177e4SLinus Torvalds 	pig->ngrec = 0;
4041da177e4SLinus Torvalds 	return skb;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb)
4081da177e4SLinus Torvalds {
409d9edf9e2SArnaldo Carvalho de Melo 	struct igmphdr *pig = igmp_hdr(skb);
410f7c0c2aeSSimon Horman 	const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
4111da177e4SLinus Torvalds 
412d9edf9e2SArnaldo Carvalho de Melo 	pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
4131da177e4SLinus Torvalds 
41433224b16SEric W. Biederman 	return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
423b5476022SEric Dumazet 	int type, struct igmpv3_grec **ppgr, unsigned int mtu)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct net_device *dev = pmc->interface->dev;
4261da177e4SLinus Torvalds 	struct igmpv3_report *pih;
4271da177e4SLinus Torvalds 	struct igmpv3_grec *pgr;
4281da177e4SLinus Torvalds 
429b5476022SEric Dumazet 	if (!skb) {
430b5476022SEric Dumazet 		skb = igmpv3_newpack(dev, mtu);
4311da177e4SLinus Torvalds 		if (!skb)
4321da177e4SLinus Torvalds 			return NULL;
433b5476022SEric Dumazet 	}
4344df864c1SJohannes Berg 	pgr = skb_put(skb, sizeof(struct igmpv3_grec));
4351da177e4SLinus Torvalds 	pgr->grec_type = type;
4361da177e4SLinus Torvalds 	pgr->grec_auxwords = 0;
4371da177e4SLinus Torvalds 	pgr->grec_nsrcs = 0;
4381da177e4SLinus Torvalds 	pgr->grec_mca = pmc->multiaddr;
439d9edf9e2SArnaldo Carvalho de Melo 	pih = igmpv3_report_hdr(skb);
4401da177e4SLinus Torvalds 	pih->ngrec = htons(ntohs(pih->ngrec)+1);
4411da177e4SLinus Torvalds 	*ppgr = pgr;
4421da177e4SLinus Torvalds 	return skb;
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4454c672e4bSDaniel Borkmann #define AVAILABLE(skb)	((skb) ? skb_availroom(skb) : 0)
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
4481da177e4SLinus Torvalds 	int type, int gdeleted, int sdeleted)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	struct net_device *dev = pmc->interface->dev;
45187a8a2aeSNikolay Borisov 	struct net *net = dev_net(dev);
4521da177e4SLinus Torvalds 	struct igmpv3_report *pih;
4531da177e4SLinus Torvalds 	struct igmpv3_grec *pgr = NULL;
4541da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
455ad12583fSDavid L Stevens 	int scount, stotal, first, isquery, truncate;
456b5476022SEric Dumazet 	unsigned int mtu;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	if (pmc->multiaddr == IGMP_ALL_HOSTS)
4591da177e4SLinus Torvalds 		return skb;
46087a8a2aeSNikolay Borisov 	if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
461df2cf4a7SPhilip Downey 		return skb;
4621da177e4SLinus Torvalds 
463b5476022SEric Dumazet 	mtu = READ_ONCE(dev->mtu);
464b5476022SEric Dumazet 	if (mtu < IPV4_MIN_MTU)
465b5476022SEric Dumazet 		return skb;
466b5476022SEric Dumazet 
4671da177e4SLinus Torvalds 	isquery = type == IGMPV3_MODE_IS_INCLUDE ||
4681da177e4SLinus Torvalds 		  type == IGMPV3_MODE_IS_EXCLUDE;
4691da177e4SLinus Torvalds 	truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
4701da177e4SLinus Torvalds 		    type == IGMPV3_CHANGE_TO_EXCLUDE;
4711da177e4SLinus Torvalds 
472ad12583fSDavid L Stevens 	stotal = scount = 0;
473ad12583fSDavid L Stevens 
4741da177e4SLinus Torvalds 	psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
4751da177e4SLinus Torvalds 
476ad12583fSDavid L Stevens 	if (!*psf_list)
477ad12583fSDavid L Stevens 		goto empty_source;
478ad12583fSDavid L Stevens 
479d9edf9e2SArnaldo Carvalho de Melo 	pih = skb ? igmpv3_report_hdr(skb) : NULL;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/* EX and TO_EX get a fresh packet, if needed */
4821da177e4SLinus Torvalds 	if (truncate) {
4831da177e4SLinus Torvalds 		if (pih && pih->ngrec &&
4841da177e4SLinus Torvalds 		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
4851da177e4SLinus Torvalds 			if (skb)
4861da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
487b5476022SEric Dumazet 			skb = igmpv3_newpack(dev, mtu);
4881da177e4SLinus Torvalds 		}
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 	first = 1;
4911da177e4SLinus Torvalds 	psf_prev = NULL;
4921da177e4SLinus Torvalds 	for (psf = *psf_list; psf; psf = psf_next) {
493ea4d9e72SAl Viro 		__be32 *psrc;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 		psf_next = psf->sf_next;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
4981da177e4SLinus Torvalds 			psf_prev = psf;
4991da177e4SLinus Torvalds 			continue;
5001da177e4SLinus Torvalds 		}
5011da177e4SLinus Torvalds 
502a052517aSHangbin Liu 		/* Based on RFC3376 5.1. Should not send source-list change
503a052517aSHangbin Liu 		 * records when there is a filter mode change.
504a052517aSHangbin Liu 		 */
505a052517aSHangbin Liu 		if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
506a052517aSHangbin Liu 		     (!gdeleted && pmc->crcount)) &&
507a052517aSHangbin Liu 		    (type == IGMPV3_ALLOW_NEW_SOURCES ||
508a052517aSHangbin Liu 		     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
509a052517aSHangbin Liu 			goto decrease_sf_crcount;
510a052517aSHangbin Liu 
5111da177e4SLinus Torvalds 		/* clear marks on query responses */
5121da177e4SLinus Torvalds 		if (isquery)
5131da177e4SLinus Torvalds 			psf->sf_gsresp = 0;
5141da177e4SLinus Torvalds 
51563007727SAl Viro 		if (AVAILABLE(skb) < sizeof(__be32) +
5161da177e4SLinus Torvalds 		    first*sizeof(struct igmpv3_grec)) {
5171da177e4SLinus Torvalds 			if (truncate && !first)
5181da177e4SLinus Torvalds 				break;	 /* truncate these */
5191da177e4SLinus Torvalds 			if (pgr)
5201da177e4SLinus Torvalds 				pgr->grec_nsrcs = htons(scount);
5211da177e4SLinus Torvalds 			if (skb)
5221da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
523b5476022SEric Dumazet 			skb = igmpv3_newpack(dev, mtu);
5241da177e4SLinus Torvalds 			first = 1;
5251da177e4SLinus Torvalds 			scount = 0;
5261da177e4SLinus Torvalds 		}
5271da177e4SLinus Torvalds 		if (first) {
528b5476022SEric Dumazet 			skb = add_grhead(skb, pmc, type, &pgr, mtu);
5291da177e4SLinus Torvalds 			first = 0;
5301da177e4SLinus Torvalds 		}
531cc63f70bSAlexey Dobriyan 		if (!skb)
532cc63f70bSAlexey Dobriyan 			return NULL;
5334df864c1SJohannes Berg 		psrc = skb_put(skb, sizeof(__be32));
5341da177e4SLinus Torvalds 		*psrc = psf->sf_inaddr;
535ad12583fSDavid L Stevens 		scount++; stotal++;
5361da177e4SLinus Torvalds 		if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
5371da177e4SLinus Torvalds 		     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
538a052517aSHangbin Liu decrease_sf_crcount:
5391da177e4SLinus Torvalds 			psf->sf_crcount--;
5401da177e4SLinus Torvalds 			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
5411da177e4SLinus Torvalds 				if (psf_prev)
5421da177e4SLinus Torvalds 					psf_prev->sf_next = psf->sf_next;
5431da177e4SLinus Torvalds 				else
5441da177e4SLinus Torvalds 					*psf_list = psf->sf_next;
5451da177e4SLinus Torvalds 				kfree(psf);
5461da177e4SLinus Torvalds 				continue;
5471da177e4SLinus Torvalds 			}
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 		psf_prev = psf;
5501da177e4SLinus Torvalds 	}
551ad12583fSDavid L Stevens 
552ad12583fSDavid L Stevens empty_source:
553ad12583fSDavid L Stevens 	if (!stotal) {
554ad12583fSDavid L Stevens 		if (type == IGMPV3_ALLOW_NEW_SOURCES ||
555ad12583fSDavid L Stevens 		    type == IGMPV3_BLOCK_OLD_SOURCES)
556ad12583fSDavid L Stevens 			return skb;
557ad12583fSDavid L Stevens 		if (pmc->crcount || isquery) {
558ad12583fSDavid L Stevens 			/* make sure we have room for group header */
559ad12583fSDavid L Stevens 			if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
560ad12583fSDavid L Stevens 				igmpv3_sendpack(skb);
561ad12583fSDavid L Stevens 				skb = NULL; /* add_grhead will get a new one */
562ad12583fSDavid L Stevens 			}
563b5476022SEric Dumazet 			skb = add_grhead(skb, pmc, type, &pgr, mtu);
564ad12583fSDavid L Stevens 		}
565ad12583fSDavid L Stevens 	}
5661da177e4SLinus Torvalds 	if (pgr)
5671da177e4SLinus Torvalds 		pgr->grec_nsrcs = htons(scount);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	if (isquery)
5701da177e4SLinus Torvalds 		pmc->gsquery = 0;	/* clear query state on report */
5711da177e4SLinus Torvalds 	return skb;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
57787a8a2aeSNikolay Borisov 	struct net *net = dev_net(in_dev->dev);
5781da177e4SLinus Torvalds 	int type;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	if (!pmc) {
5811d7138deSEric Dumazet 		rcu_read_lock();
5821d7138deSEric Dumazet 		for_each_pmc_rcu(in_dev, pmc) {
5831da177e4SLinus Torvalds 			if (pmc->multiaddr == IGMP_ALL_HOSTS)
5841da177e4SLinus Torvalds 				continue;
585df2cf4a7SPhilip Downey 			if (ipv4_is_local_multicast(pmc->multiaddr) &&
58687a8a2aeSNikolay Borisov 			     !net->ipv4.sysctl_igmp_llm_reports)
587df2cf4a7SPhilip Downey 				continue;
5881da177e4SLinus Torvalds 			spin_lock_bh(&pmc->lock);
5891da177e4SLinus Torvalds 			if (pmc->sfcount[MCAST_EXCLUDE])
5901da177e4SLinus Torvalds 				type = IGMPV3_MODE_IS_EXCLUDE;
5911da177e4SLinus Torvalds 			else
5921da177e4SLinus Torvalds 				type = IGMPV3_MODE_IS_INCLUDE;
5931da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 0, 0);
5941da177e4SLinus Torvalds 			spin_unlock_bh(&pmc->lock);
5951da177e4SLinus Torvalds 		}
5961d7138deSEric Dumazet 		rcu_read_unlock();
5971da177e4SLinus Torvalds 	} else {
5981da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
5991da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE])
6001da177e4SLinus Torvalds 			type = IGMPV3_MODE_IS_EXCLUDE;
6011da177e4SLinus Torvalds 		else
6021da177e4SLinus Torvalds 			type = IGMPV3_MODE_IS_INCLUDE;
6031da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, type, 0, 0);
6041da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 	if (!skb)
6071da177e4SLinus Torvalds 		return 0;
6081da177e4SLinus Torvalds 	return igmpv3_sendpack(skb);
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds /*
6121da177e4SLinus Torvalds  * remove zero-count source records from a source filter list
6131da177e4SLinus Torvalds  */
6141da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	struct ip_sf_list *psf_prev, *psf_next, *psf;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	psf_prev = NULL;
6191da177e4SLinus Torvalds 	for (psf = *ppsf; psf; psf = psf_next) {
6201da177e4SLinus Torvalds 		psf_next = psf->sf_next;
6211da177e4SLinus Torvalds 		if (psf->sf_crcount == 0) {
6221da177e4SLinus Torvalds 			if (psf_prev)
6231da177e4SLinus Torvalds 				psf_prev->sf_next = psf->sf_next;
6241da177e4SLinus Torvalds 			else
6251da177e4SLinus Torvalds 				*ppsf = psf->sf_next;
6261da177e4SLinus Torvalds 			kfree(psf);
6271da177e4SLinus Torvalds 		} else
6281da177e4SLinus Torvalds 			psf_prev = psf;
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev)
6331da177e4SLinus Torvalds {
6341da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
6351da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
6361da177e4SLinus Torvalds 	int type, dtype;
6371da177e4SLinus Torvalds 
6381d7138deSEric Dumazet 	rcu_read_lock();
6391da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	/* deleted MCA's */
6421da177e4SLinus Torvalds 	pmc_prev = NULL;
6431da177e4SLinus Torvalds 	for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
6441da177e4SLinus Torvalds 		pmc_next = pmc->next;
6451da177e4SLinus Torvalds 		if (pmc->sfmode == MCAST_INCLUDE) {
6461da177e4SLinus Torvalds 			type = IGMPV3_BLOCK_OLD_SOURCES;
6471da177e4SLinus Torvalds 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
6481da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 1, 0);
6491da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, dtype, 1, 1);
6501da177e4SLinus Torvalds 		}
6511da177e4SLinus Torvalds 		if (pmc->crcount) {
6521da177e4SLinus Torvalds 			if (pmc->sfmode == MCAST_EXCLUDE) {
6531da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_INCLUDE;
6541da177e4SLinus Torvalds 				skb = add_grec(skb, pmc, type, 1, 0);
6551da177e4SLinus Torvalds 			}
656ad12583fSDavid L Stevens 			pmc->crcount--;
6571da177e4SLinus Torvalds 			if (pmc->crcount == 0) {
6581da177e4SLinus Torvalds 				igmpv3_clear_zeros(&pmc->tomb);
6591da177e4SLinus Torvalds 				igmpv3_clear_zeros(&pmc->sources);
6601da177e4SLinus Torvalds 			}
6611da177e4SLinus Torvalds 		}
6621da177e4SLinus Torvalds 		if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
6631da177e4SLinus Torvalds 			if (pmc_prev)
6641da177e4SLinus Torvalds 				pmc_prev->next = pmc_next;
6651da177e4SLinus Torvalds 			else
6661da177e4SLinus Torvalds 				in_dev->mc_tomb = pmc_next;
6671da177e4SLinus Torvalds 			in_dev_put(pmc->interface);
6681da177e4SLinus Torvalds 			kfree(pmc);
6691da177e4SLinus Torvalds 		} else
6701da177e4SLinus Torvalds 			pmc_prev = pmc;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	/* change recs */
6751d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
6761da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
6771da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
6781da177e4SLinus Torvalds 			type = IGMPV3_BLOCK_OLD_SOURCES;
6791da177e4SLinus Torvalds 			dtype = IGMPV3_ALLOW_NEW_SOURCES;
6801da177e4SLinus Torvalds 		} else {
6811da177e4SLinus Torvalds 			type = IGMPV3_ALLOW_NEW_SOURCES;
6821da177e4SLinus Torvalds 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
6831da177e4SLinus Torvalds 		}
6841da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, type, 0, 0);
6851da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, dtype, 0, 1);	/* deleted sources */
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 		/* filter mode changes */
6881da177e4SLinus Torvalds 		if (pmc->crcount) {
6891da177e4SLinus Torvalds 			if (pmc->sfmode == MCAST_EXCLUDE)
6901da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_EXCLUDE;
6911da177e4SLinus Torvalds 			else
6921da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_INCLUDE;
6931da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 0, 0);
694ad12583fSDavid L Stevens 			pmc->crcount--;
6951da177e4SLinus Torvalds 		}
6961da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
6971da177e4SLinus Torvalds 	}
6981d7138deSEric Dumazet 	rcu_read_unlock();
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	if (!skb)
7011da177e4SLinus Torvalds 		return;
7021da177e4SLinus Torvalds 	(void) igmpv3_sendpack(skb);
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
7061da177e4SLinus Torvalds 	int type)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	struct sk_buff *skb;
7091da177e4SLinus Torvalds 	struct iphdr *iph;
7101da177e4SLinus Torvalds 	struct igmphdr *ih;
7111da177e4SLinus Torvalds 	struct rtable *rt;
7121da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
713877acedcSDaniel Lezcano 	struct net *net = dev_net(dev);
71463007727SAl Viro 	__be32	group = pmc ? pmc->multiaddr : 0;
71531e4543dSDavid S. Miller 	struct flowi4 fl4;
71663007727SAl Viro 	__be32	dst;
71766088243SHerbert Xu 	int hlen, tlen;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
7201da177e4SLinus Torvalds 		return igmpv3_send_report(in_dev, pmc);
721df2cf4a7SPhilip Downey 
72287a8a2aeSNikolay Borisov 	if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
723df2cf4a7SPhilip Downey 		return 0;
724df2cf4a7SPhilip Downey 
725df2cf4a7SPhilip Downey 	if (type == IGMP_HOST_LEAVE_MESSAGE)
7261da177e4SLinus Torvalds 		dst = IGMP_ALL_ROUTER;
7271da177e4SLinus Torvalds 	else
7281da177e4SLinus Torvalds 		dst = group;
7291da177e4SLinus Torvalds 
73031e4543dSDavid S. Miller 	rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
73178fbfd8aSDavid S. Miller 				   0, 0,
73278fbfd8aSDavid S. Miller 				   IPPROTO_IGMP, 0, dev->ifindex);
733b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
7341da177e4SLinus Torvalds 		return -1;
73578fbfd8aSDavid S. Miller 
73666088243SHerbert Xu 	hlen = LL_RESERVED_SPACE(dev);
73766088243SHerbert Xu 	tlen = dev->needed_tailroom;
73866088243SHerbert Xu 	skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
73951456b29SIan Morris 	if (!skb) {
7401da177e4SLinus Torvalds 		ip_rt_put(rt);
7411da177e4SLinus Torvalds 		return -1;
7421da177e4SLinus Torvalds 	}
7439d4a0314SHannes Frederic Sowa 	skb->priority = TC_PRIO_CONTROL;
7441da177e4SLinus Torvalds 
745d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
7461da177e4SLinus Torvalds 
74766088243SHerbert Xu 	skb_reserve(skb, hlen);
7481da177e4SLinus Torvalds 
7497e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
750eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
7517e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + 4);
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	iph->version  = 4;
7541da177e4SLinus Torvalds 	iph->ihl      = (sizeof(struct iphdr)+4)>>2;
7551da177e4SLinus Torvalds 	iph->tos      = 0xc0;
7561da177e4SLinus Torvalds 	iph->frag_off = htons(IP_DF);
7571da177e4SLinus Torvalds 	iph->ttl      = 1;
7581da177e4SLinus Torvalds 	iph->daddr    = dst;
759492f64ceSDavid S. Miller 	iph->saddr    = fl4.saddr;
7601da177e4SLinus Torvalds 	iph->protocol = IPPROTO_IGMP;
761b6a7719aSHannes Frederic Sowa 	ip_select_ident(net, skb, NULL);
7621da177e4SLinus Torvalds 	((u8 *)&iph[1])[0] = IPOPT_RA;
7631da177e4SLinus Torvalds 	((u8 *)&iph[1])[1] = 4;
7641da177e4SLinus Torvalds 	((u8 *)&iph[1])[2] = 0;
7651da177e4SLinus Torvalds 	((u8 *)&iph[1])[3] = 0;
7661da177e4SLinus Torvalds 
7674df864c1SJohannes Berg 	ih = skb_put(skb, sizeof(struct igmphdr));
7681da177e4SLinus Torvalds 	ih->type = type;
7691da177e4SLinus Torvalds 	ih->code = 0;
7701da177e4SLinus Torvalds 	ih->csum = 0;
7711da177e4SLinus Torvalds 	ih->group = group;
7721da177e4SLinus Torvalds 	ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
7731da177e4SLinus Torvalds 
77433224b16SEric W. Biederman 	return ip_local_out(net, skb->sk, skb);
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
777e99e88a9SKees Cook static void igmp_gq_timer_expire(struct timer_list *t)
7781da177e4SLinus Torvalds {
779e99e88a9SKees Cook 	struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
7821da177e4SLinus Torvalds 	igmpv3_send_report(in_dev, NULL);
783e2401654SSalam Noureddine 	in_dev_put(in_dev);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
786e99e88a9SKees Cook static void igmp_ifc_timer_expire(struct timer_list *t)
7871da177e4SLinus Torvalds {
788e99e88a9SKees Cook 	struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	igmpv3_send_cr(in_dev);
7911da177e4SLinus Torvalds 	if (in_dev->mr_ifc_count) {
7921da177e4SLinus Torvalds 		in_dev->mr_ifc_count--;
793cab70040SWilliam Manley 		igmp_ifc_start_timer(in_dev,
794cab70040SWilliam Manley 				     unsolicited_report_interval(in_dev));
7951da177e4SLinus Torvalds 	}
796e2401654SSalam Noureddine 	in_dev_put(in_dev);
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev)
8001da177e4SLinus Torvalds {
801165094afSNikolay Borisov 	struct net *net = dev_net(in_dev->dev);
8021da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
8031da177e4SLinus Torvalds 		return;
804165094afSNikolay Borisov 	in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
8051da177e4SLinus Torvalds 	igmp_ifc_start_timer(in_dev, 1);
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 
809e99e88a9SKees Cook static void igmp_timer_expire(struct timer_list *t)
8101da177e4SLinus Torvalds {
811e99e88a9SKees Cook 	struct ip_mc_list *im = from_timer(im, t, timer);
8121da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	spin_lock(&im->lock);
8151da177e4SLinus Torvalds 	im->tm_running = 0;
8161da177e4SLinus Torvalds 
8174fb7253eSHangbin Liu 	if (im->unsolicit_count && --im->unsolicit_count)
818cab70040SWilliam Manley 		igmp_start_timer(im, unsolicited_report_interval(in_dev));
8194fb7253eSHangbin Liu 
8201da177e4SLinus Torvalds 	im->reporter = 1;
8211da177e4SLinus Torvalds 	spin_unlock(&im->lock);
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev))
8241da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
8251da177e4SLinus Torvalds 	else if (IGMP_V2_SEEN(in_dev))
8261da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
8271da177e4SLinus Torvalds 	else
8281da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	ip_ma_put(im);
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
833ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */
834ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	struct ip_sf_list *psf;
8371da177e4SLinus Torvalds 	int i, scount;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	scount = 0;
8401da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
8411da177e4SLinus Torvalds 		if (scount == nsrcs)
8421da177e4SLinus Torvalds 			break;
843ad12583fSDavid L Stevens 		for (i = 0; i < nsrcs; i++) {
844ad12583fSDavid L Stevens 			/* skip inactive filters */
845e05c4ad3SYan, Zheng 			if (psf->sf_count[MCAST_INCLUDE] ||
846ad12583fSDavid L Stevens 			    pmc->sfcount[MCAST_EXCLUDE] !=
847ad12583fSDavid L Stevens 			    psf->sf_count[MCAST_EXCLUDE])
848ce713ee5SRongQing.Li 				break;
849ad12583fSDavid L Stevens 			if (srcs[i] == psf->sf_inaddr) {
850ad12583fSDavid L Stevens 				scount++;
851ad12583fSDavid L Stevens 				break;
852ad12583fSDavid L Stevens 			}
853ad12583fSDavid L Stevens 		}
854ad12583fSDavid L Stevens 	}
855ad12583fSDavid L Stevens 	pmc->gsquery = 0;
856ad12583fSDavid L Stevens 	if (scount == nsrcs)	/* all sources excluded */
857ad12583fSDavid L Stevens 		return 0;
858ad12583fSDavid L Stevens 	return 1;
859ad12583fSDavid L Stevens }
860ad12583fSDavid L Stevens 
861ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
862ad12583fSDavid L Stevens {
863ad12583fSDavid L Stevens 	struct ip_sf_list *psf;
864ad12583fSDavid L Stevens 	int i, scount;
865ad12583fSDavid L Stevens 
866ad12583fSDavid L Stevens 	if (pmc->sfmode == MCAST_EXCLUDE)
867ad12583fSDavid L Stevens 		return igmp_xmarksources(pmc, nsrcs, srcs);
868ad12583fSDavid L Stevens 
869ad12583fSDavid L Stevens 	/* mark INCLUDE-mode sources */
870ad12583fSDavid L Stevens 	scount = 0;
871ad12583fSDavid L Stevens 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
872ad12583fSDavid L Stevens 		if (scount == nsrcs)
873ad12583fSDavid L Stevens 			break;
8741da177e4SLinus Torvalds 		for (i = 0; i < nsrcs; i++)
8751da177e4SLinus Torvalds 			if (srcs[i] == psf->sf_inaddr) {
8761da177e4SLinus Torvalds 				psf->sf_gsresp = 1;
8771da177e4SLinus Torvalds 				scount++;
8781da177e4SLinus Torvalds 				break;
8791da177e4SLinus Torvalds 			}
8801da177e4SLinus Torvalds 	}
881ad12583fSDavid L Stevens 	if (!scount) {
882ad12583fSDavid L Stevens 		pmc->gsquery = 0;
883ad12583fSDavid L Stevens 		return 0;
884ad12583fSDavid L Stevens 	}
885ad12583fSDavid L Stevens 	pmc->gsquery = 1;
886ad12583fSDavid L Stevens 	return 1;
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
889d679c532SEric Dumazet /* return true if packet was dropped */
890d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	struct ip_mc_list *im;
89387a8a2aeSNikolay Borisov 	struct net *net = dev_net(in_dev->dev);
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	/* Timers are only set for non-local groups */
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	if (group == IGMP_ALL_HOSTS)
898d679c532SEric Dumazet 		return false;
89987a8a2aeSNikolay Borisov 	if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
900df2cf4a7SPhilip Downey 		return false;
9011da177e4SLinus Torvalds 
9021d7138deSEric Dumazet 	rcu_read_lock();
9031d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, im) {
9041da177e4SLinus Torvalds 		if (im->multiaddr == group) {
9051da177e4SLinus Torvalds 			igmp_stop_timer(im);
9061da177e4SLinus Torvalds 			break;
9071da177e4SLinus Torvalds 		}
9081da177e4SLinus Torvalds 	}
9091d7138deSEric Dumazet 	rcu_read_unlock();
910d679c532SEric Dumazet 	return false;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
913d679c532SEric Dumazet /* return true if packet was dropped */
914d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
9151da177e4SLinus Torvalds 	int len)
9161da177e4SLinus Torvalds {
917d9edf9e2SArnaldo Carvalho de Melo 	struct igmphdr 		*ih = igmp_hdr(skb);
918d9edf9e2SArnaldo Carvalho de Melo 	struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
9191da177e4SLinus Torvalds 	struct ip_mc_list	*im;
92063007727SAl Viro 	__be32			group = ih->group;
9211da177e4SLinus Torvalds 	int			max_delay;
9221da177e4SLinus Torvalds 	int			mark = 0;
92387a8a2aeSNikolay Borisov 	struct net		*net = dev_net(in_dev->dev);
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 
9265b7c8406SDavid Stevens 	if (len == 8) {
9271da177e4SLinus Torvalds 		if (ih->code == 0) {
9281da177e4SLinus Torvalds 			/* Alas, old v1 router presents here. */
9291da177e4SLinus Torvalds 
930436f7c20SFabian Frederick 			max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
9311da177e4SLinus Torvalds 			in_dev->mr_v1_seen = jiffies +
932966c37f2SHangbin Liu 				(in_dev->mr_qrv * in_dev->mr_qi) +
933966c37f2SHangbin Liu 				in_dev->mr_qri;
9341da177e4SLinus Torvalds 			group = 0;
9351da177e4SLinus Torvalds 		} else {
9361da177e4SLinus Torvalds 			/* v2 router present */
9371da177e4SLinus Torvalds 			max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
9381da177e4SLinus Torvalds 			in_dev->mr_v2_seen = jiffies +
939966c37f2SHangbin Liu 				(in_dev->mr_qrv * in_dev->mr_qi) +
940966c37f2SHangbin Liu 				in_dev->mr_qri;
9411da177e4SLinus Torvalds 		}
9421da177e4SLinus Torvalds 		/* cancel the interface change timer */
9431da177e4SLinus Torvalds 		in_dev->mr_ifc_count = 0;
9441da177e4SLinus Torvalds 		if (del_timer(&in_dev->mr_ifc_timer))
9451da177e4SLinus Torvalds 			__in_dev_put(in_dev);
9461da177e4SLinus Torvalds 		/* clear deleted report items */
9471da177e4SLinus Torvalds 		igmpv3_clear_delrec(in_dev);
9481da177e4SLinus Torvalds 	} else if (len < 12) {
949d679c532SEric Dumazet 		return true;	/* ignore bogus packet; freed by caller */
9505b7c8406SDavid Stevens 	} else if (IGMP_V1_SEEN(in_dev)) {
9515b7c8406SDavid Stevens 		/* This is a v3 query with v1 queriers present */
952436f7c20SFabian Frederick 		max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
9535b7c8406SDavid Stevens 		group = 0;
9545b7c8406SDavid Stevens 	} else if (IGMP_V2_SEEN(in_dev)) {
9555b7c8406SDavid Stevens 		/* this is a v3 query with v2 queriers present;
9565b7c8406SDavid Stevens 		 * Interpretation of the max_delay code is problematic here.
9575b7c8406SDavid Stevens 		 * A real v2 host would use ih_code directly, while v3 has a
9585b7c8406SDavid Stevens 		 * different encoding. We use the v3 encoding as more likely
9595b7c8406SDavid Stevens 		 * to be intended in a v3 query.
9605b7c8406SDavid Stevens 		 */
9615b7c8406SDavid Stevens 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
962a8c1f65cSBen Hutchings 		if (!max_delay)
963a8c1f65cSBen Hutchings 			max_delay = 1;	/* can't mod w/ 0 */
9641da177e4SLinus Torvalds 	} else { /* v3 */
9651da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
966d679c532SEric Dumazet 			return true;
9671da177e4SLinus Torvalds 
968d9edf9e2SArnaldo Carvalho de Melo 		ih3 = igmpv3_query_hdr(skb);
9691da177e4SLinus Torvalds 		if (ih3->nsrcs) {
9701da177e4SLinus Torvalds 			if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
97163007727SAl Viro 					   + ntohs(ih3->nsrcs)*sizeof(__be32)))
972d679c532SEric Dumazet 				return true;
973d9edf9e2SArnaldo Carvalho de Melo 			ih3 = igmpv3_query_hdr(skb);
9741da177e4SLinus Torvalds 		}
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
9771da177e4SLinus Torvalds 		if (!max_delay)
9781da177e4SLinus Torvalds 			max_delay = 1;	/* can't mod w/ 0 */
9791da177e4SLinus Torvalds 		in_dev->mr_maxdelay = max_delay;
980966c37f2SHangbin Liu 
981966c37f2SHangbin Liu 		/* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
982966c37f2SHangbin Liu 		 * received value was zero, use the default or statically
983966c37f2SHangbin Liu 		 * configured value.
984966c37f2SHangbin Liu 		 */
985966c37f2SHangbin Liu 		in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
986966c37f2SHangbin Liu 		in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
987966c37f2SHangbin Liu 
988966c37f2SHangbin Liu 		/* RFC3376, 8.3. Query Response Interval:
989966c37f2SHangbin Liu 		 * The number of seconds represented by the [Query Response
990966c37f2SHangbin Liu 		 * Interval] must be less than the [Query Interval].
991966c37f2SHangbin Liu 		 */
992966c37f2SHangbin Liu 		if (in_dev->mr_qri >= in_dev->mr_qi)
993966c37f2SHangbin Liu 			in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
994966c37f2SHangbin Liu 
9951da177e4SLinus Torvalds 		if (!group) { /* general query */
9961da177e4SLinus Torvalds 			if (ih3->nsrcs)
997b47bd8d2SDaniel Borkmann 				return true;	/* no sources allowed */
9981da177e4SLinus Torvalds 			igmp_gq_start_timer(in_dev);
999d679c532SEric Dumazet 			return false;
10001da177e4SLinus Torvalds 		}
10011da177e4SLinus Torvalds 		/* mark sources to include, if group & source-specific */
10021da177e4SLinus Torvalds 		mark = ih3->nsrcs != 0;
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	/*
10061da177e4SLinus Torvalds 	 * - Start the timers in all of our membership records
10071da177e4SLinus Torvalds 	 *   that the query applies to for the interface on
10081da177e4SLinus Torvalds 	 *   which the query arrived excl. those that belong
10091da177e4SLinus Torvalds 	 *   to a "local" group (224.0.0.X)
10101da177e4SLinus Torvalds 	 * - For timers already running check if they need to
10111da177e4SLinus Torvalds 	 *   be reset.
10121da177e4SLinus Torvalds 	 * - Use the igmp->igmp_code field as the maximum
10131da177e4SLinus Torvalds 	 *   delay possible
10141da177e4SLinus Torvalds 	 */
10151d7138deSEric Dumazet 	rcu_read_lock();
10161d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, im) {
1017ad12583fSDavid L Stevens 		int changed;
1018ad12583fSDavid L Stevens 
10191da177e4SLinus Torvalds 		if (group && group != im->multiaddr)
10201da177e4SLinus Torvalds 			continue;
10211da177e4SLinus Torvalds 		if (im->multiaddr == IGMP_ALL_HOSTS)
10221da177e4SLinus Torvalds 			continue;
1023df2cf4a7SPhilip Downey 		if (ipv4_is_local_multicast(im->multiaddr) &&
102487a8a2aeSNikolay Borisov 		    !net->ipv4.sysctl_igmp_llm_reports)
1025df2cf4a7SPhilip Downey 			continue;
10261da177e4SLinus Torvalds 		spin_lock_bh(&im->lock);
10271da177e4SLinus Torvalds 		if (im->tm_running)
10281da177e4SLinus Torvalds 			im->gsquery = im->gsquery && mark;
10291da177e4SLinus Torvalds 		else
10301da177e4SLinus Torvalds 			im->gsquery = mark;
1031ad12583fSDavid L Stevens 		changed = !im->gsquery ||
10321da177e4SLinus Torvalds 			igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
10331da177e4SLinus Torvalds 		spin_unlock_bh(&im->lock);
1034ad12583fSDavid L Stevens 		if (changed)
10351da177e4SLinus Torvalds 			igmp_mod_timer(im, max_delay);
10361da177e4SLinus Torvalds 	}
10371d7138deSEric Dumazet 	rcu_read_unlock();
1038d679c532SEric Dumazet 	return false;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10419a57a9d2SEric Dumazet /* called in rcu_read_lock() section */
10421da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb)
10431da177e4SLinus Torvalds {
10441da177e4SLinus Torvalds 	/* This basically follows the spec line by line -- see RFC1112 */
10451da177e4SLinus Torvalds 	struct igmphdr *ih;
1046c7b725beSDavid Ahern 	struct net_device *dev = skb->dev;
1047c7b725beSDavid Ahern 	struct in_device *in_dev;
10481da177e4SLinus Torvalds 	int len = skb->len;
1049d679c532SEric Dumazet 	bool dropped = true;
10501da177e4SLinus Torvalds 
1051c7b725beSDavid Ahern 	if (netif_is_l3_master(dev)) {
1052c7b725beSDavid Ahern 		dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1053c7b725beSDavid Ahern 		if (!dev)
1054c7b725beSDavid Ahern 			goto drop;
1055c7b725beSDavid Ahern 	}
1056c7b725beSDavid Ahern 
1057c7b725beSDavid Ahern 	in_dev = __in_dev_get_rcu(dev);
105851456b29SIan Morris 	if (!in_dev)
1059cd557bc1SDenis V. Lunev 		goto drop;
10601da177e4SLinus Torvalds 
1061fb286bb2SHerbert Xu 	if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
10629a57a9d2SEric Dumazet 		goto drop;
1063fb286bb2SHerbert Xu 
1064de08dc1aSTom Herbert 	if (skb_checksum_simple_validate(skb))
10659a57a9d2SEric Dumazet 		goto drop;
10661da177e4SLinus Torvalds 
1067d9edf9e2SArnaldo Carvalho de Melo 	ih = igmp_hdr(skb);
10681da177e4SLinus Torvalds 	switch (ih->type) {
10691da177e4SLinus Torvalds 	case IGMP_HOST_MEMBERSHIP_QUERY:
1070d679c532SEric Dumazet 		dropped = igmp_heard_query(in_dev, skb, len);
10711da177e4SLinus Torvalds 		break;
10721da177e4SLinus Torvalds 	case IGMP_HOST_MEMBERSHIP_REPORT:
10731da177e4SLinus Torvalds 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
10741da177e4SLinus Torvalds 		/* Is it our report looped back? */
1075c7537967SDavid S. Miller 		if (rt_is_output_route(skb_rtable(skb)))
10761da177e4SLinus Torvalds 			break;
107724c69275SDavid Stevens 		/* don't rely on MC router hearing unicast reports */
107824c69275SDavid Stevens 		if (skb->pkt_type == PACKET_MULTICAST ||
107924c69275SDavid Stevens 		    skb->pkt_type == PACKET_BROADCAST)
1080d679c532SEric Dumazet 			dropped = igmp_heard_report(in_dev, ih->group);
10811da177e4SLinus Torvalds 		break;
10821da177e4SLinus Torvalds 	case IGMP_PIM:
10831da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1
10841da177e4SLinus Torvalds 		return pim_rcv_v1(skb);
10851da177e4SLinus Torvalds #endif
1086c6b471e6SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
10871da177e4SLinus Torvalds 	case IGMP_DVMRP:
10881da177e4SLinus Torvalds 	case IGMP_TRACE:
10891da177e4SLinus Torvalds 	case IGMP_HOST_LEAVE_MESSAGE:
10901da177e4SLinus Torvalds 	case IGMP_MTRACE:
10911da177e4SLinus Torvalds 	case IGMP_MTRACE_RESP:
10921da177e4SLinus Torvalds 		break;
10931da177e4SLinus Torvalds 	default:
1094dd1c1853SLinus Torvalds 		break;
10951da177e4SLinus Torvalds 	}
1096fb286bb2SHerbert Xu 
1097cd557bc1SDenis V. Lunev drop:
1098d679c532SEric Dumazet 	if (dropped)
10991da177e4SLinus Torvalds 		kfree_skb(skb);
1100d679c532SEric Dumazet 	else
1101d679c532SEric Dumazet 		consume_skb(skb);
11021da177e4SLinus Torvalds 	return 0;
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds #endif
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds /*
11091da177e4SLinus Torvalds  *	Add a filter to a device
11101da177e4SLinus Torvalds  */
11111da177e4SLinus Torvalds 
111263007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
11131da177e4SLinus Torvalds {
11141da177e4SLinus Torvalds 	char buf[MAX_ADDR_LEN];
11151da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	/* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
11181da177e4SLinus Torvalds 	   We will get multicast token leakage, when IFF_MULTICAST
1119b81693d9SJiri Pirko 	   is changed. This check should be done in ndo_set_rx_mode
11201da177e4SLinus Torvalds 	   routine. Something sort of:
11211da177e4SLinus Torvalds 	   if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
11221da177e4SLinus Torvalds 	   --ANK
11231da177e4SLinus Torvalds 	   */
11241da177e4SLinus Torvalds 	if (arp_mc_map(addr, buf, dev, 0) == 0)
112522bedad3SJiri Pirko 		dev_mc_add(dev, buf);
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds /*
11291da177e4SLinus Torvalds  *	Remove a filter from a device
11301da177e4SLinus Torvalds  */
11311da177e4SLinus Torvalds 
113263007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	char buf[MAX_ADDR_LEN];
11351da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	if (arp_mc_map(addr, buf, dev, 0) == 0)
113822bedad3SJiri Pirko 		dev_mc_del(dev, buf);
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
11421da177e4SLinus Torvalds /*
11431da177e4SLinus Torvalds  * deleted ip_mc_list manipulation
11441da177e4SLinus Torvalds  */
11459fb20801SFlorian Fainelli static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
11469fb20801SFlorian Fainelli 			      gfp_t gfp)
11471da177e4SLinus Torvalds {
11481da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
1149165094afSNikolay Borisov 	struct net *net = dev_net(in_dev->dev);
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	/* this is an "ip_mc_list" for convenience; only the fields below
11521da177e4SLinus Torvalds 	 * are actually used. In particular, the refcnt and users are not
11531da177e4SLinus Torvalds 	 * used for management of the delete list. Using the same structure
11541da177e4SLinus Torvalds 	 * for deleted items allows change reports to use common code with
11551da177e4SLinus Torvalds 	 * non-deleted or query-response MCA's.
11561da177e4SLinus Torvalds 	 */
11579fb20801SFlorian Fainelli 	pmc = kzalloc(sizeof(*pmc), gfp);
11581da177e4SLinus Torvalds 	if (!pmc)
11591da177e4SLinus Torvalds 		return;
1160b4846fc3SWANG Cong 	spin_lock_init(&pmc->lock);
11611da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
11621da177e4SLinus Torvalds 	pmc->interface = im->interface;
11631da177e4SLinus Torvalds 	in_dev_hold(in_dev);
11641da177e4SLinus Torvalds 	pmc->multiaddr = im->multiaddr;
1165165094afSNikolay Borisov 	pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
11661da177e4SLinus Torvalds 	pmc->sfmode = im->sfmode;
11671da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_INCLUDE) {
11681da177e4SLinus Torvalds 		struct ip_sf_list *psf;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		pmc->tomb = im->tomb;
11711da177e4SLinus Torvalds 		pmc->sources = im->sources;
11721da177e4SLinus Torvalds 		im->tomb = im->sources = NULL;
11731da177e4SLinus Torvalds 		for (psf = pmc->sources; psf; psf = psf->sf_next)
11741da177e4SLinus Torvalds 			psf->sf_crcount = pmc->crcount;
11751da177e4SLinus Torvalds 	}
11761da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
11791da177e4SLinus Torvalds 	pmc->next = in_dev->mc_tomb;
11801da177e4SLinus Torvalds 	in_dev->mc_tomb = pmc;
11811da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
118424803f38SHangbin Liu /*
118524803f38SHangbin Liu  * restore ip_mc_list deleted records
118624803f38SHangbin Liu  */
118724803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
11881da177e4SLinus Torvalds {
11891da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *pmc_prev;
119024803f38SHangbin Liu 	struct ip_sf_list *psf;
119124803f38SHangbin Liu 	struct net *net = dev_net(in_dev->dev);
119224803f38SHangbin Liu 	__be32 multiaddr = im->multiaddr;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
11951da177e4SLinus Torvalds 	pmc_prev = NULL;
11961da177e4SLinus Torvalds 	for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
11971da177e4SLinus Torvalds 		if (pmc->multiaddr == multiaddr)
11981da177e4SLinus Torvalds 			break;
11991da177e4SLinus Torvalds 		pmc_prev = pmc;
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds 	if (pmc) {
12021da177e4SLinus Torvalds 		if (pmc_prev)
12031da177e4SLinus Torvalds 			pmc_prev->next = pmc->next;
12041da177e4SLinus Torvalds 		else
12051da177e4SLinus Torvalds 			in_dev->mc_tomb = pmc->next;
12061da177e4SLinus Torvalds 	}
12071da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
120824803f38SHangbin Liu 
120924803f38SHangbin Liu 	spin_lock_bh(&im->lock);
12101da177e4SLinus Torvalds 	if (pmc) {
121124803f38SHangbin Liu 		im->interface = pmc->interface;
121208d3ffccSHangbin Liu 		if (im->sfmode == MCAST_INCLUDE) {
121324803f38SHangbin Liu 			im->tomb = pmc->tomb;
121424803f38SHangbin Liu 			im->sources = pmc->sources;
121524803f38SHangbin Liu 			for (psf = im->sources; psf; psf = psf->sf_next)
12166e2059b5SHangbin Liu 				psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
12176e2059b5SHangbin Liu 		} else {
12186e2059b5SHangbin Liu 			im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
12191da177e4SLinus Torvalds 		}
12201da177e4SLinus Torvalds 		in_dev_put(pmc->interface);
12219c8bb163SHangbin Liu 		kfree(pmc);
12221da177e4SLinus Torvalds 	}
122324803f38SHangbin Liu 	spin_unlock_bh(&im->lock);
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
122624803f38SHangbin Liu /*
122724803f38SHangbin Liu  * flush ip_mc_list deleted records
122824803f38SHangbin Liu  */
12291da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *nextpmc;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
12341da177e4SLinus Torvalds 	pmc = in_dev->mc_tomb;
12351da177e4SLinus Torvalds 	in_dev->mc_tomb = NULL;
12361da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	for (; pmc; pmc = nextpmc) {
12391da177e4SLinus Torvalds 		nextpmc = pmc->next;
12401da177e4SLinus Torvalds 		ip_mc_clear_src(pmc);
12411da177e4SLinus Torvalds 		in_dev_put(pmc->interface);
12421da177e4SLinus Torvalds 		kfree(pmc);
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 	/* clear dead sources, too */
12451d7138deSEric Dumazet 	rcu_read_lock();
12461d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
12471da177e4SLinus Torvalds 		struct ip_sf_list *psf, *psf_next;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
12501da177e4SLinus Torvalds 		psf = pmc->tomb;
12511da177e4SLinus Torvalds 		pmc->tomb = NULL;
12521da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
12531da177e4SLinus Torvalds 		for (; psf; psf = psf_next) {
12541da177e4SLinus Torvalds 			psf_next = psf->sf_next;
12551da177e4SLinus Torvalds 			kfree(psf);
12561da177e4SLinus Torvalds 		}
12571da177e4SLinus Torvalds 	}
12581d7138deSEric Dumazet 	rcu_read_unlock();
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds #endif
12611da177e4SLinus Torvalds 
12629fb20801SFlorian Fainelli static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
12651da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
126687a8a2aeSNikolay Borisov 	struct net *net = dev_net(in_dev->dev);
12671da177e4SLinus Torvalds 	int reporter;
12681da177e4SLinus Torvalds #endif
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	if (im->loaded) {
12711da177e4SLinus Torvalds 		im->loaded = 0;
12721da177e4SLinus Torvalds 		ip_mc_filter_del(in_dev, im->multiaddr);
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
12761da177e4SLinus Torvalds 	if (im->multiaddr == IGMP_ALL_HOSTS)
12771da177e4SLinus Torvalds 		return;
127887a8a2aeSNikolay Borisov 	if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1279df2cf4a7SPhilip Downey 		return;
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	reporter = im->reporter;
12821da177e4SLinus Torvalds 	igmp_stop_timer(im);
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	if (!in_dev->dead) {
12851da177e4SLinus Torvalds 		if (IGMP_V1_SEEN(in_dev))
128624cf3af3SVeaceslav Falico 			return;
12871da177e4SLinus Torvalds 		if (IGMP_V2_SEEN(in_dev)) {
12881da177e4SLinus Torvalds 			if (reporter)
12891da177e4SLinus Torvalds 				igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
129024cf3af3SVeaceslav Falico 			return;
12911da177e4SLinus Torvalds 		}
12921da177e4SLinus Torvalds 		/* IGMPv3 */
12939fb20801SFlorian Fainelli 		igmpv3_add_delrec(in_dev, im, gfp);
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
12961da177e4SLinus Torvalds 	}
12971da177e4SLinus Torvalds #endif
12981da177e4SLinus Torvalds }
12991da177e4SLinus Torvalds 
13009fb20801SFlorian Fainelli static void igmp_group_dropped(struct ip_mc_list *im)
13019fb20801SFlorian Fainelli {
13029fb20801SFlorian Fainelli 	__igmp_group_dropped(im, GFP_KERNEL);
13039fb20801SFlorian Fainelli }
13049fb20801SFlorian Fainelli 
13050ae0d60aSHangbin Liu static void igmp_group_added(struct ip_mc_list *im)
13061da177e4SLinus Torvalds {
13071da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
1308dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST
130987a8a2aeSNikolay Borisov 	struct net *net = dev_net(in_dev->dev);
1310dcd87999SNikolay Borisov #endif
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	if (im->loaded == 0) {
13131da177e4SLinus Torvalds 		im->loaded = 1;
13141da177e4SLinus Torvalds 		ip_mc_filter_add(in_dev, im->multiaddr);
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
13181da177e4SLinus Torvalds 	if (im->multiaddr == IGMP_ALL_HOSTS)
13191da177e4SLinus Torvalds 		return;
132087a8a2aeSNikolay Borisov 	if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
1321df2cf4a7SPhilip Downey 		return;
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	if (in_dev->dead)
13241da177e4SLinus Torvalds 		return;
1325ff06525fSHangbin Liu 
1326ff06525fSHangbin Liu 	im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
13271da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
13281da177e4SLinus Torvalds 		spin_lock_bh(&im->lock);
1329436f7c20SFabian Frederick 		igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
13301da177e4SLinus Torvalds 		spin_unlock_bh(&im->lock);
13311da177e4SLinus Torvalds 		return;
13321da177e4SLinus Torvalds 	}
13331da177e4SLinus Torvalds 	/* else, v3 */
13341da177e4SLinus Torvalds 
13356e2059b5SHangbin Liu 	/* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
13366e2059b5SHangbin Liu 	 * not send filter-mode change record as the mode should be from
13376e2059b5SHangbin Liu 	 * IN() to IN(A).
13386e2059b5SHangbin Liu 	 */
13390ae0d60aSHangbin Liu 	if (im->sfmode == MCAST_EXCLUDE)
1340165094afSNikolay Borisov 		im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
13416e2059b5SHangbin Liu 
13421da177e4SLinus Torvalds 	igmp_ifc_event(in_dev);
13431da177e4SLinus Torvalds #endif
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds /*
13481da177e4SLinus Torvalds  *	Multicast list managers
13491da177e4SLinus Torvalds  */
13501da177e4SLinus Torvalds 
1351e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im)
1352e9897071SEric Dumazet {
1353c70eba74SEric Dumazet 	return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1354e9897071SEric Dumazet }
1355e9897071SEric Dumazet 
1356e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev,
1357e9897071SEric Dumazet 			   struct ip_mc_list *im)
1358e9897071SEric Dumazet {
1359e9897071SEric Dumazet 	struct ip_mc_list __rcu **mc_hash;
1360e9897071SEric Dumazet 	u32 hash;
1361e9897071SEric Dumazet 
1362e9897071SEric Dumazet 	mc_hash = rtnl_dereference(in_dev->mc_hash);
1363e9897071SEric Dumazet 	if (mc_hash) {
1364e9897071SEric Dumazet 		hash = ip_mc_hash(im);
1365c70eba74SEric Dumazet 		im->next_hash = mc_hash[hash];
1366e9897071SEric Dumazet 		rcu_assign_pointer(mc_hash[hash], im);
1367e9897071SEric Dumazet 		return;
1368e9897071SEric Dumazet 	}
1369e9897071SEric Dumazet 
1370e9897071SEric Dumazet 	/* do not use a hash table for small number of items */
1371e9897071SEric Dumazet 	if (in_dev->mc_count < 4)
1372e9897071SEric Dumazet 		return;
1373e9897071SEric Dumazet 
1374e9897071SEric Dumazet 	mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1375e9897071SEric Dumazet 			  GFP_KERNEL);
1376e9897071SEric Dumazet 	if (!mc_hash)
1377e9897071SEric Dumazet 		return;
1378e9897071SEric Dumazet 
1379e9897071SEric Dumazet 	for_each_pmc_rtnl(in_dev, im) {
1380e9897071SEric Dumazet 		hash = ip_mc_hash(im);
1381c70eba74SEric Dumazet 		im->next_hash = mc_hash[hash];
1382e9897071SEric Dumazet 		RCU_INIT_POINTER(mc_hash[hash], im);
1383e9897071SEric Dumazet 	}
1384e9897071SEric Dumazet 
1385e9897071SEric Dumazet 	rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1386e9897071SEric Dumazet }
1387e9897071SEric Dumazet 
1388e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev,
1389e9897071SEric Dumazet 			      struct ip_mc_list *im)
1390e9897071SEric Dumazet {
1391e9897071SEric Dumazet 	struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1392e9897071SEric Dumazet 	struct ip_mc_list *aux;
1393e9897071SEric Dumazet 
1394e9897071SEric Dumazet 	if (!mc_hash)
1395e9897071SEric Dumazet 		return;
1396e9897071SEric Dumazet 	mc_hash += ip_mc_hash(im);
1397e9897071SEric Dumazet 	while ((aux = rtnl_dereference(*mc_hash)) != im)
1398e9897071SEric Dumazet 		mc_hash = &aux->next_hash;
1399e9897071SEric Dumazet 	*mc_hash = im->next_hash;
1400e9897071SEric Dumazet }
1401e9897071SEric Dumazet 
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds /*
14041da177e4SLinus Torvalds  *	A socket has joined a multicast group on device dev.
14051da177e4SLinus Torvalds  */
14069fb20801SFlorian Fainelli static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
14079fb20801SFlorian Fainelli 				unsigned int mode, gfp_t gfp)
14081da177e4SLinus Torvalds {
14091da177e4SLinus Torvalds 	struct ip_mc_list *im;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	ASSERT_RTNL();
14121da177e4SLinus Torvalds 
14131d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, im) {
14141da177e4SLinus Torvalds 		if (im->multiaddr == addr) {
14151da177e4SLinus Torvalds 			im->users++;
14166e2059b5SHangbin Liu 			ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
14171da177e4SLinus Torvalds 			goto out;
14181da177e4SLinus Torvalds 		}
14191da177e4SLinus Torvalds 	}
14201da177e4SLinus Torvalds 
14219fb20801SFlorian Fainelli 	im = kzalloc(sizeof(*im), gfp);
14221da177e4SLinus Torvalds 	if (!im)
14231da177e4SLinus Torvalds 		goto out;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	im->users = 1;
14261da177e4SLinus Torvalds 	im->interface = in_dev;
14271da177e4SLinus Torvalds 	in_dev_hold(in_dev);
14281da177e4SLinus Torvalds 	im->multiaddr = addr;
14291da177e4SLinus Torvalds 	/* initial mode is (EX, empty) */
14306e2059b5SHangbin Liu 	im->sfmode = mode;
14316e2059b5SHangbin Liu 	im->sfcount[mode] = 1;
14328851ab52SReshetova, Elena 	refcount_set(&im->refcnt, 1);
14331da177e4SLinus Torvalds 	spin_lock_init(&im->lock);
14341da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1435e99e88a9SKees Cook 	timer_setup(&im->timer, igmp_timer_expire, 0);
14361da177e4SLinus Torvalds #endif
14371d7138deSEric Dumazet 
14381d7138deSEric Dumazet 	im->next_rcu = in_dev->mc_list;
1439b8bae41eSRami Rosen 	in_dev->mc_count++;
1440cf778b00SEric Dumazet 	rcu_assign_pointer(in_dev->mc_list, im);
14411d7138deSEric Dumazet 
1442e9897071SEric Dumazet 	ip_mc_hash_add(in_dev, im);
1443e9897071SEric Dumazet 
14441da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
144524803f38SHangbin Liu 	igmpv3_del_delrec(in_dev, im);
14461da177e4SLinus Torvalds #endif
14470ae0d60aSHangbin Liu 	igmp_group_added(im);
14481da177e4SLinus Torvalds 	if (!in_dev->dead)
14491da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
14501da177e4SLinus Torvalds out:
14511da177e4SLinus Torvalds 	return;
14521da177e4SLinus Torvalds }
14536e2059b5SHangbin Liu 
14549fb20801SFlorian Fainelli void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
14559fb20801SFlorian Fainelli {
14569fb20801SFlorian Fainelli 	____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
14579fb20801SFlorian Fainelli }
14589fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_inc_group);
14599fb20801SFlorian Fainelli 
14606e2059b5SHangbin Liu void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
14616e2059b5SHangbin Liu {
14626e2059b5SHangbin Liu 	__ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE);
14636e2059b5SHangbin Liu }
14644bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group);
14651da177e4SLinus Torvalds 
14669afd85c9SLinus Lüssing static int ip_mc_check_iphdr(struct sk_buff *skb)
14679afd85c9SLinus Lüssing {
14689afd85c9SLinus Lüssing 	const struct iphdr *iph;
14699afd85c9SLinus Lüssing 	unsigned int len;
14709afd85c9SLinus Lüssing 	unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
14719afd85c9SLinus Lüssing 
14729afd85c9SLinus Lüssing 	if (!pskb_may_pull(skb, offset))
14739afd85c9SLinus Lüssing 		return -EINVAL;
14749afd85c9SLinus Lüssing 
14759afd85c9SLinus Lüssing 	iph = ip_hdr(skb);
14769afd85c9SLinus Lüssing 
14779afd85c9SLinus Lüssing 	if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
14789afd85c9SLinus Lüssing 		return -EINVAL;
14799afd85c9SLinus Lüssing 
14809afd85c9SLinus Lüssing 	offset += ip_hdrlen(skb) - sizeof(*iph);
14819afd85c9SLinus Lüssing 
14829afd85c9SLinus Lüssing 	if (!pskb_may_pull(skb, offset))
14839afd85c9SLinus Lüssing 		return -EINVAL;
14849afd85c9SLinus Lüssing 
14859afd85c9SLinus Lüssing 	iph = ip_hdr(skb);
14869afd85c9SLinus Lüssing 
14879afd85c9SLinus Lüssing 	if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
14889afd85c9SLinus Lüssing 		return -EINVAL;
14899afd85c9SLinus Lüssing 
14909afd85c9SLinus Lüssing 	len = skb_network_offset(skb) + ntohs(iph->tot_len);
14919afd85c9SLinus Lüssing 	if (skb->len < len || len < offset)
14929afd85c9SLinus Lüssing 		return -EINVAL;
14939afd85c9SLinus Lüssing 
14949afd85c9SLinus Lüssing 	skb_set_transport_header(skb, offset);
14959afd85c9SLinus Lüssing 
14969afd85c9SLinus Lüssing 	return 0;
14979afd85c9SLinus Lüssing }
14989afd85c9SLinus Lüssing 
14999afd85c9SLinus Lüssing static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
15009afd85c9SLinus Lüssing {
15019afd85c9SLinus Lüssing 	unsigned int len = skb_transport_offset(skb);
15029afd85c9SLinus Lüssing 
15039afd85c9SLinus Lüssing 	len += sizeof(struct igmpv3_report);
15049afd85c9SLinus Lüssing 
1505a2e2ca3bSLinus Lüssing 	return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
15069afd85c9SLinus Lüssing }
15079afd85c9SLinus Lüssing 
15089afd85c9SLinus Lüssing static int ip_mc_check_igmp_query(struct sk_buff *skb)
15099afd85c9SLinus Lüssing {
1510a2e2ca3bSLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
1511a2e2ca3bSLinus Lüssing 	unsigned int len;
15129afd85c9SLinus Lüssing 
15139afd85c9SLinus Lüssing 	/* IGMPv{1,2}? */
1514a2e2ca3bSLinus Lüssing 	if (transport_len != sizeof(struct igmphdr)) {
15159afd85c9SLinus Lüssing 		/* or IGMPv3? */
1516a2e2ca3bSLinus Lüssing 		if (transport_len < sizeof(struct igmpv3_query))
1517a2e2ca3bSLinus Lüssing 			return -EINVAL;
1518a2e2ca3bSLinus Lüssing 
1519a2e2ca3bSLinus Lüssing 		len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1520a2e2ca3bSLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
15219afd85c9SLinus Lüssing 			return -EINVAL;
15229afd85c9SLinus Lüssing 	}
15239afd85c9SLinus Lüssing 
15249afd85c9SLinus Lüssing 	/* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
15259afd85c9SLinus Lüssing 	 * all-systems destination addresses (224.0.0.1) for general queries
15269afd85c9SLinus Lüssing 	 */
15279afd85c9SLinus Lüssing 	if (!igmp_hdr(skb)->group &&
15289afd85c9SLinus Lüssing 	    ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
15299afd85c9SLinus Lüssing 		return -EINVAL;
15309afd85c9SLinus Lüssing 
15319afd85c9SLinus Lüssing 	return 0;
15329afd85c9SLinus Lüssing }
15339afd85c9SLinus Lüssing 
15349afd85c9SLinus Lüssing static int ip_mc_check_igmp_msg(struct sk_buff *skb)
15359afd85c9SLinus Lüssing {
15369afd85c9SLinus Lüssing 	switch (igmp_hdr(skb)->type) {
15379afd85c9SLinus Lüssing 	case IGMP_HOST_LEAVE_MESSAGE:
15389afd85c9SLinus Lüssing 	case IGMP_HOST_MEMBERSHIP_REPORT:
15399afd85c9SLinus Lüssing 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
15409afd85c9SLinus Lüssing 		return 0;
15419afd85c9SLinus Lüssing 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
15429afd85c9SLinus Lüssing 		return ip_mc_check_igmp_reportv3(skb);
15439afd85c9SLinus Lüssing 	case IGMP_HOST_MEMBERSHIP_QUERY:
15449afd85c9SLinus Lüssing 		return ip_mc_check_igmp_query(skb);
15459afd85c9SLinus Lüssing 	default:
15469afd85c9SLinus Lüssing 		return -ENOMSG;
15479afd85c9SLinus Lüssing 	}
15489afd85c9SLinus Lüssing }
15499afd85c9SLinus Lüssing 
15509afd85c9SLinus Lüssing static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
15519afd85c9SLinus Lüssing {
15529afd85c9SLinus Lüssing 	return skb_checksum_simple_validate(skb);
15539afd85c9SLinus Lüssing }
15549afd85c9SLinus Lüssing 
1555a2e2ca3bSLinus Lüssing static int ip_mc_check_igmp_csum(struct sk_buff *skb)
15569afd85c9SLinus Lüssing {
15579afd85c9SLinus Lüssing 	unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1558a2e2ca3bSLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
1559a2e2ca3bSLinus Lüssing 	struct sk_buff *skb_chk;
15609afd85c9SLinus Lüssing 
1561a2e2ca3bSLinus Lüssing 	if (!ip_mc_may_pull(skb, len))
1562a2e2ca3bSLinus Lüssing 		return -EINVAL;
15639afd85c9SLinus Lüssing 
15649afd85c9SLinus Lüssing 	skb_chk = skb_checksum_trimmed(skb, transport_len,
15659afd85c9SLinus Lüssing 				       ip_mc_validate_checksum);
15669afd85c9SLinus Lüssing 	if (!skb_chk)
1567a2e2ca3bSLinus Lüssing 		return -EINVAL;
15689afd85c9SLinus Lüssing 
1569a2e2ca3bSLinus Lüssing 	if (skb_chk != skb)
1570a516993fSLinus Lüssing 		kfree_skb(skb_chk);
1571a516993fSLinus Lüssing 
1572a2e2ca3bSLinus Lüssing 	return 0;
15739afd85c9SLinus Lüssing }
15749afd85c9SLinus Lüssing 
15759afd85c9SLinus Lüssing /**
15769afd85c9SLinus Lüssing  * ip_mc_check_igmp - checks whether this is a sane IGMP packet
15779afd85c9SLinus Lüssing  * @skb: the skb to validate
15789afd85c9SLinus Lüssing  *
15799afd85c9SLinus Lüssing  * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1580a516993fSLinus Lüssing  * skb transport header accordingly and returns zero.
15819afd85c9SLinus Lüssing  *
15829afd85c9SLinus Lüssing  * -EINVAL: A broken packet was detected, i.e. it violates some internet
15839afd85c9SLinus Lüssing  *  standard
15849afd85c9SLinus Lüssing  * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
15859afd85c9SLinus Lüssing  * -ENOMEM: A memory allocation failure happened.
15869afd85c9SLinus Lüssing  *
1587a516993fSLinus Lüssing  * Caller needs to set the skb network header and free any returned skb if it
1588a516993fSLinus Lüssing  * differs from the provided skb.
15899afd85c9SLinus Lüssing  */
1590ba5ea614SLinus Lüssing int ip_mc_check_igmp(struct sk_buff *skb)
15919afd85c9SLinus Lüssing {
15929afd85c9SLinus Lüssing 	int ret = ip_mc_check_iphdr(skb);
15939afd85c9SLinus Lüssing 
15949afd85c9SLinus Lüssing 	if (ret < 0)
15959afd85c9SLinus Lüssing 		return ret;
15969afd85c9SLinus Lüssing 
15979afd85c9SLinus Lüssing 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
15989afd85c9SLinus Lüssing 		return -ENOMSG;
15999afd85c9SLinus Lüssing 
1600a2e2ca3bSLinus Lüssing 	ret = ip_mc_check_igmp_csum(skb);
1601a2e2ca3bSLinus Lüssing 	if (ret < 0)
1602a2e2ca3bSLinus Lüssing 		return ret;
1603a2e2ca3bSLinus Lüssing 
1604a2e2ca3bSLinus Lüssing 	return ip_mc_check_igmp_msg(skb);
16059afd85c9SLinus Lüssing }
16069afd85c9SLinus Lüssing EXPORT_SYMBOL(ip_mc_check_igmp);
16079afd85c9SLinus Lüssing 
16081da177e4SLinus Torvalds /*
16094aa5dee4SJiri Pirko  *	Resend IGMP JOIN report; used by netdev notifier.
1610a816c7c7SJay Vosburgh  */
16114aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev)
1612a816c7c7SJay Vosburgh {
161308882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST
1614866f3b25SEric Dumazet 	struct ip_mc_list *im;
1615866f3b25SEric Dumazet 	int type;
161687a8a2aeSNikolay Borisov 	struct net *net = dev_net(in_dev->dev);
1617a816c7c7SJay Vosburgh 
16184aa5dee4SJiri Pirko 	ASSERT_RTNL();
16194aa5dee4SJiri Pirko 
16204aa5dee4SJiri Pirko 	for_each_pmc_rtnl(in_dev, im) {
1621a816c7c7SJay Vosburgh 		if (im->multiaddr == IGMP_ALL_HOSTS)
1622866f3b25SEric Dumazet 			continue;
1623df2cf4a7SPhilip Downey 		if (ipv4_is_local_multicast(im->multiaddr) &&
162487a8a2aeSNikolay Borisov 		    !net->ipv4.sysctl_igmp_llm_reports)
1625df2cf4a7SPhilip Downey 			continue;
1626a816c7c7SJay Vosburgh 
1627e12b4539SFlavio Leitner 		/* a failover is happening and switches
1628866f3b25SEric Dumazet 		 * must be notified immediately
1629866f3b25SEric Dumazet 		 */
1630e12b4539SFlavio Leitner 		if (IGMP_V1_SEEN(in_dev))
1631866f3b25SEric Dumazet 			type = IGMP_HOST_MEMBERSHIP_REPORT;
1632e12b4539SFlavio Leitner 		else if (IGMP_V2_SEEN(in_dev))
1633866f3b25SEric Dumazet 			type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1634e12b4539SFlavio Leitner 		else
1635866f3b25SEric Dumazet 			type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1636866f3b25SEric Dumazet 		igmp_send_report(in_dev, im, type);
1637866f3b25SEric Dumazet 	}
1638a816c7c7SJay Vosburgh #endif
1639a816c7c7SJay Vosburgh }
1640a816c7c7SJay Vosburgh 
1641a816c7c7SJay Vosburgh /*
16421da177e4SLinus Torvalds  *	A socket has left a multicast group on device dev
16431da177e4SLinus Torvalds  */
16441da177e4SLinus Torvalds 
16459fb20801SFlorian Fainelli void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
16461da177e4SLinus Torvalds {
16471d7138deSEric Dumazet 	struct ip_mc_list *i;
16481d7138deSEric Dumazet 	struct ip_mc_list __rcu **ip;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	ASSERT_RTNL();
16511da177e4SLinus Torvalds 
16521d7138deSEric Dumazet 	for (ip = &in_dev->mc_list;
16531d7138deSEric Dumazet 	     (i = rtnl_dereference(*ip)) != NULL;
16541d7138deSEric Dumazet 	     ip = &i->next_rcu) {
16551da177e4SLinus Torvalds 		if (i->multiaddr == addr) {
16561da177e4SLinus Torvalds 			if (--i->users == 0) {
1657e9897071SEric Dumazet 				ip_mc_hash_remove(in_dev, i);
16581d7138deSEric Dumazet 				*ip = i->next_rcu;
1659b8bae41eSRami Rosen 				in_dev->mc_count--;
16609fb20801SFlorian Fainelli 				__igmp_group_dropped(i, gfp);
166124cf3af3SVeaceslav Falico 				ip_mc_clear_src(i);
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 				if (!in_dev->dead)
16641da177e4SLinus Torvalds 					ip_rt_multicast_event(in_dev);
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 				ip_ma_put(i);
16671da177e4SLinus Torvalds 				return;
16681da177e4SLinus Torvalds 			}
16691da177e4SLinus Torvalds 			break;
16701da177e4SLinus Torvalds 		}
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds }
16739fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_dec_group);
16741da177e4SLinus Torvalds 
167575c78500SMoni Shoua /* Device changing type */
167675c78500SMoni Shoua 
167775c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev)
167875c78500SMoni Shoua {
16791d7138deSEric Dumazet 	struct ip_mc_list *pmc;
168075c78500SMoni Shoua 
168175c78500SMoni Shoua 	ASSERT_RTNL();
168275c78500SMoni Shoua 
16831d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
16841d7138deSEric Dumazet 		igmp_group_dropped(pmc);
168575c78500SMoni Shoua }
168675c78500SMoni Shoua 
168775c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev)
168875c78500SMoni Shoua {
16891d7138deSEric Dumazet 	struct ip_mc_list *pmc;
169075c78500SMoni Shoua 
169175c78500SMoni Shoua 	ASSERT_RTNL();
169275c78500SMoni Shoua 
169324803f38SHangbin Liu 	for_each_pmc_rtnl(in_dev, pmc) {
169424803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST
169524803f38SHangbin Liu 		igmpv3_del_delrec(in_dev, pmc);
169624803f38SHangbin Liu #endif
16970ae0d60aSHangbin Liu 		igmp_group_added(pmc);
169875c78500SMoni Shoua 	}
169924803f38SHangbin Liu }
170075c78500SMoni Shoua 
17011da177e4SLinus Torvalds /* Device going down */
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev)
17041da177e4SLinus Torvalds {
17051d7138deSEric Dumazet 	struct ip_mc_list *pmc;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	ASSERT_RTNL();
17081da177e4SLinus Torvalds 
17091d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
17101d7138deSEric Dumazet 		igmp_group_dropped(pmc);
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
17131da177e4SLinus Torvalds 	in_dev->mr_ifc_count = 0;
17141da177e4SLinus Torvalds 	if (del_timer(&in_dev->mr_ifc_timer))
17151da177e4SLinus Torvalds 		__in_dev_put(in_dev);
17161da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
17171da177e4SLinus Torvalds 	if (del_timer(&in_dev->mr_gq_timer))
17181da177e4SLinus Torvalds 		__in_dev_put(in_dev);
17191da177e4SLinus Torvalds #endif
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds 
1724966c37f2SHangbin Liu #ifdef CONFIG_IP_MULTICAST
1725966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev)
1726966c37f2SHangbin Liu {
1727966c37f2SHangbin Liu 	struct net *net = dev_net(in_dev->dev);
1728966c37f2SHangbin Liu 
1729966c37f2SHangbin Liu 	in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1730966c37f2SHangbin Liu 	in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1731966c37f2SHangbin Liu 	in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1732966c37f2SHangbin Liu }
1733966c37f2SHangbin Liu #else
1734966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev)
1735966c37f2SHangbin Liu {
1736966c37f2SHangbin Liu }
1737966c37f2SHangbin Liu #endif
1738966c37f2SHangbin Liu 
17391da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev)
17401da177e4SLinus Torvalds {
17411da177e4SLinus Torvalds 	ASSERT_RTNL();
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1744e99e88a9SKees Cook 	timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1745e99e88a9SKees Cook 	timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
17461da177e4SLinus Torvalds #endif
1747966c37f2SHangbin Liu 	ip_mc_reset(in_dev);
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	spin_lock_init(&in_dev->mc_tomb_lock);
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds /* Device going up */
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev)
17551da177e4SLinus Torvalds {
17561d7138deSEric Dumazet 	struct ip_mc_list *pmc;
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	ASSERT_RTNL();
17591da177e4SLinus Torvalds 
1760966c37f2SHangbin Liu 	ip_mc_reset(in_dev);
17611da177e4SLinus Torvalds 	ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
17621da177e4SLinus Torvalds 
176324803f38SHangbin Liu 	for_each_pmc_rtnl(in_dev, pmc) {
176424803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST
176524803f38SHangbin Liu 		igmpv3_del_delrec(in_dev, pmc);
176624803f38SHangbin Liu #endif
17670ae0d60aSHangbin Liu 		igmp_group_added(pmc);
17681da177e4SLinus Torvalds 	}
176924803f38SHangbin Liu }
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds /*
17721da177e4SLinus Torvalds  *	Device is about to be destroyed: clean up.
17731da177e4SLinus Torvalds  */
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	struct ip_mc_list *i;
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	ASSERT_RTNL();
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	/* Deactivate timers */
17821da177e4SLinus Torvalds 	ip_mc_down(in_dev);
178324803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST
178424803f38SHangbin Liu 	igmpv3_clear_delrec(in_dev);
178524803f38SHangbin Liu #endif
17861da177e4SLinus Torvalds 
17871d7138deSEric Dumazet 	while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
17881d7138deSEric Dumazet 		in_dev->mc_list = i->next_rcu;
1789b8bae41eSRami Rosen 		in_dev->mc_count--;
17901da177e4SLinus Torvalds 		ip_ma_put(i);
17911da177e4SLinus Torvalds 	}
17921da177e4SLinus Torvalds }
17931da177e4SLinus Torvalds 
17949e917dcaSEric Dumazet /* RTNL is locked */
1795877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
17961da177e4SLinus Torvalds {
17971da177e4SLinus Torvalds 	struct net_device *dev = NULL;
17981da177e4SLinus Torvalds 	struct in_device *idev = NULL;
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	if (imr->imr_ifindex) {
1801877acedcSDaniel Lezcano 		idev = inetdev_by_index(net, imr->imr_ifindex);
18021da177e4SLinus Torvalds 		return idev;
18031da177e4SLinus Torvalds 	}
18041da177e4SLinus Torvalds 	if (imr->imr_address.s_addr) {
18059e917dcaSEric Dumazet 		dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
18061da177e4SLinus Torvalds 		if (!dev)
18071da177e4SLinus Torvalds 			return NULL;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 
1810b23dd4feSDavid S. Miller 	if (!dev) {
181178fbfd8aSDavid S. Miller 		struct rtable *rt = ip_route_output(net,
181278fbfd8aSDavid S. Miller 						    imr->imr_multiaddr.s_addr,
181378fbfd8aSDavid S. Miller 						    0, 0, 0);
1814b23dd4feSDavid S. Miller 		if (!IS_ERR(rt)) {
1815d8d1f30bSChangli Gao 			dev = rt->dst.dev;
18161da177e4SLinus Torvalds 			ip_rt_put(rt);
18171da177e4SLinus Torvalds 		}
1818b23dd4feSDavid S. Miller 	}
18191da177e4SLinus Torvalds 	if (dev) {
18201da177e4SLinus Torvalds 		imr->imr_ifindex = dev->ifindex;
1821e5ed6399SHerbert Xu 		idev = __in_dev_get_rtnl(dev);
18221da177e4SLinus Torvalds 	}
18231da177e4SLinus Torvalds 	return idev;
18241da177e4SLinus Torvalds }
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds /*
18271da177e4SLinus Torvalds  *	Join a socket to a group
18281da177e4SLinus Torvalds  */
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
18318f935bbdSAl Viro 	__be32 *psfsrc)
18321da177e4SLinus Torvalds {
18331da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_prev;
18341da177e4SLinus Torvalds 	int rv = 0;
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 	psf_prev = NULL;
18371da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
18381da177e4SLinus Torvalds 		if (psf->sf_inaddr == *psfsrc)
18391da177e4SLinus Torvalds 			break;
18401da177e4SLinus Torvalds 		psf_prev = psf;
18411da177e4SLinus Torvalds 	}
18421da177e4SLinus Torvalds 	if (!psf || psf->sf_count[sfmode] == 0) {
18431da177e4SLinus Torvalds 		/* source filter not found, or count wrong =>  bug */
18441da177e4SLinus Torvalds 		return -ESRCH;
18451da177e4SLinus Torvalds 	}
18461da177e4SLinus Torvalds 	psf->sf_count[sfmode]--;
18471da177e4SLinus Torvalds 	if (psf->sf_count[sfmode] == 0) {
18481da177e4SLinus Torvalds 		ip_rt_multicast_event(pmc->interface);
18491da177e4SLinus Torvalds 	}
18501da177e4SLinus Torvalds 	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
18511da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
18521da177e4SLinus Torvalds 		struct in_device *in_dev = pmc->interface;
1853165094afSNikolay Borisov 		struct net *net = dev_net(in_dev->dev);
18541da177e4SLinus Torvalds #endif
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 		/* no more filters for this source */
18571da177e4SLinus Torvalds 		if (psf_prev)
18581da177e4SLinus Torvalds 			psf_prev->sf_next = psf->sf_next;
18591da177e4SLinus Torvalds 		else
18601da177e4SLinus Torvalds 			pmc->sources = psf->sf_next;
18611da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
18621da177e4SLinus Torvalds 		if (psf->sf_oldin &&
18631da177e4SLinus Torvalds 		    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1864165094afSNikolay Borisov 			psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
18651da177e4SLinus Torvalds 			psf->sf_next = pmc->tomb;
18661da177e4SLinus Torvalds 			pmc->tomb = psf;
18671da177e4SLinus Torvalds 			rv = 1;
18681da177e4SLinus Torvalds 		} else
18691da177e4SLinus Torvalds #endif
18701da177e4SLinus Torvalds 			kfree(psf);
18711da177e4SLinus Torvalds 	}
18721da177e4SLinus Torvalds 	return rv;
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST
18761da177e4SLinus Torvalds #define igmp_ifc_event(x)	do { } while (0)
18771da177e4SLinus Torvalds #endif
18781da177e4SLinus Torvalds 
18798f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
18808f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta)
18811da177e4SLinus Torvalds {
18821da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
18831da177e4SLinus Torvalds 	int	changerec = 0;
18841da177e4SLinus Torvalds 	int	i, err;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	if (!in_dev)
18871da177e4SLinus Torvalds 		return -ENODEV;
18881d7138deSEric Dumazet 	rcu_read_lock();
18891d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
18901da177e4SLinus Torvalds 		if (*pmca == pmc->multiaddr)
18911da177e4SLinus Torvalds 			break;
18921da177e4SLinus Torvalds 	}
18931da177e4SLinus Torvalds 	if (!pmc) {
18941da177e4SLinus Torvalds 		/* MCA not found?? bug */
18951d7138deSEric Dumazet 		rcu_read_unlock();
18961da177e4SLinus Torvalds 		return -ESRCH;
18971da177e4SLinus Torvalds 	}
18981da177e4SLinus Torvalds 	spin_lock_bh(&pmc->lock);
18991d7138deSEric Dumazet 	rcu_read_unlock();
19001da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
19011da177e4SLinus Torvalds 	sf_markstate(pmc);
19021da177e4SLinus Torvalds #endif
19031da177e4SLinus Torvalds 	if (!delta) {
19041da177e4SLinus Torvalds 		err = -EINVAL;
19051da177e4SLinus Torvalds 		if (!pmc->sfcount[sfmode])
19061da177e4SLinus Torvalds 			goto out_unlock;
19071da177e4SLinus Torvalds 		pmc->sfcount[sfmode]--;
19081da177e4SLinus Torvalds 	}
19091da177e4SLinus Torvalds 	err = 0;
19101da177e4SLinus Torvalds 	for (i = 0; i < sfcount; i++) {
19111da177e4SLinus Torvalds 		int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 		changerec |= rv > 0;
19141da177e4SLinus Torvalds 		if (!err && rv < 0)
19151da177e4SLinus Torvalds 			err = rv;
19161da177e4SLinus Torvalds 	}
19171da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_EXCLUDE &&
19181da177e4SLinus Torvalds 	    pmc->sfcount[MCAST_EXCLUDE] == 0 &&
19191da177e4SLinus Torvalds 	    pmc->sfcount[MCAST_INCLUDE]) {
19201da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
19211da177e4SLinus Torvalds 		struct ip_sf_list *psf;
1922165094afSNikolay Borisov 		struct net *net = dev_net(in_dev->dev);
19231da177e4SLinus Torvalds #endif
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 		/* filter mode change */
19261da177e4SLinus Torvalds 		pmc->sfmode = MCAST_INCLUDE;
19271da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1928165094afSNikolay Borisov 		pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
19291da177e4SLinus Torvalds 		in_dev->mr_ifc_count = pmc->crcount;
19301da177e4SLinus Torvalds 		for (psf = pmc->sources; psf; psf = psf->sf_next)
19311da177e4SLinus Torvalds 			psf->sf_crcount = 0;
19321da177e4SLinus Torvalds 		igmp_ifc_event(pmc->interface);
19331da177e4SLinus Torvalds 	} else if (sf_setstate(pmc) || changerec) {
19341da177e4SLinus Torvalds 		igmp_ifc_event(pmc->interface);
19351da177e4SLinus Torvalds #endif
19361da177e4SLinus Torvalds 	}
19371da177e4SLinus Torvalds out_unlock:
19381da177e4SLinus Torvalds 	spin_unlock_bh(&pmc->lock);
19391da177e4SLinus Torvalds 	return err;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds /*
19431da177e4SLinus Torvalds  * Add multicast single-source filter to the interface list
19441da177e4SLinus Torvalds  */
19451da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
19465eb81e89SJun Zhao 	__be32 *psfsrc)
19471da177e4SLinus Torvalds {
19481da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_prev;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	psf_prev = NULL;
19511da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
19521da177e4SLinus Torvalds 		if (psf->sf_inaddr == *psfsrc)
19531da177e4SLinus Torvalds 			break;
19541da177e4SLinus Torvalds 		psf_prev = psf;
19551da177e4SLinus Torvalds 	}
19561da177e4SLinus Torvalds 	if (!psf) {
19570da974f4SPanagiotis Issaris 		psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
19581da177e4SLinus Torvalds 		if (!psf)
19591da177e4SLinus Torvalds 			return -ENOBUFS;
19601da177e4SLinus Torvalds 		psf->sf_inaddr = *psfsrc;
19611da177e4SLinus Torvalds 		if (psf_prev) {
19621da177e4SLinus Torvalds 			psf_prev->sf_next = psf;
19631da177e4SLinus Torvalds 		} else
19641da177e4SLinus Torvalds 			pmc->sources = psf;
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds 	psf->sf_count[sfmode]++;
19671da177e4SLinus Torvalds 	if (psf->sf_count[sfmode] == 1) {
19681da177e4SLinus Torvalds 		ip_rt_multicast_event(pmc->interface);
19691da177e4SLinus Torvalds 	}
19701da177e4SLinus Torvalds 	return 0;
19711da177e4SLinus Torvalds }
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
19741da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc)
19751da177e4SLinus Torvalds {
19761da177e4SLinus Torvalds 	struct ip_sf_list *psf;
19771da177e4SLinus Torvalds 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next)
19801da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
19811da177e4SLinus Torvalds 			psf->sf_oldin = mca_xcount ==
19821da177e4SLinus Torvalds 				psf->sf_count[MCAST_EXCLUDE] &&
19831da177e4SLinus Torvalds 				!psf->sf_count[MCAST_INCLUDE];
19841da177e4SLinus Torvalds 		} else
19851da177e4SLinus Torvalds 			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
19861da177e4SLinus Torvalds }
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc)
19891da177e4SLinus Torvalds {
1990ad12583fSDavid L Stevens 	struct ip_sf_list *psf, *dpsf;
19911da177e4SLinus Torvalds 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
19921da177e4SLinus Torvalds 	int qrv = pmc->interface->mr_qrv;
19931da177e4SLinus Torvalds 	int new_in, rv;
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds 	rv = 0;
19961da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
19971da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
19981da177e4SLinus Torvalds 			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
19991da177e4SLinus Torvalds 				!psf->sf_count[MCAST_INCLUDE];
20001da177e4SLinus Torvalds 		} else
20011da177e4SLinus Torvalds 			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2002ad12583fSDavid L Stevens 		if (new_in) {
2003ad12583fSDavid L Stevens 			if (!psf->sf_oldin) {
200476edc605SAl Viro 				struct ip_sf_list *prev = NULL;
2005ad12583fSDavid L Stevens 
2006ad12583fSDavid L Stevens 				for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2007ad12583fSDavid L Stevens 					if (dpsf->sf_inaddr == psf->sf_inaddr)
2008ad12583fSDavid L Stevens 						break;
2009ad12583fSDavid L Stevens 					prev = dpsf;
2010ad12583fSDavid L Stevens 				}
2011ad12583fSDavid L Stevens 				if (dpsf) {
2012ad12583fSDavid L Stevens 					if (prev)
2013ad12583fSDavid L Stevens 						prev->sf_next = dpsf->sf_next;
2014ad12583fSDavid L Stevens 					else
2015ad12583fSDavid L Stevens 						pmc->tomb = dpsf->sf_next;
2016ad12583fSDavid L Stevens 					kfree(dpsf);
2017ad12583fSDavid L Stevens 				}
20181da177e4SLinus Torvalds 				psf->sf_crcount = qrv;
20191da177e4SLinus Torvalds 				rv++;
20201da177e4SLinus Torvalds 			}
2021ad12583fSDavid L Stevens 		} else if (psf->sf_oldin) {
2022ad12583fSDavid L Stevens 
2023ad12583fSDavid L Stevens 			psf->sf_crcount = 0;
2024ad12583fSDavid L Stevens 			/*
2025ad12583fSDavid L Stevens 			 * add or update "delete" records if an active filter
2026ad12583fSDavid L Stevens 			 * is now inactive
2027ad12583fSDavid L Stevens 			 */
2028ad12583fSDavid L Stevens 			for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2029ad12583fSDavid L Stevens 				if (dpsf->sf_inaddr == psf->sf_inaddr)
2030ad12583fSDavid L Stevens 					break;
2031ad12583fSDavid L Stevens 			if (!dpsf) {
20323ed37a6fSJoe Perches 				dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2033ad12583fSDavid L Stevens 				if (!dpsf)
2034ad12583fSDavid L Stevens 					continue;
2035ad12583fSDavid L Stevens 				*dpsf = *psf;
2036ad12583fSDavid L Stevens 				/* pmc->lock held by callers */
2037ad12583fSDavid L Stevens 				dpsf->sf_next = pmc->tomb;
2038ad12583fSDavid L Stevens 				pmc->tomb = dpsf;
2039ad12583fSDavid L Stevens 			}
2040ad12583fSDavid L Stevens 			dpsf->sf_crcount = qrv;
2041ad12583fSDavid L Stevens 			rv++;
2042ad12583fSDavid L Stevens 		}
20431da177e4SLinus Torvalds 	}
20441da177e4SLinus Torvalds 	return rv;
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds #endif
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds /*
20491da177e4SLinus Torvalds  * Add multicast source filter list to the interface list
20501da177e4SLinus Torvalds  */
20518f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
20528f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta)
20531da177e4SLinus Torvalds {
20541da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
20551da177e4SLinus Torvalds 	int	isexclude;
20561da177e4SLinus Torvalds 	int	i, err;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	if (!in_dev)
20591da177e4SLinus Torvalds 		return -ENODEV;
20601d7138deSEric Dumazet 	rcu_read_lock();
20611d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
20621da177e4SLinus Torvalds 		if (*pmca == pmc->multiaddr)
20631da177e4SLinus Torvalds 			break;
20641da177e4SLinus Torvalds 	}
20651da177e4SLinus Torvalds 	if (!pmc) {
20661da177e4SLinus Torvalds 		/* MCA not found?? bug */
20671d7138deSEric Dumazet 		rcu_read_unlock();
20681da177e4SLinus Torvalds 		return -ESRCH;
20691da177e4SLinus Torvalds 	}
20701da177e4SLinus Torvalds 	spin_lock_bh(&pmc->lock);
20711d7138deSEric Dumazet 	rcu_read_unlock();
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
20741da177e4SLinus Torvalds 	sf_markstate(pmc);
20751da177e4SLinus Torvalds #endif
20761da177e4SLinus Torvalds 	isexclude = pmc->sfmode == MCAST_EXCLUDE;
20771da177e4SLinus Torvalds 	if (!delta)
20781da177e4SLinus Torvalds 		pmc->sfcount[sfmode]++;
20791da177e4SLinus Torvalds 	err = 0;
20801da177e4SLinus Torvalds 	for (i = 0; i < sfcount; i++) {
20815eb81e89SJun Zhao 		err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
20821da177e4SLinus Torvalds 		if (err)
20831da177e4SLinus Torvalds 			break;
20841da177e4SLinus Torvalds 	}
20851da177e4SLinus Torvalds 	if (err) {
20861da177e4SLinus Torvalds 		int j;
20871da177e4SLinus Torvalds 
2088685f94e6SJun Zhao 		if (!delta)
20891da177e4SLinus Torvalds 			pmc->sfcount[sfmode]--;
20901da177e4SLinus Torvalds 		for (j = 0; j < i; j++)
2091a1889c0dSJulia Lawall 			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
20921da177e4SLinus Torvalds 	} else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
20931da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
20941da177e4SLinus Torvalds 		struct ip_sf_list *psf;
2095165094afSNikolay Borisov 		struct net *net = dev_net(pmc->interface->dev);
2096cfcabdccSStephen Hemminger 		in_dev = pmc->interface;
20971da177e4SLinus Torvalds #endif
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 		/* filter mode change */
21001da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE])
21011da177e4SLinus Torvalds 			pmc->sfmode = MCAST_EXCLUDE;
21021da177e4SLinus Torvalds 		else if (pmc->sfcount[MCAST_INCLUDE])
21031da177e4SLinus Torvalds 			pmc->sfmode = MCAST_INCLUDE;
21041da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
21051da177e4SLinus Torvalds 		/* else no filters; keep old mode for reports */
21061da177e4SLinus Torvalds 
2107165094afSNikolay Borisov 		pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
21081da177e4SLinus Torvalds 		in_dev->mr_ifc_count = pmc->crcount;
21091da177e4SLinus Torvalds 		for (psf = pmc->sources; psf; psf = psf->sf_next)
21101da177e4SLinus Torvalds 			psf->sf_crcount = 0;
21111da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
21121da177e4SLinus Torvalds 	} else if (sf_setstate(pmc)) {
21131da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
21141da177e4SLinus Torvalds #endif
21151da177e4SLinus Torvalds 	}
21161da177e4SLinus Torvalds 	spin_unlock_bh(&pmc->lock);
21171da177e4SLinus Torvalds 	return err;
21181da177e4SLinus Torvalds }
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc)
21211da177e4SLinus Torvalds {
2122c38b7d32SWANG Cong 	struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
21231da177e4SLinus Torvalds 
2124c38b7d32SWANG Cong 	spin_lock_bh(&pmc->lock);
2125c38b7d32SWANG Cong 	tomb = pmc->tomb;
21261da177e4SLinus Torvalds 	pmc->tomb = NULL;
2127c38b7d32SWANG Cong 	sources = pmc->sources;
21281da177e4SLinus Torvalds 	pmc->sources = NULL;
21291da177e4SLinus Torvalds 	pmc->sfmode = MCAST_EXCLUDE;
2130de9daad9SDenis Lukianov 	pmc->sfcount[MCAST_INCLUDE] = 0;
21311da177e4SLinus Torvalds 	pmc->sfcount[MCAST_EXCLUDE] = 1;
2132c38b7d32SWANG Cong 	spin_unlock_bh(&pmc->lock);
2133c38b7d32SWANG Cong 
2134c38b7d32SWANG Cong 	for (psf = tomb; psf; psf = nextpsf) {
2135c38b7d32SWANG Cong 		nextpsf = psf->sf_next;
2136c38b7d32SWANG Cong 		kfree(psf);
2137c38b7d32SWANG Cong 	}
2138c38b7d32SWANG Cong 	for (psf = sources; psf; psf = nextpsf) {
2139c38b7d32SWANG Cong 		nextpsf = psf->sf_next;
2140c38b7d32SWANG Cong 		kfree(psf);
2141c38b7d32SWANG Cong 	}
21421da177e4SLinus Torvalds }
21431da177e4SLinus Torvalds 
214454ff9ef3SMarcelo Ricardo Leitner /* Join a multicast group
214554ff9ef3SMarcelo Ricardo Leitner  */
21466e2059b5SHangbin Liu static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
21476e2059b5SHangbin Liu 			      unsigned int mode)
21481da177e4SLinus Torvalds {
21498f935bbdSAl Viro 	__be32 addr = imr->imr_multiaddr.s_addr;
2150959d10f6SEric Dumazet 	struct ip_mc_socklist *iml, *i;
21511da177e4SLinus Torvalds 	struct in_device *in_dev;
21521da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2153877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
2154ca9b907dSDavid L Stevens 	int ifindex;
21551da177e4SLinus Torvalds 	int count = 0;
2156959d10f6SEric Dumazet 	int err;
2157959d10f6SEric Dumazet 
2158959d10f6SEric Dumazet 	ASSERT_RTNL();
21591da177e4SLinus Torvalds 
2160f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
21611da177e4SLinus Torvalds 		return -EINVAL;
21621da177e4SLinus Torvalds 
2163877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, imr);
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds 	if (!in_dev) {
21661da177e4SLinus Torvalds 		err = -ENODEV;
21671da177e4SLinus Torvalds 		goto done;
21681da177e4SLinus Torvalds 	}
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	err = -EADDRINUSE;
2171ca9b907dSDavid L Stevens 	ifindex = imr->imr_ifindex;
21721d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, i) {
2173ca9b907dSDavid L Stevens 		if (i->multi.imr_multiaddr.s_addr == addr &&
2174ca9b907dSDavid L Stevens 		    i->multi.imr_ifindex == ifindex)
21751da177e4SLinus Torvalds 			goto done;
21761da177e4SLinus Torvalds 		count++;
21771da177e4SLinus Torvalds 	}
21781da177e4SLinus Torvalds 	err = -ENOBUFS;
2179815c5270SNikolay Borisov 	if (count >= net->ipv4.sysctl_igmp_max_memberships)
21801da177e4SLinus Torvalds 		goto done;
21818b3a7005SKris Katterjohn 	iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
218251456b29SIan Morris 	if (!iml)
2183ca9b907dSDavid L Stevens 		goto done;
2184ca9b907dSDavid L Stevens 
21851da177e4SLinus Torvalds 	memcpy(&iml->multi, imr, sizeof(*imr));
21861d7138deSEric Dumazet 	iml->next_rcu = inet->mc_list;
21871da177e4SLinus Torvalds 	iml->sflist = NULL;
21886e2059b5SHangbin Liu 	iml->sfmode = mode;
2189cf778b00SEric Dumazet 	rcu_assign_pointer(inet->mc_list, iml);
21906e2059b5SHangbin Liu 	__ip_mc_inc_group(in_dev, addr, mode);
21911da177e4SLinus Torvalds 	err = 0;
21921da177e4SLinus Torvalds done:
21931da177e4SLinus Torvalds 	return err;
21941da177e4SLinus Torvalds }
21956e2059b5SHangbin Liu 
21966e2059b5SHangbin Liu /* Join ASM (Any-Source Multicast) group
21976e2059b5SHangbin Liu  */
21986e2059b5SHangbin Liu int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
21996e2059b5SHangbin Liu {
22006e2059b5SHangbin Liu 	return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
22016e2059b5SHangbin Liu }
22024bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group);
22031da177e4SLinus Torvalds 
22046e2059b5SHangbin Liu /* Join SSM (Source-Specific Multicast) group
22056e2059b5SHangbin Liu  */
22066e2059b5SHangbin Liu int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
22076e2059b5SHangbin Liu 			 unsigned int mode)
22086e2059b5SHangbin Liu {
22096e2059b5SHangbin Liu 	return __ip_mc_join_group(sk, imr, mode);
22106e2059b5SHangbin Liu }
22116e2059b5SHangbin Liu 
22121da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
22131da177e4SLinus Torvalds 			   struct in_device *in_dev)
22141da177e4SLinus Torvalds {
22151d7138deSEric Dumazet 	struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
22161da177e4SLinus Torvalds 	int err;
22171da177e4SLinus Torvalds 
221851456b29SIan Morris 	if (!psf) {
22191da177e4SLinus Torvalds 		/* any-source empty exclude case */
22201da177e4SLinus Torvalds 		return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
22211da177e4SLinus Torvalds 			iml->sfmode, 0, NULL, 0);
22221da177e4SLinus Torvalds 	}
22231da177e4SLinus Torvalds 	err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2224c85bb41eSFlavio Leitner 			iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2225a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(iml->sflist, NULL);
2226c85bb41eSFlavio Leitner 	/* decrease mem now to avoid the memleak warning */
2227c85bb41eSFlavio Leitner 	atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
22287519cce4SLai Jiangshan 	kfree_rcu(psf, rcu);
22291da177e4SLinus Torvalds 	return err;
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
223254ff9ef3SMarcelo Ricardo Leitner int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
22331da177e4SLinus Torvalds {
22341da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
22351d7138deSEric Dumazet 	struct ip_mc_socklist *iml;
22361d7138deSEric Dumazet 	struct ip_mc_socklist __rcu **imlp;
223784b42baeSDavid L Stevens 	struct in_device *in_dev;
2238877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
22398f935bbdSAl Viro 	__be32 group = imr->imr_multiaddr.s_addr;
224084b42baeSDavid L Stevens 	u32 ifindex;
2241acd6e00bSDavid L Stevens 	int ret = -EADDRNOTAVAIL;
22421da177e4SLinus Torvalds 
2243959d10f6SEric Dumazet 	ASSERT_RTNL();
2244959d10f6SEric Dumazet 
2245877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, imr);
22464eba7bb1SAndrew Lunn 	if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
224752ad353aSdingtianhong 		ret = -ENODEV;
224852ad353aSdingtianhong 		goto out;
224952ad353aSdingtianhong 	}
225084b42baeSDavid L Stevens 	ifindex = imr->imr_ifindex;
22511d7138deSEric Dumazet 	for (imlp = &inet->mc_list;
22521d7138deSEric Dumazet 	     (iml = rtnl_dereference(*imlp)) != NULL;
22531d7138deSEric Dumazet 	     imlp = &iml->next_rcu) {
2254acd6e00bSDavid L Stevens 		if (iml->multi.imr_multiaddr.s_addr != group)
2255acd6e00bSDavid L Stevens 			continue;
2256acd6e00bSDavid L Stevens 		if (ifindex) {
2257acd6e00bSDavid L Stevens 			if (iml->multi.imr_ifindex != ifindex)
2258acd6e00bSDavid L Stevens 				continue;
2259acd6e00bSDavid L Stevens 		} else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2260acd6e00bSDavid L Stevens 				iml->multi.imr_address.s_addr)
2261acd6e00bSDavid L Stevens 			continue;
2262acd6e00bSDavid L Stevens 
22631da177e4SLinus Torvalds 		(void) ip_mc_leave_src(sk, iml, in_dev);
22641da177e4SLinus Torvalds 
22651d7138deSEric Dumazet 		*imlp = iml->next_rcu;
22661da177e4SLinus Torvalds 
22674eba7bb1SAndrew Lunn 		if (in_dev)
226884b42baeSDavid L Stevens 			ip_mc_dec_group(in_dev, group);
2269959d10f6SEric Dumazet 
2270c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
2271c85bb41eSFlavio Leitner 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
227210d50e74SLai Jiangshan 		kfree_rcu(iml, rcu);
22731da177e4SLinus Torvalds 		return 0;
22741da177e4SLinus Torvalds 	}
227552ad353aSdingtianhong out:
2276959d10f6SEric Dumazet 	return ret;
2277959d10f6SEric Dumazet }
2278193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group);
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct
22811da177e4SLinus Torvalds 	ip_mreq_source *mreqs, int ifindex)
22821da177e4SLinus Torvalds {
22831da177e4SLinus Torvalds 	int err;
22841da177e4SLinus Torvalds 	struct ip_mreqn imr;
228563007727SAl Viro 	__be32 addr = mreqs->imr_multiaddr;
22861da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
22871da177e4SLinus Torvalds 	struct in_device *in_dev = NULL;
22881da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
22891da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
2290877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
22918cdaaa15SDavid L Stevens 	int leavegroup = 0;
22921da177e4SLinus Torvalds 	int i, j, rv;
22931da177e4SLinus Torvalds 
2294f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
22951da177e4SLinus Torvalds 		return -EINVAL;
22961da177e4SLinus Torvalds 
229754ff9ef3SMarcelo Ricardo Leitner 	ASSERT_RTNL();
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
23001da177e4SLinus Torvalds 	imr.imr_address.s_addr = mreqs->imr_interface;
23011da177e4SLinus Torvalds 	imr.imr_ifindex = ifindex;
2302877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	if (!in_dev) {
23051da177e4SLinus Torvalds 		err = -ENODEV;
23061da177e4SLinus Torvalds 		goto done;
23071da177e4SLinus Torvalds 	}
23081da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
23091da177e4SLinus Torvalds 
23101d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
2311f64f9e71SJoe Perches 		if ((pmc->multi.imr_multiaddr.s_addr ==
2312f64f9e71SJoe Perches 		     imr.imr_multiaddr.s_addr) &&
2313f64f9e71SJoe Perches 		    (pmc->multi.imr_ifindex == imr.imr_ifindex))
23141da177e4SLinus Torvalds 			break;
23151da177e4SLinus Torvalds 	}
2316917f2f10SDavid L Stevens 	if (!pmc) {		/* must have a prior join */
2317917f2f10SDavid L Stevens 		err = -EINVAL;
23181da177e4SLinus Torvalds 		goto done;
2319917f2f10SDavid L Stevens 	}
23201da177e4SLinus Torvalds 	/* if a source filter was set, must be the same mode as before */
23211da177e4SLinus Torvalds 	if (pmc->sflist) {
2322917f2f10SDavid L Stevens 		if (pmc->sfmode != omode) {
2323917f2f10SDavid L Stevens 			err = -EINVAL;
23241da177e4SLinus Torvalds 			goto done;
2325917f2f10SDavid L Stevens 		}
23261da177e4SLinus Torvalds 	} else if (pmc->sfmode != omode) {
23271da177e4SLinus Torvalds 		/* allow mode switches for empty-set filters */
23281da177e4SLinus Torvalds 		ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
23291da177e4SLinus Torvalds 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
23301da177e4SLinus Torvalds 			NULL, 0);
23311da177e4SLinus Torvalds 		pmc->sfmode = omode;
23321da177e4SLinus Torvalds 	}
23331da177e4SLinus Torvalds 
23341d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
23351da177e4SLinus Torvalds 	if (!add) {
23361da177e4SLinus Torvalds 		if (!psl)
2337917f2f10SDavid L Stevens 			goto done;	/* err = -EADDRNOTAVAIL */
23381da177e4SLinus Torvalds 		rv = !0;
23391da177e4SLinus Torvalds 		for (i = 0; i < psl->sl_count; i++) {
23401da177e4SLinus Torvalds 			rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
234163007727SAl Viro 				sizeof(__be32));
23421da177e4SLinus Torvalds 			if (rv == 0)
23431da177e4SLinus Torvalds 				break;
23441da177e4SLinus Torvalds 		}
23451da177e4SLinus Torvalds 		if (rv)		/* source not found */
2346917f2f10SDavid L Stevens 			goto done;	/* err = -EADDRNOTAVAIL */
23471da177e4SLinus Torvalds 
23488cdaaa15SDavid L Stevens 		/* special case - (INCLUDE, empty) == LEAVE_GROUP */
23498cdaaa15SDavid L Stevens 		if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
23508cdaaa15SDavid L Stevens 			leavegroup = 1;
23518cdaaa15SDavid L Stevens 			goto done;
23528cdaaa15SDavid L Stevens 		}
23538cdaaa15SDavid L Stevens 
23541da177e4SLinus Torvalds 		/* update the interface filter */
23551da177e4SLinus Torvalds 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
23561da177e4SLinus Torvalds 			&mreqs->imr_sourceaddr, 1);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 		for (j = i+1; j < psl->sl_count; j++)
23591da177e4SLinus Torvalds 			psl->sl_addr[j-1] = psl->sl_addr[j];
23601da177e4SLinus Torvalds 		psl->sl_count--;
23611da177e4SLinus Torvalds 		err = 0;
23621da177e4SLinus Torvalds 		goto done;
23631da177e4SLinus Torvalds 	}
23641da177e4SLinus Torvalds 	/* else, add a new source to the filter */
23651da177e4SLinus Torvalds 
2366166b6b2dSNikolay Borisov 	if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
23671da177e4SLinus Torvalds 		err = -ENOBUFS;
23681da177e4SLinus Torvalds 		goto done;
23691da177e4SLinus Torvalds 	}
23701da177e4SLinus Torvalds 	if (!psl || psl->sl_count == psl->sl_max) {
23711da177e4SLinus Torvalds 		struct ip_sf_socklist *newpsl;
23721da177e4SLinus Torvalds 		int count = IP_SFBLOCK;
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 		if (psl)
23751da177e4SLinus Torvalds 			count += psl->sl_max;
23768b3a7005SKris Katterjohn 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
23771da177e4SLinus Torvalds 		if (!newpsl) {
23781da177e4SLinus Torvalds 			err = -ENOBUFS;
23791da177e4SLinus Torvalds 			goto done;
23801da177e4SLinus Torvalds 		}
23811da177e4SLinus Torvalds 		newpsl->sl_max = count;
23821da177e4SLinus Torvalds 		newpsl->sl_count = count - IP_SFBLOCK;
23831da177e4SLinus Torvalds 		if (psl) {
23841da177e4SLinus Torvalds 			for (i = 0; i < psl->sl_count; i++)
23851da177e4SLinus Torvalds 				newpsl->sl_addr[i] = psl->sl_addr[i];
2386c85bb41eSFlavio Leitner 			/* decrease mem now to avoid the memleak warning */
2387c85bb41eSFlavio Leitner 			atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
23887519cce4SLai Jiangshan 			kfree_rcu(psl, rcu);
23891da177e4SLinus Torvalds 		}
2390cf778b00SEric Dumazet 		rcu_assign_pointer(pmc->sflist, newpsl);
2391c85bb41eSFlavio Leitner 		psl = newpsl;
23921da177e4SLinus Torvalds 	}
23931da177e4SLinus Torvalds 	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
23941da177e4SLinus Torvalds 	for (i = 0; i < psl->sl_count; i++) {
23951da177e4SLinus Torvalds 		rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
239663007727SAl Viro 			sizeof(__be32));
23971da177e4SLinus Torvalds 		if (rv == 0)
23981da177e4SLinus Torvalds 			break;
23991da177e4SLinus Torvalds 	}
24001da177e4SLinus Torvalds 	if (rv == 0)		/* address already there is an error */
24011da177e4SLinus Torvalds 		goto done;
24021da177e4SLinus Torvalds 	for (j = psl->sl_count-1; j >= i; j--)
24031da177e4SLinus Torvalds 		psl->sl_addr[j+1] = psl->sl_addr[j];
24041da177e4SLinus Torvalds 	psl->sl_addr[i] = mreqs->imr_sourceaddr;
24051da177e4SLinus Torvalds 	psl->sl_count++;
24061da177e4SLinus Torvalds 	err = 0;
24071da177e4SLinus Torvalds 	/* update the interface list */
24081da177e4SLinus Torvalds 	ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
24091da177e4SLinus Torvalds 		&mreqs->imr_sourceaddr, 1);
24101da177e4SLinus Torvalds done:
24118cdaaa15SDavid L Stevens 	if (leavegroup)
241254ff9ef3SMarcelo Ricardo Leitner 		err = ip_mc_leave_group(sk, &imr);
24131da177e4SLinus Torvalds 	return err;
24141da177e4SLinus Torvalds }
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
24171da177e4SLinus Torvalds {
24189951f036SDavid L Stevens 	int err = 0;
24191da177e4SLinus Torvalds 	struct ip_mreqn	imr;
242063007727SAl Viro 	__be32 addr = msf->imsf_multiaddr;
24211da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
24221da177e4SLinus Torvalds 	struct in_device *in_dev;
24231da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
24241da177e4SLinus Torvalds 	struct ip_sf_socklist *newpsl, *psl;
2425877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
24269951f036SDavid L Stevens 	int leavegroup = 0;
24271da177e4SLinus Torvalds 
2428f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
24291da177e4SLinus Torvalds 		return -EINVAL;
24301da177e4SLinus Torvalds 	if (msf->imsf_fmode != MCAST_INCLUDE &&
24311da177e4SLinus Torvalds 	    msf->imsf_fmode != MCAST_EXCLUDE)
24321da177e4SLinus Torvalds 		return -EINVAL;
24331da177e4SLinus Torvalds 
243454ff9ef3SMarcelo Ricardo Leitner 	ASSERT_RTNL();
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
24371da177e4SLinus Torvalds 	imr.imr_address.s_addr = msf->imsf_interface;
24381da177e4SLinus Torvalds 	imr.imr_ifindex = ifindex;
2439877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	if (!in_dev) {
24421da177e4SLinus Torvalds 		err = -ENODEV;
24431da177e4SLinus Torvalds 		goto done;
24441da177e4SLinus Torvalds 	}
24451da177e4SLinus Torvalds 
24469951f036SDavid L Stevens 	/* special case - (INCLUDE, empty) == LEAVE_GROUP */
24479951f036SDavid L Stevens 	if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
24489951f036SDavid L Stevens 		leavegroup = 1;
24499951f036SDavid L Stevens 		goto done;
24509951f036SDavid L Stevens 	}
24519951f036SDavid L Stevens 
24521d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
24531da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
24541da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
24551da177e4SLinus Torvalds 			break;
24561da177e4SLinus Torvalds 	}
2457917f2f10SDavid L Stevens 	if (!pmc) {		/* must have a prior join */
2458917f2f10SDavid L Stevens 		err = -EINVAL;
24591da177e4SLinus Torvalds 		goto done;
2460917f2f10SDavid L Stevens 	}
24611da177e4SLinus Torvalds 	if (msf->imsf_numsrc) {
24628b3a7005SKris Katterjohn 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
24638b3a7005SKris Katterjohn 							   GFP_KERNEL);
24641da177e4SLinus Torvalds 		if (!newpsl) {
24651da177e4SLinus Torvalds 			err = -ENOBUFS;
24661da177e4SLinus Torvalds 			goto done;
24671da177e4SLinus Torvalds 		}
24681da177e4SLinus Torvalds 		newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
24691da177e4SLinus Torvalds 		memcpy(newpsl->sl_addr, msf->imsf_slist,
24701da177e4SLinus Torvalds 			msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
24711da177e4SLinus Torvalds 		err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
24721da177e4SLinus Torvalds 			msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
24731da177e4SLinus Torvalds 		if (err) {
24741da177e4SLinus Torvalds 			sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
24751da177e4SLinus Torvalds 			goto done;
24761da177e4SLinus Torvalds 		}
24778713dbf0SYan Zheng 	} else {
24781da177e4SLinus Torvalds 		newpsl = NULL;
24798713dbf0SYan Zheng 		(void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
24808713dbf0SYan Zheng 				     msf->imsf_fmode, 0, NULL, 0);
24818713dbf0SYan Zheng 	}
24821d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
24831da177e4SLinus Torvalds 	if (psl) {
24841da177e4SLinus Torvalds 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
24851da177e4SLinus Torvalds 			psl->sl_count, psl->sl_addr, 0);
2486c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
2487c85bb41eSFlavio Leitner 		atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
24887519cce4SLai Jiangshan 		kfree_rcu(psl, rcu);
24891da177e4SLinus Torvalds 	} else
24901da177e4SLinus Torvalds 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
24911da177e4SLinus Torvalds 			0, NULL, 0);
2492cf778b00SEric Dumazet 	rcu_assign_pointer(pmc->sflist, newpsl);
24931da177e4SLinus Torvalds 	pmc->sfmode = msf->imsf_fmode;
2494917f2f10SDavid L Stevens 	err = 0;
24951da177e4SLinus Torvalds done:
24969951f036SDavid L Stevens 	if (leavegroup)
24979951f036SDavid L Stevens 		err = ip_mc_leave_group(sk, &imr);
24981da177e4SLinus Torvalds 	return err;
24991da177e4SLinus Torvalds }
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
25021da177e4SLinus Torvalds 	struct ip_msfilter __user *optval, int __user *optlen)
25031da177e4SLinus Torvalds {
25041da177e4SLinus Torvalds 	int err, len, count, copycount;
25051da177e4SLinus Torvalds 	struct ip_mreqn	imr;
250663007727SAl Viro 	__be32 addr = msf->imsf_multiaddr;
25071da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
25081da177e4SLinus Torvalds 	struct in_device *in_dev;
25091da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
25101da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
2511877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
25121da177e4SLinus Torvalds 
251387e9f031SWANG Cong 	ASSERT_RTNL();
251487e9f031SWANG Cong 
2515f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
25161da177e4SLinus Torvalds 		return -EINVAL;
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
25191da177e4SLinus Torvalds 	imr.imr_address.s_addr = msf->imsf_interface;
25201da177e4SLinus Torvalds 	imr.imr_ifindex = 0;
2521877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 	if (!in_dev) {
25241da177e4SLinus Torvalds 		err = -ENODEV;
25251da177e4SLinus Torvalds 		goto done;
25261da177e4SLinus Torvalds 	}
25271da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
25281da177e4SLinus Torvalds 
25291d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
25301da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
25311da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
25321da177e4SLinus Torvalds 			break;
25331da177e4SLinus Torvalds 	}
25341da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
25351da177e4SLinus Torvalds 		goto done;
25361da177e4SLinus Torvalds 	msf->imsf_fmode = pmc->sfmode;
25371d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
25381da177e4SLinus Torvalds 	if (!psl) {
25391da177e4SLinus Torvalds 		len = 0;
25401da177e4SLinus Torvalds 		count = 0;
25411da177e4SLinus Torvalds 	} else {
25421da177e4SLinus Torvalds 		count = psl->sl_count;
25431da177e4SLinus Torvalds 	}
25441da177e4SLinus Torvalds 	copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
25451da177e4SLinus Torvalds 	len = copycount * sizeof(psl->sl_addr[0]);
25461da177e4SLinus Torvalds 	msf->imsf_numsrc = count;
25471da177e4SLinus Torvalds 	if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
25481da177e4SLinus Torvalds 	    copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
25491da177e4SLinus Torvalds 		return -EFAULT;
25501da177e4SLinus Torvalds 	}
25511da177e4SLinus Torvalds 	if (len &&
25521da177e4SLinus Torvalds 	    copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
25531da177e4SLinus Torvalds 		return -EFAULT;
25541da177e4SLinus Torvalds 	return 0;
25551da177e4SLinus Torvalds done:
25561da177e4SLinus Torvalds 	return err;
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
25601da177e4SLinus Torvalds 	struct group_filter __user *optval, int __user *optlen)
25611da177e4SLinus Torvalds {
25621da177e4SLinus Torvalds 	int err, i, count, copycount;
25631da177e4SLinus Torvalds 	struct sockaddr_in *psin;
256463007727SAl Viro 	__be32 addr;
25651da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
25661da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
25671da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
25681da177e4SLinus Torvalds 
256987e9f031SWANG Cong 	ASSERT_RTNL();
257087e9f031SWANG Cong 
25711da177e4SLinus Torvalds 	psin = (struct sockaddr_in *)&gsf->gf_group;
25721da177e4SLinus Torvalds 	if (psin->sin_family != AF_INET)
25731da177e4SLinus Torvalds 		return -EINVAL;
25741da177e4SLinus Torvalds 	addr = psin->sin_addr.s_addr;
2575f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
25761da177e4SLinus Torvalds 		return -EINVAL;
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
25791da177e4SLinus Torvalds 
25801d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
25811da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == addr &&
25821da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == gsf->gf_interface)
25831da177e4SLinus Torvalds 			break;
25841da177e4SLinus Torvalds 	}
25851da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
25861da177e4SLinus Torvalds 		goto done;
25871da177e4SLinus Torvalds 	gsf->gf_fmode = pmc->sfmode;
25881d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
25891da177e4SLinus Torvalds 	count = psl ? psl->sl_count : 0;
25901da177e4SLinus Torvalds 	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
25911da177e4SLinus Torvalds 	gsf->gf_numsrc = count;
25921da177e4SLinus Torvalds 	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
25931da177e4SLinus Torvalds 	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
25941da177e4SLinus Torvalds 		return -EFAULT;
25951da177e4SLinus Torvalds 	}
25961da177e4SLinus Torvalds 	for (i = 0; i < copycount; i++) {
25971da177e4SLinus Torvalds 		struct sockaddr_storage ss;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&ss;
26001da177e4SLinus Torvalds 		memset(&ss, 0, sizeof(ss));
26011da177e4SLinus Torvalds 		psin->sin_family = AF_INET;
26021da177e4SLinus Torvalds 		psin->sin_addr.s_addr = psl->sl_addr[i];
26031da177e4SLinus Torvalds 		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
26041da177e4SLinus Torvalds 			return -EFAULT;
26051da177e4SLinus Torvalds 	}
26061da177e4SLinus Torvalds 	return 0;
26071da177e4SLinus Torvalds done:
26081da177e4SLinus Torvalds 	return err;
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds /*
26121da177e4SLinus Torvalds  * check if a multicast source filter allows delivery for a given <src,dst,intf>
26131da177e4SLinus Torvalds  */
261460d9b031SDavid Ahern int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
261560d9b031SDavid Ahern 		   int dif, int sdif)
26161da177e4SLinus Torvalds {
26171da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
26181da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
26191da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
26201da177e4SLinus Torvalds 	int i;
2621c85bb41eSFlavio Leitner 	int ret;
26221da177e4SLinus Torvalds 
2623c85bb41eSFlavio Leitner 	ret = 1;
2624f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(loc_addr))
2625c85bb41eSFlavio Leitner 		goto out;
26261da177e4SLinus Torvalds 
2627c85bb41eSFlavio Leitner 	rcu_read_lock();
26281d7138deSEric Dumazet 	for_each_pmc_rcu(inet, pmc) {
26291da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
263060d9b031SDavid Ahern 		    (pmc->multi.imr_ifindex == dif ||
263160d9b031SDavid Ahern 		     (sdif && pmc->multi.imr_ifindex == sdif)))
26321da177e4SLinus Torvalds 			break;
26331da177e4SLinus Torvalds 	}
2634c85bb41eSFlavio Leitner 	ret = inet->mc_all;
26351da177e4SLinus Torvalds 	if (!pmc)
2636c85bb41eSFlavio Leitner 		goto unlock;
26371d7138deSEric Dumazet 	psl = rcu_dereference(pmc->sflist);
2638c85bb41eSFlavio Leitner 	ret = (pmc->sfmode == MCAST_EXCLUDE);
26391da177e4SLinus Torvalds 	if (!psl)
2640c85bb41eSFlavio Leitner 		goto unlock;
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds 	for (i = 0; i < psl->sl_count; i++) {
26431da177e4SLinus Torvalds 		if (psl->sl_addr[i] == rmt_addr)
26441da177e4SLinus Torvalds 			break;
26451da177e4SLinus Torvalds 	}
2646c85bb41eSFlavio Leitner 	ret = 0;
26471da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2648c85bb41eSFlavio Leitner 		goto unlock;
26491da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2650c85bb41eSFlavio Leitner 		goto unlock;
2651c85bb41eSFlavio Leitner 	ret = 1;
2652c85bb41eSFlavio Leitner unlock:
2653c85bb41eSFlavio Leitner 	rcu_read_unlock();
2654c85bb41eSFlavio Leitner out:
2655c85bb41eSFlavio Leitner 	return ret;
26561da177e4SLinus Torvalds }
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds /*
26591da177e4SLinus Torvalds  *	A socket is closing.
26601da177e4SLinus Torvalds  */
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk)
26631da177e4SLinus Torvalds {
26641da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
26651da177e4SLinus Torvalds 	struct ip_mc_socklist *iml;
2666877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
26671da177e4SLinus Torvalds 
266851456b29SIan Morris 	if (!inet->mc_list)
26691da177e4SLinus Torvalds 		return;
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	rtnl_lock();
26721d7138deSEric Dumazet 	while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
26731da177e4SLinus Torvalds 		struct in_device *in_dev;
26741da177e4SLinus Torvalds 
26751d7138deSEric Dumazet 		inet->mc_list = iml->next_rcu;
2676877acedcSDaniel Lezcano 		in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
26771da177e4SLinus Torvalds 		(void) ip_mc_leave_src(sk, iml, in_dev);
267800db4124SIan Morris 		if (in_dev)
26791da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2680c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
2681c85bb41eSFlavio Leitner 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
268210d50e74SLai Jiangshan 		kfree_rcu(iml, rcu);
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds 	rtnl_unlock();
26851da177e4SLinus Torvalds }
26861da177e4SLinus Torvalds 
2687dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */
26882094acbbSAlexander Duyck int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
26891da177e4SLinus Torvalds {
26901da177e4SLinus Torvalds 	struct ip_mc_list *im;
2691e9897071SEric Dumazet 	struct ip_mc_list __rcu **mc_hash;
26921da177e4SLinus Torvalds 	struct ip_sf_list *psf;
26931da177e4SLinus Torvalds 	int rv = 0;
26941da177e4SLinus Torvalds 
2695e9897071SEric Dumazet 	mc_hash = rcu_dereference(in_dev->mc_hash);
2696e9897071SEric Dumazet 	if (mc_hash) {
2697c70eba74SEric Dumazet 		u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2698e9897071SEric Dumazet 
2699e9897071SEric Dumazet 		for (im = rcu_dereference(mc_hash[hash]);
2700e9897071SEric Dumazet 		     im != NULL;
2701e9897071SEric Dumazet 		     im = rcu_dereference(im->next_hash)) {
2702e9897071SEric Dumazet 			if (im->multiaddr == mc_addr)
2703e9897071SEric Dumazet 				break;
2704e9897071SEric Dumazet 		}
2705e9897071SEric Dumazet 	} else {
27061d7138deSEric Dumazet 		for_each_pmc_rcu(in_dev, im) {
27071da177e4SLinus Torvalds 			if (im->multiaddr == mc_addr)
27081da177e4SLinus Torvalds 				break;
27091da177e4SLinus Torvalds 		}
2710e9897071SEric Dumazet 	}
27111da177e4SLinus Torvalds 	if (im && proto == IPPROTO_IGMP) {
27121da177e4SLinus Torvalds 		rv = 1;
27131da177e4SLinus Torvalds 	} else if (im) {
27141da177e4SLinus Torvalds 		if (src_addr) {
27151da177e4SLinus Torvalds 			for (psf = im->sources; psf; psf = psf->sf_next) {
27161da177e4SLinus Torvalds 				if (psf->sf_inaddr == src_addr)
27171da177e4SLinus Torvalds 					break;
27181da177e4SLinus Torvalds 			}
27191da177e4SLinus Torvalds 			if (psf)
27201da177e4SLinus Torvalds 				rv = psf->sf_count[MCAST_INCLUDE] ||
27211da177e4SLinus Torvalds 					psf->sf_count[MCAST_EXCLUDE] !=
27221da177e4SLinus Torvalds 					im->sfcount[MCAST_EXCLUDE];
27231da177e4SLinus Torvalds 			else
27241da177e4SLinus Torvalds 				rv = im->sfcount[MCAST_EXCLUDE] != 0;
27251da177e4SLinus Torvalds 		} else
27261da177e4SLinus Torvalds 			rv = 1; /* unspecified source; tentatively allow */
27271da177e4SLinus Torvalds 	}
27281da177e4SLinus Torvalds 	return rv;
27291da177e4SLinus Torvalds }
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS)
27321da177e4SLinus Torvalds struct igmp_mc_iter_state {
27337091e728SAlexey Dobriyan 	struct seq_net_private p;
27341da177e4SLinus Torvalds 	struct net_device *dev;
27351da177e4SLinus Torvalds 	struct in_device *in_dev;
27361da177e4SLinus Torvalds };
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds #define	igmp_mc_seq_private(seq)	((struct igmp_mc_iter_state *)(seq)->private)
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
27411da177e4SLinus Torvalds {
27427091e728SAlexey Dobriyan 	struct net *net = seq_file_net(seq);
27431da177e4SLinus Torvalds 	struct ip_mc_list *im = NULL;
27441da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
27451da177e4SLinus Torvalds 
27467562f876SPavel Emelianov 	state->in_dev = NULL;
274761fbab77Sstephen hemminger 	for_each_netdev_rcu(net, state->dev) {
27481da177e4SLinus Torvalds 		struct in_device *in_dev;
27496baff150SEric Dumazet 
27506baff150SEric Dumazet 		in_dev = __in_dev_get_rcu(state->dev);
27511da177e4SLinus Torvalds 		if (!in_dev)
27521da177e4SLinus Torvalds 			continue;
27531d7138deSEric Dumazet 		im = rcu_dereference(in_dev->mc_list);
27541da177e4SLinus Torvalds 		if (im) {
27551da177e4SLinus Torvalds 			state->in_dev = in_dev;
27561da177e4SLinus Torvalds 			break;
27571da177e4SLinus Torvalds 		}
27581da177e4SLinus Torvalds 	}
27591da177e4SLinus Torvalds 	return im;
27601da177e4SLinus Torvalds }
27611da177e4SLinus Torvalds 
27621da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
27631da177e4SLinus Torvalds {
27641da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
27656baff150SEric Dumazet 
27661d7138deSEric Dumazet 	im = rcu_dereference(im->next_rcu);
27671d7138deSEric Dumazet 	while (!im) {
27686baff150SEric Dumazet 		state->dev = next_net_device_rcu(state->dev);
27691da177e4SLinus Torvalds 		if (!state->dev) {
27701da177e4SLinus Torvalds 			state->in_dev = NULL;
27711da177e4SLinus Torvalds 			break;
27721da177e4SLinus Torvalds 		}
27736baff150SEric Dumazet 		state->in_dev = __in_dev_get_rcu(state->dev);
27741da177e4SLinus Torvalds 		if (!state->in_dev)
27751da177e4SLinus Torvalds 			continue;
27761d7138deSEric Dumazet 		im = rcu_dereference(state->in_dev->mc_list);
27771da177e4SLinus Torvalds 	}
27781da177e4SLinus Torvalds 	return im;
27791da177e4SLinus Torvalds }
27801da177e4SLinus Torvalds 
27811da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
27821da177e4SLinus Torvalds {
27831da177e4SLinus Torvalds 	struct ip_mc_list *im = igmp_mc_get_first(seq);
27841da177e4SLinus Torvalds 	if (im)
27851da177e4SLinus Torvalds 		while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
27861da177e4SLinus Torvalds 			--pos;
27871da177e4SLinus Torvalds 	return pos ? NULL : im;
27881da177e4SLinus Torvalds }
27891da177e4SLinus Torvalds 
27901da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
279161fbab77Sstephen hemminger 	__acquires(rcu)
27921da177e4SLinus Torvalds {
279361fbab77Sstephen hemminger 	rcu_read_lock();
27941da177e4SLinus Torvalds 	return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
27951da177e4SLinus Torvalds }
27961da177e4SLinus Torvalds 
27971da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
27981da177e4SLinus Torvalds {
27991da177e4SLinus Torvalds 	struct ip_mc_list *im;
28001da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
28011da177e4SLinus Torvalds 		im = igmp_mc_get_first(seq);
28021da177e4SLinus Torvalds 	else
28031da177e4SLinus Torvalds 		im = igmp_mc_get_next(seq, v);
28041da177e4SLinus Torvalds 	++*pos;
28051da177e4SLinus Torvalds 	return im;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
280961fbab77Sstephen hemminger 	__releases(rcu)
28101da177e4SLinus Torvalds {
28111da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
28121d7138deSEric Dumazet 
28131da177e4SLinus Torvalds 	state->in_dev = NULL;
28141da177e4SLinus Torvalds 	state->dev = NULL;
281561fbab77Sstephen hemminger 	rcu_read_unlock();
28161da177e4SLinus Torvalds }
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v)
28191da177e4SLinus Torvalds {
28201da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
28211da177e4SLinus Torvalds 		seq_puts(seq,
28221da177e4SLinus Torvalds 			 "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
28231da177e4SLinus Torvalds 	else {
28241da177e4SLinus Torvalds 		struct ip_mc_list *im = (struct ip_mc_list *)v;
28251da177e4SLinus Torvalds 		struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
28261da177e4SLinus Torvalds 		char   *querier;
2827a399a805SEric Dumazet 		long delta;
2828a399a805SEric Dumazet 
28291da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
28301da177e4SLinus Torvalds 		querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
28311da177e4SLinus Torvalds 			  IGMP_V2_SEEN(state->in_dev) ? "V2" :
28321da177e4SLinus Torvalds 			  "V3";
28331da177e4SLinus Torvalds #else
28341da177e4SLinus Torvalds 		querier = "NONE";
28351da177e4SLinus Torvalds #endif
28361da177e4SLinus Torvalds 
2837e6b68883SAndreea-Cristina Bernat 		if (rcu_access_pointer(state->in_dev->mc_list) == im) {
28381da177e4SLinus Torvalds 			seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2839b8bae41eSRami Rosen 				   state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
28401da177e4SLinus Torvalds 		}
28411da177e4SLinus Torvalds 
2842a399a805SEric Dumazet 		delta = im->timer.expires - jiffies;
28431da177e4SLinus Torvalds 		seq_printf(seq,
2844338fcf98SAlexey Dobriyan 			   "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
28451da177e4SLinus Torvalds 			   im->multiaddr, im->users,
2846a399a805SEric Dumazet 			   im->tm_running,
2847a399a805SEric Dumazet 			   im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
28481da177e4SLinus Torvalds 			   im->reporter);
28491da177e4SLinus Torvalds 	}
28501da177e4SLinus Torvalds 	return 0;
28511da177e4SLinus Torvalds }
28521da177e4SLinus Torvalds 
2853f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = {
28541da177e4SLinus Torvalds 	.start	=	igmp_mc_seq_start,
28551da177e4SLinus Torvalds 	.next	=	igmp_mc_seq_next,
28561da177e4SLinus Torvalds 	.stop	=	igmp_mc_seq_stop,
28571da177e4SLinus Torvalds 	.show	=	igmp_mc_seq_show,
28581da177e4SLinus Torvalds };
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds struct igmp_mcf_iter_state {
28617091e728SAlexey Dobriyan 	struct seq_net_private p;
28621da177e4SLinus Torvalds 	struct net_device *dev;
28631da177e4SLinus Torvalds 	struct in_device *idev;
28641da177e4SLinus Torvalds 	struct ip_mc_list *im;
28651da177e4SLinus Torvalds };
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq)	((struct igmp_mcf_iter_state *)(seq)->private)
28681da177e4SLinus Torvalds 
28691da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
28701da177e4SLinus Torvalds {
28717091e728SAlexey Dobriyan 	struct net *net = seq_file_net(seq);
28721da177e4SLinus Torvalds 	struct ip_sf_list *psf = NULL;
28731da177e4SLinus Torvalds 	struct ip_mc_list *im = NULL;
28741da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
28751da177e4SLinus Torvalds 
28767562f876SPavel Emelianov 	state->idev = NULL;
28777562f876SPavel Emelianov 	state->im = NULL;
287861fbab77Sstephen hemminger 	for_each_netdev_rcu(net, state->dev) {
28791da177e4SLinus Torvalds 		struct in_device *idev;
28806baff150SEric Dumazet 		idev = __in_dev_get_rcu(state->dev);
288151456b29SIan Morris 		if (unlikely(!idev))
28821da177e4SLinus Torvalds 			continue;
28831d7138deSEric Dumazet 		im = rcu_dereference(idev->mc_list);
288400db4124SIan Morris 		if (likely(im)) {
28851da177e4SLinus Torvalds 			spin_lock_bh(&im->lock);
28861da177e4SLinus Torvalds 			psf = im->sources;
288700db4124SIan Morris 			if (likely(psf)) {
28881da177e4SLinus Torvalds 				state->im = im;
28891da177e4SLinus Torvalds 				state->idev = idev;
28901da177e4SLinus Torvalds 				break;
28911da177e4SLinus Torvalds 			}
28921da177e4SLinus Torvalds 			spin_unlock_bh(&im->lock);
28931da177e4SLinus Torvalds 		}
28941da177e4SLinus Torvalds 	}
28951da177e4SLinus Torvalds 	return psf;
28961da177e4SLinus Torvalds }
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
28991da177e4SLinus Torvalds {
29001da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
29011da177e4SLinus Torvalds 
29021da177e4SLinus Torvalds 	psf = psf->sf_next;
29031da177e4SLinus Torvalds 	while (!psf) {
29041da177e4SLinus Torvalds 		spin_unlock_bh(&state->im->lock);
29051da177e4SLinus Torvalds 		state->im = state->im->next;
29061da177e4SLinus Torvalds 		while (!state->im) {
29076baff150SEric Dumazet 			state->dev = next_net_device_rcu(state->dev);
29081da177e4SLinus Torvalds 			if (!state->dev) {
29091da177e4SLinus Torvalds 				state->idev = NULL;
29101da177e4SLinus Torvalds 				goto out;
29111da177e4SLinus Torvalds 			}
29126baff150SEric Dumazet 			state->idev = __in_dev_get_rcu(state->dev);
29131da177e4SLinus Torvalds 			if (!state->idev)
29141da177e4SLinus Torvalds 				continue;
29151d7138deSEric Dumazet 			state->im = rcu_dereference(state->idev->mc_list);
29161da177e4SLinus Torvalds 		}
29171da177e4SLinus Torvalds 		if (!state->im)
29181da177e4SLinus Torvalds 			break;
29191da177e4SLinus Torvalds 		spin_lock_bh(&state->im->lock);
29201da177e4SLinus Torvalds 		psf = state->im->sources;
29211da177e4SLinus Torvalds 	}
29221da177e4SLinus Torvalds out:
29231da177e4SLinus Torvalds 	return psf;
29241da177e4SLinus Torvalds }
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
29271da177e4SLinus Torvalds {
29281da177e4SLinus Torvalds 	struct ip_sf_list *psf = igmp_mcf_get_first(seq);
29291da177e4SLinus Torvalds 	if (psf)
29301da177e4SLinus Torvalds 		while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
29311da177e4SLinus Torvalds 			--pos;
29321da177e4SLinus Torvalds 	return pos ? NULL : psf;
29331da177e4SLinus Torvalds }
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
293661fbab77Sstephen hemminger 	__acquires(rcu)
29371da177e4SLinus Torvalds {
293861fbab77Sstephen hemminger 	rcu_read_lock();
29391da177e4SLinus Torvalds 	return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
29401da177e4SLinus Torvalds }
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
29431da177e4SLinus Torvalds {
29441da177e4SLinus Torvalds 	struct ip_sf_list *psf;
29451da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
29461da177e4SLinus Torvalds 		psf = igmp_mcf_get_first(seq);
29471da177e4SLinus Torvalds 	else
29481da177e4SLinus Torvalds 		psf = igmp_mcf_get_next(seq, v);
29491da177e4SLinus Torvalds 	++*pos;
29501da177e4SLinus Torvalds 	return psf;
29511da177e4SLinus Torvalds }
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
295461fbab77Sstephen hemminger 	__releases(rcu)
29551da177e4SLinus Torvalds {
29561da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
295700db4124SIan Morris 	if (likely(state->im)) {
29581da177e4SLinus Torvalds 		spin_unlock_bh(&state->im->lock);
29591da177e4SLinus Torvalds 		state->im = NULL;
29601da177e4SLinus Torvalds 	}
29611da177e4SLinus Torvalds 	state->idev = NULL;
29621da177e4SLinus Torvalds 	state->dev = NULL;
296361fbab77Sstephen hemminger 	rcu_read_unlock();
29641da177e4SLinus Torvalds }
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
29671da177e4SLinus Torvalds {
29681da177e4SLinus Torvalds 	struct ip_sf_list *psf = (struct ip_sf_list *)v;
29691da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
29701da177e4SLinus Torvalds 
29711da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
29721744bea1SJoe Perches 		seq_puts(seq, "Idx Device        MCA        SRC    INC    EXC\n");
29731da177e4SLinus Torvalds 	} else {
29741da177e4SLinus Torvalds 		seq_printf(seq,
29751da177e4SLinus Torvalds 			   "%3d %6.6s 0x%08x "
29761da177e4SLinus Torvalds 			   "0x%08x %6lu %6lu\n",
29771da177e4SLinus Torvalds 			   state->dev->ifindex, state->dev->name,
29781da177e4SLinus Torvalds 			   ntohl(state->im->multiaddr),
29791da177e4SLinus Torvalds 			   ntohl(psf->sf_inaddr),
29801da177e4SLinus Torvalds 			   psf->sf_count[MCAST_INCLUDE],
29811da177e4SLinus Torvalds 			   psf->sf_count[MCAST_EXCLUDE]);
29821da177e4SLinus Torvalds 	}
29831da177e4SLinus Torvalds 	return 0;
29841da177e4SLinus Torvalds }
29851da177e4SLinus Torvalds 
2986f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = {
29871da177e4SLinus Torvalds 	.start	=	igmp_mcf_seq_start,
29881da177e4SLinus Torvalds 	.next	=	igmp_mcf_seq_next,
29891da177e4SLinus Torvalds 	.stop	=	igmp_mcf_seq_stop,
29901da177e4SLinus Torvalds 	.show	=	igmp_mcf_seq_show,
29911da177e4SLinus Torvalds };
29921da177e4SLinus Torvalds 
29932c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net)
29947091e728SAlexey Dobriyan {
29957091e728SAlexey Dobriyan 	struct proc_dir_entry *pde;
299693a714d6SMadhu Challa 	int err;
29977091e728SAlexey Dobriyan 
2998c3506372SChristoph Hellwig 	pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
2999c3506372SChristoph Hellwig 			sizeof(struct igmp_mc_iter_state));
30007091e728SAlexey Dobriyan 	if (!pde)
30017091e728SAlexey Dobriyan 		goto out_igmp;
3002c3506372SChristoph Hellwig 	pde = proc_create_net("mcfilter", 0444, net->proc_net,
3003c3506372SChristoph Hellwig 			&igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
30047091e728SAlexey Dobriyan 	if (!pde)
30057091e728SAlexey Dobriyan 		goto out_mcfilter;
300693a714d6SMadhu Challa 	err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
300793a714d6SMadhu Challa 				   SOCK_DGRAM, 0, net);
300893a714d6SMadhu Challa 	if (err < 0) {
300993a714d6SMadhu Challa 		pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
301093a714d6SMadhu Challa 		       err);
301193a714d6SMadhu Challa 		goto out_sock;
301293a714d6SMadhu Challa 	}
301393a714d6SMadhu Challa 
30147091e728SAlexey Dobriyan 	return 0;
30157091e728SAlexey Dobriyan 
301693a714d6SMadhu Challa out_sock:
301793a714d6SMadhu Challa 	remove_proc_entry("mcfilter", net->proc_net);
30187091e728SAlexey Dobriyan out_mcfilter:
3019ece31ffdSGao feng 	remove_proc_entry("igmp", net->proc_net);
30207091e728SAlexey Dobriyan out_igmp:
30217091e728SAlexey Dobriyan 	return -ENOMEM;
30227091e728SAlexey Dobriyan }
30237091e728SAlexey Dobriyan 
30242c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net)
30257091e728SAlexey Dobriyan {
3026ece31ffdSGao feng 	remove_proc_entry("mcfilter", net->proc_net);
3027ece31ffdSGao feng 	remove_proc_entry("igmp", net->proc_net);
302893a714d6SMadhu Challa 	inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
30297091e728SAlexey Dobriyan }
30307091e728SAlexey Dobriyan 
30317091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = {
30327091e728SAlexey Dobriyan 	.init = igmp_net_init,
30337091e728SAlexey Dobriyan 	.exit = igmp_net_exit,
30341da177e4SLinus Torvalds };
303572c1d3bdSWANG Cong #endif
30361da177e4SLinus Torvalds 
30374aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this,
30384aa5dee4SJiri Pirko 			     unsigned long event, void *ptr)
30394aa5dee4SJiri Pirko {
30404aa5dee4SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
30414aa5dee4SJiri Pirko 	struct in_device *in_dev;
30424aa5dee4SJiri Pirko 
30434aa5dee4SJiri Pirko 	switch (event) {
30444aa5dee4SJiri Pirko 	case NETDEV_RESEND_IGMP:
30454aa5dee4SJiri Pirko 		in_dev = __in_dev_get_rtnl(dev);
30464aa5dee4SJiri Pirko 		if (in_dev)
30474aa5dee4SJiri Pirko 			ip_mc_rejoin_groups(in_dev);
30484aa5dee4SJiri Pirko 		break;
30494aa5dee4SJiri Pirko 	default:
30504aa5dee4SJiri Pirko 		break;
30514aa5dee4SJiri Pirko 	}
30524aa5dee4SJiri Pirko 	return NOTIFY_DONE;
30534aa5dee4SJiri Pirko }
30544aa5dee4SJiri Pirko 
30554aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = {
30564aa5dee4SJiri Pirko 	.notifier_call = igmp_netdev_event,
30574aa5dee4SJiri Pirko };
30584aa5dee4SJiri Pirko 
305972c1d3bdSWANG Cong int __init igmp_mc_init(void)
30601da177e4SLinus Torvalds {
306172c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS)
30624aa5dee4SJiri Pirko 	int err;
30634aa5dee4SJiri Pirko 
30644aa5dee4SJiri Pirko 	err = register_pernet_subsys(&igmp_net_ops);
30654aa5dee4SJiri Pirko 	if (err)
30664aa5dee4SJiri Pirko 		return err;
30674aa5dee4SJiri Pirko 	err = register_netdevice_notifier(&igmp_notifier);
30684aa5dee4SJiri Pirko 	if (err)
30694aa5dee4SJiri Pirko 		goto reg_notif_fail;
30704aa5dee4SJiri Pirko 	return 0;
30714aa5dee4SJiri Pirko 
30724aa5dee4SJiri Pirko reg_notif_fail:
30734aa5dee4SJiri Pirko 	unregister_pernet_subsys(&igmp_net_ops);
30744aa5dee4SJiri Pirko 	return err;
307572c1d3bdSWANG Cong #else
307672c1d3bdSWANG Cong 	return register_netdevice_notifier(&igmp_notifier);
30771da177e4SLinus Torvalds #endif
307872c1d3bdSWANG Cong }
3079