xref: /openbmc/linux/net/ipv4/igmp.c (revision 1744bea1fa382f67263fdd9fee51d603fddb3da6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux NET3:	Internet Group Management Protocol  [IGMP]
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	This code implements the IGMP protocol as defined in RFC1112. There has
51da177e4SLinus Torvalds  *	been a further revision of this protocol since which is now supported.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	If you have trouble with this module be careful what gcc you have used,
81da177e4SLinus Torvalds  *	the older version didn't come out right using gcc 2.5.8, the newer one
91da177e4SLinus Torvalds  *	seems to fall out with gcc 2.6.2.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	Authors:
12113aa838SAlan Cox  *		Alan Cox <alan@lxorguk.ukuu.org.uk>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
151da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
161da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
171da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *	Fixes:
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *		Alan Cox	:	Added lots of __inline__ to optimise
221da177e4SLinus Torvalds  *					the memory usage of all the tiny little
231da177e4SLinus Torvalds  *					functions.
241da177e4SLinus Torvalds  *		Alan Cox	:	Dumped the header building experiment.
251da177e4SLinus Torvalds  *		Alan Cox	:	Minor tweaks ready for multicast routing
261da177e4SLinus Torvalds  *					and extended IGMP protocol.
271da177e4SLinus Torvalds  *		Alan Cox	:	Removed a load of inline directives. Gcc 2.5.8
281da177e4SLinus Torvalds  *					writes utterly bogus code otherwise (sigh)
291da177e4SLinus Torvalds  *					fixed IGMP loopback to behave in the manner
301da177e4SLinus Torvalds  *					desired by mrouted, fixed the fact it has been
311da177e4SLinus Torvalds  *					broken since 1.3.6 and cleaned up a few minor
321da177e4SLinus Torvalds  *					points.
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Tried to revise IGMP to Version 2
351da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
361da177e4SLinus Torvalds  *					The enhancements are mainly based on Steve Deering's
371da177e4SLinus Torvalds  * 					ipmulti-3.5 source code.
381da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added the igmp_get_mrouter_info and
391da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		igmp_set_mrouter_info to keep track of
401da177e4SLinus Torvalds  *					the mrouted version on that device.
411da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added the max_resp_time parameter to
421da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		igmp_heard_query(). Using this parameter
431da177e4SLinus Torvalds  *					to identify the multicast router version
441da177e4SLinus Torvalds  *					and do what the IGMP version 2 specified.
451da177e4SLinus Torvalds  *		Chih-Jen Chang	:	Added a timer to revert to IGMP V2 router
461da177e4SLinus Torvalds  *		Tsu-Sheng Tsao		if the specified time expired.
471da177e4SLinus Torvalds  *		Alan Cox	:	Stop IGMP from 0.0.0.0 being accepted.
481da177e4SLinus Torvalds  *		Alan Cox	:	Use GFP_ATOMIC in the right places.
491da177e4SLinus Torvalds  *		Christian Daudt :	igmp timer wasn't set for local group
501da177e4SLinus Torvalds  *					memberships but was being deleted,
511da177e4SLinus Torvalds  *					which caused a "del_timer() called
521da177e4SLinus Torvalds  *					from %p with timer not initialized\n"
531da177e4SLinus Torvalds  *					message (960131).
541da177e4SLinus Torvalds  *		Christian Daudt :	removed del_timer from
551da177e4SLinus Torvalds  *					igmp_timer_expire function (960205).
561da177e4SLinus Torvalds  *             Christian Daudt :       igmp_heard_report now only calls
571da177e4SLinus Torvalds  *                                     igmp_timer_expire if tm->running is
581da177e4SLinus Torvalds  *                                     true (960216).
591da177e4SLinus Torvalds  *		Malcolm Beattie :	ttl comparison wrong in igmp_rcv made
601da177e4SLinus Torvalds  *					igmp_heard_query never trigger. Expiry
611da177e4SLinus Torvalds  *					miscalculation fixed in igmp_heard_query
621da177e4SLinus Torvalds  *					and random() made to return unsigned to
631da177e4SLinus Torvalds  *					prevent negative expiry times.
641da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Wrong group leaving behaviour, backport
651da177e4SLinus Torvalds  *					fix from pending 2.1.x patches.
661da177e4SLinus Torvalds  *		Alan Cox:		Forget to enable FDDI support earlier.
671da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Fixed leaving groups on device down.
681da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Accordance to igmp-v2-06 draft.
691da177e4SLinus Torvalds  *		David L Stevens:	IGMPv3 support, with help from
701da177e4SLinus Torvalds  *					Vinay Kulkarni
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds #include <linux/module.h>
745a0e3ad6STejun Heo #include <linux/slab.h>
751da177e4SLinus Torvalds #include <asm/uaccess.h>
761da177e4SLinus Torvalds #include <linux/types.h>
771da177e4SLinus Torvalds #include <linux/kernel.h>
781da177e4SLinus Torvalds #include <linux/jiffies.h>
791da177e4SLinus Torvalds #include <linux/string.h>
801da177e4SLinus Torvalds #include <linux/socket.h>
811da177e4SLinus Torvalds #include <linux/sockios.h>
821da177e4SLinus Torvalds #include <linux/in.h>
831da177e4SLinus Torvalds #include <linux/inet.h>
841da177e4SLinus Torvalds #include <linux/netdevice.h>
851da177e4SLinus Torvalds #include <linux/skbuff.h>
861da177e4SLinus Torvalds #include <linux/inetdevice.h>
871da177e4SLinus Torvalds #include <linux/igmp.h>
881da177e4SLinus Torvalds #include <linux/if_arp.h>
891da177e4SLinus Torvalds #include <linux/rtnetlink.h>
901da177e4SLinus Torvalds #include <linux/times.h>
919d4a0314SHannes Frederic Sowa #include <linux/pkt_sched.h>
9214c85021SArnaldo Carvalho de Melo 
93457c4cbcSEric W. Biederman #include <net/net_namespace.h>
9414c85021SArnaldo Carvalho de Melo #include <net/arp.h>
951da177e4SLinus Torvalds #include <net/ip.h>
961da177e4SLinus Torvalds #include <net/protocol.h>
971da177e4SLinus Torvalds #include <net/route.h>
981da177e4SLinus Torvalds #include <net/sock.h>
991da177e4SLinus Torvalds #include <net/checksum.h>
1001da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
1011da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1021da177e4SLinus Torvalds #include <linux/mroute.h>
1031da177e4SLinus Torvalds #endif
1041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
1051da177e4SLinus Torvalds #include <linux/proc_fs.h>
1061da177e4SLinus Torvalds #include <linux/seq_file.h>
1071da177e4SLinus Torvalds #endif
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds #define IP_MAX_MEMBERSHIPS	20
1101da177e4SLinus Torvalds #define IP_MAX_MSF		10
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1131da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */
1141da177e4SLinus Torvalds 
115436f7c20SFabian Frederick #define IGMP_V1_ROUTER_PRESENT_TIMEOUT		(400*HZ)
116436f7c20SFabian Frederick #define IGMP_V2_ROUTER_PRESENT_TIMEOUT		(400*HZ)
117436f7c20SFabian Frederick #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL	(10*HZ)
118436f7c20SFabian Frederick #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL	(1*HZ)
119436f7c20SFabian Frederick #define IGMP_QUERY_RESPONSE_INTERVAL		(10*HZ)
120436f7c20SFabian Frederick #define IGMP_QUERY_ROBUSTNESS_VARIABLE		2
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 
123436f7c20SFabian Frederick #define IGMP_INITIAL_REPORT_DELAY		(1)
1241da177e4SLinus Torvalds 
125436f7c20SFabian Frederick /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
1261da177e4SLinus Torvalds  * IGMP specs require to report membership immediately after
1271da177e4SLinus Torvalds  * joining a group, but we delay the first report by a
1281da177e4SLinus Torvalds  * small interval. It seems more natural and still does not
1291da177e4SLinus Torvalds  * contradict to specs provided this delay is small enough.
1301da177e4SLinus Torvalds  */
1311da177e4SLinus Torvalds 
13242f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \
133c346dca1SYOSHIFUJI Hideaki 	(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
13442f811b8SHerbert Xu 	 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
1351da177e4SLinus Torvalds 	 ((in_dev)->mr_v1_seen && \
1361da177e4SLinus Torvalds 	  time_before(jiffies, (in_dev)->mr_v1_seen)))
13742f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \
138c346dca1SYOSHIFUJI Hideaki 	(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
13942f811b8SHerbert Xu 	 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
1401da177e4SLinus Torvalds 	 ((in_dev)->mr_v2_seen && \
1411da177e4SLinus Torvalds 	  time_before(jiffies, (in_dev)->mr_v2_seen)))
1421da177e4SLinus Torvalds 
143cab70040SWilliam Manley static int unsolicited_report_interval(struct in_device *in_dev)
144cab70040SWilliam Manley {
1452690048cSWilliam Manley 	int interval_ms, interval_jiffies;
1462690048cSWilliam Manley 
147cab70040SWilliam Manley 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
1482690048cSWilliam Manley 		interval_ms = IN_DEV_CONF_GET(
1492690048cSWilliam Manley 			in_dev,
1502690048cSWilliam Manley 			IGMPV2_UNSOLICITED_REPORT_INTERVAL);
151cab70040SWilliam Manley 	else /* v3 */
1522690048cSWilliam Manley 		interval_ms = IN_DEV_CONF_GET(
1532690048cSWilliam Manley 			in_dev,
1542690048cSWilliam Manley 			IGMPV3_UNSOLICITED_REPORT_INTERVAL);
1552690048cSWilliam Manley 
1562690048cSWilliam Manley 	interval_jiffies = msecs_to_jiffies(interval_ms);
1572690048cSWilliam Manley 
1582690048cSWilliam Manley 	/* _timer functions can't handle a delay of 0 jiffies so ensure
1592690048cSWilliam Manley 	 *  we always return a positive value.
1602690048cSWilliam Manley 	 */
1612690048cSWilliam Manley 	if (interval_jiffies <= 0)
1622690048cSWilliam Manley 		interval_jiffies = 1;
1632690048cSWilliam Manley 	return interval_jiffies;
164cab70040SWilliam Manley }
165cab70040SWilliam Manley 
1661da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
16763007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
1681da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev);
1691da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc);
1701da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc);
1711da177e4SLinus Torvalds #endif
1721da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc);
1738f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1748f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	if (atomic_dec_and_test(&im->refcnt)) {
1791da177e4SLinus Torvalds 		in_dev_put(im->interface);
18042ea299dSLai Jiangshan 		kfree_rcu(im, rcu);
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
184d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc)				\
185d9aa9380SDavid S. Miller 	for (pmc = rcu_dereference(in_dev->mc_list);		\
186d9aa9380SDavid S. Miller 	     pmc != NULL;					\
187d9aa9380SDavid S. Miller 	     pmc = rcu_dereference(pmc->next_rcu))
188d9aa9380SDavid S. Miller 
189d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc)				\
190d9aa9380SDavid S. Miller 	for (pmc = rtnl_dereference(in_dev->mc_list);		\
191d9aa9380SDavid S. Miller 	     pmc != NULL;					\
192d9aa9380SDavid S. Miller 	     pmc = rtnl_dereference(pmc->next_rcu))
193d9aa9380SDavid S. Miller 
1941da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  *	Timer management
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds 
2001d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
2031da177e4SLinus Torvalds 	if (del_timer(&im->timer))
2041da177e4SLinus Torvalds 		atomic_dec(&im->refcnt);
2051da177e4SLinus Torvalds 	im->tm_running = 0;
2061da177e4SLinus Torvalds 	im->reporter = 0;
2071da177e4SLinus Torvalds 	im->unsolicit_count = 0;
2081da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* It must be called with locked im->lock */
2121da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
2131da177e4SLinus Torvalds {
21463862b5bSAruna-Hewapathirane 	int tv = prandom_u32() % max_delay;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	im->tm_running = 1;
2171da177e4SLinus Torvalds 	if (!mod_timer(&im->timer, jiffies+tv+2))
2181da177e4SLinus Torvalds 		atomic_inc(&im->refcnt);
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev)
2221da177e4SLinus Torvalds {
22363862b5bSAruna-Hewapathirane 	int tv = prandom_u32() % in_dev->mr_maxdelay;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	in_dev->mr_gq_running = 1;
2261da177e4SLinus Torvalds 	if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
2271da177e4SLinus Torvalds 		in_dev_hold(in_dev);
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
2311da177e4SLinus Torvalds {
23263862b5bSAruna-Hewapathirane 	int tv = prandom_u32() % delay;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
2351da177e4SLinus Torvalds 		in_dev_hold(in_dev);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
2411da177e4SLinus Torvalds 	im->unsolicit_count = 0;
2421da177e4SLinus Torvalds 	if (del_timer(&im->timer)) {
2431da177e4SLinus Torvalds 		if ((long)(im->timer.expires-jiffies) < max_delay) {
2441da177e4SLinus Torvalds 			add_timer(&im->timer);
2451da177e4SLinus Torvalds 			im->tm_running = 1;
2461da177e4SLinus Torvalds 			spin_unlock_bh(&im->lock);
2471da177e4SLinus Torvalds 			return;
2481da177e4SLinus Torvalds 		}
2491da177e4SLinus Torvalds 		atomic_dec(&im->refcnt);
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds 	igmp_start_timer(im, max_delay);
2521da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /*
2571da177e4SLinus Torvalds  *	Send an IGMP report.
2581da177e4SLinus Torvalds  */
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
2641da177e4SLinus Torvalds 	int gdeleted, int sdeleted)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	switch (type) {
2671da177e4SLinus Torvalds 	case IGMPV3_MODE_IS_INCLUDE:
2681da177e4SLinus Torvalds 	case IGMPV3_MODE_IS_EXCLUDE:
2691da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2701da177e4SLinus Torvalds 			return 0;
271ad12583fSDavid L Stevens 		if (!(pmc->gsquery && !psf->sf_gsresp)) {
272ad12583fSDavid L Stevens 			if (pmc->sfmode == MCAST_INCLUDE)
273ad12583fSDavid L Stevens 				return 1;
274ad12583fSDavid L Stevens 			/* don't include if this source is excluded
275ad12583fSDavid L Stevens 			 * in all filters
276ad12583fSDavid L Stevens 			 */
277ad12583fSDavid L Stevens 			if (psf->sf_count[MCAST_INCLUDE])
278ad12583fSDavid L Stevens 				return type == IGMPV3_MODE_IS_INCLUDE;
279ad12583fSDavid L Stevens 			return pmc->sfcount[MCAST_EXCLUDE] ==
280ad12583fSDavid L Stevens 				psf->sf_count[MCAST_EXCLUDE];
281ad12583fSDavid L Stevens 		}
282ad12583fSDavid L Stevens 		return 0;
2831da177e4SLinus Torvalds 	case IGMPV3_CHANGE_TO_INCLUDE:
2841da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2851da177e4SLinus Torvalds 			return 0;
2861da177e4SLinus Torvalds 		return psf->sf_count[MCAST_INCLUDE] != 0;
2871da177e4SLinus Torvalds 	case IGMPV3_CHANGE_TO_EXCLUDE:
2881da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2891da177e4SLinus Torvalds 			return 0;
2901da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
2911da177e4SLinus Torvalds 		    psf->sf_count[MCAST_INCLUDE])
2921da177e4SLinus Torvalds 			return 0;
2931da177e4SLinus Torvalds 		return pmc->sfcount[MCAST_EXCLUDE] ==
2941da177e4SLinus Torvalds 			psf->sf_count[MCAST_EXCLUDE];
2951da177e4SLinus Torvalds 	case IGMPV3_ALLOW_NEW_SOURCES:
2961da177e4SLinus Torvalds 		if (gdeleted || !psf->sf_crcount)
2971da177e4SLinus Torvalds 			return 0;
2981da177e4SLinus Torvalds 		return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
2991da177e4SLinus Torvalds 	case IGMPV3_BLOCK_OLD_SOURCES:
3001da177e4SLinus Torvalds 		if (pmc->sfmode == MCAST_INCLUDE)
3011da177e4SLinus Torvalds 			return gdeleted || (psf->sf_crcount && sdeleted);
3021da177e4SLinus Torvalds 		return psf->sf_crcount && !gdeleted && !sdeleted;
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 	return 0;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds static int
3081da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	struct ip_sf_list *psf;
3111da177e4SLinus Torvalds 	int scount = 0;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
3141da177e4SLinus Torvalds 		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
3151da177e4SLinus Torvalds 			continue;
3161da177e4SLinus Torvalds 		scount++;
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 	return scount;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
32157e1ab6eSEric Dumazet #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
32257e1ab6eSEric Dumazet 
3231da177e4SLinus Torvalds static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	struct sk_buff *skb;
3261da177e4SLinus Torvalds 	struct rtable *rt;
3271da177e4SLinus Torvalds 	struct iphdr *pip;
3281da177e4SLinus Torvalds 	struct igmpv3_report *pig;
329877acedcSDaniel Lezcano 	struct net *net = dev_net(dev);
33031e4543dSDavid S. Miller 	struct flowi4 fl4;
33166088243SHerbert Xu 	int hlen = LL_RESERVED_SPACE(dev);
33266088243SHerbert Xu 	int tlen = dev->needed_tailroom;
3331da177e4SLinus Torvalds 
33457e1ab6eSEric Dumazet 	while (1) {
33566088243SHerbert Xu 		skb = alloc_skb(size + hlen + tlen,
33657e1ab6eSEric Dumazet 				GFP_ATOMIC | __GFP_NOWARN);
33757e1ab6eSEric Dumazet 		if (skb)
33857e1ab6eSEric Dumazet 			break;
33957e1ab6eSEric Dumazet 		size >>= 1;
34057e1ab6eSEric Dumazet 		if (size < 256)
3411da177e4SLinus Torvalds 			return NULL;
34257e1ab6eSEric Dumazet 	}
3439d4a0314SHannes Frederic Sowa 	skb->priority = TC_PRIO_CONTROL;
34457e1ab6eSEric Dumazet 	igmp_skb_size(skb) = size;
3451da177e4SLinus Torvalds 
34631e4543dSDavid S. Miller 	rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
34778fbfd8aSDavid S. Miller 				   0, 0,
34878fbfd8aSDavid S. Miller 				   IPPROTO_IGMP, 0, dev->ifindex);
349b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
3501da177e4SLinus Torvalds 		kfree_skb(skb);
3511da177e4SLinus Torvalds 		return NULL;
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 
354d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
3551da177e4SLinus Torvalds 	skb->dev = dev;
3561da177e4SLinus Torvalds 
35766088243SHerbert Xu 	skb_reserve(skb, hlen);
3581da177e4SLinus Torvalds 
3597e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
360eddc9ec5SArnaldo Carvalho de Melo 	pip = ip_hdr(skb);
3617e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + 4);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	pip->version  = 4;
3641da177e4SLinus Torvalds 	pip->ihl      = (sizeof(struct iphdr)+4)>>2;
3651da177e4SLinus Torvalds 	pip->tos      = 0xc0;
3661da177e4SLinus Torvalds 	pip->frag_off = htons(IP_DF);
3671da177e4SLinus Torvalds 	pip->ttl      = 1;
368492f64ceSDavid S. Miller 	pip->daddr    = fl4.daddr;
369492f64ceSDavid S. Miller 	pip->saddr    = fl4.saddr;
3701da177e4SLinus Torvalds 	pip->protocol = IPPROTO_IGMP;
3711da177e4SLinus Torvalds 	pip->tot_len  = 0;	/* filled in later */
37273f156a6SEric Dumazet 	ip_select_ident(skb, NULL);
3731da177e4SLinus Torvalds 	((u8 *)&pip[1])[0] = IPOPT_RA;
3741da177e4SLinus Torvalds 	((u8 *)&pip[1])[1] = 4;
3751da177e4SLinus Torvalds 	((u8 *)&pip[1])[2] = 0;
3761da177e4SLinus Torvalds 	((u8 *)&pip[1])[3] = 0;
3771da177e4SLinus Torvalds 
378b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
379d10ba34bSArnaldo Carvalho de Melo 	skb_put(skb, sizeof(*pig));
380d9edf9e2SArnaldo Carvalho de Melo 	pig = igmpv3_report_hdr(skb);
3811da177e4SLinus Torvalds 	pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
3821da177e4SLinus Torvalds 	pig->resv1 = 0;
3831da177e4SLinus Torvalds 	pig->csum = 0;
3841da177e4SLinus Torvalds 	pig->resv2 = 0;
3851da177e4SLinus Torvalds 	pig->ngrec = 0;
3861da177e4SLinus Torvalds 	return skb;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb)
3901da177e4SLinus Torvalds {
391d9edf9e2SArnaldo Carvalho de Melo 	struct igmphdr *pig = igmp_hdr(skb);
392f7c0c2aeSSimon Horman 	const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
3931da177e4SLinus Torvalds 
394d9edf9e2SArnaldo Carvalho de Melo 	pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
3951da177e4SLinus Torvalds 
396c439cb2eSHerbert Xu 	return ip_local_out(skb);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
4051da177e4SLinus Torvalds 	int type, struct igmpv3_grec **ppgr)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct net_device *dev = pmc->interface->dev;
4081da177e4SLinus Torvalds 	struct igmpv3_report *pih;
4091da177e4SLinus Torvalds 	struct igmpv3_grec *pgr;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	if (!skb)
4121da177e4SLinus Torvalds 		skb = igmpv3_newpack(dev, dev->mtu);
4131da177e4SLinus Torvalds 	if (!skb)
4141da177e4SLinus Torvalds 		return NULL;
4151da177e4SLinus Torvalds 	pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
4161da177e4SLinus Torvalds 	pgr->grec_type = type;
4171da177e4SLinus Torvalds 	pgr->grec_auxwords = 0;
4181da177e4SLinus Torvalds 	pgr->grec_nsrcs = 0;
4191da177e4SLinus Torvalds 	pgr->grec_mca = pmc->multiaddr;
420d9edf9e2SArnaldo Carvalho de Melo 	pih = igmpv3_report_hdr(skb);
4211da177e4SLinus Torvalds 	pih->ngrec = htons(ntohs(pih->ngrec)+1);
4221da177e4SLinus Torvalds 	*ppgr = pgr;
4231da177e4SLinus Torvalds 	return skb;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
42657e1ab6eSEric Dumazet #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
4271da177e4SLinus Torvalds 	skb_tailroom(skb)) : 0)
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
4301da177e4SLinus Torvalds 	int type, int gdeleted, int sdeleted)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	struct net_device *dev = pmc->interface->dev;
4331da177e4SLinus Torvalds 	struct igmpv3_report *pih;
4341da177e4SLinus Torvalds 	struct igmpv3_grec *pgr = NULL;
4351da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
436ad12583fSDavid L Stevens 	int scount, stotal, first, isquery, truncate;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	if (pmc->multiaddr == IGMP_ALL_HOSTS)
4391da177e4SLinus Torvalds 		return skb;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	isquery = type == IGMPV3_MODE_IS_INCLUDE ||
4421da177e4SLinus Torvalds 		  type == IGMPV3_MODE_IS_EXCLUDE;
4431da177e4SLinus Torvalds 	truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
4441da177e4SLinus Torvalds 		    type == IGMPV3_CHANGE_TO_EXCLUDE;
4451da177e4SLinus Torvalds 
446ad12583fSDavid L Stevens 	stotal = scount = 0;
447ad12583fSDavid L Stevens 
4481da177e4SLinus Torvalds 	psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
4491da177e4SLinus Torvalds 
450ad12583fSDavid L Stevens 	if (!*psf_list)
451ad12583fSDavid L Stevens 		goto empty_source;
452ad12583fSDavid L Stevens 
453d9edf9e2SArnaldo Carvalho de Melo 	pih = skb ? igmpv3_report_hdr(skb) : NULL;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	/* EX and TO_EX get a fresh packet, if needed */
4561da177e4SLinus Torvalds 	if (truncate) {
4571da177e4SLinus Torvalds 		if (pih && pih->ngrec &&
4581da177e4SLinus Torvalds 		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
4591da177e4SLinus Torvalds 			if (skb)
4601da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
4611da177e4SLinus Torvalds 			skb = igmpv3_newpack(dev, dev->mtu);
4621da177e4SLinus Torvalds 		}
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 	first = 1;
4651da177e4SLinus Torvalds 	psf_prev = NULL;
4661da177e4SLinus Torvalds 	for (psf = *psf_list; psf; psf = psf_next) {
467ea4d9e72SAl Viro 		__be32 *psrc;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 		psf_next = psf->sf_next;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
4721da177e4SLinus Torvalds 			psf_prev = psf;
4731da177e4SLinus Torvalds 			continue;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 		/* clear marks on query responses */
4771da177e4SLinus Torvalds 		if (isquery)
4781da177e4SLinus Torvalds 			psf->sf_gsresp = 0;
4791da177e4SLinus Torvalds 
48063007727SAl Viro 		if (AVAILABLE(skb) < sizeof(__be32) +
4811da177e4SLinus Torvalds 		    first*sizeof(struct igmpv3_grec)) {
4821da177e4SLinus Torvalds 			if (truncate && !first)
4831da177e4SLinus Torvalds 				break;	 /* truncate these */
4841da177e4SLinus Torvalds 			if (pgr)
4851da177e4SLinus Torvalds 				pgr->grec_nsrcs = htons(scount);
4861da177e4SLinus Torvalds 			if (skb)
4871da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
4881da177e4SLinus Torvalds 			skb = igmpv3_newpack(dev, dev->mtu);
4891da177e4SLinus Torvalds 			first = 1;
4901da177e4SLinus Torvalds 			scount = 0;
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 		if (first) {
4931da177e4SLinus Torvalds 			skb = add_grhead(skb, pmc, type, &pgr);
4941da177e4SLinus Torvalds 			first = 0;
4951da177e4SLinus Torvalds 		}
496cc63f70bSAlexey Dobriyan 		if (!skb)
497cc63f70bSAlexey Dobriyan 			return NULL;
49863007727SAl Viro 		psrc = (__be32 *)skb_put(skb, sizeof(__be32));
4991da177e4SLinus Torvalds 		*psrc = psf->sf_inaddr;
500ad12583fSDavid L Stevens 		scount++; stotal++;
5011da177e4SLinus Torvalds 		if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
5021da177e4SLinus Torvalds 		     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
5031da177e4SLinus Torvalds 			psf->sf_crcount--;
5041da177e4SLinus Torvalds 			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
5051da177e4SLinus Torvalds 				if (psf_prev)
5061da177e4SLinus Torvalds 					psf_prev->sf_next = psf->sf_next;
5071da177e4SLinus Torvalds 				else
5081da177e4SLinus Torvalds 					*psf_list = psf->sf_next;
5091da177e4SLinus Torvalds 				kfree(psf);
5101da177e4SLinus Torvalds 				continue;
5111da177e4SLinus Torvalds 			}
5121da177e4SLinus Torvalds 		}
5131da177e4SLinus Torvalds 		psf_prev = psf;
5141da177e4SLinus Torvalds 	}
515ad12583fSDavid L Stevens 
516ad12583fSDavid L Stevens empty_source:
517ad12583fSDavid L Stevens 	if (!stotal) {
518ad12583fSDavid L Stevens 		if (type == IGMPV3_ALLOW_NEW_SOURCES ||
519ad12583fSDavid L Stevens 		    type == IGMPV3_BLOCK_OLD_SOURCES)
520ad12583fSDavid L Stevens 			return skb;
521ad12583fSDavid L Stevens 		if (pmc->crcount || isquery) {
522ad12583fSDavid L Stevens 			/* make sure we have room for group header */
523ad12583fSDavid L Stevens 			if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
524ad12583fSDavid L Stevens 				igmpv3_sendpack(skb);
525ad12583fSDavid L Stevens 				skb = NULL; /* add_grhead will get a new one */
526ad12583fSDavid L Stevens 			}
527ad12583fSDavid L Stevens 			skb = add_grhead(skb, pmc, type, &pgr);
528ad12583fSDavid L Stevens 		}
529ad12583fSDavid L Stevens 	}
5301da177e4SLinus Torvalds 	if (pgr)
5311da177e4SLinus Torvalds 		pgr->grec_nsrcs = htons(scount);
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	if (isquery)
5341da177e4SLinus Torvalds 		pmc->gsquery = 0;	/* clear query state on report */
5351da177e4SLinus Torvalds 	return skb;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
5411da177e4SLinus Torvalds 	int type;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if (!pmc) {
5441d7138deSEric Dumazet 		rcu_read_lock();
5451d7138deSEric Dumazet 		for_each_pmc_rcu(in_dev, pmc) {
5461da177e4SLinus Torvalds 			if (pmc->multiaddr == IGMP_ALL_HOSTS)
5471da177e4SLinus Torvalds 				continue;
5481da177e4SLinus Torvalds 			spin_lock_bh(&pmc->lock);
5491da177e4SLinus Torvalds 			if (pmc->sfcount[MCAST_EXCLUDE])
5501da177e4SLinus Torvalds 				type = IGMPV3_MODE_IS_EXCLUDE;
5511da177e4SLinus Torvalds 			else
5521da177e4SLinus Torvalds 				type = IGMPV3_MODE_IS_INCLUDE;
5531da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 0, 0);
5541da177e4SLinus Torvalds 			spin_unlock_bh(&pmc->lock);
5551da177e4SLinus Torvalds 		}
5561d7138deSEric Dumazet 		rcu_read_unlock();
5571da177e4SLinus Torvalds 	} else {
5581da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
5591da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE])
5601da177e4SLinus Torvalds 			type = IGMPV3_MODE_IS_EXCLUDE;
5611da177e4SLinus Torvalds 		else
5621da177e4SLinus Torvalds 			type = IGMPV3_MODE_IS_INCLUDE;
5631da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, type, 0, 0);
5641da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 	if (!skb)
5671da177e4SLinus Torvalds 		return 0;
5681da177e4SLinus Torvalds 	return igmpv3_sendpack(skb);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /*
5721da177e4SLinus Torvalds  * remove zero-count source records from a source filter list
5731da177e4SLinus Torvalds  */
5741da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	struct ip_sf_list *psf_prev, *psf_next, *psf;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	psf_prev = NULL;
5791da177e4SLinus Torvalds 	for (psf = *ppsf; psf; psf = psf_next) {
5801da177e4SLinus Torvalds 		psf_next = psf->sf_next;
5811da177e4SLinus Torvalds 		if (psf->sf_crcount == 0) {
5821da177e4SLinus Torvalds 			if (psf_prev)
5831da177e4SLinus Torvalds 				psf_prev->sf_next = psf->sf_next;
5841da177e4SLinus Torvalds 			else
5851da177e4SLinus Torvalds 				*ppsf = psf->sf_next;
5861da177e4SLinus Torvalds 			kfree(psf);
5871da177e4SLinus Torvalds 		} else
5881da177e4SLinus Torvalds 			psf_prev = psf;
5891da177e4SLinus Torvalds 	}
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
5951da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
5961da177e4SLinus Torvalds 	int type, dtype;
5971da177e4SLinus Torvalds 
5981d7138deSEric Dumazet 	rcu_read_lock();
5991da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	/* deleted MCA's */
6021da177e4SLinus Torvalds 	pmc_prev = NULL;
6031da177e4SLinus Torvalds 	for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
6041da177e4SLinus Torvalds 		pmc_next = pmc->next;
6051da177e4SLinus Torvalds 		if (pmc->sfmode == MCAST_INCLUDE) {
6061da177e4SLinus Torvalds 			type = IGMPV3_BLOCK_OLD_SOURCES;
6071da177e4SLinus Torvalds 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
6081da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 1, 0);
6091da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, dtype, 1, 1);
6101da177e4SLinus Torvalds 		}
6111da177e4SLinus Torvalds 		if (pmc->crcount) {
6121da177e4SLinus Torvalds 			if (pmc->sfmode == MCAST_EXCLUDE) {
6131da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_INCLUDE;
6141da177e4SLinus Torvalds 				skb = add_grec(skb, pmc, type, 1, 0);
6151da177e4SLinus Torvalds 			}
616ad12583fSDavid L Stevens 			pmc->crcount--;
6171da177e4SLinus Torvalds 			if (pmc->crcount == 0) {
6181da177e4SLinus Torvalds 				igmpv3_clear_zeros(&pmc->tomb);
6191da177e4SLinus Torvalds 				igmpv3_clear_zeros(&pmc->sources);
6201da177e4SLinus Torvalds 			}
6211da177e4SLinus Torvalds 		}
6221da177e4SLinus Torvalds 		if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
6231da177e4SLinus Torvalds 			if (pmc_prev)
6241da177e4SLinus Torvalds 				pmc_prev->next = pmc_next;
6251da177e4SLinus Torvalds 			else
6261da177e4SLinus Torvalds 				in_dev->mc_tomb = pmc_next;
6271da177e4SLinus Torvalds 			in_dev_put(pmc->interface);
6281da177e4SLinus Torvalds 			kfree(pmc);
6291da177e4SLinus Torvalds 		} else
6301da177e4SLinus Torvalds 			pmc_prev = pmc;
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	/* change recs */
6351d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
6361da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
6371da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
6381da177e4SLinus Torvalds 			type = IGMPV3_BLOCK_OLD_SOURCES;
6391da177e4SLinus Torvalds 			dtype = IGMPV3_ALLOW_NEW_SOURCES;
6401da177e4SLinus Torvalds 		} else {
6411da177e4SLinus Torvalds 			type = IGMPV3_ALLOW_NEW_SOURCES;
6421da177e4SLinus Torvalds 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
6431da177e4SLinus Torvalds 		}
6441da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, type, 0, 0);
6451da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, dtype, 0, 1);	/* deleted sources */
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 		/* filter mode changes */
6481da177e4SLinus Torvalds 		if (pmc->crcount) {
6491da177e4SLinus Torvalds 			if (pmc->sfmode == MCAST_EXCLUDE)
6501da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_EXCLUDE;
6511da177e4SLinus Torvalds 			else
6521da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_INCLUDE;
6531da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 0, 0);
654ad12583fSDavid L Stevens 			pmc->crcount--;
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
6571da177e4SLinus Torvalds 	}
6581d7138deSEric Dumazet 	rcu_read_unlock();
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	if (!skb)
6611da177e4SLinus Torvalds 		return;
6621da177e4SLinus Torvalds 	(void) igmpv3_sendpack(skb);
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
6661da177e4SLinus Torvalds 	int type)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	struct sk_buff *skb;
6691da177e4SLinus Torvalds 	struct iphdr *iph;
6701da177e4SLinus Torvalds 	struct igmphdr *ih;
6711da177e4SLinus Torvalds 	struct rtable *rt;
6721da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
673877acedcSDaniel Lezcano 	struct net *net = dev_net(dev);
67463007727SAl Viro 	__be32	group = pmc ? pmc->multiaddr : 0;
67531e4543dSDavid S. Miller 	struct flowi4 fl4;
67663007727SAl Viro 	__be32	dst;
67766088243SHerbert Xu 	int hlen, tlen;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
6801da177e4SLinus Torvalds 		return igmpv3_send_report(in_dev, pmc);
6811da177e4SLinus Torvalds 	else if (type == IGMP_HOST_LEAVE_MESSAGE)
6821da177e4SLinus Torvalds 		dst = IGMP_ALL_ROUTER;
6831da177e4SLinus Torvalds 	else
6841da177e4SLinus Torvalds 		dst = group;
6851da177e4SLinus Torvalds 
68631e4543dSDavid S. Miller 	rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
68778fbfd8aSDavid S. Miller 				   0, 0,
68878fbfd8aSDavid S. Miller 				   IPPROTO_IGMP, 0, dev->ifindex);
689b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
6901da177e4SLinus Torvalds 		return -1;
69178fbfd8aSDavid S. Miller 
69266088243SHerbert Xu 	hlen = LL_RESERVED_SPACE(dev);
69366088243SHerbert Xu 	tlen = dev->needed_tailroom;
69466088243SHerbert Xu 	skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
6951da177e4SLinus Torvalds 	if (skb == NULL) {
6961da177e4SLinus Torvalds 		ip_rt_put(rt);
6971da177e4SLinus Torvalds 		return -1;
6981da177e4SLinus Torvalds 	}
6999d4a0314SHannes Frederic Sowa 	skb->priority = TC_PRIO_CONTROL;
7001da177e4SLinus Torvalds 
701d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
7021da177e4SLinus Torvalds 
70366088243SHerbert Xu 	skb_reserve(skb, hlen);
7041da177e4SLinus Torvalds 
7057e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
706eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
7077e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + 4);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	iph->version  = 4;
7101da177e4SLinus Torvalds 	iph->ihl      = (sizeof(struct iphdr)+4)>>2;
7111da177e4SLinus Torvalds 	iph->tos      = 0xc0;
7121da177e4SLinus Torvalds 	iph->frag_off = htons(IP_DF);
7131da177e4SLinus Torvalds 	iph->ttl      = 1;
7141da177e4SLinus Torvalds 	iph->daddr    = dst;
715492f64ceSDavid S. Miller 	iph->saddr    = fl4.saddr;
7161da177e4SLinus Torvalds 	iph->protocol = IPPROTO_IGMP;
71773f156a6SEric Dumazet 	ip_select_ident(skb, NULL);
7181da177e4SLinus Torvalds 	((u8 *)&iph[1])[0] = IPOPT_RA;
7191da177e4SLinus Torvalds 	((u8 *)&iph[1])[1] = 4;
7201da177e4SLinus Torvalds 	((u8 *)&iph[1])[2] = 0;
7211da177e4SLinus Torvalds 	((u8 *)&iph[1])[3] = 0;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
7241da177e4SLinus Torvalds 	ih->type = type;
7251da177e4SLinus Torvalds 	ih->code = 0;
7261da177e4SLinus Torvalds 	ih->csum = 0;
7271da177e4SLinus Torvalds 	ih->group = group;
7281da177e4SLinus Torvalds 	ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
7291da177e4SLinus Torvalds 
730c439cb2eSHerbert Xu 	return ip_local_out(skb);
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds static void igmp_gq_timer_expire(unsigned long data)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	struct in_device *in_dev = (struct in_device *)data;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
7381da177e4SLinus Torvalds 	igmpv3_send_report(in_dev, NULL);
739e2401654SSalam Noureddine 	in_dev_put(in_dev);
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds static void igmp_ifc_timer_expire(unsigned long data)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	struct in_device *in_dev = (struct in_device *)data;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	igmpv3_send_cr(in_dev);
7471da177e4SLinus Torvalds 	if (in_dev->mr_ifc_count) {
7481da177e4SLinus Torvalds 		in_dev->mr_ifc_count--;
749cab70040SWilliam Manley 		igmp_ifc_start_timer(in_dev,
750cab70040SWilliam Manley 				     unsolicited_report_interval(in_dev));
7511da177e4SLinus Torvalds 	}
752e2401654SSalam Noureddine 	in_dev_put(in_dev);
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
7581da177e4SLinus Torvalds 		return;
759a9fe8e29SHannes Frederic Sowa 	in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv;
7601da177e4SLinus Torvalds 	igmp_ifc_start_timer(in_dev, 1);
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds static void igmp_timer_expire(unsigned long data)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	struct ip_mc_list *im = (struct ip_mc_list *)data;
7671da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	spin_lock(&im->lock);
7701da177e4SLinus Torvalds 	im->tm_running = 0;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	if (im->unsolicit_count) {
7731da177e4SLinus Torvalds 		im->unsolicit_count--;
774cab70040SWilliam Manley 		igmp_start_timer(im, unsolicited_report_interval(in_dev));
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 	im->reporter = 1;
7771da177e4SLinus Torvalds 	spin_unlock(&im->lock);
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev))
7801da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
7811da177e4SLinus Torvalds 	else if (IGMP_V2_SEEN(in_dev))
7821da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
7831da177e4SLinus Torvalds 	else
7841da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	ip_ma_put(im);
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
789ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */
790ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	struct ip_sf_list *psf;
7931da177e4SLinus Torvalds 	int i, scount;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	scount = 0;
7961da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
7971da177e4SLinus Torvalds 		if (scount == nsrcs)
7981da177e4SLinus Torvalds 			break;
799ad12583fSDavid L Stevens 		for (i = 0; i < nsrcs; i++) {
800ad12583fSDavid L Stevens 			/* skip inactive filters */
801e05c4ad3SYan, Zheng 			if (psf->sf_count[MCAST_INCLUDE] ||
802ad12583fSDavid L Stevens 			    pmc->sfcount[MCAST_EXCLUDE] !=
803ad12583fSDavid L Stevens 			    psf->sf_count[MCAST_EXCLUDE])
804ce713ee5SRongQing.Li 				break;
805ad12583fSDavid L Stevens 			if (srcs[i] == psf->sf_inaddr) {
806ad12583fSDavid L Stevens 				scount++;
807ad12583fSDavid L Stevens 				break;
808ad12583fSDavid L Stevens 			}
809ad12583fSDavid L Stevens 		}
810ad12583fSDavid L Stevens 	}
811ad12583fSDavid L Stevens 	pmc->gsquery = 0;
812ad12583fSDavid L Stevens 	if (scount == nsrcs)	/* all sources excluded */
813ad12583fSDavid L Stevens 		return 0;
814ad12583fSDavid L Stevens 	return 1;
815ad12583fSDavid L Stevens }
816ad12583fSDavid L Stevens 
817ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
818ad12583fSDavid L Stevens {
819ad12583fSDavid L Stevens 	struct ip_sf_list *psf;
820ad12583fSDavid L Stevens 	int i, scount;
821ad12583fSDavid L Stevens 
822ad12583fSDavid L Stevens 	if (pmc->sfmode == MCAST_EXCLUDE)
823ad12583fSDavid L Stevens 		return igmp_xmarksources(pmc, nsrcs, srcs);
824ad12583fSDavid L Stevens 
825ad12583fSDavid L Stevens 	/* mark INCLUDE-mode sources */
826ad12583fSDavid L Stevens 	scount = 0;
827ad12583fSDavid L Stevens 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
828ad12583fSDavid L Stevens 		if (scount == nsrcs)
829ad12583fSDavid L Stevens 			break;
8301da177e4SLinus Torvalds 		for (i = 0; i < nsrcs; i++)
8311da177e4SLinus Torvalds 			if (srcs[i] == psf->sf_inaddr) {
8321da177e4SLinus Torvalds 				psf->sf_gsresp = 1;
8331da177e4SLinus Torvalds 				scount++;
8341da177e4SLinus Torvalds 				break;
8351da177e4SLinus Torvalds 			}
8361da177e4SLinus Torvalds 	}
837ad12583fSDavid L Stevens 	if (!scount) {
838ad12583fSDavid L Stevens 		pmc->gsquery = 0;
839ad12583fSDavid L Stevens 		return 0;
840ad12583fSDavid L Stevens 	}
841ad12583fSDavid L Stevens 	pmc->gsquery = 1;
842ad12583fSDavid L Stevens 	return 1;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
845d679c532SEric Dumazet /* return true if packet was dropped */
846d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	struct ip_mc_list *im;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	/* Timers are only set for non-local groups */
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	if (group == IGMP_ALL_HOSTS)
853d679c532SEric Dumazet 		return false;
8541da177e4SLinus Torvalds 
8551d7138deSEric Dumazet 	rcu_read_lock();
8561d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, im) {
8571da177e4SLinus Torvalds 		if (im->multiaddr == group) {
8581da177e4SLinus Torvalds 			igmp_stop_timer(im);
8591da177e4SLinus Torvalds 			break;
8601da177e4SLinus Torvalds 		}
8611da177e4SLinus Torvalds 	}
8621d7138deSEric Dumazet 	rcu_read_unlock();
863d679c532SEric Dumazet 	return false;
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
866d679c532SEric Dumazet /* return true if packet was dropped */
867d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
8681da177e4SLinus Torvalds 	int len)
8691da177e4SLinus Torvalds {
870d9edf9e2SArnaldo Carvalho de Melo 	struct igmphdr 		*ih = igmp_hdr(skb);
871d9edf9e2SArnaldo Carvalho de Melo 	struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
8721da177e4SLinus Torvalds 	struct ip_mc_list	*im;
87363007727SAl Viro 	__be32			group = ih->group;
8741da177e4SLinus Torvalds 	int			max_delay;
8751da177e4SLinus Torvalds 	int			mark = 0;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 
8785b7c8406SDavid Stevens 	if (len == 8) {
8791da177e4SLinus Torvalds 		if (ih->code == 0) {
8801da177e4SLinus Torvalds 			/* Alas, old v1 router presents here. */
8811da177e4SLinus Torvalds 
882436f7c20SFabian Frederick 			max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
8831da177e4SLinus Torvalds 			in_dev->mr_v1_seen = jiffies +
884436f7c20SFabian Frederick 				IGMP_V1_ROUTER_PRESENT_TIMEOUT;
8851da177e4SLinus Torvalds 			group = 0;
8861da177e4SLinus Torvalds 		} else {
8871da177e4SLinus Torvalds 			/* v2 router present */
8881da177e4SLinus Torvalds 			max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
8891da177e4SLinus Torvalds 			in_dev->mr_v2_seen = jiffies +
890436f7c20SFabian Frederick 				IGMP_V2_ROUTER_PRESENT_TIMEOUT;
8911da177e4SLinus Torvalds 		}
8921da177e4SLinus Torvalds 		/* cancel the interface change timer */
8931da177e4SLinus Torvalds 		in_dev->mr_ifc_count = 0;
8941da177e4SLinus Torvalds 		if (del_timer(&in_dev->mr_ifc_timer))
8951da177e4SLinus Torvalds 			__in_dev_put(in_dev);
8961da177e4SLinus Torvalds 		/* clear deleted report items */
8971da177e4SLinus Torvalds 		igmpv3_clear_delrec(in_dev);
8981da177e4SLinus Torvalds 	} else if (len < 12) {
899d679c532SEric Dumazet 		return true;	/* ignore bogus packet; freed by caller */
9005b7c8406SDavid Stevens 	} else if (IGMP_V1_SEEN(in_dev)) {
9015b7c8406SDavid Stevens 		/* This is a v3 query with v1 queriers present */
902436f7c20SFabian Frederick 		max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
9035b7c8406SDavid Stevens 		group = 0;
9045b7c8406SDavid Stevens 	} else if (IGMP_V2_SEEN(in_dev)) {
9055b7c8406SDavid Stevens 		/* this is a v3 query with v2 queriers present;
9065b7c8406SDavid Stevens 		 * Interpretation of the max_delay code is problematic here.
9075b7c8406SDavid Stevens 		 * A real v2 host would use ih_code directly, while v3 has a
9085b7c8406SDavid Stevens 		 * different encoding. We use the v3 encoding as more likely
9095b7c8406SDavid Stevens 		 * to be intended in a v3 query.
9105b7c8406SDavid Stevens 		 */
9115b7c8406SDavid Stevens 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
912a8c1f65cSBen Hutchings 		if (!max_delay)
913a8c1f65cSBen Hutchings 			max_delay = 1;	/* can't mod w/ 0 */
9141da177e4SLinus Torvalds 	} else { /* v3 */
9151da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
916d679c532SEric Dumazet 			return true;
9171da177e4SLinus Torvalds 
918d9edf9e2SArnaldo Carvalho de Melo 		ih3 = igmpv3_query_hdr(skb);
9191da177e4SLinus Torvalds 		if (ih3->nsrcs) {
9201da177e4SLinus Torvalds 			if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
92163007727SAl Viro 					   + ntohs(ih3->nsrcs)*sizeof(__be32)))
922d679c532SEric Dumazet 				return true;
923d9edf9e2SArnaldo Carvalho de Melo 			ih3 = igmpv3_query_hdr(skb);
9241da177e4SLinus Torvalds 		}
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
9271da177e4SLinus Torvalds 		if (!max_delay)
9281da177e4SLinus Torvalds 			max_delay = 1;	/* can't mod w/ 0 */
9291da177e4SLinus Torvalds 		in_dev->mr_maxdelay = max_delay;
9301da177e4SLinus Torvalds 		if (ih3->qrv)
9311da177e4SLinus Torvalds 			in_dev->mr_qrv = ih3->qrv;
9321da177e4SLinus Torvalds 		if (!group) { /* general query */
9331da177e4SLinus Torvalds 			if (ih3->nsrcs)
934b47bd8d2SDaniel Borkmann 				return true;	/* no sources allowed */
9351da177e4SLinus Torvalds 			igmp_gq_start_timer(in_dev);
936d679c532SEric Dumazet 			return false;
9371da177e4SLinus Torvalds 		}
9381da177e4SLinus Torvalds 		/* mark sources to include, if group & source-specific */
9391da177e4SLinus Torvalds 		mark = ih3->nsrcs != 0;
9401da177e4SLinus Torvalds 	}
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	/*
9431da177e4SLinus Torvalds 	 * - Start the timers in all of our membership records
9441da177e4SLinus Torvalds 	 *   that the query applies to for the interface on
9451da177e4SLinus Torvalds 	 *   which the query arrived excl. those that belong
9461da177e4SLinus Torvalds 	 *   to a "local" group (224.0.0.X)
9471da177e4SLinus Torvalds 	 * - For timers already running check if they need to
9481da177e4SLinus Torvalds 	 *   be reset.
9491da177e4SLinus Torvalds 	 * - Use the igmp->igmp_code field as the maximum
9501da177e4SLinus Torvalds 	 *   delay possible
9511da177e4SLinus Torvalds 	 */
9521d7138deSEric Dumazet 	rcu_read_lock();
9531d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, im) {
954ad12583fSDavid L Stevens 		int changed;
955ad12583fSDavid L Stevens 
9561da177e4SLinus Torvalds 		if (group && group != im->multiaddr)
9571da177e4SLinus Torvalds 			continue;
9581da177e4SLinus Torvalds 		if (im->multiaddr == IGMP_ALL_HOSTS)
9591da177e4SLinus Torvalds 			continue;
9601da177e4SLinus Torvalds 		spin_lock_bh(&im->lock);
9611da177e4SLinus Torvalds 		if (im->tm_running)
9621da177e4SLinus Torvalds 			im->gsquery = im->gsquery && mark;
9631da177e4SLinus Torvalds 		else
9641da177e4SLinus Torvalds 			im->gsquery = mark;
965ad12583fSDavid L Stevens 		changed = !im->gsquery ||
9661da177e4SLinus Torvalds 			igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
9671da177e4SLinus Torvalds 		spin_unlock_bh(&im->lock);
968ad12583fSDavid L Stevens 		if (changed)
9691da177e4SLinus Torvalds 			igmp_mod_timer(im, max_delay);
9701da177e4SLinus Torvalds 	}
9711d7138deSEric Dumazet 	rcu_read_unlock();
972d679c532SEric Dumazet 	return false;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
9759a57a9d2SEric Dumazet /* called in rcu_read_lock() section */
9761da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb)
9771da177e4SLinus Torvalds {
9781da177e4SLinus Torvalds 	/* This basically follows the spec line by line -- see RFC1112 */
9791da177e4SLinus Torvalds 	struct igmphdr *ih;
9809a57a9d2SEric Dumazet 	struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
9811da177e4SLinus Torvalds 	int len = skb->len;
982d679c532SEric Dumazet 	bool dropped = true;
9831da177e4SLinus Torvalds 
984cd557bc1SDenis V. Lunev 	if (in_dev == NULL)
985cd557bc1SDenis V. Lunev 		goto drop;
9861da177e4SLinus Torvalds 
987fb286bb2SHerbert Xu 	if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
9889a57a9d2SEric Dumazet 		goto drop;
989fb286bb2SHerbert Xu 
990de08dc1aSTom Herbert 	if (skb_checksum_simple_validate(skb))
9919a57a9d2SEric Dumazet 		goto drop;
9921da177e4SLinus Torvalds 
993d9edf9e2SArnaldo Carvalho de Melo 	ih = igmp_hdr(skb);
9941da177e4SLinus Torvalds 	switch (ih->type) {
9951da177e4SLinus Torvalds 	case IGMP_HOST_MEMBERSHIP_QUERY:
996d679c532SEric Dumazet 		dropped = igmp_heard_query(in_dev, skb, len);
9971da177e4SLinus Torvalds 		break;
9981da177e4SLinus Torvalds 	case IGMP_HOST_MEMBERSHIP_REPORT:
9991da177e4SLinus Torvalds 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
10001da177e4SLinus Torvalds 		/* Is it our report looped back? */
1001c7537967SDavid S. Miller 		if (rt_is_output_route(skb_rtable(skb)))
10021da177e4SLinus Torvalds 			break;
100324c69275SDavid Stevens 		/* don't rely on MC router hearing unicast reports */
100424c69275SDavid Stevens 		if (skb->pkt_type == PACKET_MULTICAST ||
100524c69275SDavid Stevens 		    skb->pkt_type == PACKET_BROADCAST)
1006d679c532SEric Dumazet 			dropped = igmp_heard_report(in_dev, ih->group);
10071da177e4SLinus Torvalds 		break;
10081da177e4SLinus Torvalds 	case IGMP_PIM:
10091da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1
10101da177e4SLinus Torvalds 		return pim_rcv_v1(skb);
10111da177e4SLinus Torvalds #endif
1012c6b471e6SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
10131da177e4SLinus Torvalds 	case IGMP_DVMRP:
10141da177e4SLinus Torvalds 	case IGMP_TRACE:
10151da177e4SLinus Torvalds 	case IGMP_HOST_LEAVE_MESSAGE:
10161da177e4SLinus Torvalds 	case IGMP_MTRACE:
10171da177e4SLinus Torvalds 	case IGMP_MTRACE_RESP:
10181da177e4SLinus Torvalds 		break;
10191da177e4SLinus Torvalds 	default:
1020dd1c1853SLinus Torvalds 		break;
10211da177e4SLinus Torvalds 	}
1022fb286bb2SHerbert Xu 
1023cd557bc1SDenis V. Lunev drop:
1024d679c532SEric Dumazet 	if (dropped)
10251da177e4SLinus Torvalds 		kfree_skb(skb);
1026d679c532SEric Dumazet 	else
1027d679c532SEric Dumazet 		consume_skb(skb);
10281da177e4SLinus Torvalds 	return 0;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds #endif
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds /*
10351da177e4SLinus Torvalds  *	Add a filter to a device
10361da177e4SLinus Torvalds  */
10371da177e4SLinus Torvalds 
103863007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	char buf[MAX_ADDR_LEN];
10411da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	/* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
10441da177e4SLinus Torvalds 	   We will get multicast token leakage, when IFF_MULTICAST
1045b81693d9SJiri Pirko 	   is changed. This check should be done in ndo_set_rx_mode
10461da177e4SLinus Torvalds 	   routine. Something sort of:
10471da177e4SLinus Torvalds 	   if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
10481da177e4SLinus Torvalds 	   --ANK
10491da177e4SLinus Torvalds 	   */
10501da177e4SLinus Torvalds 	if (arp_mc_map(addr, buf, dev, 0) == 0)
105122bedad3SJiri Pirko 		dev_mc_add(dev, buf);
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds /*
10551da177e4SLinus Torvalds  *	Remove a filter from a device
10561da177e4SLinus Torvalds  */
10571da177e4SLinus Torvalds 
105863007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
10591da177e4SLinus Torvalds {
10601da177e4SLinus Torvalds 	char buf[MAX_ADDR_LEN];
10611da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	if (arp_mc_map(addr, buf, dev, 0) == 0)
106422bedad3SJiri Pirko 		dev_mc_del(dev, buf);
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
10681da177e4SLinus Torvalds /*
10691da177e4SLinus Torvalds  * deleted ip_mc_list manipulation
10701da177e4SLinus Torvalds  */
10711da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
10721da177e4SLinus Torvalds {
10731da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	/* this is an "ip_mc_list" for convenience; only the fields below
10761da177e4SLinus Torvalds 	 * are actually used. In particular, the refcnt and users are not
10771da177e4SLinus Torvalds 	 * used for management of the delete list. Using the same structure
10781da177e4SLinus Torvalds 	 * for deleted items allows change reports to use common code with
10791da177e4SLinus Torvalds 	 * non-deleted or query-response MCA's.
10801da177e4SLinus Torvalds 	 */
10810da974f4SPanagiotis Issaris 	pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
10821da177e4SLinus Torvalds 	if (!pmc)
10831da177e4SLinus Torvalds 		return;
10841da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
10851da177e4SLinus Torvalds 	pmc->interface = im->interface;
10861da177e4SLinus Torvalds 	in_dev_hold(in_dev);
10871da177e4SLinus Torvalds 	pmc->multiaddr = im->multiaddr;
1088a9fe8e29SHannes Frederic Sowa 	pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
10891da177e4SLinus Torvalds 	pmc->sfmode = im->sfmode;
10901da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_INCLUDE) {
10911da177e4SLinus Torvalds 		struct ip_sf_list *psf;
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 		pmc->tomb = im->tomb;
10941da177e4SLinus Torvalds 		pmc->sources = im->sources;
10951da177e4SLinus Torvalds 		im->tomb = im->sources = NULL;
10961da177e4SLinus Torvalds 		for (psf = pmc->sources; psf; psf = psf->sf_next)
10971da177e4SLinus Torvalds 			psf->sf_crcount = pmc->crcount;
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
11021da177e4SLinus Torvalds 	pmc->next = in_dev->mc_tomb;
11031da177e4SLinus Torvalds 	in_dev->mc_tomb = pmc;
11041da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
110763007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *pmc_prev;
11101da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_next;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
11131da177e4SLinus Torvalds 	pmc_prev = NULL;
11141da177e4SLinus Torvalds 	for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
11151da177e4SLinus Torvalds 		if (pmc->multiaddr == multiaddr)
11161da177e4SLinus Torvalds 			break;
11171da177e4SLinus Torvalds 		pmc_prev = pmc;
11181da177e4SLinus Torvalds 	}
11191da177e4SLinus Torvalds 	if (pmc) {
11201da177e4SLinus Torvalds 		if (pmc_prev)
11211da177e4SLinus Torvalds 			pmc_prev->next = pmc->next;
11221da177e4SLinus Torvalds 		else
11231da177e4SLinus Torvalds 			in_dev->mc_tomb = pmc->next;
11241da177e4SLinus Torvalds 	}
11251da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
11261da177e4SLinus Torvalds 	if (pmc) {
11271da177e4SLinus Torvalds 		for (psf = pmc->tomb; psf; psf = psf_next) {
11281da177e4SLinus Torvalds 			psf_next = psf->sf_next;
11291da177e4SLinus Torvalds 			kfree(psf);
11301da177e4SLinus Torvalds 		}
11311da177e4SLinus Torvalds 		in_dev_put(pmc->interface);
11321da177e4SLinus Torvalds 		kfree(pmc);
11331da177e4SLinus Torvalds 	}
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev)
11371da177e4SLinus Torvalds {
11381da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *nextpmc;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
11411da177e4SLinus Torvalds 	pmc = in_dev->mc_tomb;
11421da177e4SLinus Torvalds 	in_dev->mc_tomb = NULL;
11431da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	for (; pmc; pmc = nextpmc) {
11461da177e4SLinus Torvalds 		nextpmc = pmc->next;
11471da177e4SLinus Torvalds 		ip_mc_clear_src(pmc);
11481da177e4SLinus Torvalds 		in_dev_put(pmc->interface);
11491da177e4SLinus Torvalds 		kfree(pmc);
11501da177e4SLinus Torvalds 	}
11511da177e4SLinus Torvalds 	/* clear dead sources, too */
11521d7138deSEric Dumazet 	rcu_read_lock();
11531d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
11541da177e4SLinus Torvalds 		struct ip_sf_list *psf, *psf_next;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
11571da177e4SLinus Torvalds 		psf = pmc->tomb;
11581da177e4SLinus Torvalds 		pmc->tomb = NULL;
11591da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
11601da177e4SLinus Torvalds 		for (; psf; psf = psf_next) {
11611da177e4SLinus Torvalds 			psf_next = psf->sf_next;
11621da177e4SLinus Torvalds 			kfree(psf);
11631da177e4SLinus Torvalds 		}
11641da177e4SLinus Torvalds 	}
11651d7138deSEric Dumazet 	rcu_read_unlock();
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds #endif
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
11721da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
11731da177e4SLinus Torvalds 	int reporter;
11741da177e4SLinus Torvalds #endif
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	if (im->loaded) {
11771da177e4SLinus Torvalds 		im->loaded = 0;
11781da177e4SLinus Torvalds 		ip_mc_filter_del(in_dev, im->multiaddr);
11791da177e4SLinus Torvalds 	}
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
11821da177e4SLinus Torvalds 	if (im->multiaddr == IGMP_ALL_HOSTS)
11831da177e4SLinus Torvalds 		return;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	reporter = im->reporter;
11861da177e4SLinus Torvalds 	igmp_stop_timer(im);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	if (!in_dev->dead) {
11891da177e4SLinus Torvalds 		if (IGMP_V1_SEEN(in_dev))
119024cf3af3SVeaceslav Falico 			return;
11911da177e4SLinus Torvalds 		if (IGMP_V2_SEEN(in_dev)) {
11921da177e4SLinus Torvalds 			if (reporter)
11931da177e4SLinus Torvalds 				igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
119424cf3af3SVeaceslav Falico 			return;
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 		/* IGMPv3 */
11971da177e4SLinus Torvalds 		igmpv3_add_delrec(in_dev, im);
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds #endif
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds static void igmp_group_added(struct ip_mc_list *im)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	if (im->loaded == 0) {
12091da177e4SLinus Torvalds 		im->loaded = 1;
12101da177e4SLinus Torvalds 		ip_mc_filter_add(in_dev, im->multiaddr);
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
12141da177e4SLinus Torvalds 	if (im->multiaddr == IGMP_ALL_HOSTS)
12151da177e4SLinus Torvalds 		return;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	if (in_dev->dead)
12181da177e4SLinus Torvalds 		return;
12191da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
12201da177e4SLinus Torvalds 		spin_lock_bh(&im->lock);
1221436f7c20SFabian Frederick 		igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
12221da177e4SLinus Torvalds 		spin_unlock_bh(&im->lock);
12231da177e4SLinus Torvalds 		return;
12241da177e4SLinus Torvalds 	}
12251da177e4SLinus Torvalds 	/* else, v3 */
12261da177e4SLinus Torvalds 
1227a9fe8e29SHannes Frederic Sowa 	im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
12281da177e4SLinus Torvalds 	igmp_ifc_event(in_dev);
12291da177e4SLinus Torvalds #endif
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds /*
12341da177e4SLinus Torvalds  *	Multicast list managers
12351da177e4SLinus Torvalds  */
12361da177e4SLinus Torvalds 
1237e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im)
1238e9897071SEric Dumazet {
1239c70eba74SEric Dumazet 	return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1240e9897071SEric Dumazet }
1241e9897071SEric Dumazet 
1242e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev,
1243e9897071SEric Dumazet 			   struct ip_mc_list *im)
1244e9897071SEric Dumazet {
1245e9897071SEric Dumazet 	struct ip_mc_list __rcu **mc_hash;
1246e9897071SEric Dumazet 	u32 hash;
1247e9897071SEric Dumazet 
1248e9897071SEric Dumazet 	mc_hash = rtnl_dereference(in_dev->mc_hash);
1249e9897071SEric Dumazet 	if (mc_hash) {
1250e9897071SEric Dumazet 		hash = ip_mc_hash(im);
1251c70eba74SEric Dumazet 		im->next_hash = mc_hash[hash];
1252e9897071SEric Dumazet 		rcu_assign_pointer(mc_hash[hash], im);
1253e9897071SEric Dumazet 		return;
1254e9897071SEric Dumazet 	}
1255e9897071SEric Dumazet 
1256e9897071SEric Dumazet 	/* do not use a hash table for small number of items */
1257e9897071SEric Dumazet 	if (in_dev->mc_count < 4)
1258e9897071SEric Dumazet 		return;
1259e9897071SEric Dumazet 
1260e9897071SEric Dumazet 	mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1261e9897071SEric Dumazet 			  GFP_KERNEL);
1262e9897071SEric Dumazet 	if (!mc_hash)
1263e9897071SEric Dumazet 		return;
1264e9897071SEric Dumazet 
1265e9897071SEric Dumazet 	for_each_pmc_rtnl(in_dev, im) {
1266e9897071SEric Dumazet 		hash = ip_mc_hash(im);
1267c70eba74SEric Dumazet 		im->next_hash = mc_hash[hash];
1268e9897071SEric Dumazet 		RCU_INIT_POINTER(mc_hash[hash], im);
1269e9897071SEric Dumazet 	}
1270e9897071SEric Dumazet 
1271e9897071SEric Dumazet 	rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1272e9897071SEric Dumazet }
1273e9897071SEric Dumazet 
1274e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev,
1275e9897071SEric Dumazet 			      struct ip_mc_list *im)
1276e9897071SEric Dumazet {
1277e9897071SEric Dumazet 	struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1278e9897071SEric Dumazet 	struct ip_mc_list *aux;
1279e9897071SEric Dumazet 
1280e9897071SEric Dumazet 	if (!mc_hash)
1281e9897071SEric Dumazet 		return;
1282e9897071SEric Dumazet 	mc_hash += ip_mc_hash(im);
1283e9897071SEric Dumazet 	while ((aux = rtnl_dereference(*mc_hash)) != im)
1284e9897071SEric Dumazet 		mc_hash = &aux->next_hash;
1285e9897071SEric Dumazet 	*mc_hash = im->next_hash;
1286e9897071SEric Dumazet }
1287e9897071SEric Dumazet 
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds /*
12901da177e4SLinus Torvalds  *	A socket has joined a multicast group on device dev.
12911da177e4SLinus Torvalds  */
12921da177e4SLinus Torvalds 
12938f935bbdSAl Viro void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	struct ip_mc_list *im;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	ASSERT_RTNL();
12981da177e4SLinus Torvalds 
12991d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, im) {
13001da177e4SLinus Torvalds 		if (im->multiaddr == addr) {
13011da177e4SLinus Torvalds 			im->users++;
13021da177e4SLinus Torvalds 			ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
13031da177e4SLinus Torvalds 			goto out;
13041da177e4SLinus Torvalds 		}
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 
13071d7138deSEric Dumazet 	im = kzalloc(sizeof(*im), GFP_KERNEL);
13081da177e4SLinus Torvalds 	if (!im)
13091da177e4SLinus Torvalds 		goto out;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	im->users = 1;
13121da177e4SLinus Torvalds 	im->interface = in_dev;
13131da177e4SLinus Torvalds 	in_dev_hold(in_dev);
13141da177e4SLinus Torvalds 	im->multiaddr = addr;
13151da177e4SLinus Torvalds 	/* initial mode is (EX, empty) */
13161da177e4SLinus Torvalds 	im->sfmode = MCAST_EXCLUDE;
13171da177e4SLinus Torvalds 	im->sfcount[MCAST_EXCLUDE] = 1;
13181da177e4SLinus Torvalds 	atomic_set(&im->refcnt, 1);
13191da177e4SLinus Torvalds 	spin_lock_init(&im->lock);
13201da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1321179542a5SHimangi Saraogi 	setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
1322a9fe8e29SHannes Frederic Sowa 	im->unsolicit_count = sysctl_igmp_qrv;
13231da177e4SLinus Torvalds #endif
13241d7138deSEric Dumazet 
13251d7138deSEric Dumazet 	im->next_rcu = in_dev->mc_list;
1326b8bae41eSRami Rosen 	in_dev->mc_count++;
1327cf778b00SEric Dumazet 	rcu_assign_pointer(in_dev->mc_list, im);
13281d7138deSEric Dumazet 
1329e9897071SEric Dumazet 	ip_mc_hash_add(in_dev, im);
1330e9897071SEric Dumazet 
13311da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
13321da177e4SLinus Torvalds 	igmpv3_del_delrec(in_dev, im->multiaddr);
13331da177e4SLinus Torvalds #endif
13341da177e4SLinus Torvalds 	igmp_group_added(im);
13351da177e4SLinus Torvalds 	if (!in_dev->dead)
13361da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
13371da177e4SLinus Torvalds out:
13381da177e4SLinus Torvalds 	return;
13391da177e4SLinus Torvalds }
13404bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group);
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds /*
13434aa5dee4SJiri Pirko  *	Resend IGMP JOIN report; used by netdev notifier.
1344a816c7c7SJay Vosburgh  */
13454aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev)
1346a816c7c7SJay Vosburgh {
134708882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST
1348866f3b25SEric Dumazet 	struct ip_mc_list *im;
1349866f3b25SEric Dumazet 	int type;
1350a816c7c7SJay Vosburgh 
13514aa5dee4SJiri Pirko 	ASSERT_RTNL();
13524aa5dee4SJiri Pirko 
13534aa5dee4SJiri Pirko 	for_each_pmc_rtnl(in_dev, im) {
1354a816c7c7SJay Vosburgh 		if (im->multiaddr == IGMP_ALL_HOSTS)
1355866f3b25SEric Dumazet 			continue;
1356a816c7c7SJay Vosburgh 
1357e12b4539SFlavio Leitner 		/* a failover is happening and switches
1358866f3b25SEric Dumazet 		 * must be notified immediately
1359866f3b25SEric Dumazet 		 */
1360e12b4539SFlavio Leitner 		if (IGMP_V1_SEEN(in_dev))
1361866f3b25SEric Dumazet 			type = IGMP_HOST_MEMBERSHIP_REPORT;
1362e12b4539SFlavio Leitner 		else if (IGMP_V2_SEEN(in_dev))
1363866f3b25SEric Dumazet 			type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1364e12b4539SFlavio Leitner 		else
1365866f3b25SEric Dumazet 			type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1366866f3b25SEric Dumazet 		igmp_send_report(in_dev, im, type);
1367866f3b25SEric Dumazet 	}
1368a816c7c7SJay Vosburgh #endif
1369a816c7c7SJay Vosburgh }
1370a816c7c7SJay Vosburgh 
1371a816c7c7SJay Vosburgh /*
13721da177e4SLinus Torvalds  *	A socket has left a multicast group on device dev
13731da177e4SLinus Torvalds  */
13741da177e4SLinus Torvalds 
13758f935bbdSAl Viro void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
13761da177e4SLinus Torvalds {
13771d7138deSEric Dumazet 	struct ip_mc_list *i;
13781d7138deSEric Dumazet 	struct ip_mc_list __rcu **ip;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	ASSERT_RTNL();
13811da177e4SLinus Torvalds 
13821d7138deSEric Dumazet 	for (ip = &in_dev->mc_list;
13831d7138deSEric Dumazet 	     (i = rtnl_dereference(*ip)) != NULL;
13841d7138deSEric Dumazet 	     ip = &i->next_rcu) {
13851da177e4SLinus Torvalds 		if (i->multiaddr == addr) {
13861da177e4SLinus Torvalds 			if (--i->users == 0) {
1387e9897071SEric Dumazet 				ip_mc_hash_remove(in_dev, i);
13881d7138deSEric Dumazet 				*ip = i->next_rcu;
1389b8bae41eSRami Rosen 				in_dev->mc_count--;
13901da177e4SLinus Torvalds 				igmp_group_dropped(i);
139124cf3af3SVeaceslav Falico 				ip_mc_clear_src(i);
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 				if (!in_dev->dead)
13941da177e4SLinus Torvalds 					ip_rt_multicast_event(in_dev);
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 				ip_ma_put(i);
13971da177e4SLinus Torvalds 				return;
13981da177e4SLinus Torvalds 			}
13991da177e4SLinus Torvalds 			break;
14001da177e4SLinus Torvalds 		}
14011da177e4SLinus Torvalds 	}
14021da177e4SLinus Torvalds }
14034bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_dec_group);
14041da177e4SLinus Torvalds 
140575c78500SMoni Shoua /* Device changing type */
140675c78500SMoni Shoua 
140775c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev)
140875c78500SMoni Shoua {
14091d7138deSEric Dumazet 	struct ip_mc_list *pmc;
141075c78500SMoni Shoua 
141175c78500SMoni Shoua 	ASSERT_RTNL();
141275c78500SMoni Shoua 
14131d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
14141d7138deSEric Dumazet 		igmp_group_dropped(pmc);
141575c78500SMoni Shoua }
141675c78500SMoni Shoua 
141775c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev)
141875c78500SMoni Shoua {
14191d7138deSEric Dumazet 	struct ip_mc_list *pmc;
142075c78500SMoni Shoua 
142175c78500SMoni Shoua 	ASSERT_RTNL();
142275c78500SMoni Shoua 
14231d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
14241d7138deSEric Dumazet 		igmp_group_added(pmc);
142575c78500SMoni Shoua }
142675c78500SMoni Shoua 
14271da177e4SLinus Torvalds /* Device going down */
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev)
14301da177e4SLinus Torvalds {
14311d7138deSEric Dumazet 	struct ip_mc_list *pmc;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	ASSERT_RTNL();
14341da177e4SLinus Torvalds 
14351d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
14361d7138deSEric Dumazet 		igmp_group_dropped(pmc);
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
14391da177e4SLinus Torvalds 	in_dev->mr_ifc_count = 0;
14401da177e4SLinus Torvalds 	if (del_timer(&in_dev->mr_ifc_timer))
14411da177e4SLinus Torvalds 		__in_dev_put(in_dev);
14421da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
14431da177e4SLinus Torvalds 	if (del_timer(&in_dev->mr_gq_timer))
14441da177e4SLinus Torvalds 		__in_dev_put(in_dev);
14451da177e4SLinus Torvalds 	igmpv3_clear_delrec(in_dev);
14461da177e4SLinus Torvalds #endif
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	ASSERT_RTNL();
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1456b24b8a24SPavel Emelyanov 	setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1457b24b8a24SPavel Emelyanov 			(unsigned long)in_dev);
1458b24b8a24SPavel Emelyanov 	setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1459b24b8a24SPavel Emelyanov 			(unsigned long)in_dev);
1460a9fe8e29SHannes Frederic Sowa 	in_dev->mr_qrv = sysctl_igmp_qrv;
14611da177e4SLinus Torvalds #endif
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	spin_lock_init(&in_dev->mc_tomb_lock);
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds /* Device going up */
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev)
14691da177e4SLinus Torvalds {
14701d7138deSEric Dumazet 	struct ip_mc_list *pmc;
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	ASSERT_RTNL();
14731da177e4SLinus Torvalds 
1474a9fe8e29SHannes Frederic Sowa #ifdef CONFIG_IP_MULTICAST
1475a9fe8e29SHannes Frederic Sowa 	in_dev->mr_qrv = sysctl_igmp_qrv;
1476a9fe8e29SHannes Frederic Sowa #endif
14771da177e4SLinus Torvalds 	ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
14781da177e4SLinus Torvalds 
14791d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
14801d7138deSEric Dumazet 		igmp_group_added(pmc);
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds /*
14841da177e4SLinus Torvalds  *	Device is about to be destroyed: clean up.
14851da177e4SLinus Torvalds  */
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev)
14881da177e4SLinus Torvalds {
14891da177e4SLinus Torvalds 	struct ip_mc_list *i;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	ASSERT_RTNL();
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	/* Deactivate timers */
14941da177e4SLinus Torvalds 	ip_mc_down(in_dev);
14951da177e4SLinus Torvalds 
14961d7138deSEric Dumazet 	while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
14971d7138deSEric Dumazet 		in_dev->mc_list = i->next_rcu;
1498b8bae41eSRami Rosen 		in_dev->mc_count--;
14991d7138deSEric Dumazet 
150024cf3af3SVeaceslav Falico 		/* We've dropped the groups in ip_mc_down already */
150124cf3af3SVeaceslav Falico 		ip_mc_clear_src(i);
15021da177e4SLinus Torvalds 		ip_ma_put(i);
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15069e917dcaSEric Dumazet /* RTNL is locked */
1507877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	struct net_device *dev = NULL;
15101da177e4SLinus Torvalds 	struct in_device *idev = NULL;
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	if (imr->imr_ifindex) {
1513877acedcSDaniel Lezcano 		idev = inetdev_by_index(net, imr->imr_ifindex);
15141da177e4SLinus Torvalds 		return idev;
15151da177e4SLinus Torvalds 	}
15161da177e4SLinus Torvalds 	if (imr->imr_address.s_addr) {
15179e917dcaSEric Dumazet 		dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
15181da177e4SLinus Torvalds 		if (!dev)
15191da177e4SLinus Torvalds 			return NULL;
15201da177e4SLinus Torvalds 	}
15211da177e4SLinus Torvalds 
1522b23dd4feSDavid S. Miller 	if (!dev) {
152378fbfd8aSDavid S. Miller 		struct rtable *rt = ip_route_output(net,
152478fbfd8aSDavid S. Miller 						    imr->imr_multiaddr.s_addr,
152578fbfd8aSDavid S. Miller 						    0, 0, 0);
1526b23dd4feSDavid S. Miller 		if (!IS_ERR(rt)) {
1527d8d1f30bSChangli Gao 			dev = rt->dst.dev;
15281da177e4SLinus Torvalds 			ip_rt_put(rt);
15291da177e4SLinus Torvalds 		}
1530b23dd4feSDavid S. Miller 	}
15311da177e4SLinus Torvalds 	if (dev) {
15321da177e4SLinus Torvalds 		imr->imr_ifindex = dev->ifindex;
1533e5ed6399SHerbert Xu 		idev = __in_dev_get_rtnl(dev);
15341da177e4SLinus Torvalds 	}
15351da177e4SLinus Torvalds 	return idev;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds /*
15391da177e4SLinus Torvalds  *	Join a socket to a group
15401da177e4SLinus Torvalds  */
1541ab32ea5dSBrian Haley int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1542ab32ea5dSBrian Haley int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1543a9fe8e29SHannes Frederic Sowa #ifdef CONFIG_IP_MULTICAST
1544436f7c20SFabian Frederick int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE;
1545a9fe8e29SHannes Frederic Sowa #endif
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
15488f935bbdSAl Viro 	__be32 *psfsrc)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_prev;
15511da177e4SLinus Torvalds 	int rv = 0;
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	psf_prev = NULL;
15541da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
15551da177e4SLinus Torvalds 		if (psf->sf_inaddr == *psfsrc)
15561da177e4SLinus Torvalds 			break;
15571da177e4SLinus Torvalds 		psf_prev = psf;
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 	if (!psf || psf->sf_count[sfmode] == 0) {
15601da177e4SLinus Torvalds 		/* source filter not found, or count wrong =>  bug */
15611da177e4SLinus Torvalds 		return -ESRCH;
15621da177e4SLinus Torvalds 	}
15631da177e4SLinus Torvalds 	psf->sf_count[sfmode]--;
15641da177e4SLinus Torvalds 	if (psf->sf_count[sfmode] == 0) {
15651da177e4SLinus Torvalds 		ip_rt_multicast_event(pmc->interface);
15661da177e4SLinus Torvalds 	}
15671da177e4SLinus Torvalds 	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
15681da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
15691da177e4SLinus Torvalds 		struct in_device *in_dev = pmc->interface;
15701da177e4SLinus Torvalds #endif
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 		/* no more filters for this source */
15731da177e4SLinus Torvalds 		if (psf_prev)
15741da177e4SLinus Torvalds 			psf_prev->sf_next = psf->sf_next;
15751da177e4SLinus Torvalds 		else
15761da177e4SLinus Torvalds 			pmc->sources = psf->sf_next;
15771da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
15781da177e4SLinus Torvalds 		if (psf->sf_oldin &&
15791da177e4SLinus Torvalds 		    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1580a9fe8e29SHannes Frederic Sowa 			psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
15811da177e4SLinus Torvalds 			psf->sf_next = pmc->tomb;
15821da177e4SLinus Torvalds 			pmc->tomb = psf;
15831da177e4SLinus Torvalds 			rv = 1;
15841da177e4SLinus Torvalds 		} else
15851da177e4SLinus Torvalds #endif
15861da177e4SLinus Torvalds 			kfree(psf);
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 	return rv;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST
15921da177e4SLinus Torvalds #define igmp_ifc_event(x)	do { } while (0)
15931da177e4SLinus Torvalds #endif
15941da177e4SLinus Torvalds 
15958f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
15968f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
15991da177e4SLinus Torvalds 	int	changerec = 0;
16001da177e4SLinus Torvalds 	int	i, err;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	if (!in_dev)
16031da177e4SLinus Torvalds 		return -ENODEV;
16041d7138deSEric Dumazet 	rcu_read_lock();
16051d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
16061da177e4SLinus Torvalds 		if (*pmca == pmc->multiaddr)
16071da177e4SLinus Torvalds 			break;
16081da177e4SLinus Torvalds 	}
16091da177e4SLinus Torvalds 	if (!pmc) {
16101da177e4SLinus Torvalds 		/* MCA not found?? bug */
16111d7138deSEric Dumazet 		rcu_read_unlock();
16121da177e4SLinus Torvalds 		return -ESRCH;
16131da177e4SLinus Torvalds 	}
16141da177e4SLinus Torvalds 	spin_lock_bh(&pmc->lock);
16151d7138deSEric Dumazet 	rcu_read_unlock();
16161da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
16171da177e4SLinus Torvalds 	sf_markstate(pmc);
16181da177e4SLinus Torvalds #endif
16191da177e4SLinus Torvalds 	if (!delta) {
16201da177e4SLinus Torvalds 		err = -EINVAL;
16211da177e4SLinus Torvalds 		if (!pmc->sfcount[sfmode])
16221da177e4SLinus Torvalds 			goto out_unlock;
16231da177e4SLinus Torvalds 		pmc->sfcount[sfmode]--;
16241da177e4SLinus Torvalds 	}
16251da177e4SLinus Torvalds 	err = 0;
16261da177e4SLinus Torvalds 	for (i = 0; i < sfcount; i++) {
16271da177e4SLinus Torvalds 		int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 		changerec |= rv > 0;
16301da177e4SLinus Torvalds 		if (!err && rv < 0)
16311da177e4SLinus Torvalds 			err = rv;
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_EXCLUDE &&
16341da177e4SLinus Torvalds 	    pmc->sfcount[MCAST_EXCLUDE] == 0 &&
16351da177e4SLinus Torvalds 	    pmc->sfcount[MCAST_INCLUDE]) {
16361da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
16371da177e4SLinus Torvalds 		struct ip_sf_list *psf;
16381da177e4SLinus Torvalds #endif
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds 		/* filter mode change */
16411da177e4SLinus Torvalds 		pmc->sfmode = MCAST_INCLUDE;
16421da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1643a9fe8e29SHannes Frederic Sowa 		pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
16441da177e4SLinus Torvalds 		in_dev->mr_ifc_count = pmc->crcount;
16451da177e4SLinus Torvalds 		for (psf = pmc->sources; psf; psf = psf->sf_next)
16461da177e4SLinus Torvalds 			psf->sf_crcount = 0;
16471da177e4SLinus Torvalds 		igmp_ifc_event(pmc->interface);
16481da177e4SLinus Torvalds 	} else if (sf_setstate(pmc) || changerec) {
16491da177e4SLinus Torvalds 		igmp_ifc_event(pmc->interface);
16501da177e4SLinus Torvalds #endif
16511da177e4SLinus Torvalds 	}
16521da177e4SLinus Torvalds out_unlock:
16531da177e4SLinus Torvalds 	spin_unlock_bh(&pmc->lock);
16541da177e4SLinus Torvalds 	return err;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds /*
16581da177e4SLinus Torvalds  * Add multicast single-source filter to the interface list
16591da177e4SLinus Torvalds  */
16601da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
16615eb81e89SJun Zhao 	__be32 *psfsrc)
16621da177e4SLinus Torvalds {
16631da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_prev;
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	psf_prev = NULL;
16661da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
16671da177e4SLinus Torvalds 		if (psf->sf_inaddr == *psfsrc)
16681da177e4SLinus Torvalds 			break;
16691da177e4SLinus Torvalds 		psf_prev = psf;
16701da177e4SLinus Torvalds 	}
16711da177e4SLinus Torvalds 	if (!psf) {
16720da974f4SPanagiotis Issaris 		psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
16731da177e4SLinus Torvalds 		if (!psf)
16741da177e4SLinus Torvalds 			return -ENOBUFS;
16751da177e4SLinus Torvalds 		psf->sf_inaddr = *psfsrc;
16761da177e4SLinus Torvalds 		if (psf_prev) {
16771da177e4SLinus Torvalds 			psf_prev->sf_next = psf;
16781da177e4SLinus Torvalds 		} else
16791da177e4SLinus Torvalds 			pmc->sources = psf;
16801da177e4SLinus Torvalds 	}
16811da177e4SLinus Torvalds 	psf->sf_count[sfmode]++;
16821da177e4SLinus Torvalds 	if (psf->sf_count[sfmode] == 1) {
16831da177e4SLinus Torvalds 		ip_rt_multicast_event(pmc->interface);
16841da177e4SLinus Torvalds 	}
16851da177e4SLinus Torvalds 	return 0;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
16891da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc)
16901da177e4SLinus Torvalds {
16911da177e4SLinus Torvalds 	struct ip_sf_list *psf;
16921da177e4SLinus Torvalds 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next)
16951da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
16961da177e4SLinus Torvalds 			psf->sf_oldin = mca_xcount ==
16971da177e4SLinus Torvalds 				psf->sf_count[MCAST_EXCLUDE] &&
16981da177e4SLinus Torvalds 				!psf->sf_count[MCAST_INCLUDE];
16991da177e4SLinus Torvalds 		} else
17001da177e4SLinus Torvalds 			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc)
17041da177e4SLinus Torvalds {
1705ad12583fSDavid L Stevens 	struct ip_sf_list *psf, *dpsf;
17061da177e4SLinus Torvalds 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
17071da177e4SLinus Torvalds 	int qrv = pmc->interface->mr_qrv;
17081da177e4SLinus Torvalds 	int new_in, rv;
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	rv = 0;
17111da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = psf->sf_next) {
17121da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
17131da177e4SLinus Torvalds 			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
17141da177e4SLinus Torvalds 				!psf->sf_count[MCAST_INCLUDE];
17151da177e4SLinus Torvalds 		} else
17161da177e4SLinus Torvalds 			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1717ad12583fSDavid L Stevens 		if (new_in) {
1718ad12583fSDavid L Stevens 			if (!psf->sf_oldin) {
171976edc605SAl Viro 				struct ip_sf_list *prev = NULL;
1720ad12583fSDavid L Stevens 
1721ad12583fSDavid L Stevens 				for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
1722ad12583fSDavid L Stevens 					if (dpsf->sf_inaddr == psf->sf_inaddr)
1723ad12583fSDavid L Stevens 						break;
1724ad12583fSDavid L Stevens 					prev = dpsf;
1725ad12583fSDavid L Stevens 				}
1726ad12583fSDavid L Stevens 				if (dpsf) {
1727ad12583fSDavid L Stevens 					if (prev)
1728ad12583fSDavid L Stevens 						prev->sf_next = dpsf->sf_next;
1729ad12583fSDavid L Stevens 					else
1730ad12583fSDavid L Stevens 						pmc->tomb = dpsf->sf_next;
1731ad12583fSDavid L Stevens 					kfree(dpsf);
1732ad12583fSDavid L Stevens 				}
17331da177e4SLinus Torvalds 				psf->sf_crcount = qrv;
17341da177e4SLinus Torvalds 				rv++;
17351da177e4SLinus Torvalds 			}
1736ad12583fSDavid L Stevens 		} else if (psf->sf_oldin) {
1737ad12583fSDavid L Stevens 
1738ad12583fSDavid L Stevens 			psf->sf_crcount = 0;
1739ad12583fSDavid L Stevens 			/*
1740ad12583fSDavid L Stevens 			 * add or update "delete" records if an active filter
1741ad12583fSDavid L Stevens 			 * is now inactive
1742ad12583fSDavid L Stevens 			 */
1743ad12583fSDavid L Stevens 			for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
1744ad12583fSDavid L Stevens 				if (dpsf->sf_inaddr == psf->sf_inaddr)
1745ad12583fSDavid L Stevens 					break;
1746ad12583fSDavid L Stevens 			if (!dpsf) {
17473ed37a6fSJoe Perches 				dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1748ad12583fSDavid L Stevens 				if (!dpsf)
1749ad12583fSDavid L Stevens 					continue;
1750ad12583fSDavid L Stevens 				*dpsf = *psf;
1751ad12583fSDavid L Stevens 				/* pmc->lock held by callers */
1752ad12583fSDavid L Stevens 				dpsf->sf_next = pmc->tomb;
1753ad12583fSDavid L Stevens 				pmc->tomb = dpsf;
1754ad12583fSDavid L Stevens 			}
1755ad12583fSDavid L Stevens 			dpsf->sf_crcount = qrv;
1756ad12583fSDavid L Stevens 			rv++;
1757ad12583fSDavid L Stevens 		}
17581da177e4SLinus Torvalds 	}
17591da177e4SLinus Torvalds 	return rv;
17601da177e4SLinus Torvalds }
17611da177e4SLinus Torvalds #endif
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds /*
17641da177e4SLinus Torvalds  * Add multicast source filter list to the interface list
17651da177e4SLinus Torvalds  */
17668f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
17678f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta)
17681da177e4SLinus Torvalds {
17691da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
17701da177e4SLinus Torvalds 	int	isexclude;
17711da177e4SLinus Torvalds 	int	i, err;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	if (!in_dev)
17741da177e4SLinus Torvalds 		return -ENODEV;
17751d7138deSEric Dumazet 	rcu_read_lock();
17761d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
17771da177e4SLinus Torvalds 		if (*pmca == pmc->multiaddr)
17781da177e4SLinus Torvalds 			break;
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 	if (!pmc) {
17811da177e4SLinus Torvalds 		/* MCA not found?? bug */
17821d7138deSEric Dumazet 		rcu_read_unlock();
17831da177e4SLinus Torvalds 		return -ESRCH;
17841da177e4SLinus Torvalds 	}
17851da177e4SLinus Torvalds 	spin_lock_bh(&pmc->lock);
17861d7138deSEric Dumazet 	rcu_read_unlock();
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
17891da177e4SLinus Torvalds 	sf_markstate(pmc);
17901da177e4SLinus Torvalds #endif
17911da177e4SLinus Torvalds 	isexclude = pmc->sfmode == MCAST_EXCLUDE;
17921da177e4SLinus Torvalds 	if (!delta)
17931da177e4SLinus Torvalds 		pmc->sfcount[sfmode]++;
17941da177e4SLinus Torvalds 	err = 0;
17951da177e4SLinus Torvalds 	for (i = 0; i < sfcount; i++) {
17965eb81e89SJun Zhao 		err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
17971da177e4SLinus Torvalds 		if (err)
17981da177e4SLinus Torvalds 			break;
17991da177e4SLinus Torvalds 	}
18001da177e4SLinus Torvalds 	if (err) {
18011da177e4SLinus Torvalds 		int j;
18021da177e4SLinus Torvalds 
1803685f94e6SJun Zhao 		if (!delta)
18041da177e4SLinus Torvalds 			pmc->sfcount[sfmode]--;
18051da177e4SLinus Torvalds 		for (j = 0; j < i; j++)
1806a1889c0dSJulia Lawall 			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
18071da177e4SLinus Torvalds 	} else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
18081da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
18091da177e4SLinus Torvalds 		struct ip_sf_list *psf;
1810cfcabdccSStephen Hemminger 		in_dev = pmc->interface;
18111da177e4SLinus Torvalds #endif
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 		/* filter mode change */
18141da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE])
18151da177e4SLinus Torvalds 			pmc->sfmode = MCAST_EXCLUDE;
18161da177e4SLinus Torvalds 		else if (pmc->sfcount[MCAST_INCLUDE])
18171da177e4SLinus Torvalds 			pmc->sfmode = MCAST_INCLUDE;
18181da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
18191da177e4SLinus Torvalds 		/* else no filters; keep old mode for reports */
18201da177e4SLinus Torvalds 
1821a9fe8e29SHannes Frederic Sowa 		pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
18221da177e4SLinus Torvalds 		in_dev->mr_ifc_count = pmc->crcount;
18231da177e4SLinus Torvalds 		for (psf = pmc->sources; psf; psf = psf->sf_next)
18241da177e4SLinus Torvalds 			psf->sf_crcount = 0;
18251da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
18261da177e4SLinus Torvalds 	} else if (sf_setstate(pmc)) {
18271da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
18281da177e4SLinus Torvalds #endif
18291da177e4SLinus Torvalds 	}
18301da177e4SLinus Torvalds 	spin_unlock_bh(&pmc->lock);
18311da177e4SLinus Torvalds 	return err;
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc)
18351da177e4SLinus Torvalds {
18361da177e4SLinus Torvalds 	struct ip_sf_list *psf, *nextpsf;
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 	for (psf = pmc->tomb; psf; psf = nextpsf) {
18391da177e4SLinus Torvalds 		nextpsf = psf->sf_next;
18401da177e4SLinus Torvalds 		kfree(psf);
18411da177e4SLinus Torvalds 	}
18421da177e4SLinus Torvalds 	pmc->tomb = NULL;
18431da177e4SLinus Torvalds 	for (psf = pmc->sources; psf; psf = nextpsf) {
18441da177e4SLinus Torvalds 		nextpsf = psf->sf_next;
18451da177e4SLinus Torvalds 		kfree(psf);
18461da177e4SLinus Torvalds 	}
18471da177e4SLinus Torvalds 	pmc->sources = NULL;
18481da177e4SLinus Torvalds 	pmc->sfmode = MCAST_EXCLUDE;
1849de9daad9SDenis Lukianov 	pmc->sfcount[MCAST_INCLUDE] = 0;
18501da177e4SLinus Torvalds 	pmc->sfcount[MCAST_EXCLUDE] = 1;
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds /*
18551da177e4SLinus Torvalds  * Join a multicast group
18561da177e4SLinus Torvalds  */
18571da177e4SLinus Torvalds int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
18581da177e4SLinus Torvalds {
18591da177e4SLinus Torvalds 	int err;
18608f935bbdSAl Viro 	__be32 addr = imr->imr_multiaddr.s_addr;
1861ca9b907dSDavid L Stevens 	struct ip_mc_socklist *iml = NULL, *i;
18621da177e4SLinus Torvalds 	struct in_device *in_dev;
18631da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1864877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
1865ca9b907dSDavid L Stevens 	int ifindex;
18661da177e4SLinus Torvalds 	int count = 0;
18671da177e4SLinus Torvalds 
1868f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
18691da177e4SLinus Torvalds 		return -EINVAL;
18701da177e4SLinus Torvalds 
18716756ae4bSStephen Hemminger 	rtnl_lock();
18721da177e4SLinus Torvalds 
1873877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, imr);
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	if (!in_dev) {
18761da177e4SLinus Torvalds 		iml = NULL;
18771da177e4SLinus Torvalds 		err = -ENODEV;
18781da177e4SLinus Torvalds 		goto done;
18791da177e4SLinus Torvalds 	}
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	err = -EADDRINUSE;
1882ca9b907dSDavid L Stevens 	ifindex = imr->imr_ifindex;
18831d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, i) {
1884ca9b907dSDavid L Stevens 		if (i->multi.imr_multiaddr.s_addr == addr &&
1885ca9b907dSDavid L Stevens 		    i->multi.imr_ifindex == ifindex)
18861da177e4SLinus Torvalds 			goto done;
18871da177e4SLinus Torvalds 		count++;
18881da177e4SLinus Torvalds 	}
18891da177e4SLinus Torvalds 	err = -ENOBUFS;
1890ca9b907dSDavid L Stevens 	if (count >= sysctl_igmp_max_memberships)
18911da177e4SLinus Torvalds 		goto done;
18928b3a7005SKris Katterjohn 	iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1893ca9b907dSDavid L Stevens 	if (iml == NULL)
1894ca9b907dSDavid L Stevens 		goto done;
1895ca9b907dSDavid L Stevens 
18961da177e4SLinus Torvalds 	memcpy(&iml->multi, imr, sizeof(*imr));
18971d7138deSEric Dumazet 	iml->next_rcu = inet->mc_list;
18981da177e4SLinus Torvalds 	iml->sflist = NULL;
18991da177e4SLinus Torvalds 	iml->sfmode = MCAST_EXCLUDE;
1900cf778b00SEric Dumazet 	rcu_assign_pointer(inet->mc_list, iml);
19011da177e4SLinus Torvalds 	ip_mc_inc_group(in_dev, addr);
19021da177e4SLinus Torvalds 	err = 0;
19031da177e4SLinus Torvalds done:
19046756ae4bSStephen Hemminger 	rtnl_unlock();
19051da177e4SLinus Torvalds 	return err;
19061da177e4SLinus Torvalds }
19074bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group);
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
19101da177e4SLinus Torvalds 			   struct in_device *in_dev)
19111da177e4SLinus Torvalds {
19121d7138deSEric Dumazet 	struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
19131da177e4SLinus Torvalds 	int err;
19141da177e4SLinus Torvalds 
1915c85bb41eSFlavio Leitner 	if (psf == NULL) {
19161da177e4SLinus Torvalds 		/* any-source empty exclude case */
19171da177e4SLinus Torvalds 		return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
19181da177e4SLinus Torvalds 			iml->sfmode, 0, NULL, 0);
19191da177e4SLinus Torvalds 	}
19201da177e4SLinus Torvalds 	err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1921c85bb41eSFlavio Leitner 			iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1922a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(iml->sflist, NULL);
1923c85bb41eSFlavio Leitner 	/* decrease mem now to avoid the memleak warning */
1924c85bb41eSFlavio Leitner 	atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
19257519cce4SLai Jiangshan 	kfree_rcu(psf, rcu);
19261da177e4SLinus Torvalds 	return err;
19271da177e4SLinus Torvalds }
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds /*
19301da177e4SLinus Torvalds  *	Ask a socket to leave a group.
19311da177e4SLinus Torvalds  */
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
19341da177e4SLinus Torvalds {
19351da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
19361d7138deSEric Dumazet 	struct ip_mc_socklist *iml;
19371d7138deSEric Dumazet 	struct ip_mc_socklist __rcu **imlp;
193884b42baeSDavid L Stevens 	struct in_device *in_dev;
1939877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
19408f935bbdSAl Viro 	__be32 group = imr->imr_multiaddr.s_addr;
194184b42baeSDavid L Stevens 	u32 ifindex;
1942acd6e00bSDavid L Stevens 	int ret = -EADDRNOTAVAIL;
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	rtnl_lock();
1945877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, imr);
194652ad353aSdingtianhong 	if (!in_dev) {
194752ad353aSdingtianhong 		ret = -ENODEV;
194852ad353aSdingtianhong 		goto out;
194952ad353aSdingtianhong 	}
195084b42baeSDavid L Stevens 	ifindex = imr->imr_ifindex;
19511d7138deSEric Dumazet 	for (imlp = &inet->mc_list;
19521d7138deSEric Dumazet 	     (iml = rtnl_dereference(*imlp)) != NULL;
19531d7138deSEric Dumazet 	     imlp = &iml->next_rcu) {
1954acd6e00bSDavid L Stevens 		if (iml->multi.imr_multiaddr.s_addr != group)
1955acd6e00bSDavid L Stevens 			continue;
1956acd6e00bSDavid L Stevens 		if (ifindex) {
1957acd6e00bSDavid L Stevens 			if (iml->multi.imr_ifindex != ifindex)
1958acd6e00bSDavid L Stevens 				continue;
1959acd6e00bSDavid L Stevens 		} else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1960acd6e00bSDavid L Stevens 				iml->multi.imr_address.s_addr)
1961acd6e00bSDavid L Stevens 			continue;
1962acd6e00bSDavid L Stevens 
19631da177e4SLinus Torvalds 		(void) ip_mc_leave_src(sk, iml, in_dev);
19641da177e4SLinus Torvalds 
19651d7138deSEric Dumazet 		*imlp = iml->next_rcu;
19661da177e4SLinus Torvalds 
196784b42baeSDavid L Stevens 		ip_mc_dec_group(in_dev, group);
19681da177e4SLinus Torvalds 		rtnl_unlock();
1969c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
1970c85bb41eSFlavio Leitner 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
197110d50e74SLai Jiangshan 		kfree_rcu(iml, rcu);
19721da177e4SLinus Torvalds 		return 0;
19731da177e4SLinus Torvalds 	}
197452ad353aSdingtianhong out:
19751da177e4SLinus Torvalds 	rtnl_unlock();
1976acd6e00bSDavid L Stevens 	return ret;
19771da177e4SLinus Torvalds }
1978193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group);
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct
19811da177e4SLinus Torvalds 	ip_mreq_source *mreqs, int ifindex)
19821da177e4SLinus Torvalds {
19831da177e4SLinus Torvalds 	int err;
19841da177e4SLinus Torvalds 	struct ip_mreqn imr;
198563007727SAl Viro 	__be32 addr = mreqs->imr_multiaddr;
19861da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
19871da177e4SLinus Torvalds 	struct in_device *in_dev = NULL;
19881da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
19891da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
1990877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
19918cdaaa15SDavid L Stevens 	int leavegroup = 0;
19921da177e4SLinus Torvalds 	int i, j, rv;
19931da177e4SLinus Torvalds 
1994f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
19951da177e4SLinus Torvalds 		return -EINVAL;
19961da177e4SLinus Torvalds 
19976756ae4bSStephen Hemminger 	rtnl_lock();
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
20001da177e4SLinus Torvalds 	imr.imr_address.s_addr = mreqs->imr_interface;
20011da177e4SLinus Torvalds 	imr.imr_ifindex = ifindex;
2002877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 	if (!in_dev) {
20051da177e4SLinus Torvalds 		err = -ENODEV;
20061da177e4SLinus Torvalds 		goto done;
20071da177e4SLinus Torvalds 	}
20081da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
20091da177e4SLinus Torvalds 
20101d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
2011f64f9e71SJoe Perches 		if ((pmc->multi.imr_multiaddr.s_addr ==
2012f64f9e71SJoe Perches 		     imr.imr_multiaddr.s_addr) &&
2013f64f9e71SJoe Perches 		    (pmc->multi.imr_ifindex == imr.imr_ifindex))
20141da177e4SLinus Torvalds 			break;
20151da177e4SLinus Torvalds 	}
2016917f2f10SDavid L Stevens 	if (!pmc) {		/* must have a prior join */
2017917f2f10SDavid L Stevens 		err = -EINVAL;
20181da177e4SLinus Torvalds 		goto done;
2019917f2f10SDavid L Stevens 	}
20201da177e4SLinus Torvalds 	/* if a source filter was set, must be the same mode as before */
20211da177e4SLinus Torvalds 	if (pmc->sflist) {
2022917f2f10SDavid L Stevens 		if (pmc->sfmode != omode) {
2023917f2f10SDavid L Stevens 			err = -EINVAL;
20241da177e4SLinus Torvalds 			goto done;
2025917f2f10SDavid L Stevens 		}
20261da177e4SLinus Torvalds 	} else if (pmc->sfmode != omode) {
20271da177e4SLinus Torvalds 		/* allow mode switches for empty-set filters */
20281da177e4SLinus Torvalds 		ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
20291da177e4SLinus Torvalds 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
20301da177e4SLinus Torvalds 			NULL, 0);
20311da177e4SLinus Torvalds 		pmc->sfmode = omode;
20321da177e4SLinus Torvalds 	}
20331da177e4SLinus Torvalds 
20341d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
20351da177e4SLinus Torvalds 	if (!add) {
20361da177e4SLinus Torvalds 		if (!psl)
2037917f2f10SDavid L Stevens 			goto done;	/* err = -EADDRNOTAVAIL */
20381da177e4SLinus Torvalds 		rv = !0;
20391da177e4SLinus Torvalds 		for (i = 0; i < psl->sl_count; i++) {
20401da177e4SLinus Torvalds 			rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
204163007727SAl Viro 				sizeof(__be32));
20421da177e4SLinus Torvalds 			if (rv == 0)
20431da177e4SLinus Torvalds 				break;
20441da177e4SLinus Torvalds 		}
20451da177e4SLinus Torvalds 		if (rv)		/* source not found */
2046917f2f10SDavid L Stevens 			goto done;	/* err = -EADDRNOTAVAIL */
20471da177e4SLinus Torvalds 
20488cdaaa15SDavid L Stevens 		/* special case - (INCLUDE, empty) == LEAVE_GROUP */
20498cdaaa15SDavid L Stevens 		if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
20508cdaaa15SDavid L Stevens 			leavegroup = 1;
20518cdaaa15SDavid L Stevens 			goto done;
20528cdaaa15SDavid L Stevens 		}
20538cdaaa15SDavid L Stevens 
20541da177e4SLinus Torvalds 		/* update the interface filter */
20551da177e4SLinus Torvalds 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
20561da177e4SLinus Torvalds 			&mreqs->imr_sourceaddr, 1);
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 		for (j = i+1; j < psl->sl_count; j++)
20591da177e4SLinus Torvalds 			psl->sl_addr[j-1] = psl->sl_addr[j];
20601da177e4SLinus Torvalds 		psl->sl_count--;
20611da177e4SLinus Torvalds 		err = 0;
20621da177e4SLinus Torvalds 		goto done;
20631da177e4SLinus Torvalds 	}
20641da177e4SLinus Torvalds 	/* else, add a new source to the filter */
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
20671da177e4SLinus Torvalds 		err = -ENOBUFS;
20681da177e4SLinus Torvalds 		goto done;
20691da177e4SLinus Torvalds 	}
20701da177e4SLinus Torvalds 	if (!psl || psl->sl_count == psl->sl_max) {
20711da177e4SLinus Torvalds 		struct ip_sf_socklist *newpsl;
20721da177e4SLinus Torvalds 		int count = IP_SFBLOCK;
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 		if (psl)
20751da177e4SLinus Torvalds 			count += psl->sl_max;
20768b3a7005SKris Katterjohn 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
20771da177e4SLinus Torvalds 		if (!newpsl) {
20781da177e4SLinus Torvalds 			err = -ENOBUFS;
20791da177e4SLinus Torvalds 			goto done;
20801da177e4SLinus Torvalds 		}
20811da177e4SLinus Torvalds 		newpsl->sl_max = count;
20821da177e4SLinus Torvalds 		newpsl->sl_count = count - IP_SFBLOCK;
20831da177e4SLinus Torvalds 		if (psl) {
20841da177e4SLinus Torvalds 			for (i = 0; i < psl->sl_count; i++)
20851da177e4SLinus Torvalds 				newpsl->sl_addr[i] = psl->sl_addr[i];
2086c85bb41eSFlavio Leitner 			/* decrease mem now to avoid the memleak warning */
2087c85bb41eSFlavio Leitner 			atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
20887519cce4SLai Jiangshan 			kfree_rcu(psl, rcu);
20891da177e4SLinus Torvalds 		}
2090cf778b00SEric Dumazet 		rcu_assign_pointer(pmc->sflist, newpsl);
2091c85bb41eSFlavio Leitner 		psl = newpsl;
20921da177e4SLinus Torvalds 	}
20931da177e4SLinus Torvalds 	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
20941da177e4SLinus Torvalds 	for (i = 0; i < psl->sl_count; i++) {
20951da177e4SLinus Torvalds 		rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
209663007727SAl Viro 			sizeof(__be32));
20971da177e4SLinus Torvalds 		if (rv == 0)
20981da177e4SLinus Torvalds 			break;
20991da177e4SLinus Torvalds 	}
21001da177e4SLinus Torvalds 	if (rv == 0)		/* address already there is an error */
21011da177e4SLinus Torvalds 		goto done;
21021da177e4SLinus Torvalds 	for (j = psl->sl_count-1; j >= i; j--)
21031da177e4SLinus Torvalds 		psl->sl_addr[j+1] = psl->sl_addr[j];
21041da177e4SLinus Torvalds 	psl->sl_addr[i] = mreqs->imr_sourceaddr;
21051da177e4SLinus Torvalds 	psl->sl_count++;
21061da177e4SLinus Torvalds 	err = 0;
21071da177e4SLinus Torvalds 	/* update the interface list */
21081da177e4SLinus Torvalds 	ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
21091da177e4SLinus Torvalds 		&mreqs->imr_sourceaddr, 1);
21101da177e4SLinus Torvalds done:
21116756ae4bSStephen Hemminger 	rtnl_unlock();
21128cdaaa15SDavid L Stevens 	if (leavegroup)
21138cdaaa15SDavid L Stevens 		return ip_mc_leave_group(sk, &imr);
21141da177e4SLinus Torvalds 	return err;
21151da177e4SLinus Torvalds }
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
21181da177e4SLinus Torvalds {
21199951f036SDavid L Stevens 	int err = 0;
21201da177e4SLinus Torvalds 	struct ip_mreqn	imr;
212163007727SAl Viro 	__be32 addr = msf->imsf_multiaddr;
21221da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
21231da177e4SLinus Torvalds 	struct in_device *in_dev;
21241da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
21251da177e4SLinus Torvalds 	struct ip_sf_socklist *newpsl, *psl;
2126877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
21279951f036SDavid L Stevens 	int leavegroup = 0;
21281da177e4SLinus Torvalds 
2129f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
21301da177e4SLinus Torvalds 		return -EINVAL;
21311da177e4SLinus Torvalds 	if (msf->imsf_fmode != MCAST_INCLUDE &&
21321da177e4SLinus Torvalds 	    msf->imsf_fmode != MCAST_EXCLUDE)
21331da177e4SLinus Torvalds 		return -EINVAL;
21341da177e4SLinus Torvalds 
21356756ae4bSStephen Hemminger 	rtnl_lock();
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
21381da177e4SLinus Torvalds 	imr.imr_address.s_addr = msf->imsf_interface;
21391da177e4SLinus Torvalds 	imr.imr_ifindex = ifindex;
2140877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 	if (!in_dev) {
21431da177e4SLinus Torvalds 		err = -ENODEV;
21441da177e4SLinus Torvalds 		goto done;
21451da177e4SLinus Torvalds 	}
21461da177e4SLinus Torvalds 
21479951f036SDavid L Stevens 	/* special case - (INCLUDE, empty) == LEAVE_GROUP */
21489951f036SDavid L Stevens 	if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
21499951f036SDavid L Stevens 		leavegroup = 1;
21509951f036SDavid L Stevens 		goto done;
21519951f036SDavid L Stevens 	}
21529951f036SDavid L Stevens 
21531d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
21541da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
21551da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
21561da177e4SLinus Torvalds 			break;
21571da177e4SLinus Torvalds 	}
2158917f2f10SDavid L Stevens 	if (!pmc) {		/* must have a prior join */
2159917f2f10SDavid L Stevens 		err = -EINVAL;
21601da177e4SLinus Torvalds 		goto done;
2161917f2f10SDavid L Stevens 	}
21621da177e4SLinus Torvalds 	if (msf->imsf_numsrc) {
21638b3a7005SKris Katterjohn 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
21648b3a7005SKris Katterjohn 							   GFP_KERNEL);
21651da177e4SLinus Torvalds 		if (!newpsl) {
21661da177e4SLinus Torvalds 			err = -ENOBUFS;
21671da177e4SLinus Torvalds 			goto done;
21681da177e4SLinus Torvalds 		}
21691da177e4SLinus Torvalds 		newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
21701da177e4SLinus Torvalds 		memcpy(newpsl->sl_addr, msf->imsf_slist,
21711da177e4SLinus Torvalds 			msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
21721da177e4SLinus Torvalds 		err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
21731da177e4SLinus Torvalds 			msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
21741da177e4SLinus Torvalds 		if (err) {
21751da177e4SLinus Torvalds 			sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
21761da177e4SLinus Torvalds 			goto done;
21771da177e4SLinus Torvalds 		}
21788713dbf0SYan Zheng 	} else {
21791da177e4SLinus Torvalds 		newpsl = NULL;
21808713dbf0SYan Zheng 		(void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
21818713dbf0SYan Zheng 				     msf->imsf_fmode, 0, NULL, 0);
21828713dbf0SYan Zheng 	}
21831d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
21841da177e4SLinus Torvalds 	if (psl) {
21851da177e4SLinus Torvalds 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
21861da177e4SLinus Torvalds 			psl->sl_count, psl->sl_addr, 0);
2187c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
2188c85bb41eSFlavio Leitner 		atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
21897519cce4SLai Jiangshan 		kfree_rcu(psl, rcu);
21901da177e4SLinus Torvalds 	} else
21911da177e4SLinus Torvalds 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
21921da177e4SLinus Torvalds 			0, NULL, 0);
2193cf778b00SEric Dumazet 	rcu_assign_pointer(pmc->sflist, newpsl);
21941da177e4SLinus Torvalds 	pmc->sfmode = msf->imsf_fmode;
2195917f2f10SDavid L Stevens 	err = 0;
21961da177e4SLinus Torvalds done:
21976756ae4bSStephen Hemminger 	rtnl_unlock();
21989951f036SDavid L Stevens 	if (leavegroup)
21999951f036SDavid L Stevens 		err = ip_mc_leave_group(sk, &imr);
22001da177e4SLinus Torvalds 	return err;
22011da177e4SLinus Torvalds }
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
22041da177e4SLinus Torvalds 	struct ip_msfilter __user *optval, int __user *optlen)
22051da177e4SLinus Torvalds {
22061da177e4SLinus Torvalds 	int err, len, count, copycount;
22071da177e4SLinus Torvalds 	struct ip_mreqn	imr;
220863007727SAl Viro 	__be32 addr = msf->imsf_multiaddr;
22091da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
22101da177e4SLinus Torvalds 	struct in_device *in_dev;
22111da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
22121da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
2213877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
22141da177e4SLinus Torvalds 
2215f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
22161da177e4SLinus Torvalds 		return -EINVAL;
22171da177e4SLinus Torvalds 
22186756ae4bSStephen Hemminger 	rtnl_lock();
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
22211da177e4SLinus Torvalds 	imr.imr_address.s_addr = msf->imsf_interface;
22221da177e4SLinus Torvalds 	imr.imr_ifindex = 0;
2223877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	if (!in_dev) {
22261da177e4SLinus Torvalds 		err = -ENODEV;
22271da177e4SLinus Torvalds 		goto done;
22281da177e4SLinus Torvalds 	}
22291da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
22301da177e4SLinus Torvalds 
22311d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
22321da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
22331da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
22341da177e4SLinus Torvalds 			break;
22351da177e4SLinus Torvalds 	}
22361da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
22371da177e4SLinus Torvalds 		goto done;
22381da177e4SLinus Torvalds 	msf->imsf_fmode = pmc->sfmode;
22391d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
22406756ae4bSStephen Hemminger 	rtnl_unlock();
22411da177e4SLinus Torvalds 	if (!psl) {
22421da177e4SLinus Torvalds 		len = 0;
22431da177e4SLinus Torvalds 		count = 0;
22441da177e4SLinus Torvalds 	} else {
22451da177e4SLinus Torvalds 		count = psl->sl_count;
22461da177e4SLinus Torvalds 	}
22471da177e4SLinus Torvalds 	copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
22481da177e4SLinus Torvalds 	len = copycount * sizeof(psl->sl_addr[0]);
22491da177e4SLinus Torvalds 	msf->imsf_numsrc = count;
22501da177e4SLinus Torvalds 	if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
22511da177e4SLinus Torvalds 	    copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
22521da177e4SLinus Torvalds 		return -EFAULT;
22531da177e4SLinus Torvalds 	}
22541da177e4SLinus Torvalds 	if (len &&
22551da177e4SLinus Torvalds 	    copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
22561da177e4SLinus Torvalds 		return -EFAULT;
22571da177e4SLinus Torvalds 	return 0;
22581da177e4SLinus Torvalds done:
22596756ae4bSStephen Hemminger 	rtnl_unlock();
22601da177e4SLinus Torvalds 	return err;
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
22641da177e4SLinus Torvalds 	struct group_filter __user *optval, int __user *optlen)
22651da177e4SLinus Torvalds {
22661da177e4SLinus Torvalds 	int err, i, count, copycount;
22671da177e4SLinus Torvalds 	struct sockaddr_in *psin;
226863007727SAl Viro 	__be32 addr;
22691da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
22701da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
22711da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	psin = (struct sockaddr_in *)&gsf->gf_group;
22741da177e4SLinus Torvalds 	if (psin->sin_family != AF_INET)
22751da177e4SLinus Torvalds 		return -EINVAL;
22761da177e4SLinus Torvalds 	addr = psin->sin_addr.s_addr;
2277f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
22781da177e4SLinus Torvalds 		return -EINVAL;
22791da177e4SLinus Torvalds 
22806756ae4bSStephen Hemminger 	rtnl_lock();
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
22831da177e4SLinus Torvalds 
22841d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
22851da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == addr &&
22861da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == gsf->gf_interface)
22871da177e4SLinus Torvalds 			break;
22881da177e4SLinus Torvalds 	}
22891da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
22901da177e4SLinus Torvalds 		goto done;
22911da177e4SLinus Torvalds 	gsf->gf_fmode = pmc->sfmode;
22921d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
22936756ae4bSStephen Hemminger 	rtnl_unlock();
22941da177e4SLinus Torvalds 	count = psl ? psl->sl_count : 0;
22951da177e4SLinus Torvalds 	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
22961da177e4SLinus Torvalds 	gsf->gf_numsrc = count;
22971da177e4SLinus Torvalds 	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
22981da177e4SLinus Torvalds 	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
22991da177e4SLinus Torvalds 		return -EFAULT;
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 	for (i = 0; i < copycount; i++) {
23021da177e4SLinus Torvalds 		struct sockaddr_storage ss;
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&ss;
23051da177e4SLinus Torvalds 		memset(&ss, 0, sizeof(ss));
23061da177e4SLinus Torvalds 		psin->sin_family = AF_INET;
23071da177e4SLinus Torvalds 		psin->sin_addr.s_addr = psl->sl_addr[i];
23081da177e4SLinus Torvalds 		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
23091da177e4SLinus Torvalds 			return -EFAULT;
23101da177e4SLinus Torvalds 	}
23111da177e4SLinus Torvalds 	return 0;
23121da177e4SLinus Torvalds done:
23136756ae4bSStephen Hemminger 	rtnl_unlock();
23141da177e4SLinus Torvalds 	return err;
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds /*
23181da177e4SLinus Torvalds  * check if a multicast source filter allows delivery for a given <src,dst,intf>
23191da177e4SLinus Torvalds  */
2320c0cda068SAl Viro int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
23211da177e4SLinus Torvalds {
23221da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
23231da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
23241da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
23251da177e4SLinus Torvalds 	int i;
2326c85bb41eSFlavio Leitner 	int ret;
23271da177e4SLinus Torvalds 
2328c85bb41eSFlavio Leitner 	ret = 1;
2329f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(loc_addr))
2330c85bb41eSFlavio Leitner 		goto out;
23311da177e4SLinus Torvalds 
2332c85bb41eSFlavio Leitner 	rcu_read_lock();
23331d7138deSEric Dumazet 	for_each_pmc_rcu(inet, pmc) {
23341da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
23351da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == dif)
23361da177e4SLinus Torvalds 			break;
23371da177e4SLinus Torvalds 	}
2338c85bb41eSFlavio Leitner 	ret = inet->mc_all;
23391da177e4SLinus Torvalds 	if (!pmc)
2340c85bb41eSFlavio Leitner 		goto unlock;
23411d7138deSEric Dumazet 	psl = rcu_dereference(pmc->sflist);
2342c85bb41eSFlavio Leitner 	ret = (pmc->sfmode == MCAST_EXCLUDE);
23431da177e4SLinus Torvalds 	if (!psl)
2344c85bb41eSFlavio Leitner 		goto unlock;
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	for (i = 0; i < psl->sl_count; i++) {
23471da177e4SLinus Torvalds 		if (psl->sl_addr[i] == rmt_addr)
23481da177e4SLinus Torvalds 			break;
23491da177e4SLinus Torvalds 	}
2350c85bb41eSFlavio Leitner 	ret = 0;
23511da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2352c85bb41eSFlavio Leitner 		goto unlock;
23531da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2354c85bb41eSFlavio Leitner 		goto unlock;
2355c85bb41eSFlavio Leitner 	ret = 1;
2356c85bb41eSFlavio Leitner unlock:
2357c85bb41eSFlavio Leitner 	rcu_read_unlock();
2358c85bb41eSFlavio Leitner out:
2359c85bb41eSFlavio Leitner 	return ret;
23601da177e4SLinus Torvalds }
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds /*
23631da177e4SLinus Torvalds  *	A socket is closing.
23641da177e4SLinus Torvalds  */
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk)
23671da177e4SLinus Torvalds {
23681da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
23691da177e4SLinus Torvalds 	struct ip_mc_socklist *iml;
2370877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	if (inet->mc_list == NULL)
23731da177e4SLinus Torvalds 		return;
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 	rtnl_lock();
23761d7138deSEric Dumazet 	while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
23771da177e4SLinus Torvalds 		struct in_device *in_dev;
23781da177e4SLinus Torvalds 
23791d7138deSEric Dumazet 		inet->mc_list = iml->next_rcu;
2380877acedcSDaniel Lezcano 		in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
23811da177e4SLinus Torvalds 		(void) ip_mc_leave_src(sk, iml, in_dev);
238218943d29SEric Dumazet 		if (in_dev != NULL)
23831da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2384c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
2385c85bb41eSFlavio Leitner 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
238610d50e74SLai Jiangshan 		kfree_rcu(iml, rcu);
23871da177e4SLinus Torvalds 	}
23881da177e4SLinus Torvalds 	rtnl_unlock();
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds 
2391dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */
2392dbdd9a52SDavid S. Miller int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
23931da177e4SLinus Torvalds {
23941da177e4SLinus Torvalds 	struct ip_mc_list *im;
2395e9897071SEric Dumazet 	struct ip_mc_list __rcu **mc_hash;
23961da177e4SLinus Torvalds 	struct ip_sf_list *psf;
23971da177e4SLinus Torvalds 	int rv = 0;
23981da177e4SLinus Torvalds 
2399e9897071SEric Dumazet 	mc_hash = rcu_dereference(in_dev->mc_hash);
2400e9897071SEric Dumazet 	if (mc_hash) {
2401c70eba74SEric Dumazet 		u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2402e9897071SEric Dumazet 
2403e9897071SEric Dumazet 		for (im = rcu_dereference(mc_hash[hash]);
2404e9897071SEric Dumazet 		     im != NULL;
2405e9897071SEric Dumazet 		     im = rcu_dereference(im->next_hash)) {
2406e9897071SEric Dumazet 			if (im->multiaddr == mc_addr)
2407e9897071SEric Dumazet 				break;
2408e9897071SEric Dumazet 		}
2409e9897071SEric Dumazet 	} else {
24101d7138deSEric Dumazet 		for_each_pmc_rcu(in_dev, im) {
24111da177e4SLinus Torvalds 			if (im->multiaddr == mc_addr)
24121da177e4SLinus Torvalds 				break;
24131da177e4SLinus Torvalds 		}
2414e9897071SEric Dumazet 	}
24151da177e4SLinus Torvalds 	if (im && proto == IPPROTO_IGMP) {
24161da177e4SLinus Torvalds 		rv = 1;
24171da177e4SLinus Torvalds 	} else if (im) {
24181da177e4SLinus Torvalds 		if (src_addr) {
24191da177e4SLinus Torvalds 			for (psf = im->sources; psf; psf = psf->sf_next) {
24201da177e4SLinus Torvalds 				if (psf->sf_inaddr == src_addr)
24211da177e4SLinus Torvalds 					break;
24221da177e4SLinus Torvalds 			}
24231da177e4SLinus Torvalds 			if (psf)
24241da177e4SLinus Torvalds 				rv = psf->sf_count[MCAST_INCLUDE] ||
24251da177e4SLinus Torvalds 					psf->sf_count[MCAST_EXCLUDE] !=
24261da177e4SLinus Torvalds 					im->sfcount[MCAST_EXCLUDE];
24271da177e4SLinus Torvalds 			else
24281da177e4SLinus Torvalds 				rv = im->sfcount[MCAST_EXCLUDE] != 0;
24291da177e4SLinus Torvalds 		} else
24301da177e4SLinus Torvalds 			rv = 1; /* unspecified source; tentatively allow */
24311da177e4SLinus Torvalds 	}
24321da177e4SLinus Torvalds 	return rv;
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS)
24361da177e4SLinus Torvalds struct igmp_mc_iter_state {
24377091e728SAlexey Dobriyan 	struct seq_net_private p;
24381da177e4SLinus Torvalds 	struct net_device *dev;
24391da177e4SLinus Torvalds 	struct in_device *in_dev;
24401da177e4SLinus Torvalds };
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds #define	igmp_mc_seq_private(seq)	((struct igmp_mc_iter_state *)(seq)->private)
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
24451da177e4SLinus Torvalds {
24467091e728SAlexey Dobriyan 	struct net *net = seq_file_net(seq);
24471da177e4SLinus Torvalds 	struct ip_mc_list *im = NULL;
24481da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
24491da177e4SLinus Torvalds 
24507562f876SPavel Emelianov 	state->in_dev = NULL;
245161fbab77Sstephen hemminger 	for_each_netdev_rcu(net, state->dev) {
24521da177e4SLinus Torvalds 		struct in_device *in_dev;
24536baff150SEric Dumazet 
24546baff150SEric Dumazet 		in_dev = __in_dev_get_rcu(state->dev);
24551da177e4SLinus Torvalds 		if (!in_dev)
24561da177e4SLinus Torvalds 			continue;
24571d7138deSEric Dumazet 		im = rcu_dereference(in_dev->mc_list);
24581da177e4SLinus Torvalds 		if (im) {
24591da177e4SLinus Torvalds 			state->in_dev = in_dev;
24601da177e4SLinus Torvalds 			break;
24611da177e4SLinus Torvalds 		}
24621da177e4SLinus Torvalds 	}
24631da177e4SLinus Torvalds 	return im;
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
24671da177e4SLinus Torvalds {
24681da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
24696baff150SEric Dumazet 
24701d7138deSEric Dumazet 	im = rcu_dereference(im->next_rcu);
24711d7138deSEric Dumazet 	while (!im) {
24726baff150SEric Dumazet 		state->dev = next_net_device_rcu(state->dev);
24731da177e4SLinus Torvalds 		if (!state->dev) {
24741da177e4SLinus Torvalds 			state->in_dev = NULL;
24751da177e4SLinus Torvalds 			break;
24761da177e4SLinus Torvalds 		}
24776baff150SEric Dumazet 		state->in_dev = __in_dev_get_rcu(state->dev);
24781da177e4SLinus Torvalds 		if (!state->in_dev)
24791da177e4SLinus Torvalds 			continue;
24801d7138deSEric Dumazet 		im = rcu_dereference(state->in_dev->mc_list);
24811da177e4SLinus Torvalds 	}
24821da177e4SLinus Torvalds 	return im;
24831da177e4SLinus Torvalds }
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
24861da177e4SLinus Torvalds {
24871da177e4SLinus Torvalds 	struct ip_mc_list *im = igmp_mc_get_first(seq);
24881da177e4SLinus Torvalds 	if (im)
24891da177e4SLinus Torvalds 		while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
24901da177e4SLinus Torvalds 			--pos;
24911da177e4SLinus Torvalds 	return pos ? NULL : im;
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
249561fbab77Sstephen hemminger 	__acquires(rcu)
24961da177e4SLinus Torvalds {
249761fbab77Sstephen hemminger 	rcu_read_lock();
24981da177e4SLinus Torvalds 	return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
24991da177e4SLinus Torvalds }
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
25021da177e4SLinus Torvalds {
25031da177e4SLinus Torvalds 	struct ip_mc_list *im;
25041da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
25051da177e4SLinus Torvalds 		im = igmp_mc_get_first(seq);
25061da177e4SLinus Torvalds 	else
25071da177e4SLinus Torvalds 		im = igmp_mc_get_next(seq, v);
25081da177e4SLinus Torvalds 	++*pos;
25091da177e4SLinus Torvalds 	return im;
25101da177e4SLinus Torvalds }
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
251361fbab77Sstephen hemminger 	__releases(rcu)
25141da177e4SLinus Torvalds {
25151da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
25161d7138deSEric Dumazet 
25171da177e4SLinus Torvalds 	state->in_dev = NULL;
25181da177e4SLinus Torvalds 	state->dev = NULL;
251961fbab77Sstephen hemminger 	rcu_read_unlock();
25201da177e4SLinus Torvalds }
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v)
25231da177e4SLinus Torvalds {
25241da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
25251da177e4SLinus Torvalds 		seq_puts(seq,
25261da177e4SLinus Torvalds 			 "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
25271da177e4SLinus Torvalds 	else {
25281da177e4SLinus Torvalds 		struct ip_mc_list *im = (struct ip_mc_list *)v;
25291da177e4SLinus Torvalds 		struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
25301da177e4SLinus Torvalds 		char   *querier;
2531a399a805SEric Dumazet 		long delta;
2532a399a805SEric Dumazet 
25331da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
25341da177e4SLinus Torvalds 		querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
25351da177e4SLinus Torvalds 			  IGMP_V2_SEEN(state->in_dev) ? "V2" :
25361da177e4SLinus Torvalds 			  "V3";
25371da177e4SLinus Torvalds #else
25381da177e4SLinus Torvalds 		querier = "NONE";
25391da177e4SLinus Torvalds #endif
25401da177e4SLinus Torvalds 
2541e6b68883SAndreea-Cristina Bernat 		if (rcu_access_pointer(state->in_dev->mc_list) == im) {
25421da177e4SLinus Torvalds 			seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2543b8bae41eSRami Rosen 				   state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
25441da177e4SLinus Torvalds 		}
25451da177e4SLinus Torvalds 
2546a399a805SEric Dumazet 		delta = im->timer.expires - jiffies;
25471da177e4SLinus Torvalds 		seq_printf(seq,
2548338fcf98SAlexey Dobriyan 			   "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
25491da177e4SLinus Torvalds 			   im->multiaddr, im->users,
2550a399a805SEric Dumazet 			   im->tm_running,
2551a399a805SEric Dumazet 			   im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
25521da177e4SLinus Torvalds 			   im->reporter);
25531da177e4SLinus Torvalds 	}
25541da177e4SLinus Torvalds 	return 0;
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
2557f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = {
25581da177e4SLinus Torvalds 	.start	=	igmp_mc_seq_start,
25591da177e4SLinus Torvalds 	.next	=	igmp_mc_seq_next,
25601da177e4SLinus Torvalds 	.stop	=	igmp_mc_seq_stop,
25611da177e4SLinus Torvalds 	.show	=	igmp_mc_seq_show,
25621da177e4SLinus Torvalds };
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file)
25651da177e4SLinus Torvalds {
25667091e728SAlexey Dobriyan 	return seq_open_net(inode, file, &igmp_mc_seq_ops,
2567cf7732e4SPavel Emelyanov 			sizeof(struct igmp_mc_iter_state));
25681da177e4SLinus Torvalds }
25691da177e4SLinus Torvalds 
25709a32144eSArjan van de Ven static const struct file_operations igmp_mc_seq_fops = {
25711da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
25721da177e4SLinus Torvalds 	.open		=	igmp_mc_seq_open,
25731da177e4SLinus Torvalds 	.read		=	seq_read,
25741da177e4SLinus Torvalds 	.llseek		=	seq_lseek,
25757091e728SAlexey Dobriyan 	.release	=	seq_release_net,
25761da177e4SLinus Torvalds };
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds struct igmp_mcf_iter_state {
25797091e728SAlexey Dobriyan 	struct seq_net_private p;
25801da177e4SLinus Torvalds 	struct net_device *dev;
25811da177e4SLinus Torvalds 	struct in_device *idev;
25821da177e4SLinus Torvalds 	struct ip_mc_list *im;
25831da177e4SLinus Torvalds };
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq)	((struct igmp_mcf_iter_state *)(seq)->private)
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
25881da177e4SLinus Torvalds {
25897091e728SAlexey Dobriyan 	struct net *net = seq_file_net(seq);
25901da177e4SLinus Torvalds 	struct ip_sf_list *psf = NULL;
25911da177e4SLinus Torvalds 	struct ip_mc_list *im = NULL;
25921da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
25931da177e4SLinus Torvalds 
25947562f876SPavel Emelianov 	state->idev = NULL;
25957562f876SPavel Emelianov 	state->im = NULL;
259661fbab77Sstephen hemminger 	for_each_netdev_rcu(net, state->dev) {
25971da177e4SLinus Torvalds 		struct in_device *idev;
25986baff150SEric Dumazet 		idev = __in_dev_get_rcu(state->dev);
25991da177e4SLinus Torvalds 		if (unlikely(idev == NULL))
26001da177e4SLinus Torvalds 			continue;
26011d7138deSEric Dumazet 		im = rcu_dereference(idev->mc_list);
26021da177e4SLinus Torvalds 		if (likely(im != NULL)) {
26031da177e4SLinus Torvalds 			spin_lock_bh(&im->lock);
26041da177e4SLinus Torvalds 			psf = im->sources;
26051da177e4SLinus Torvalds 			if (likely(psf != NULL)) {
26061da177e4SLinus Torvalds 				state->im = im;
26071da177e4SLinus Torvalds 				state->idev = idev;
26081da177e4SLinus Torvalds 				break;
26091da177e4SLinus Torvalds 			}
26101da177e4SLinus Torvalds 			spin_unlock_bh(&im->lock);
26111da177e4SLinus Torvalds 		}
26121da177e4SLinus Torvalds 	}
26131da177e4SLinus Torvalds 	return psf;
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
26171da177e4SLinus Torvalds {
26181da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 	psf = psf->sf_next;
26211da177e4SLinus Torvalds 	while (!psf) {
26221da177e4SLinus Torvalds 		spin_unlock_bh(&state->im->lock);
26231da177e4SLinus Torvalds 		state->im = state->im->next;
26241da177e4SLinus Torvalds 		while (!state->im) {
26256baff150SEric Dumazet 			state->dev = next_net_device_rcu(state->dev);
26261da177e4SLinus Torvalds 			if (!state->dev) {
26271da177e4SLinus Torvalds 				state->idev = NULL;
26281da177e4SLinus Torvalds 				goto out;
26291da177e4SLinus Torvalds 			}
26306baff150SEric Dumazet 			state->idev = __in_dev_get_rcu(state->dev);
26311da177e4SLinus Torvalds 			if (!state->idev)
26321da177e4SLinus Torvalds 				continue;
26331d7138deSEric Dumazet 			state->im = rcu_dereference(state->idev->mc_list);
26341da177e4SLinus Torvalds 		}
26351da177e4SLinus Torvalds 		if (!state->im)
26361da177e4SLinus Torvalds 			break;
26371da177e4SLinus Torvalds 		spin_lock_bh(&state->im->lock);
26381da177e4SLinus Torvalds 		psf = state->im->sources;
26391da177e4SLinus Torvalds 	}
26401da177e4SLinus Torvalds out:
26411da177e4SLinus Torvalds 	return psf;
26421da177e4SLinus Torvalds }
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
26451da177e4SLinus Torvalds {
26461da177e4SLinus Torvalds 	struct ip_sf_list *psf = igmp_mcf_get_first(seq);
26471da177e4SLinus Torvalds 	if (psf)
26481da177e4SLinus Torvalds 		while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
26491da177e4SLinus Torvalds 			--pos;
26501da177e4SLinus Torvalds 	return pos ? NULL : psf;
26511da177e4SLinus Torvalds }
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
265461fbab77Sstephen hemminger 	__acquires(rcu)
26551da177e4SLinus Torvalds {
265661fbab77Sstephen hemminger 	rcu_read_lock();
26571da177e4SLinus Torvalds 	return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
26581da177e4SLinus Torvalds }
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
26611da177e4SLinus Torvalds {
26621da177e4SLinus Torvalds 	struct ip_sf_list *psf;
26631da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
26641da177e4SLinus Torvalds 		psf = igmp_mcf_get_first(seq);
26651da177e4SLinus Torvalds 	else
26661da177e4SLinus Torvalds 		psf = igmp_mcf_get_next(seq, v);
26671da177e4SLinus Torvalds 	++*pos;
26681da177e4SLinus Torvalds 	return psf;
26691da177e4SLinus Torvalds }
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
267261fbab77Sstephen hemminger 	__releases(rcu)
26731da177e4SLinus Torvalds {
26741da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
26751da177e4SLinus Torvalds 	if (likely(state->im != NULL)) {
26761da177e4SLinus Torvalds 		spin_unlock_bh(&state->im->lock);
26771da177e4SLinus Torvalds 		state->im = NULL;
26781da177e4SLinus Torvalds 	}
26791da177e4SLinus Torvalds 	state->idev = NULL;
26801da177e4SLinus Torvalds 	state->dev = NULL;
268161fbab77Sstephen hemminger 	rcu_read_unlock();
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
26851da177e4SLinus Torvalds {
26861da177e4SLinus Torvalds 	struct ip_sf_list *psf = (struct ip_sf_list *)v;
26871da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2690*1744bea1SJoe Perches 		seq_puts(seq, "Idx Device        MCA        SRC    INC    EXC\n");
26911da177e4SLinus Torvalds 	} else {
26921da177e4SLinus Torvalds 		seq_printf(seq,
26931da177e4SLinus Torvalds 			   "%3d %6.6s 0x%08x "
26941da177e4SLinus Torvalds 			   "0x%08x %6lu %6lu\n",
26951da177e4SLinus Torvalds 			   state->dev->ifindex, state->dev->name,
26961da177e4SLinus Torvalds 			   ntohl(state->im->multiaddr),
26971da177e4SLinus Torvalds 			   ntohl(psf->sf_inaddr),
26981da177e4SLinus Torvalds 			   psf->sf_count[MCAST_INCLUDE],
26991da177e4SLinus Torvalds 			   psf->sf_count[MCAST_EXCLUDE]);
27001da177e4SLinus Torvalds 	}
27011da177e4SLinus Torvalds 	return 0;
27021da177e4SLinus Torvalds }
27031da177e4SLinus Torvalds 
2704f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = {
27051da177e4SLinus Torvalds 	.start	=	igmp_mcf_seq_start,
27061da177e4SLinus Torvalds 	.next	=	igmp_mcf_seq_next,
27071da177e4SLinus Torvalds 	.stop	=	igmp_mcf_seq_stop,
27081da177e4SLinus Torvalds 	.show	=	igmp_mcf_seq_show,
27091da177e4SLinus Torvalds };
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
27121da177e4SLinus Torvalds {
27137091e728SAlexey Dobriyan 	return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2714cf7732e4SPavel Emelyanov 			sizeof(struct igmp_mcf_iter_state));
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds 
27179a32144eSArjan van de Ven static const struct file_operations igmp_mcf_seq_fops = {
27181da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
27191da177e4SLinus Torvalds 	.open		=	igmp_mcf_seq_open,
27201da177e4SLinus Torvalds 	.read		=	seq_read,
27211da177e4SLinus Torvalds 	.llseek		=	seq_lseek,
27227091e728SAlexey Dobriyan 	.release	=	seq_release_net,
27237091e728SAlexey Dobriyan };
27247091e728SAlexey Dobriyan 
27252c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net)
27267091e728SAlexey Dobriyan {
27277091e728SAlexey Dobriyan 	struct proc_dir_entry *pde;
27287091e728SAlexey Dobriyan 
2729d4beaa66SGao feng 	pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
27307091e728SAlexey Dobriyan 	if (!pde)
27317091e728SAlexey Dobriyan 		goto out_igmp;
2732d4beaa66SGao feng 	pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2733d4beaa66SGao feng 			  &igmp_mcf_seq_fops);
27347091e728SAlexey Dobriyan 	if (!pde)
27357091e728SAlexey Dobriyan 		goto out_mcfilter;
27367091e728SAlexey Dobriyan 	return 0;
27377091e728SAlexey Dobriyan 
27387091e728SAlexey Dobriyan out_mcfilter:
2739ece31ffdSGao feng 	remove_proc_entry("igmp", net->proc_net);
27407091e728SAlexey Dobriyan out_igmp:
27417091e728SAlexey Dobriyan 	return -ENOMEM;
27427091e728SAlexey Dobriyan }
27437091e728SAlexey Dobriyan 
27442c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net)
27457091e728SAlexey Dobriyan {
2746ece31ffdSGao feng 	remove_proc_entry("mcfilter", net->proc_net);
2747ece31ffdSGao feng 	remove_proc_entry("igmp", net->proc_net);
27487091e728SAlexey Dobriyan }
27497091e728SAlexey Dobriyan 
27507091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = {
27517091e728SAlexey Dobriyan 	.init = igmp_net_init,
27527091e728SAlexey Dobriyan 	.exit = igmp_net_exit,
27531da177e4SLinus Torvalds };
275472c1d3bdSWANG Cong #endif
27551da177e4SLinus Torvalds 
27564aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this,
27574aa5dee4SJiri Pirko 			     unsigned long event, void *ptr)
27584aa5dee4SJiri Pirko {
27594aa5dee4SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
27604aa5dee4SJiri Pirko 	struct in_device *in_dev;
27614aa5dee4SJiri Pirko 
27624aa5dee4SJiri Pirko 	switch (event) {
27634aa5dee4SJiri Pirko 	case NETDEV_RESEND_IGMP:
27644aa5dee4SJiri Pirko 		in_dev = __in_dev_get_rtnl(dev);
27654aa5dee4SJiri Pirko 		if (in_dev)
27664aa5dee4SJiri Pirko 			ip_mc_rejoin_groups(in_dev);
27674aa5dee4SJiri Pirko 		break;
27684aa5dee4SJiri Pirko 	default:
27694aa5dee4SJiri Pirko 		break;
27704aa5dee4SJiri Pirko 	}
27714aa5dee4SJiri Pirko 	return NOTIFY_DONE;
27724aa5dee4SJiri Pirko }
27734aa5dee4SJiri Pirko 
27744aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = {
27754aa5dee4SJiri Pirko 	.notifier_call = igmp_netdev_event,
27764aa5dee4SJiri Pirko };
27774aa5dee4SJiri Pirko 
277872c1d3bdSWANG Cong int __init igmp_mc_init(void)
27791da177e4SLinus Torvalds {
278072c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS)
27814aa5dee4SJiri Pirko 	int err;
27824aa5dee4SJiri Pirko 
27834aa5dee4SJiri Pirko 	err = register_pernet_subsys(&igmp_net_ops);
27844aa5dee4SJiri Pirko 	if (err)
27854aa5dee4SJiri Pirko 		return err;
27864aa5dee4SJiri Pirko 	err = register_netdevice_notifier(&igmp_notifier);
27874aa5dee4SJiri Pirko 	if (err)
27884aa5dee4SJiri Pirko 		goto reg_notif_fail;
27894aa5dee4SJiri Pirko 	return 0;
27904aa5dee4SJiri Pirko 
27914aa5dee4SJiri Pirko reg_notif_fail:
27924aa5dee4SJiri Pirko 	unregister_pernet_subsys(&igmp_net_ops);
27934aa5dee4SJiri Pirko 	return err;
279472c1d3bdSWANG Cong #else
279572c1d3bdSWANG Cong 	return register_netdevice_notifier(&igmp_notifier);
27961da177e4SLinus Torvalds #endif
279772c1d3bdSWANG Cong }
2798