xref: /linux/net/ipv4/igmp.c (revision a1889c0d2039a53ae04abb9f20c62500bd312bf3)
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 <asm/system.h>
771da177e4SLinus Torvalds #include <linux/types.h>
781da177e4SLinus Torvalds #include <linux/kernel.h>
791da177e4SLinus Torvalds #include <linux/jiffies.h>
801da177e4SLinus Torvalds #include <linux/string.h>
811da177e4SLinus Torvalds #include <linux/socket.h>
821da177e4SLinus Torvalds #include <linux/sockios.h>
831da177e4SLinus Torvalds #include <linux/in.h>
841da177e4SLinus Torvalds #include <linux/inet.h>
851da177e4SLinus Torvalds #include <linux/netdevice.h>
861da177e4SLinus Torvalds #include <linux/skbuff.h>
871da177e4SLinus Torvalds #include <linux/inetdevice.h>
881da177e4SLinus Torvalds #include <linux/igmp.h>
891da177e4SLinus Torvalds #include <linux/if_arp.h>
901da177e4SLinus Torvalds #include <linux/rtnetlink.h>
911da177e4SLinus Torvalds #include <linux/times.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 
1151da177e4SLinus Torvalds #define IGMP_V1_Router_Present_Timeout		(400*HZ)
1161da177e4SLinus Torvalds #define IGMP_V2_Router_Present_Timeout		(400*HZ)
1171da177e4SLinus Torvalds #define IGMP_Unsolicited_Report_Interval	(10*HZ)
1181da177e4SLinus Torvalds #define IGMP_Query_Response_Interval		(10*HZ)
1191da177e4SLinus Torvalds #define IGMP_Unsolicited_Report_Count		2
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds #define IGMP_Initial_Report_Delay		(1)
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /* IGMP_Initial_Report_Delay is not from IGMP specs!
1251da177e4SLinus Torvalds  * IGMP specs require to report membership immediately after
1261da177e4SLinus Torvalds  * joining a group, but we delay the first report by a
1271da177e4SLinus Torvalds  * small interval. It seems more natural and still does not
1281da177e4SLinus Torvalds  * contradict to specs provided this delay is small enough.
1291da177e4SLinus Torvalds  */
1301da177e4SLinus Torvalds 
13142f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \
132c346dca1SYOSHIFUJI Hideaki 	(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
13342f811b8SHerbert Xu 	 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
1341da177e4SLinus Torvalds 	 ((in_dev)->mr_v1_seen && \
1351da177e4SLinus Torvalds 	  time_before(jiffies, (in_dev)->mr_v1_seen)))
13642f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \
137c346dca1SYOSHIFUJI Hideaki 	(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
13842f811b8SHerbert Xu 	 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
1391da177e4SLinus Torvalds 	 ((in_dev)->mr_v2_seen && \
1401da177e4SLinus Torvalds 	  time_before(jiffies, (in_dev)->mr_v2_seen)))
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
14363007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
1441da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev);
1451da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc);
1461da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc);
1471da177e4SLinus Torvalds #endif
1481da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc);
1498f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1508f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	if (atomic_dec_and_test(&im->refcnt)) {
1551da177e4SLinus Torvalds 		in_dev_put(im->interface);
15642ea299dSLai Jiangshan 		kfree_rcu(im, rcu);
1571da177e4SLinus Torvalds 	}
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc)				\
161d9aa9380SDavid S. Miller 	for (pmc = rcu_dereference(in_dev->mc_list);		\
162d9aa9380SDavid S. Miller 	     pmc != NULL;					\
163d9aa9380SDavid S. Miller 	     pmc = rcu_dereference(pmc->next_rcu))
164d9aa9380SDavid S. Miller 
165d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc)				\
166d9aa9380SDavid S. Miller 	for (pmc = rtnl_dereference(in_dev->mc_list);		\
167d9aa9380SDavid S. Miller 	     pmc != NULL;					\
168d9aa9380SDavid S. Miller 	     pmc = rtnl_dereference(pmc->next_rcu))
169d9aa9380SDavid S. Miller 
1701da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds  *	Timer management
1741da177e4SLinus Torvalds  */
1751da177e4SLinus Torvalds 
1761d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
1791da177e4SLinus Torvalds 	if (del_timer(&im->timer))
1801da177e4SLinus Torvalds 		atomic_dec(&im->refcnt);
1811da177e4SLinus Torvalds 	im->tm_running = 0;
1821da177e4SLinus Torvalds 	im->reporter = 0;
1831da177e4SLinus Torvalds 	im->unsolicit_count = 0;
1841da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /* It must be called with locked im->lock */
1881da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	int tv = net_random() % max_delay;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	im->tm_running = 1;
1931da177e4SLinus Torvalds 	if (!mod_timer(&im->timer, jiffies+tv+2))
1941da177e4SLinus Torvalds 		atomic_inc(&im->refcnt);
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	int tv = net_random() % in_dev->mr_maxdelay;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	in_dev->mr_gq_running = 1;
2021da177e4SLinus Torvalds 	if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
2031da177e4SLinus Torvalds 		in_dev_hold(in_dev);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	int tv = net_random() % delay;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
2111da177e4SLinus Torvalds 		in_dev_hold(in_dev);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
2171da177e4SLinus Torvalds 	im->unsolicit_count = 0;
2181da177e4SLinus Torvalds 	if (del_timer(&im->timer)) {
2191da177e4SLinus Torvalds 		if ((long)(im->timer.expires-jiffies) < max_delay) {
2201da177e4SLinus Torvalds 			add_timer(&im->timer);
2211da177e4SLinus Torvalds 			im->tm_running = 1;
2221da177e4SLinus Torvalds 			spin_unlock_bh(&im->lock);
2231da177e4SLinus Torvalds 			return;
2241da177e4SLinus Torvalds 		}
2251da177e4SLinus Torvalds 		atomic_dec(&im->refcnt);
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 	igmp_start_timer(im, max_delay);
2281da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds /*
2331da177e4SLinus Torvalds  *	Send an IGMP report.
2341da177e4SLinus Torvalds  */
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
2401da177e4SLinus Torvalds 	int gdeleted, int sdeleted)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	switch (type) {
2431da177e4SLinus Torvalds 	case IGMPV3_MODE_IS_INCLUDE:
2441da177e4SLinus Torvalds 	case IGMPV3_MODE_IS_EXCLUDE:
2451da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2461da177e4SLinus Torvalds 			return 0;
247ad12583fSDavid L Stevens 		if (!(pmc->gsquery && !psf->sf_gsresp)) {
248ad12583fSDavid L Stevens 			if (pmc->sfmode == MCAST_INCLUDE)
249ad12583fSDavid L Stevens 				return 1;
250ad12583fSDavid L Stevens 			/* don't include if this source is excluded
251ad12583fSDavid L Stevens 			 * in all filters
252ad12583fSDavid L Stevens 			 */
253ad12583fSDavid L Stevens 			if (psf->sf_count[MCAST_INCLUDE])
254ad12583fSDavid L Stevens 				return type == IGMPV3_MODE_IS_INCLUDE;
255ad12583fSDavid L Stevens 			return pmc->sfcount[MCAST_EXCLUDE] ==
256ad12583fSDavid L Stevens 				psf->sf_count[MCAST_EXCLUDE];
257ad12583fSDavid L Stevens 		}
258ad12583fSDavid L Stevens 		return 0;
2591da177e4SLinus Torvalds 	case IGMPV3_CHANGE_TO_INCLUDE:
2601da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2611da177e4SLinus Torvalds 			return 0;
2621da177e4SLinus Torvalds 		return psf->sf_count[MCAST_INCLUDE] != 0;
2631da177e4SLinus Torvalds 	case IGMPV3_CHANGE_TO_EXCLUDE:
2641da177e4SLinus Torvalds 		if (gdeleted || sdeleted)
2651da177e4SLinus Torvalds 			return 0;
2661da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
2671da177e4SLinus Torvalds 		    psf->sf_count[MCAST_INCLUDE])
2681da177e4SLinus Torvalds 			return 0;
2691da177e4SLinus Torvalds 		return pmc->sfcount[MCAST_EXCLUDE] ==
2701da177e4SLinus Torvalds 			psf->sf_count[MCAST_EXCLUDE];
2711da177e4SLinus Torvalds 	case IGMPV3_ALLOW_NEW_SOURCES:
2721da177e4SLinus Torvalds 		if (gdeleted || !psf->sf_crcount)
2731da177e4SLinus Torvalds 			return 0;
2741da177e4SLinus Torvalds 		return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
2751da177e4SLinus Torvalds 	case IGMPV3_BLOCK_OLD_SOURCES:
2761da177e4SLinus Torvalds 		if (pmc->sfmode == MCAST_INCLUDE)
2771da177e4SLinus Torvalds 			return gdeleted || (psf->sf_crcount && sdeleted);
2781da177e4SLinus Torvalds 		return psf->sf_crcount && !gdeleted && !sdeleted;
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 	return 0;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static int
2841da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct ip_sf_list *psf;
2871da177e4SLinus Torvalds 	int scount = 0;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
2901da177e4SLinus Torvalds 		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
2911da177e4SLinus Torvalds 			continue;
2921da177e4SLinus Torvalds 		scount++;
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 	return scount;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
29757e1ab6eSEric Dumazet #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
29857e1ab6eSEric Dumazet 
2991da177e4SLinus Torvalds static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct sk_buff *skb;
3021da177e4SLinus Torvalds 	struct rtable *rt;
3031da177e4SLinus Torvalds 	struct iphdr *pip;
3041da177e4SLinus Torvalds 	struct igmpv3_report *pig;
305877acedcSDaniel Lezcano 	struct net *net = dev_net(dev);
30631e4543dSDavid S. Miller 	struct flowi4 fl4;
3071da177e4SLinus Torvalds 
30857e1ab6eSEric Dumazet 	while (1) {
30957e1ab6eSEric Dumazet 		skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
31057e1ab6eSEric Dumazet 				GFP_ATOMIC | __GFP_NOWARN);
31157e1ab6eSEric Dumazet 		if (skb)
31257e1ab6eSEric Dumazet 			break;
31357e1ab6eSEric Dumazet 		size >>= 1;
31457e1ab6eSEric Dumazet 		if (size < 256)
3151da177e4SLinus Torvalds 			return NULL;
31657e1ab6eSEric Dumazet 	}
31757e1ab6eSEric Dumazet 	igmp_skb_size(skb) = size;
3181da177e4SLinus Torvalds 
31931e4543dSDavid S. Miller 	rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
32078fbfd8aSDavid S. Miller 				   0, 0,
32178fbfd8aSDavid S. Miller 				   IPPROTO_IGMP, 0, dev->ifindex);
322b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
3231da177e4SLinus Torvalds 		kfree_skb(skb);
3241da177e4SLinus Torvalds 		return NULL;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
327d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
3281da177e4SLinus Torvalds 	skb->dev = dev;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	skb_reserve(skb, LL_RESERVED_SPACE(dev));
3311da177e4SLinus Torvalds 
3327e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
333eddc9ec5SArnaldo Carvalho de Melo 	pip = ip_hdr(skb);
3347e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + 4);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	pip->version  = 4;
3371da177e4SLinus Torvalds 	pip->ihl      = (sizeof(struct iphdr)+4)>>2;
3381da177e4SLinus Torvalds 	pip->tos      = 0xc0;
3391da177e4SLinus Torvalds 	pip->frag_off = htons(IP_DF);
3401da177e4SLinus Torvalds 	pip->ttl      = 1;
341492f64ceSDavid S. Miller 	pip->daddr    = fl4.daddr;
342492f64ceSDavid S. Miller 	pip->saddr    = fl4.saddr;
3431da177e4SLinus Torvalds 	pip->protocol = IPPROTO_IGMP;
3441da177e4SLinus Torvalds 	pip->tot_len  = 0;	/* filled in later */
345d8d1f30bSChangli Gao 	ip_select_ident(pip, &rt->dst, NULL);
3461da177e4SLinus Torvalds 	((u8*)&pip[1])[0] = IPOPT_RA;
3471da177e4SLinus Torvalds 	((u8*)&pip[1])[1] = 4;
3481da177e4SLinus Torvalds 	((u8*)&pip[1])[2] = 0;
3491da177e4SLinus Torvalds 	((u8*)&pip[1])[3] = 0;
3501da177e4SLinus Torvalds 
351b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
352d10ba34bSArnaldo Carvalho de Melo 	skb_put(skb, sizeof(*pig));
353d9edf9e2SArnaldo Carvalho de Melo 	pig = igmpv3_report_hdr(skb);
3541da177e4SLinus Torvalds 	pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
3551da177e4SLinus Torvalds 	pig->resv1 = 0;
3561da177e4SLinus Torvalds 	pig->csum = 0;
3571da177e4SLinus Torvalds 	pig->resv2 = 0;
3581da177e4SLinus Torvalds 	pig->ngrec = 0;
3591da177e4SLinus Torvalds 	return skb;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb)
3631da177e4SLinus Torvalds {
364d9edf9e2SArnaldo Carvalho de Melo 	struct igmphdr *pig = igmp_hdr(skb);
36527a884dcSArnaldo Carvalho de Melo 	const int igmplen = skb->tail - skb->transport_header;
3661da177e4SLinus Torvalds 
367d9edf9e2SArnaldo Carvalho de Melo 	pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
3681da177e4SLinus Torvalds 
369c439cb2eSHerbert Xu 	return ip_local_out(skb);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds 	return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
3781da177e4SLinus Torvalds 	int type, struct igmpv3_grec **ppgr)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	struct net_device *dev = pmc->interface->dev;
3811da177e4SLinus Torvalds 	struct igmpv3_report *pih;
3821da177e4SLinus Torvalds 	struct igmpv3_grec *pgr;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	if (!skb)
3851da177e4SLinus Torvalds 		skb = igmpv3_newpack(dev, dev->mtu);
3861da177e4SLinus Torvalds 	if (!skb)
3871da177e4SLinus Torvalds 		return NULL;
3881da177e4SLinus Torvalds 	pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
3891da177e4SLinus Torvalds 	pgr->grec_type = type;
3901da177e4SLinus Torvalds 	pgr->grec_auxwords = 0;
3911da177e4SLinus Torvalds 	pgr->grec_nsrcs = 0;
3921da177e4SLinus Torvalds 	pgr->grec_mca = pmc->multiaddr;
393d9edf9e2SArnaldo Carvalho de Melo 	pih = igmpv3_report_hdr(skb);
3941da177e4SLinus Torvalds 	pih->ngrec = htons(ntohs(pih->ngrec)+1);
3951da177e4SLinus Torvalds 	*ppgr = pgr;
3961da177e4SLinus Torvalds 	return skb;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
39957e1ab6eSEric Dumazet #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
4001da177e4SLinus Torvalds 	skb_tailroom(skb)) : 0)
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
4031da177e4SLinus Torvalds 	int type, int gdeleted, int sdeleted)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct net_device *dev = pmc->interface->dev;
4061da177e4SLinus Torvalds 	struct igmpv3_report *pih;
4071da177e4SLinus Torvalds 	struct igmpv3_grec *pgr = NULL;
4081da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
409ad12583fSDavid L Stevens 	int scount, stotal, first, isquery, truncate;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	if (pmc->multiaddr == IGMP_ALL_HOSTS)
4121da177e4SLinus Torvalds 		return skb;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	isquery = type == IGMPV3_MODE_IS_INCLUDE ||
4151da177e4SLinus Torvalds 		  type == IGMPV3_MODE_IS_EXCLUDE;
4161da177e4SLinus Torvalds 	truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
4171da177e4SLinus Torvalds 		    type == IGMPV3_CHANGE_TO_EXCLUDE;
4181da177e4SLinus Torvalds 
419ad12583fSDavid L Stevens 	stotal = scount = 0;
420ad12583fSDavid L Stevens 
4211da177e4SLinus Torvalds 	psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
4221da177e4SLinus Torvalds 
423ad12583fSDavid L Stevens 	if (!*psf_list)
424ad12583fSDavid L Stevens 		goto empty_source;
425ad12583fSDavid L Stevens 
426d9edf9e2SArnaldo Carvalho de Melo 	pih = skb ? igmpv3_report_hdr(skb) : NULL;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/* EX and TO_EX get a fresh packet, if needed */
4291da177e4SLinus Torvalds 	if (truncate) {
4301da177e4SLinus Torvalds 		if (pih && pih->ngrec &&
4311da177e4SLinus Torvalds 		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
4321da177e4SLinus Torvalds 			if (skb)
4331da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
4341da177e4SLinus Torvalds 			skb = igmpv3_newpack(dev, dev->mtu);
4351da177e4SLinus Torvalds 		}
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 	first = 1;
4381da177e4SLinus Torvalds 	psf_prev = NULL;
4391da177e4SLinus Torvalds 	for (psf=*psf_list; psf; psf=psf_next) {
440ea4d9e72SAl Viro 		__be32 *psrc;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 		psf_next = psf->sf_next;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
4451da177e4SLinus Torvalds 			psf_prev = psf;
4461da177e4SLinus Torvalds 			continue;
4471da177e4SLinus Torvalds 		}
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 		/* clear marks on query responses */
4501da177e4SLinus Torvalds 		if (isquery)
4511da177e4SLinus Torvalds 			psf->sf_gsresp = 0;
4521da177e4SLinus Torvalds 
45363007727SAl Viro 		if (AVAILABLE(skb) < sizeof(__be32) +
4541da177e4SLinus Torvalds 		    first*sizeof(struct igmpv3_grec)) {
4551da177e4SLinus Torvalds 			if (truncate && !first)
4561da177e4SLinus Torvalds 				break;	 /* truncate these */
4571da177e4SLinus Torvalds 			if (pgr)
4581da177e4SLinus Torvalds 				pgr->grec_nsrcs = htons(scount);
4591da177e4SLinus Torvalds 			if (skb)
4601da177e4SLinus Torvalds 				igmpv3_sendpack(skb);
4611da177e4SLinus Torvalds 			skb = igmpv3_newpack(dev, dev->mtu);
4621da177e4SLinus Torvalds 			first = 1;
4631da177e4SLinus Torvalds 			scount = 0;
4641da177e4SLinus Torvalds 		}
4651da177e4SLinus Torvalds 		if (first) {
4661da177e4SLinus Torvalds 			skb = add_grhead(skb, pmc, type, &pgr);
4671da177e4SLinus Torvalds 			first = 0;
4681da177e4SLinus Torvalds 		}
469cc63f70bSAlexey Dobriyan 		if (!skb)
470cc63f70bSAlexey Dobriyan 			return NULL;
47163007727SAl Viro 		psrc = (__be32 *)skb_put(skb, sizeof(__be32));
4721da177e4SLinus Torvalds 		*psrc = psf->sf_inaddr;
473ad12583fSDavid L Stevens 		scount++; stotal++;
4741da177e4SLinus Torvalds 		if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
4751da177e4SLinus Torvalds 		     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
4761da177e4SLinus Torvalds 			psf->sf_crcount--;
4771da177e4SLinus Torvalds 			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
4781da177e4SLinus Torvalds 				if (psf_prev)
4791da177e4SLinus Torvalds 					psf_prev->sf_next = psf->sf_next;
4801da177e4SLinus Torvalds 				else
4811da177e4SLinus Torvalds 					*psf_list = psf->sf_next;
4821da177e4SLinus Torvalds 				kfree(psf);
4831da177e4SLinus Torvalds 				continue;
4841da177e4SLinus Torvalds 			}
4851da177e4SLinus Torvalds 		}
4861da177e4SLinus Torvalds 		psf_prev = psf;
4871da177e4SLinus Torvalds 	}
488ad12583fSDavid L Stevens 
489ad12583fSDavid L Stevens empty_source:
490ad12583fSDavid L Stevens 	if (!stotal) {
491ad12583fSDavid L Stevens 		if (type == IGMPV3_ALLOW_NEW_SOURCES ||
492ad12583fSDavid L Stevens 		    type == IGMPV3_BLOCK_OLD_SOURCES)
493ad12583fSDavid L Stevens 			return skb;
494ad12583fSDavid L Stevens 		if (pmc->crcount || isquery) {
495ad12583fSDavid L Stevens 			/* make sure we have room for group header */
496ad12583fSDavid L Stevens 			if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
497ad12583fSDavid L Stevens 				igmpv3_sendpack(skb);
498ad12583fSDavid L Stevens 				skb = NULL; /* add_grhead will get a new one */
499ad12583fSDavid L Stevens 			}
500ad12583fSDavid L Stevens 			skb = add_grhead(skb, pmc, type, &pgr);
501ad12583fSDavid L Stevens 		}
502ad12583fSDavid L Stevens 	}
5031da177e4SLinus Torvalds 	if (pgr)
5041da177e4SLinus Torvalds 		pgr->grec_nsrcs = htons(scount);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (isquery)
5071da177e4SLinus Torvalds 		pmc->gsquery = 0;	/* clear query state on report */
5081da177e4SLinus Torvalds 	return skb;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
5141da177e4SLinus Torvalds 	int type;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	if (!pmc) {
5171d7138deSEric Dumazet 		rcu_read_lock();
5181d7138deSEric Dumazet 		for_each_pmc_rcu(in_dev, pmc) {
5191da177e4SLinus Torvalds 			if (pmc->multiaddr == IGMP_ALL_HOSTS)
5201da177e4SLinus Torvalds 				continue;
5211da177e4SLinus Torvalds 			spin_lock_bh(&pmc->lock);
5221da177e4SLinus Torvalds 			if (pmc->sfcount[MCAST_EXCLUDE])
5231da177e4SLinus Torvalds 				type = IGMPV3_MODE_IS_EXCLUDE;
5241da177e4SLinus Torvalds 			else
5251da177e4SLinus Torvalds 				type = IGMPV3_MODE_IS_INCLUDE;
5261da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 0, 0);
5271da177e4SLinus Torvalds 			spin_unlock_bh(&pmc->lock);
5281da177e4SLinus Torvalds 		}
5291d7138deSEric Dumazet 		rcu_read_unlock();
5301da177e4SLinus Torvalds 	} else {
5311da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
5321da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE])
5331da177e4SLinus Torvalds 			type = IGMPV3_MODE_IS_EXCLUDE;
5341da177e4SLinus Torvalds 		else
5351da177e4SLinus Torvalds 			type = IGMPV3_MODE_IS_INCLUDE;
5361da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, type, 0, 0);
5371da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 	if (!skb)
5401da177e4SLinus Torvalds 		return 0;
5411da177e4SLinus Torvalds 	return igmpv3_sendpack(skb);
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds /*
5451da177e4SLinus Torvalds  * remove zero-count source records from a source filter list
5461da177e4SLinus Torvalds  */
5471da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	struct ip_sf_list *psf_prev, *psf_next, *psf;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	psf_prev = NULL;
5521da177e4SLinus Torvalds 	for (psf=*ppsf; psf; psf = psf_next) {
5531da177e4SLinus Torvalds 		psf_next = psf->sf_next;
5541da177e4SLinus Torvalds 		if (psf->sf_crcount == 0) {
5551da177e4SLinus Torvalds 			if (psf_prev)
5561da177e4SLinus Torvalds 				psf_prev->sf_next = psf->sf_next;
5571da177e4SLinus Torvalds 			else
5581da177e4SLinus Torvalds 				*ppsf = psf->sf_next;
5591da177e4SLinus Torvalds 			kfree(psf);
5601da177e4SLinus Torvalds 		} else
5611da177e4SLinus Torvalds 			psf_prev = psf;
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev)
5661da177e4SLinus Torvalds {
5671da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
5681da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
5691da177e4SLinus Torvalds 	int type, dtype;
5701da177e4SLinus Torvalds 
5711d7138deSEric Dumazet 	rcu_read_lock();
5721da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/* deleted MCA's */
5751da177e4SLinus Torvalds 	pmc_prev = NULL;
5761da177e4SLinus Torvalds 	for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
5771da177e4SLinus Torvalds 		pmc_next = pmc->next;
5781da177e4SLinus Torvalds 		if (pmc->sfmode == MCAST_INCLUDE) {
5791da177e4SLinus Torvalds 			type = IGMPV3_BLOCK_OLD_SOURCES;
5801da177e4SLinus Torvalds 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
5811da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 1, 0);
5821da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, dtype, 1, 1);
5831da177e4SLinus Torvalds 		}
5841da177e4SLinus Torvalds 		if (pmc->crcount) {
5851da177e4SLinus Torvalds 			if (pmc->sfmode == MCAST_EXCLUDE) {
5861da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_INCLUDE;
5871da177e4SLinus Torvalds 				skb = add_grec(skb, pmc, type, 1, 0);
5881da177e4SLinus Torvalds 			}
589ad12583fSDavid L Stevens 			pmc->crcount--;
5901da177e4SLinus Torvalds 			if (pmc->crcount == 0) {
5911da177e4SLinus Torvalds 				igmpv3_clear_zeros(&pmc->tomb);
5921da177e4SLinus Torvalds 				igmpv3_clear_zeros(&pmc->sources);
5931da177e4SLinus Torvalds 			}
5941da177e4SLinus Torvalds 		}
5951da177e4SLinus Torvalds 		if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
5961da177e4SLinus Torvalds 			if (pmc_prev)
5971da177e4SLinus Torvalds 				pmc_prev->next = pmc_next;
5981da177e4SLinus Torvalds 			else
5991da177e4SLinus Torvalds 				in_dev->mc_tomb = pmc_next;
6001da177e4SLinus Torvalds 			in_dev_put(pmc->interface);
6011da177e4SLinus Torvalds 			kfree(pmc);
6021da177e4SLinus Torvalds 		} else
6031da177e4SLinus Torvalds 			pmc_prev = pmc;
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* change recs */
6081d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
6091da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
6101da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
6111da177e4SLinus Torvalds 			type = IGMPV3_BLOCK_OLD_SOURCES;
6121da177e4SLinus Torvalds 			dtype = IGMPV3_ALLOW_NEW_SOURCES;
6131da177e4SLinus Torvalds 		} else {
6141da177e4SLinus Torvalds 			type = IGMPV3_ALLOW_NEW_SOURCES;
6151da177e4SLinus Torvalds 			dtype = IGMPV3_BLOCK_OLD_SOURCES;
6161da177e4SLinus Torvalds 		}
6171da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, type, 0, 0);
6181da177e4SLinus Torvalds 		skb = add_grec(skb, pmc, dtype, 0, 1);	/* deleted sources */
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 		/* filter mode changes */
6211da177e4SLinus Torvalds 		if (pmc->crcount) {
6221da177e4SLinus Torvalds 			if (pmc->sfmode == MCAST_EXCLUDE)
6231da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_EXCLUDE;
6241da177e4SLinus Torvalds 			else
6251da177e4SLinus Torvalds 				type = IGMPV3_CHANGE_TO_INCLUDE;
6261da177e4SLinus Torvalds 			skb = add_grec(skb, pmc, type, 0, 0);
627ad12583fSDavid L Stevens 			pmc->crcount--;
6281da177e4SLinus Torvalds 		}
6291da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
6301da177e4SLinus Torvalds 	}
6311d7138deSEric Dumazet 	rcu_read_unlock();
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	if (!skb)
6341da177e4SLinus Torvalds 		return;
6351da177e4SLinus Torvalds 	(void) igmpv3_sendpack(skb);
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
6391da177e4SLinus Torvalds 	int type)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	struct sk_buff *skb;
6421da177e4SLinus Torvalds 	struct iphdr *iph;
6431da177e4SLinus Torvalds 	struct igmphdr *ih;
6441da177e4SLinus Torvalds 	struct rtable *rt;
6451da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
646877acedcSDaniel Lezcano 	struct net *net = dev_net(dev);
64763007727SAl Viro 	__be32	group = pmc ? pmc->multiaddr : 0;
64831e4543dSDavid S. Miller 	struct flowi4 fl4;
64963007727SAl Viro 	__be32	dst;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
6521da177e4SLinus Torvalds 		return igmpv3_send_report(in_dev, pmc);
6531da177e4SLinus Torvalds 	else if (type == IGMP_HOST_LEAVE_MESSAGE)
6541da177e4SLinus Torvalds 		dst = IGMP_ALL_ROUTER;
6551da177e4SLinus Torvalds 	else
6561da177e4SLinus Torvalds 		dst = group;
6571da177e4SLinus Torvalds 
65831e4543dSDavid S. Miller 	rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
65978fbfd8aSDavid S. Miller 				   0, 0,
66078fbfd8aSDavid S. Miller 				   IPPROTO_IGMP, 0, dev->ifindex);
661b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
6621da177e4SLinus Torvalds 		return -1;
66378fbfd8aSDavid S. Miller 
664f5184d26SJohannes Berg 	skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
6651da177e4SLinus Torvalds 	if (skb == NULL) {
6661da177e4SLinus Torvalds 		ip_rt_put(rt);
6671da177e4SLinus Torvalds 		return -1;
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 
670d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	skb_reserve(skb, LL_RESERVED_SPACE(dev));
6731da177e4SLinus Torvalds 
6747e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
675eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
6767e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + 4);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	iph->version  = 4;
6791da177e4SLinus Torvalds 	iph->ihl      = (sizeof(struct iphdr)+4)>>2;
6801da177e4SLinus Torvalds 	iph->tos      = 0xc0;
6811da177e4SLinus Torvalds 	iph->frag_off = htons(IP_DF);
6821da177e4SLinus Torvalds 	iph->ttl      = 1;
6831da177e4SLinus Torvalds 	iph->daddr    = dst;
684492f64ceSDavid S. Miller 	iph->saddr    = fl4.saddr;
6851da177e4SLinus Torvalds 	iph->protocol = IPPROTO_IGMP;
686d8d1f30bSChangli Gao 	ip_select_ident(iph, &rt->dst, NULL);
6871da177e4SLinus Torvalds 	((u8*)&iph[1])[0] = IPOPT_RA;
6881da177e4SLinus Torvalds 	((u8*)&iph[1])[1] = 4;
6891da177e4SLinus Torvalds 	((u8*)&iph[1])[2] = 0;
6901da177e4SLinus Torvalds 	((u8*)&iph[1])[3] = 0;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
6931da177e4SLinus Torvalds 	ih->type = type;
6941da177e4SLinus Torvalds 	ih->code = 0;
6951da177e4SLinus Torvalds 	ih->csum = 0;
6961da177e4SLinus Torvalds 	ih->group = group;
6971da177e4SLinus Torvalds 	ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
6981da177e4SLinus Torvalds 
699c439cb2eSHerbert Xu 	return ip_local_out(skb);
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds static void igmp_gq_timer_expire(unsigned long data)
7031da177e4SLinus Torvalds {
7041da177e4SLinus Torvalds 	struct in_device *in_dev = (struct in_device *)data;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
7071da177e4SLinus Torvalds 	igmpv3_send_report(in_dev, NULL);
7081da177e4SLinus Torvalds 	__in_dev_put(in_dev);
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds static void igmp_ifc_timer_expire(unsigned long data)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	struct in_device *in_dev = (struct in_device *)data;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	igmpv3_send_cr(in_dev);
7161da177e4SLinus Torvalds 	if (in_dev->mr_ifc_count) {
7171da177e4SLinus Torvalds 		in_dev->mr_ifc_count--;
7181da177e4SLinus Torvalds 		igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 	__in_dev_put(in_dev);
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
7261da177e4SLinus Torvalds 		return;
7271da177e4SLinus Torvalds 	in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
7281da177e4SLinus Torvalds 		IGMP_Unsolicited_Report_Count;
7291da177e4SLinus Torvalds 	igmp_ifc_start_timer(in_dev, 1);
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds static void igmp_timer_expire(unsigned long data)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	struct ip_mc_list *im=(struct ip_mc_list *)data;
7361da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	spin_lock(&im->lock);
7391da177e4SLinus Torvalds 	im->tm_running = 0;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	if (im->unsolicit_count) {
7421da177e4SLinus Torvalds 		im->unsolicit_count--;
7431da177e4SLinus Torvalds 		igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 	im->reporter = 1;
7461da177e4SLinus Torvalds 	spin_unlock(&im->lock);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev))
7491da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
7501da177e4SLinus Torvalds 	else if (IGMP_V2_SEEN(in_dev))
7511da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
7521da177e4SLinus Torvalds 	else
7531da177e4SLinus Torvalds 		igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	ip_ma_put(im);
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
758ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */
759ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
7601da177e4SLinus Torvalds {
7611da177e4SLinus Torvalds 	struct ip_sf_list *psf;
7621da177e4SLinus Torvalds 	int i, scount;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	scount = 0;
7651da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
7661da177e4SLinus Torvalds 		if (scount == nsrcs)
7671da177e4SLinus Torvalds 			break;
768ad12583fSDavid L Stevens 		for (i=0; i<nsrcs; i++) {
769ad12583fSDavid L Stevens 			/* skip inactive filters */
770ad12583fSDavid L Stevens 			if (pmc->sfcount[MCAST_INCLUDE] ||
771ad12583fSDavid L Stevens 			    pmc->sfcount[MCAST_EXCLUDE] !=
772ad12583fSDavid L Stevens 			    psf->sf_count[MCAST_EXCLUDE])
773ad12583fSDavid L Stevens 				continue;
774ad12583fSDavid L Stevens 			if (srcs[i] == psf->sf_inaddr) {
775ad12583fSDavid L Stevens 				scount++;
776ad12583fSDavid L Stevens 				break;
777ad12583fSDavid L Stevens 			}
778ad12583fSDavid L Stevens 		}
779ad12583fSDavid L Stevens 	}
780ad12583fSDavid L Stevens 	pmc->gsquery = 0;
781ad12583fSDavid L Stevens 	if (scount == nsrcs)	/* all sources excluded */
782ad12583fSDavid L Stevens 		return 0;
783ad12583fSDavid L Stevens 	return 1;
784ad12583fSDavid L Stevens }
785ad12583fSDavid L Stevens 
786ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
787ad12583fSDavid L Stevens {
788ad12583fSDavid L Stevens 	struct ip_sf_list *psf;
789ad12583fSDavid L Stevens 	int i, scount;
790ad12583fSDavid L Stevens 
791ad12583fSDavid L Stevens 	if (pmc->sfmode == MCAST_EXCLUDE)
792ad12583fSDavid L Stevens 		return igmp_xmarksources(pmc, nsrcs, srcs);
793ad12583fSDavid L Stevens 
794ad12583fSDavid L Stevens 	/* mark INCLUDE-mode sources */
795ad12583fSDavid L Stevens 	scount = 0;
796ad12583fSDavid L Stevens 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
797ad12583fSDavid L Stevens 		if (scount == nsrcs)
798ad12583fSDavid L Stevens 			break;
7991da177e4SLinus Torvalds 		for (i=0; i<nsrcs; i++)
8001da177e4SLinus Torvalds 			if (srcs[i] == psf->sf_inaddr) {
8011da177e4SLinus Torvalds 				psf->sf_gsresp = 1;
8021da177e4SLinus Torvalds 				scount++;
8031da177e4SLinus Torvalds 				break;
8041da177e4SLinus Torvalds 			}
8051da177e4SLinus Torvalds 	}
806ad12583fSDavid L Stevens 	if (!scount) {
807ad12583fSDavid L Stevens 		pmc->gsquery = 0;
808ad12583fSDavid L Stevens 		return 0;
809ad12583fSDavid L Stevens 	}
810ad12583fSDavid L Stevens 	pmc->gsquery = 1;
811ad12583fSDavid L Stevens 	return 1;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
81463007727SAl Viro static void igmp_heard_report(struct in_device *in_dev, __be32 group)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	struct ip_mc_list *im;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	/* Timers are only set for non-local groups */
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	if (group == IGMP_ALL_HOSTS)
8211da177e4SLinus Torvalds 		return;
8221da177e4SLinus Torvalds 
8231d7138deSEric Dumazet 	rcu_read_lock();
8241d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, im) {
8251da177e4SLinus Torvalds 		if (im->multiaddr == group) {
8261da177e4SLinus Torvalds 			igmp_stop_timer(im);
8271da177e4SLinus Torvalds 			break;
8281da177e4SLinus Torvalds 		}
8291da177e4SLinus Torvalds 	}
8301d7138deSEric Dumazet 	rcu_read_unlock();
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
8341da177e4SLinus Torvalds 	int len)
8351da177e4SLinus Torvalds {
836d9edf9e2SArnaldo Carvalho de Melo 	struct igmphdr 		*ih = igmp_hdr(skb);
837d9edf9e2SArnaldo Carvalho de Melo 	struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
8381da177e4SLinus Torvalds 	struct ip_mc_list	*im;
83963007727SAl Viro 	__be32			group = ih->group;
8401da177e4SLinus Torvalds 	int			max_delay;
8411da177e4SLinus Torvalds 	int			mark = 0;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 
8445b7c8406SDavid Stevens 	if (len == 8) {
8451da177e4SLinus Torvalds 		if (ih->code == 0) {
8461da177e4SLinus Torvalds 			/* Alas, old v1 router presents here. */
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 			max_delay = IGMP_Query_Response_Interval;
8491da177e4SLinus Torvalds 			in_dev->mr_v1_seen = jiffies +
8501da177e4SLinus Torvalds 				IGMP_V1_Router_Present_Timeout;
8511da177e4SLinus Torvalds 			group = 0;
8521da177e4SLinus Torvalds 		} else {
8531da177e4SLinus Torvalds 			/* v2 router present */
8541da177e4SLinus Torvalds 			max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
8551da177e4SLinus Torvalds 			in_dev->mr_v2_seen = jiffies +
8561da177e4SLinus Torvalds 				IGMP_V2_Router_Present_Timeout;
8571da177e4SLinus Torvalds 		}
8581da177e4SLinus Torvalds 		/* cancel the interface change timer */
8591da177e4SLinus Torvalds 		in_dev->mr_ifc_count = 0;
8601da177e4SLinus Torvalds 		if (del_timer(&in_dev->mr_ifc_timer))
8611da177e4SLinus Torvalds 			__in_dev_put(in_dev);
8621da177e4SLinus Torvalds 		/* clear deleted report items */
8631da177e4SLinus Torvalds 		igmpv3_clear_delrec(in_dev);
8641da177e4SLinus Torvalds 	} else if (len < 12) {
8651da177e4SLinus Torvalds 		return;	/* ignore bogus packet; freed by caller */
8665b7c8406SDavid Stevens 	} else if (IGMP_V1_SEEN(in_dev)) {
8675b7c8406SDavid Stevens 		/* This is a v3 query with v1 queriers present */
8685b7c8406SDavid Stevens 		max_delay = IGMP_Query_Response_Interval;
8695b7c8406SDavid Stevens 		group = 0;
8705b7c8406SDavid Stevens 	} else if (IGMP_V2_SEEN(in_dev)) {
8715b7c8406SDavid Stevens 		/* this is a v3 query with v2 queriers present;
8725b7c8406SDavid Stevens 		 * Interpretation of the max_delay code is problematic here.
8735b7c8406SDavid Stevens 		 * A real v2 host would use ih_code directly, while v3 has a
8745b7c8406SDavid Stevens 		 * different encoding. We use the v3 encoding as more likely
8755b7c8406SDavid Stevens 		 * to be intended in a v3 query.
8765b7c8406SDavid Stevens 		 */
8775b7c8406SDavid Stevens 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
8781da177e4SLinus Torvalds 	} else { /* v3 */
8791da177e4SLinus Torvalds 		if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
8801da177e4SLinus Torvalds 			return;
8811da177e4SLinus Torvalds 
882d9edf9e2SArnaldo Carvalho de Melo 		ih3 = igmpv3_query_hdr(skb);
8831da177e4SLinus Torvalds 		if (ih3->nsrcs) {
8841da177e4SLinus Torvalds 			if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
88563007727SAl Viro 					   + ntohs(ih3->nsrcs)*sizeof(__be32)))
8861da177e4SLinus Torvalds 				return;
887d9edf9e2SArnaldo Carvalho de Melo 			ih3 = igmpv3_query_hdr(skb);
8881da177e4SLinus Torvalds 		}
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
8911da177e4SLinus Torvalds 		if (!max_delay)
8921da177e4SLinus Torvalds 			max_delay = 1;	/* can't mod w/ 0 */
8931da177e4SLinus Torvalds 		in_dev->mr_maxdelay = max_delay;
8941da177e4SLinus Torvalds 		if (ih3->qrv)
8951da177e4SLinus Torvalds 			in_dev->mr_qrv = ih3->qrv;
8961da177e4SLinus Torvalds 		if (!group) { /* general query */
8971da177e4SLinus Torvalds 			if (ih3->nsrcs)
8981da177e4SLinus Torvalds 				return;	/* no sources allowed */
8991da177e4SLinus Torvalds 			igmp_gq_start_timer(in_dev);
9001da177e4SLinus Torvalds 			return;
9011da177e4SLinus Torvalds 		}
9021da177e4SLinus Torvalds 		/* mark sources to include, if group & source-specific */
9031da177e4SLinus Torvalds 		mark = ih3->nsrcs != 0;
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	/*
9071da177e4SLinus Torvalds 	 * - Start the timers in all of our membership records
9081da177e4SLinus Torvalds 	 *   that the query applies to for the interface on
9091da177e4SLinus Torvalds 	 *   which the query arrived excl. those that belong
9101da177e4SLinus Torvalds 	 *   to a "local" group (224.0.0.X)
9111da177e4SLinus Torvalds 	 * - For timers already running check if they need to
9121da177e4SLinus Torvalds 	 *   be reset.
9131da177e4SLinus Torvalds 	 * - Use the igmp->igmp_code field as the maximum
9141da177e4SLinus Torvalds 	 *   delay possible
9151da177e4SLinus Torvalds 	 */
9161d7138deSEric Dumazet 	rcu_read_lock();
9171d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, im) {
918ad12583fSDavid L Stevens 		int changed;
919ad12583fSDavid L Stevens 
9201da177e4SLinus Torvalds 		if (group && group != im->multiaddr)
9211da177e4SLinus Torvalds 			continue;
9221da177e4SLinus Torvalds 		if (im->multiaddr == IGMP_ALL_HOSTS)
9231da177e4SLinus Torvalds 			continue;
9241da177e4SLinus Torvalds 		spin_lock_bh(&im->lock);
9251da177e4SLinus Torvalds 		if (im->tm_running)
9261da177e4SLinus Torvalds 			im->gsquery = im->gsquery && mark;
9271da177e4SLinus Torvalds 		else
9281da177e4SLinus Torvalds 			im->gsquery = mark;
929ad12583fSDavid L Stevens 		changed = !im->gsquery ||
9301da177e4SLinus Torvalds 			igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
9311da177e4SLinus Torvalds 		spin_unlock_bh(&im->lock);
932ad12583fSDavid L Stevens 		if (changed)
9331da177e4SLinus Torvalds 			igmp_mod_timer(im, max_delay);
9341da177e4SLinus Torvalds 	}
9351d7138deSEric Dumazet 	rcu_read_unlock();
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9389a57a9d2SEric Dumazet /* called in rcu_read_lock() section */
9391da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	/* This basically follows the spec line by line -- see RFC1112 */
9421da177e4SLinus Torvalds 	struct igmphdr *ih;
9439a57a9d2SEric Dumazet 	struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
9441da177e4SLinus Torvalds 	int len = skb->len;
9451da177e4SLinus Torvalds 
946cd557bc1SDenis V. Lunev 	if (in_dev == NULL)
947cd557bc1SDenis V. Lunev 		goto drop;
9481da177e4SLinus Torvalds 
949fb286bb2SHerbert Xu 	if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
9509a57a9d2SEric Dumazet 		goto drop;
951fb286bb2SHerbert Xu 
952fb286bb2SHerbert Xu 	switch (skb->ip_summed) {
95384fa7933SPatrick McHardy 	case CHECKSUM_COMPLETE:
954d3bc23e7SAl Viro 		if (!csum_fold(skb->csum))
955fb286bb2SHerbert Xu 			break;
956fb286bb2SHerbert Xu 		/* fall through */
957fb286bb2SHerbert Xu 	case CHECKSUM_NONE:
958fb286bb2SHerbert Xu 		skb->csum = 0;
959fb286bb2SHerbert Xu 		if (__skb_checksum_complete(skb))
9609a57a9d2SEric Dumazet 			goto drop;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 
963d9edf9e2SArnaldo Carvalho de Melo 	ih = igmp_hdr(skb);
9641da177e4SLinus Torvalds 	switch (ih->type) {
9651da177e4SLinus Torvalds 	case IGMP_HOST_MEMBERSHIP_QUERY:
9661da177e4SLinus Torvalds 		igmp_heard_query(in_dev, skb, len);
9671da177e4SLinus Torvalds 		break;
9681da177e4SLinus Torvalds 	case IGMP_HOST_MEMBERSHIP_REPORT:
9691da177e4SLinus Torvalds 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
9701da177e4SLinus Torvalds 		/* Is it our report looped back? */
971c7537967SDavid S. Miller 		if (rt_is_output_route(skb_rtable(skb)))
9721da177e4SLinus Torvalds 			break;
97324c69275SDavid Stevens 		/* don't rely on MC router hearing unicast reports */
97424c69275SDavid Stevens 		if (skb->pkt_type == PACKET_MULTICAST ||
97524c69275SDavid Stevens 		    skb->pkt_type == PACKET_BROADCAST)
9761da177e4SLinus Torvalds 			igmp_heard_report(in_dev, ih->group);
9771da177e4SLinus Torvalds 		break;
9781da177e4SLinus Torvalds 	case IGMP_PIM:
9791da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1
9801da177e4SLinus Torvalds 		return pim_rcv_v1(skb);
9811da177e4SLinus Torvalds #endif
982c6b471e6SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
9831da177e4SLinus Torvalds 	case IGMP_DVMRP:
9841da177e4SLinus Torvalds 	case IGMP_TRACE:
9851da177e4SLinus Torvalds 	case IGMP_HOST_LEAVE_MESSAGE:
9861da177e4SLinus Torvalds 	case IGMP_MTRACE:
9871da177e4SLinus Torvalds 	case IGMP_MTRACE_RESP:
9881da177e4SLinus Torvalds 		break;
9891da177e4SLinus Torvalds 	default:
990dd1c1853SLinus Torvalds 		break;
9911da177e4SLinus Torvalds 	}
992fb286bb2SHerbert Xu 
993cd557bc1SDenis V. Lunev drop:
9941da177e4SLinus Torvalds 	kfree_skb(skb);
9951da177e4SLinus Torvalds 	return 0;
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds #endif
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds /*
10021da177e4SLinus Torvalds  *	Add a filter to a device
10031da177e4SLinus Torvalds  */
10041da177e4SLinus Torvalds 
100563007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
10061da177e4SLinus Torvalds {
10071da177e4SLinus Torvalds 	char buf[MAX_ADDR_LEN];
10081da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	/* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
10111da177e4SLinus Torvalds 	   We will get multicast token leakage, when IFF_MULTICAST
10121da177e4SLinus Torvalds 	   is changed. This check should be done in dev->set_multicast_list
10131da177e4SLinus Torvalds 	   routine. Something sort of:
10141da177e4SLinus Torvalds 	   if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
10151da177e4SLinus Torvalds 	   --ANK
10161da177e4SLinus Torvalds 	   */
10171da177e4SLinus Torvalds 	if (arp_mc_map(addr, buf, dev, 0) == 0)
101822bedad3SJiri Pirko 		dev_mc_add(dev, buf);
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds /*
10221da177e4SLinus Torvalds  *	Remove a filter from a device
10231da177e4SLinus Torvalds  */
10241da177e4SLinus Torvalds 
102563007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	char buf[MAX_ADDR_LEN];
10281da177e4SLinus Torvalds 	struct net_device *dev = in_dev->dev;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	if (arp_mc_map(addr, buf, dev, 0) == 0)
103122bedad3SJiri Pirko 		dev_mc_del(dev, buf);
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
10351da177e4SLinus Torvalds /*
10361da177e4SLinus Torvalds  * deleted ip_mc_list manipulation
10371da177e4SLinus Torvalds  */
10381da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	/* this is an "ip_mc_list" for convenience; only the fields below
10431da177e4SLinus Torvalds 	 * are actually used. In particular, the refcnt and users are not
10441da177e4SLinus Torvalds 	 * used for management of the delete list. Using the same structure
10451da177e4SLinus Torvalds 	 * for deleted items allows change reports to use common code with
10461da177e4SLinus Torvalds 	 * non-deleted or query-response MCA's.
10471da177e4SLinus Torvalds 	 */
10480da974f4SPanagiotis Issaris 	pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
10491da177e4SLinus Torvalds 	if (!pmc)
10501da177e4SLinus Torvalds 		return;
10511da177e4SLinus Torvalds 	spin_lock_bh(&im->lock);
10521da177e4SLinus Torvalds 	pmc->interface = im->interface;
10531da177e4SLinus Torvalds 	in_dev_hold(in_dev);
10541da177e4SLinus Torvalds 	pmc->multiaddr = im->multiaddr;
10551da177e4SLinus Torvalds 	pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
10561da177e4SLinus Torvalds 		IGMP_Unsolicited_Report_Count;
10571da177e4SLinus Torvalds 	pmc->sfmode = im->sfmode;
10581da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_INCLUDE) {
10591da177e4SLinus Torvalds 		struct ip_sf_list *psf;
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 		pmc->tomb = im->tomb;
10621da177e4SLinus Torvalds 		pmc->sources = im->sources;
10631da177e4SLinus Torvalds 		im->tomb = im->sources = NULL;
10641da177e4SLinus Torvalds 		for (psf=pmc->sources; psf; psf=psf->sf_next)
10651da177e4SLinus Torvalds 			psf->sf_crcount = pmc->crcount;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 	spin_unlock_bh(&im->lock);
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
10701da177e4SLinus Torvalds 	pmc->next = in_dev->mc_tomb;
10711da177e4SLinus Torvalds 	in_dev->mc_tomb = pmc;
10721da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
107563007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
10761da177e4SLinus Torvalds {
10771da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *pmc_prev;
10781da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_next;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
10811da177e4SLinus Torvalds 	pmc_prev = NULL;
10821da177e4SLinus Torvalds 	for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
10831da177e4SLinus Torvalds 		if (pmc->multiaddr == multiaddr)
10841da177e4SLinus Torvalds 			break;
10851da177e4SLinus Torvalds 		pmc_prev = pmc;
10861da177e4SLinus Torvalds 	}
10871da177e4SLinus Torvalds 	if (pmc) {
10881da177e4SLinus Torvalds 		if (pmc_prev)
10891da177e4SLinus Torvalds 			pmc_prev->next = pmc->next;
10901da177e4SLinus Torvalds 		else
10911da177e4SLinus Torvalds 			in_dev->mc_tomb = pmc->next;
10921da177e4SLinus Torvalds 	}
10931da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
10941da177e4SLinus Torvalds 	if (pmc) {
10951da177e4SLinus Torvalds 		for (psf=pmc->tomb; psf; psf=psf_next) {
10961da177e4SLinus Torvalds 			psf_next = psf->sf_next;
10971da177e4SLinus Torvalds 			kfree(psf);
10981da177e4SLinus Torvalds 		}
10991da177e4SLinus Torvalds 		in_dev_put(pmc->interface);
11001da177e4SLinus Torvalds 		kfree(pmc);
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev)
11051da177e4SLinus Torvalds {
11061da177e4SLinus Torvalds 	struct ip_mc_list *pmc, *nextpmc;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	spin_lock_bh(&in_dev->mc_tomb_lock);
11091da177e4SLinus Torvalds 	pmc = in_dev->mc_tomb;
11101da177e4SLinus Torvalds 	in_dev->mc_tomb = NULL;
11111da177e4SLinus Torvalds 	spin_unlock_bh(&in_dev->mc_tomb_lock);
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	for (; pmc; pmc = nextpmc) {
11141da177e4SLinus Torvalds 		nextpmc = pmc->next;
11151da177e4SLinus Torvalds 		ip_mc_clear_src(pmc);
11161da177e4SLinus Torvalds 		in_dev_put(pmc->interface);
11171da177e4SLinus Torvalds 		kfree(pmc);
11181da177e4SLinus Torvalds 	}
11191da177e4SLinus Torvalds 	/* clear dead sources, too */
11201d7138deSEric Dumazet 	rcu_read_lock();
11211d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
11221da177e4SLinus Torvalds 		struct ip_sf_list *psf, *psf_next;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 		spin_lock_bh(&pmc->lock);
11251da177e4SLinus Torvalds 		psf = pmc->tomb;
11261da177e4SLinus Torvalds 		pmc->tomb = NULL;
11271da177e4SLinus Torvalds 		spin_unlock_bh(&pmc->lock);
11281da177e4SLinus Torvalds 		for (; psf; psf=psf_next) {
11291da177e4SLinus Torvalds 			psf_next = psf->sf_next;
11301da177e4SLinus Torvalds 			kfree(psf);
11311da177e4SLinus Torvalds 		}
11321da177e4SLinus Torvalds 	}
11331d7138deSEric Dumazet 	rcu_read_unlock();
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds #endif
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
11401da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
11411da177e4SLinus Torvalds 	int reporter;
11421da177e4SLinus Torvalds #endif
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	if (im->loaded) {
11451da177e4SLinus Torvalds 		im->loaded = 0;
11461da177e4SLinus Torvalds 		ip_mc_filter_del(in_dev, im->multiaddr);
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
11501da177e4SLinus Torvalds 	if (im->multiaddr == IGMP_ALL_HOSTS)
11511da177e4SLinus Torvalds 		return;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	reporter = im->reporter;
11541da177e4SLinus Torvalds 	igmp_stop_timer(im);
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	if (!in_dev->dead) {
11571da177e4SLinus Torvalds 		if (IGMP_V1_SEEN(in_dev))
115824cf3af3SVeaceslav Falico 			return;
11591da177e4SLinus Torvalds 		if (IGMP_V2_SEEN(in_dev)) {
11601da177e4SLinus Torvalds 			if (reporter)
11611da177e4SLinus Torvalds 				igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
116224cf3af3SVeaceslav Falico 			return;
11631da177e4SLinus Torvalds 		}
11641da177e4SLinus Torvalds 		/* IGMPv3 */
11651da177e4SLinus Torvalds 		igmpv3_add_delrec(in_dev, im);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
11681da177e4SLinus Torvalds 	}
11691da177e4SLinus Torvalds #endif
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds static void igmp_group_added(struct ip_mc_list *im)
11731da177e4SLinus Torvalds {
11741da177e4SLinus Torvalds 	struct in_device *in_dev = im->interface;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	if (im->loaded == 0) {
11771da177e4SLinus Torvalds 		im->loaded = 1;
11781da177e4SLinus Torvalds 		ip_mc_filter_add(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 	if (in_dev->dead)
11861da177e4SLinus Torvalds 		return;
11871da177e4SLinus Torvalds 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
11881da177e4SLinus Torvalds 		spin_lock_bh(&im->lock);
11891da177e4SLinus Torvalds 		igmp_start_timer(im, IGMP_Initial_Report_Delay);
11901da177e4SLinus Torvalds 		spin_unlock_bh(&im->lock);
11911da177e4SLinus Torvalds 		return;
11921da177e4SLinus Torvalds 	}
11931da177e4SLinus Torvalds 	/* else, v3 */
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
11961da177e4SLinus Torvalds 		IGMP_Unsolicited_Report_Count;
11971da177e4SLinus Torvalds 	igmp_ifc_event(in_dev);
11981da177e4SLinus Torvalds #endif
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds /*
12031da177e4SLinus Torvalds  *	Multicast list managers
12041da177e4SLinus Torvalds  */
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds /*
12081da177e4SLinus Torvalds  *	A socket has joined a multicast group on device dev.
12091da177e4SLinus Torvalds  */
12101da177e4SLinus Torvalds 
12118f935bbdSAl Viro void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	struct ip_mc_list *im;
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	ASSERT_RTNL();
12161da177e4SLinus Torvalds 
12171d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, im) {
12181da177e4SLinus Torvalds 		if (im->multiaddr == addr) {
12191da177e4SLinus Torvalds 			im->users++;
12201da177e4SLinus Torvalds 			ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
12211da177e4SLinus Torvalds 			goto out;
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 	}
12241da177e4SLinus Torvalds 
12251d7138deSEric Dumazet 	im = kzalloc(sizeof(*im), GFP_KERNEL);
12261da177e4SLinus Torvalds 	if (!im)
12271da177e4SLinus Torvalds 		goto out;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	im->users = 1;
12301da177e4SLinus Torvalds 	im->interface = in_dev;
12311da177e4SLinus Torvalds 	in_dev_hold(in_dev);
12321da177e4SLinus Torvalds 	im->multiaddr = addr;
12331da177e4SLinus Torvalds 	/* initial mode is (EX, empty) */
12341da177e4SLinus Torvalds 	im->sfmode = MCAST_EXCLUDE;
12351da177e4SLinus Torvalds 	im->sfcount[MCAST_EXCLUDE] = 1;
12361da177e4SLinus Torvalds 	atomic_set(&im->refcnt, 1);
12371da177e4SLinus Torvalds 	spin_lock_init(&im->lock);
12381da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1239b24b8a24SPavel Emelyanov 	setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
12401da177e4SLinus Torvalds 	im->unsolicit_count = IGMP_Unsolicited_Report_Count;
12411da177e4SLinus Torvalds #endif
12421d7138deSEric Dumazet 
12431d7138deSEric Dumazet 	im->next_rcu = in_dev->mc_list;
1244b8bae41eSRami Rosen 	in_dev->mc_count++;
12451d7138deSEric Dumazet 	rcu_assign_pointer(in_dev->mc_list, im);
12461d7138deSEric Dumazet 
12471da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
12481da177e4SLinus Torvalds 	igmpv3_del_delrec(in_dev, im->multiaddr);
12491da177e4SLinus Torvalds #endif
12501da177e4SLinus Torvalds 	igmp_group_added(im);
12511da177e4SLinus Torvalds 	if (!in_dev->dead)
12521da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
12531da177e4SLinus Torvalds out:
12541da177e4SLinus Torvalds 	return;
12551da177e4SLinus Torvalds }
12564bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group);
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds /*
1259a816c7c7SJay Vosburgh  *	Resend IGMP JOIN report; used for bonding.
1260866f3b25SEric Dumazet  *	Called with rcu_read_lock()
1261a816c7c7SJay Vosburgh  */
1262866f3b25SEric Dumazet void ip_mc_rejoin_groups(struct in_device *in_dev)
1263a816c7c7SJay Vosburgh {
126408882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST
1265866f3b25SEric Dumazet 	struct ip_mc_list *im;
1266866f3b25SEric Dumazet 	int type;
1267a816c7c7SJay Vosburgh 
1268866f3b25SEric Dumazet 	for_each_pmc_rcu(in_dev, im) {
1269a816c7c7SJay Vosburgh 		if (im->multiaddr == IGMP_ALL_HOSTS)
1270866f3b25SEric Dumazet 			continue;
1271a816c7c7SJay Vosburgh 
1272e12b4539SFlavio Leitner 		/* a failover is happening and switches
1273866f3b25SEric Dumazet 		 * must be notified immediately
1274866f3b25SEric Dumazet 		 */
1275e12b4539SFlavio Leitner 		if (IGMP_V1_SEEN(in_dev))
1276866f3b25SEric Dumazet 			type = IGMP_HOST_MEMBERSHIP_REPORT;
1277e12b4539SFlavio Leitner 		else if (IGMP_V2_SEEN(in_dev))
1278866f3b25SEric Dumazet 			type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1279e12b4539SFlavio Leitner 		else
1280866f3b25SEric Dumazet 			type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1281866f3b25SEric Dumazet 		igmp_send_report(in_dev, im, type);
1282866f3b25SEric Dumazet 	}
1283a816c7c7SJay Vosburgh #endif
1284a816c7c7SJay Vosburgh }
1285866f3b25SEric Dumazet EXPORT_SYMBOL(ip_mc_rejoin_groups);
1286a816c7c7SJay Vosburgh 
1287a816c7c7SJay Vosburgh /*
12881da177e4SLinus Torvalds  *	A socket has left a multicast group on device dev
12891da177e4SLinus Torvalds  */
12901da177e4SLinus Torvalds 
12918f935bbdSAl Viro void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
12921da177e4SLinus Torvalds {
12931d7138deSEric Dumazet 	struct ip_mc_list *i;
12941d7138deSEric Dumazet 	struct ip_mc_list __rcu **ip;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	ASSERT_RTNL();
12971da177e4SLinus Torvalds 
12981d7138deSEric Dumazet 	for (ip = &in_dev->mc_list;
12991d7138deSEric Dumazet 	     (i = rtnl_dereference(*ip)) != NULL;
13001d7138deSEric Dumazet 	     ip = &i->next_rcu) {
13011da177e4SLinus Torvalds 		if (i->multiaddr == addr) {
13021da177e4SLinus Torvalds 			if (--i->users == 0) {
13031d7138deSEric Dumazet 				*ip = i->next_rcu;
1304b8bae41eSRami Rosen 				in_dev->mc_count--;
13051da177e4SLinus Torvalds 				igmp_group_dropped(i);
130624cf3af3SVeaceslav Falico 				ip_mc_clear_src(i);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 				if (!in_dev->dead)
13091da177e4SLinus Torvalds 					ip_rt_multicast_event(in_dev);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 				ip_ma_put(i);
13121da177e4SLinus Torvalds 				return;
13131da177e4SLinus Torvalds 			}
13141da177e4SLinus Torvalds 			break;
13151da177e4SLinus Torvalds 		}
13161da177e4SLinus Torvalds 	}
13171da177e4SLinus Torvalds }
13184bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_dec_group);
13191da177e4SLinus Torvalds 
132075c78500SMoni Shoua /* Device changing type */
132175c78500SMoni Shoua 
132275c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev)
132375c78500SMoni Shoua {
13241d7138deSEric Dumazet 	struct ip_mc_list *pmc;
132575c78500SMoni Shoua 
132675c78500SMoni Shoua 	ASSERT_RTNL();
132775c78500SMoni Shoua 
13281d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
13291d7138deSEric Dumazet 		igmp_group_dropped(pmc);
133075c78500SMoni Shoua }
133175c78500SMoni Shoua 
133275c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev)
133375c78500SMoni Shoua {
13341d7138deSEric Dumazet 	struct ip_mc_list *pmc;
133575c78500SMoni Shoua 
133675c78500SMoni Shoua 	ASSERT_RTNL();
133775c78500SMoni Shoua 
13381d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
13391d7138deSEric Dumazet 		igmp_group_added(pmc);
134075c78500SMoni Shoua }
134175c78500SMoni Shoua 
13421da177e4SLinus Torvalds /* Device going down */
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev)
13451da177e4SLinus Torvalds {
13461d7138deSEric Dumazet 	struct ip_mc_list *pmc;
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	ASSERT_RTNL();
13491da177e4SLinus Torvalds 
13501d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
13511d7138deSEric Dumazet 		igmp_group_dropped(pmc);
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
13541da177e4SLinus Torvalds 	in_dev->mr_ifc_count = 0;
13551da177e4SLinus Torvalds 	if (del_timer(&in_dev->mr_ifc_timer))
13561da177e4SLinus Torvalds 		__in_dev_put(in_dev);
13571da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
13581da177e4SLinus Torvalds 	if (del_timer(&in_dev->mr_gq_timer))
13591da177e4SLinus Torvalds 		__in_dev_put(in_dev);
13601da177e4SLinus Torvalds 	igmpv3_clear_delrec(in_dev);
13611da177e4SLinus Torvalds #endif
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev)
13671da177e4SLinus Torvalds {
13681da177e4SLinus Torvalds 	ASSERT_RTNL();
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	in_dev->mc_tomb = NULL;
13711da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
13721da177e4SLinus Torvalds 	in_dev->mr_gq_running = 0;
1373b24b8a24SPavel Emelyanov 	setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1374b24b8a24SPavel Emelyanov 			(unsigned long)in_dev);
13751da177e4SLinus Torvalds 	in_dev->mr_ifc_count = 0;
1376b8bae41eSRami Rosen 	in_dev->mc_count     = 0;
1377b24b8a24SPavel Emelyanov 	setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1378b24b8a24SPavel Emelyanov 			(unsigned long)in_dev);
13791da177e4SLinus Torvalds 	in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
13801da177e4SLinus Torvalds #endif
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	spin_lock_init(&in_dev->mc_tomb_lock);
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds /* Device going up */
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev)
13881da177e4SLinus Torvalds {
13891d7138deSEric Dumazet 	struct ip_mc_list *pmc;
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	ASSERT_RTNL();
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
13941da177e4SLinus Torvalds 
13951d7138deSEric Dumazet 	for_each_pmc_rtnl(in_dev, pmc)
13961d7138deSEric Dumazet 		igmp_group_added(pmc);
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds /*
14001da177e4SLinus Torvalds  *	Device is about to be destroyed: clean up.
14011da177e4SLinus Torvalds  */
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev)
14041da177e4SLinus Torvalds {
14051da177e4SLinus Torvalds 	struct ip_mc_list *i;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	ASSERT_RTNL();
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	/* Deactivate timers */
14101da177e4SLinus Torvalds 	ip_mc_down(in_dev);
14111da177e4SLinus Torvalds 
14121d7138deSEric Dumazet 	while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
14131d7138deSEric Dumazet 		in_dev->mc_list = i->next_rcu;
1414b8bae41eSRami Rosen 		in_dev->mc_count--;
14151d7138deSEric Dumazet 
141624cf3af3SVeaceslav Falico 		/* We've dropped the groups in ip_mc_down already */
141724cf3af3SVeaceslav Falico 		ip_mc_clear_src(i);
14181da177e4SLinus Torvalds 		ip_ma_put(i);
14191da177e4SLinus Torvalds 	}
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds 
14229e917dcaSEric Dumazet /* RTNL is locked */
1423877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
14241da177e4SLinus Torvalds {
14251da177e4SLinus Torvalds 	struct net_device *dev = NULL;
14261da177e4SLinus Torvalds 	struct in_device *idev = NULL;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	if (imr->imr_ifindex) {
1429877acedcSDaniel Lezcano 		idev = inetdev_by_index(net, imr->imr_ifindex);
14301da177e4SLinus Torvalds 		return idev;
14311da177e4SLinus Torvalds 	}
14321da177e4SLinus Torvalds 	if (imr->imr_address.s_addr) {
14339e917dcaSEric Dumazet 		dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
14341da177e4SLinus Torvalds 		if (!dev)
14351da177e4SLinus Torvalds 			return NULL;
14361da177e4SLinus Torvalds 	}
14371da177e4SLinus Torvalds 
1438b23dd4feSDavid S. Miller 	if (!dev) {
143978fbfd8aSDavid S. Miller 		struct rtable *rt = ip_route_output(net,
144078fbfd8aSDavid S. Miller 						    imr->imr_multiaddr.s_addr,
144178fbfd8aSDavid S. Miller 						    0, 0, 0);
1442b23dd4feSDavid S. Miller 		if (!IS_ERR(rt)) {
1443d8d1f30bSChangli Gao 			dev = rt->dst.dev;
14441da177e4SLinus Torvalds 			ip_rt_put(rt);
14451da177e4SLinus Torvalds 		}
1446b23dd4feSDavid S. Miller 	}
14471da177e4SLinus Torvalds 	if (dev) {
14481da177e4SLinus Torvalds 		imr->imr_ifindex = dev->ifindex;
1449e5ed6399SHerbert Xu 		idev = __in_dev_get_rtnl(dev);
14501da177e4SLinus Torvalds 	}
14511da177e4SLinus Torvalds 	return idev;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds /*
14551da177e4SLinus Torvalds  *	Join a socket to a group
14561da177e4SLinus Torvalds  */
1457ab32ea5dSBrian Haley int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1458ab32ea5dSBrian Haley int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
14628f935bbdSAl Viro 	__be32 *psfsrc)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_prev;
14651da177e4SLinus Torvalds 	int rv = 0;
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	psf_prev = NULL;
14681da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
14691da177e4SLinus Torvalds 		if (psf->sf_inaddr == *psfsrc)
14701da177e4SLinus Torvalds 			break;
14711da177e4SLinus Torvalds 		psf_prev = psf;
14721da177e4SLinus Torvalds 	}
14731da177e4SLinus Torvalds 	if (!psf || psf->sf_count[sfmode] == 0) {
14741da177e4SLinus Torvalds 		/* source filter not found, or count wrong =>  bug */
14751da177e4SLinus Torvalds 		return -ESRCH;
14761da177e4SLinus Torvalds 	}
14771da177e4SLinus Torvalds 	psf->sf_count[sfmode]--;
14781da177e4SLinus Torvalds 	if (psf->sf_count[sfmode] == 0) {
14791da177e4SLinus Torvalds 		ip_rt_multicast_event(pmc->interface);
14801da177e4SLinus Torvalds 	}
14811da177e4SLinus Torvalds 	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
14821da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
14831da177e4SLinus Torvalds 		struct in_device *in_dev = pmc->interface;
14841da177e4SLinus Torvalds #endif
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 		/* no more filters for this source */
14871da177e4SLinus Torvalds 		if (psf_prev)
14881da177e4SLinus Torvalds 			psf_prev->sf_next = psf->sf_next;
14891da177e4SLinus Torvalds 		else
14901da177e4SLinus Torvalds 			pmc->sources = psf->sf_next;
14911da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
14921da177e4SLinus Torvalds 		if (psf->sf_oldin &&
14931da177e4SLinus Torvalds 		    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
14941da177e4SLinus Torvalds 			psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
14951da177e4SLinus Torvalds 				IGMP_Unsolicited_Report_Count;
14961da177e4SLinus Torvalds 			psf->sf_next = pmc->tomb;
14971da177e4SLinus Torvalds 			pmc->tomb = psf;
14981da177e4SLinus Torvalds 			rv = 1;
14991da177e4SLinus Torvalds 		} else
15001da177e4SLinus Torvalds #endif
15011da177e4SLinus Torvalds 			kfree(psf);
15021da177e4SLinus Torvalds 	}
15031da177e4SLinus Torvalds 	return rv;
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST
15071da177e4SLinus Torvalds #define igmp_ifc_event(x)	do { } while (0)
15081da177e4SLinus Torvalds #endif
15091da177e4SLinus Torvalds 
15108f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
15118f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta)
15121da177e4SLinus Torvalds {
15131da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
15141da177e4SLinus Torvalds 	int	changerec = 0;
15151da177e4SLinus Torvalds 	int	i, err;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	if (!in_dev)
15181da177e4SLinus Torvalds 		return -ENODEV;
15191d7138deSEric Dumazet 	rcu_read_lock();
15201d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
15211da177e4SLinus Torvalds 		if (*pmca == pmc->multiaddr)
15221da177e4SLinus Torvalds 			break;
15231da177e4SLinus Torvalds 	}
15241da177e4SLinus Torvalds 	if (!pmc) {
15251da177e4SLinus Torvalds 		/* MCA not found?? bug */
15261d7138deSEric Dumazet 		rcu_read_unlock();
15271da177e4SLinus Torvalds 		return -ESRCH;
15281da177e4SLinus Torvalds 	}
15291da177e4SLinus Torvalds 	spin_lock_bh(&pmc->lock);
15301d7138deSEric Dumazet 	rcu_read_unlock();
15311da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
15321da177e4SLinus Torvalds 	sf_markstate(pmc);
15331da177e4SLinus Torvalds #endif
15341da177e4SLinus Torvalds 	if (!delta) {
15351da177e4SLinus Torvalds 		err = -EINVAL;
15361da177e4SLinus Torvalds 		if (!pmc->sfcount[sfmode])
15371da177e4SLinus Torvalds 			goto out_unlock;
15381da177e4SLinus Torvalds 		pmc->sfcount[sfmode]--;
15391da177e4SLinus Torvalds 	}
15401da177e4SLinus Torvalds 	err = 0;
15411da177e4SLinus Torvalds 	for (i=0; i<sfcount; i++) {
15421da177e4SLinus Torvalds 		int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 		changerec |= rv > 0;
15451da177e4SLinus Torvalds 		if (!err && rv < 0)
15461da177e4SLinus Torvalds 			err = rv;
15471da177e4SLinus Torvalds 	}
15481da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_EXCLUDE &&
15491da177e4SLinus Torvalds 	    pmc->sfcount[MCAST_EXCLUDE] == 0 &&
15501da177e4SLinus Torvalds 	    pmc->sfcount[MCAST_INCLUDE]) {
15511da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
15521da177e4SLinus Torvalds 		struct ip_sf_list *psf;
15531da177e4SLinus Torvalds #endif
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 		/* filter mode change */
15561da177e4SLinus Torvalds 		pmc->sfmode = MCAST_INCLUDE;
15571da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
15581da177e4SLinus Torvalds 		pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
15591da177e4SLinus Torvalds 			IGMP_Unsolicited_Report_Count;
15601da177e4SLinus Torvalds 		in_dev->mr_ifc_count = pmc->crcount;
15611da177e4SLinus Torvalds 		for (psf=pmc->sources; psf; psf = psf->sf_next)
15621da177e4SLinus Torvalds 			psf->sf_crcount = 0;
15631da177e4SLinus Torvalds 		igmp_ifc_event(pmc->interface);
15641da177e4SLinus Torvalds 	} else if (sf_setstate(pmc) || changerec) {
15651da177e4SLinus Torvalds 		igmp_ifc_event(pmc->interface);
15661da177e4SLinus Torvalds #endif
15671da177e4SLinus Torvalds 	}
15681da177e4SLinus Torvalds out_unlock:
15691da177e4SLinus Torvalds 	spin_unlock_bh(&pmc->lock);
15701da177e4SLinus Torvalds 	return err;
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds /*
15741da177e4SLinus Torvalds  * Add multicast single-source filter to the interface list
15751da177e4SLinus Torvalds  */
15761da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
15778f935bbdSAl Viro 	__be32 *psfsrc, int delta)
15781da177e4SLinus Torvalds {
15791da177e4SLinus Torvalds 	struct ip_sf_list *psf, *psf_prev;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	psf_prev = NULL;
15821da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
15831da177e4SLinus Torvalds 		if (psf->sf_inaddr == *psfsrc)
15841da177e4SLinus Torvalds 			break;
15851da177e4SLinus Torvalds 		psf_prev = psf;
15861da177e4SLinus Torvalds 	}
15871da177e4SLinus Torvalds 	if (!psf) {
15880da974f4SPanagiotis Issaris 		psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
15891da177e4SLinus Torvalds 		if (!psf)
15901da177e4SLinus Torvalds 			return -ENOBUFS;
15911da177e4SLinus Torvalds 		psf->sf_inaddr = *psfsrc;
15921da177e4SLinus Torvalds 		if (psf_prev) {
15931da177e4SLinus Torvalds 			psf_prev->sf_next = psf;
15941da177e4SLinus Torvalds 		} else
15951da177e4SLinus Torvalds 			pmc->sources = psf;
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds 	psf->sf_count[sfmode]++;
15981da177e4SLinus Torvalds 	if (psf->sf_count[sfmode] == 1) {
15991da177e4SLinus Torvalds 		ip_rt_multicast_event(pmc->interface);
16001da177e4SLinus Torvalds 	}
16011da177e4SLinus Torvalds 	return 0;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
16051da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc)
16061da177e4SLinus Torvalds {
16071da177e4SLinus Torvalds 	struct ip_sf_list *psf;
16081da177e4SLinus Torvalds 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next)
16111da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
16121da177e4SLinus Torvalds 			psf->sf_oldin = mca_xcount ==
16131da177e4SLinus Torvalds 				psf->sf_count[MCAST_EXCLUDE] &&
16141da177e4SLinus Torvalds 				!psf->sf_count[MCAST_INCLUDE];
16151da177e4SLinus Torvalds 		} else
16161da177e4SLinus Torvalds 			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc)
16201da177e4SLinus Torvalds {
1621ad12583fSDavid L Stevens 	struct ip_sf_list *psf, *dpsf;
16221da177e4SLinus Torvalds 	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
16231da177e4SLinus Torvalds 	int qrv = pmc->interface->mr_qrv;
16241da177e4SLinus Torvalds 	int new_in, rv;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	rv = 0;
16271da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=psf->sf_next) {
16281da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE]) {
16291da177e4SLinus Torvalds 			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
16301da177e4SLinus Torvalds 				!psf->sf_count[MCAST_INCLUDE];
16311da177e4SLinus Torvalds 		} else
16321da177e4SLinus Torvalds 			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1633ad12583fSDavid L Stevens 		if (new_in) {
1634ad12583fSDavid L Stevens 			if (!psf->sf_oldin) {
163576edc605SAl Viro 				struct ip_sf_list *prev = NULL;
1636ad12583fSDavid L Stevens 
1637ad12583fSDavid L Stevens 				for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1638ad12583fSDavid L Stevens 					if (dpsf->sf_inaddr == psf->sf_inaddr)
1639ad12583fSDavid L Stevens 						break;
1640ad12583fSDavid L Stevens 					prev = dpsf;
1641ad12583fSDavid L Stevens 				}
1642ad12583fSDavid L Stevens 				if (dpsf) {
1643ad12583fSDavid L Stevens 					if (prev)
1644ad12583fSDavid L Stevens 						prev->sf_next = dpsf->sf_next;
1645ad12583fSDavid L Stevens 					else
1646ad12583fSDavid L Stevens 						pmc->tomb = dpsf->sf_next;
1647ad12583fSDavid L Stevens 					kfree(dpsf);
1648ad12583fSDavid L Stevens 				}
16491da177e4SLinus Torvalds 				psf->sf_crcount = qrv;
16501da177e4SLinus Torvalds 				rv++;
16511da177e4SLinus Torvalds 			}
1652ad12583fSDavid L Stevens 		} else if (psf->sf_oldin) {
1653ad12583fSDavid L Stevens 
1654ad12583fSDavid L Stevens 			psf->sf_crcount = 0;
1655ad12583fSDavid L Stevens 			/*
1656ad12583fSDavid L Stevens 			 * add or update "delete" records if an active filter
1657ad12583fSDavid L Stevens 			 * is now inactive
1658ad12583fSDavid L Stevens 			 */
1659ad12583fSDavid L Stevens 			for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1660ad12583fSDavid L Stevens 				if (dpsf->sf_inaddr == psf->sf_inaddr)
1661ad12583fSDavid L Stevens 					break;
1662ad12583fSDavid L Stevens 			if (!dpsf) {
16633ed37a6fSJoe Perches 				dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1664ad12583fSDavid L Stevens 				if (!dpsf)
1665ad12583fSDavid L Stevens 					continue;
1666ad12583fSDavid L Stevens 				*dpsf = *psf;
1667ad12583fSDavid L Stevens 				/* pmc->lock held by callers */
1668ad12583fSDavid L Stevens 				dpsf->sf_next = pmc->tomb;
1669ad12583fSDavid L Stevens 				pmc->tomb = dpsf;
1670ad12583fSDavid L Stevens 			}
1671ad12583fSDavid L Stevens 			dpsf->sf_crcount = qrv;
1672ad12583fSDavid L Stevens 			rv++;
1673ad12583fSDavid L Stevens 		}
16741da177e4SLinus Torvalds 	}
16751da177e4SLinus Torvalds 	return rv;
16761da177e4SLinus Torvalds }
16771da177e4SLinus Torvalds #endif
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds /*
16801da177e4SLinus Torvalds  * Add multicast source filter list to the interface list
16811da177e4SLinus Torvalds  */
16828f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
16838f935bbdSAl Viro 			 int sfcount, __be32 *psfsrc, int delta)
16841da177e4SLinus Torvalds {
16851da177e4SLinus Torvalds 	struct ip_mc_list *pmc;
16861da177e4SLinus Torvalds 	int	isexclude;
16871da177e4SLinus Torvalds 	int	i, err;
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	if (!in_dev)
16901da177e4SLinus Torvalds 		return -ENODEV;
16911d7138deSEric Dumazet 	rcu_read_lock();
16921d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, pmc) {
16931da177e4SLinus Torvalds 		if (*pmca == pmc->multiaddr)
16941da177e4SLinus Torvalds 			break;
16951da177e4SLinus Torvalds 	}
16961da177e4SLinus Torvalds 	if (!pmc) {
16971da177e4SLinus Torvalds 		/* MCA not found?? bug */
16981d7138deSEric Dumazet 		rcu_read_unlock();
16991da177e4SLinus Torvalds 		return -ESRCH;
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 	spin_lock_bh(&pmc->lock);
17021d7138deSEric Dumazet 	rcu_read_unlock();
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
17051da177e4SLinus Torvalds 	sf_markstate(pmc);
17061da177e4SLinus Torvalds #endif
17071da177e4SLinus Torvalds 	isexclude = pmc->sfmode == MCAST_EXCLUDE;
17081da177e4SLinus Torvalds 	if (!delta)
17091da177e4SLinus Torvalds 		pmc->sfcount[sfmode]++;
17101da177e4SLinus Torvalds 	err = 0;
17111da177e4SLinus Torvalds 	for (i=0; i<sfcount; i++) {
17121da177e4SLinus Torvalds 		err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
17131da177e4SLinus Torvalds 		if (err)
17141da177e4SLinus Torvalds 			break;
17151da177e4SLinus Torvalds 	}
17161da177e4SLinus Torvalds 	if (err) {
17171da177e4SLinus Torvalds 		int j;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 		pmc->sfcount[sfmode]--;
17201da177e4SLinus Torvalds 		for (j=0; j<i; j++)
1721*a1889c0dSJulia Lawall 			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
17221da177e4SLinus Torvalds 	} else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
17231da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
17241da177e4SLinus Torvalds 		struct ip_sf_list *psf;
1725cfcabdccSStephen Hemminger 		in_dev = pmc->interface;
17261da177e4SLinus Torvalds #endif
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 		/* filter mode change */
17291da177e4SLinus Torvalds 		if (pmc->sfcount[MCAST_EXCLUDE])
17301da177e4SLinus Torvalds 			pmc->sfmode = MCAST_EXCLUDE;
17311da177e4SLinus Torvalds 		else if (pmc->sfcount[MCAST_INCLUDE])
17321da177e4SLinus Torvalds 			pmc->sfmode = MCAST_INCLUDE;
17331da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
17341da177e4SLinus Torvalds 		/* else no filters; keep old mode for reports */
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 		pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
17371da177e4SLinus Torvalds 			IGMP_Unsolicited_Report_Count;
17381da177e4SLinus Torvalds 		in_dev->mr_ifc_count = pmc->crcount;
17391da177e4SLinus Torvalds 		for (psf=pmc->sources; psf; psf = psf->sf_next)
17401da177e4SLinus Torvalds 			psf->sf_crcount = 0;
17411da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
17421da177e4SLinus Torvalds 	} else if (sf_setstate(pmc)) {
17431da177e4SLinus Torvalds 		igmp_ifc_event(in_dev);
17441da177e4SLinus Torvalds #endif
17451da177e4SLinus Torvalds 	}
17461da177e4SLinus Torvalds 	spin_unlock_bh(&pmc->lock);
17471da177e4SLinus Torvalds 	return err;
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc)
17511da177e4SLinus Torvalds {
17521da177e4SLinus Torvalds 	struct ip_sf_list *psf, *nextpsf;
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	for (psf=pmc->tomb; psf; psf=nextpsf) {
17551da177e4SLinus Torvalds 		nextpsf = psf->sf_next;
17561da177e4SLinus Torvalds 		kfree(psf);
17571da177e4SLinus Torvalds 	}
17581da177e4SLinus Torvalds 	pmc->tomb = NULL;
17591da177e4SLinus Torvalds 	for (psf=pmc->sources; psf; psf=nextpsf) {
17601da177e4SLinus Torvalds 		nextpsf = psf->sf_next;
17611da177e4SLinus Torvalds 		kfree(psf);
17621da177e4SLinus Torvalds 	}
17631da177e4SLinus Torvalds 	pmc->sources = NULL;
17641da177e4SLinus Torvalds 	pmc->sfmode = MCAST_EXCLUDE;
1765de9daad9SDenis Lukianov 	pmc->sfcount[MCAST_INCLUDE] = 0;
17661da177e4SLinus Torvalds 	pmc->sfcount[MCAST_EXCLUDE] = 1;
17671da177e4SLinus Torvalds }
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds /*
17711da177e4SLinus Torvalds  * Join a multicast group
17721da177e4SLinus Torvalds  */
17731da177e4SLinus Torvalds int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
17741da177e4SLinus Torvalds {
17751da177e4SLinus Torvalds 	int err;
17768f935bbdSAl Viro 	__be32 addr = imr->imr_multiaddr.s_addr;
1777ca9b907dSDavid L Stevens 	struct ip_mc_socklist *iml = NULL, *i;
17781da177e4SLinus Torvalds 	struct in_device *in_dev;
17791da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1780877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
1781ca9b907dSDavid L Stevens 	int ifindex;
17821da177e4SLinus Torvalds 	int count = 0;
17831da177e4SLinus Torvalds 
1784f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
17851da177e4SLinus Torvalds 		return -EINVAL;
17861da177e4SLinus Torvalds 
17876756ae4bSStephen Hemminger 	rtnl_lock();
17881da177e4SLinus Torvalds 
1789877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, imr);
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	if (!in_dev) {
17921da177e4SLinus Torvalds 		iml = NULL;
17931da177e4SLinus Torvalds 		err = -ENODEV;
17941da177e4SLinus Torvalds 		goto done;
17951da177e4SLinus Torvalds 	}
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 	err = -EADDRINUSE;
1798ca9b907dSDavid L Stevens 	ifindex = imr->imr_ifindex;
17991d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, i) {
1800ca9b907dSDavid L Stevens 		if (i->multi.imr_multiaddr.s_addr == addr &&
1801ca9b907dSDavid L Stevens 		    i->multi.imr_ifindex == ifindex)
18021da177e4SLinus Torvalds 			goto done;
18031da177e4SLinus Torvalds 		count++;
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 	err = -ENOBUFS;
1806ca9b907dSDavid L Stevens 	if (count >= sysctl_igmp_max_memberships)
18071da177e4SLinus Torvalds 		goto done;
18088b3a7005SKris Katterjohn 	iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1809ca9b907dSDavid L Stevens 	if (iml == NULL)
1810ca9b907dSDavid L Stevens 		goto done;
1811ca9b907dSDavid L Stevens 
18121da177e4SLinus Torvalds 	memcpy(&iml->multi, imr, sizeof(*imr));
18131d7138deSEric Dumazet 	iml->next_rcu = inet->mc_list;
18141da177e4SLinus Torvalds 	iml->sflist = NULL;
18151da177e4SLinus Torvalds 	iml->sfmode = MCAST_EXCLUDE;
1816c85bb41eSFlavio Leitner 	rcu_assign_pointer(inet->mc_list, iml);
18171da177e4SLinus Torvalds 	ip_mc_inc_group(in_dev, addr);
18181da177e4SLinus Torvalds 	err = 0;
18191da177e4SLinus Torvalds done:
18206756ae4bSStephen Hemminger 	rtnl_unlock();
18211da177e4SLinus Torvalds 	return err;
18221da177e4SLinus Torvalds }
18234bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group);
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
18261da177e4SLinus Torvalds 			   struct in_device *in_dev)
18271da177e4SLinus Torvalds {
18281d7138deSEric Dumazet 	struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
18291da177e4SLinus Torvalds 	int err;
18301da177e4SLinus Torvalds 
1831c85bb41eSFlavio Leitner 	if (psf == NULL) {
18321da177e4SLinus Torvalds 		/* any-source empty exclude case */
18331da177e4SLinus Torvalds 		return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
18341da177e4SLinus Torvalds 			iml->sfmode, 0, NULL, 0);
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 	err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1837c85bb41eSFlavio Leitner 			iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1838c85bb41eSFlavio Leitner 	rcu_assign_pointer(iml->sflist, NULL);
1839c85bb41eSFlavio Leitner 	/* decrease mem now to avoid the memleak warning */
1840c85bb41eSFlavio Leitner 	atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
18417519cce4SLai Jiangshan 	kfree_rcu(psf, rcu);
18421da177e4SLinus Torvalds 	return err;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds /*
18461da177e4SLinus Torvalds  *	Ask a socket to leave a group.
18471da177e4SLinus Torvalds  */
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
18501da177e4SLinus Torvalds {
18511da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
18521d7138deSEric Dumazet 	struct ip_mc_socklist *iml;
18531d7138deSEric Dumazet 	struct ip_mc_socklist __rcu **imlp;
185484b42baeSDavid L Stevens 	struct in_device *in_dev;
1855877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
18568f935bbdSAl Viro 	__be32 group = imr->imr_multiaddr.s_addr;
185784b42baeSDavid L Stevens 	u32 ifindex;
1858acd6e00bSDavid L Stevens 	int ret = -EADDRNOTAVAIL;
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	rtnl_lock();
1861877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, imr);
186284b42baeSDavid L Stevens 	ifindex = imr->imr_ifindex;
18631d7138deSEric Dumazet 	for (imlp = &inet->mc_list;
18641d7138deSEric Dumazet 	     (iml = rtnl_dereference(*imlp)) != NULL;
18651d7138deSEric Dumazet 	     imlp = &iml->next_rcu) {
1866acd6e00bSDavid L Stevens 		if (iml->multi.imr_multiaddr.s_addr != group)
1867acd6e00bSDavid L Stevens 			continue;
1868acd6e00bSDavid L Stevens 		if (ifindex) {
1869acd6e00bSDavid L Stevens 			if (iml->multi.imr_ifindex != ifindex)
1870acd6e00bSDavid L Stevens 				continue;
1871acd6e00bSDavid L Stevens 		} else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1872acd6e00bSDavid L Stevens 				iml->multi.imr_address.s_addr)
1873acd6e00bSDavid L Stevens 			continue;
1874acd6e00bSDavid L Stevens 
18751da177e4SLinus Torvalds 		(void) ip_mc_leave_src(sk, iml, in_dev);
18761da177e4SLinus Torvalds 
18771d7138deSEric Dumazet 		*imlp = iml->next_rcu;
18781da177e4SLinus Torvalds 
1879acd6e00bSDavid L Stevens 		if (in_dev)
188084b42baeSDavid L Stevens 			ip_mc_dec_group(in_dev, group);
18811da177e4SLinus Torvalds 		rtnl_unlock();
1882c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
1883c85bb41eSFlavio Leitner 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
188410d50e74SLai Jiangshan 		kfree_rcu(iml, rcu);
18851da177e4SLinus Torvalds 		return 0;
18861da177e4SLinus Torvalds 	}
1887acd6e00bSDavid L Stevens 	if (!in_dev)
1888acd6e00bSDavid L Stevens 		ret = -ENODEV;
18891da177e4SLinus Torvalds 	rtnl_unlock();
1890acd6e00bSDavid L Stevens 	return ret;
18911da177e4SLinus Torvalds }
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct
18941da177e4SLinus Torvalds 	ip_mreq_source *mreqs, int ifindex)
18951da177e4SLinus Torvalds {
18961da177e4SLinus Torvalds 	int err;
18971da177e4SLinus Torvalds 	struct ip_mreqn imr;
189863007727SAl Viro 	__be32 addr = mreqs->imr_multiaddr;
18991da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
19001da177e4SLinus Torvalds 	struct in_device *in_dev = NULL;
19011da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
19021da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
1903877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
19048cdaaa15SDavid L Stevens 	int leavegroup = 0;
19051da177e4SLinus Torvalds 	int i, j, rv;
19061da177e4SLinus Torvalds 
1907f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
19081da177e4SLinus Torvalds 		return -EINVAL;
19091da177e4SLinus Torvalds 
19106756ae4bSStephen Hemminger 	rtnl_lock();
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
19131da177e4SLinus Torvalds 	imr.imr_address.s_addr = mreqs->imr_interface;
19141da177e4SLinus Torvalds 	imr.imr_ifindex = ifindex;
1915877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	if (!in_dev) {
19181da177e4SLinus Torvalds 		err = -ENODEV;
19191da177e4SLinus Torvalds 		goto done;
19201da177e4SLinus Torvalds 	}
19211da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
19221da177e4SLinus Torvalds 
19231d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
1924f64f9e71SJoe Perches 		if ((pmc->multi.imr_multiaddr.s_addr ==
1925f64f9e71SJoe Perches 		     imr.imr_multiaddr.s_addr) &&
1926f64f9e71SJoe Perches 		    (pmc->multi.imr_ifindex == imr.imr_ifindex))
19271da177e4SLinus Torvalds 			break;
19281da177e4SLinus Torvalds 	}
1929917f2f10SDavid L Stevens 	if (!pmc) {		/* must have a prior join */
1930917f2f10SDavid L Stevens 		err = -EINVAL;
19311da177e4SLinus Torvalds 		goto done;
1932917f2f10SDavid L Stevens 	}
19331da177e4SLinus Torvalds 	/* if a source filter was set, must be the same mode as before */
19341da177e4SLinus Torvalds 	if (pmc->sflist) {
1935917f2f10SDavid L Stevens 		if (pmc->sfmode != omode) {
1936917f2f10SDavid L Stevens 			err = -EINVAL;
19371da177e4SLinus Torvalds 			goto done;
1938917f2f10SDavid L Stevens 		}
19391da177e4SLinus Torvalds 	} else if (pmc->sfmode != omode) {
19401da177e4SLinus Torvalds 		/* allow mode switches for empty-set filters */
19411da177e4SLinus Torvalds 		ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
19421da177e4SLinus Torvalds 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
19431da177e4SLinus Torvalds 			NULL, 0);
19441da177e4SLinus Torvalds 		pmc->sfmode = omode;
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 
19471d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
19481da177e4SLinus Torvalds 	if (!add) {
19491da177e4SLinus Torvalds 		if (!psl)
1950917f2f10SDavid L Stevens 			goto done;	/* err = -EADDRNOTAVAIL */
19511da177e4SLinus Torvalds 		rv = !0;
19521da177e4SLinus Torvalds 		for (i=0; i<psl->sl_count; i++) {
19531da177e4SLinus Torvalds 			rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
195463007727SAl Viro 				sizeof(__be32));
19551da177e4SLinus Torvalds 			if (rv == 0)
19561da177e4SLinus Torvalds 				break;
19571da177e4SLinus Torvalds 		}
19581da177e4SLinus Torvalds 		if (rv)		/* source not found */
1959917f2f10SDavid L Stevens 			goto done;	/* err = -EADDRNOTAVAIL */
19601da177e4SLinus Torvalds 
19618cdaaa15SDavid L Stevens 		/* special case - (INCLUDE, empty) == LEAVE_GROUP */
19628cdaaa15SDavid L Stevens 		if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
19638cdaaa15SDavid L Stevens 			leavegroup = 1;
19648cdaaa15SDavid L Stevens 			goto done;
19658cdaaa15SDavid L Stevens 		}
19668cdaaa15SDavid L Stevens 
19671da177e4SLinus Torvalds 		/* update the interface filter */
19681da177e4SLinus Torvalds 		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
19691da177e4SLinus Torvalds 			&mreqs->imr_sourceaddr, 1);
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 		for (j=i+1; j<psl->sl_count; j++)
19721da177e4SLinus Torvalds 			psl->sl_addr[j-1] = psl->sl_addr[j];
19731da177e4SLinus Torvalds 		psl->sl_count--;
19741da177e4SLinus Torvalds 		err = 0;
19751da177e4SLinus Torvalds 		goto done;
19761da177e4SLinus Torvalds 	}
19771da177e4SLinus Torvalds 	/* else, add a new source to the filter */
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
19801da177e4SLinus Torvalds 		err = -ENOBUFS;
19811da177e4SLinus Torvalds 		goto done;
19821da177e4SLinus Torvalds 	}
19831da177e4SLinus Torvalds 	if (!psl || psl->sl_count == psl->sl_max) {
19841da177e4SLinus Torvalds 		struct ip_sf_socklist *newpsl;
19851da177e4SLinus Torvalds 		int count = IP_SFBLOCK;
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 		if (psl)
19881da177e4SLinus Torvalds 			count += psl->sl_max;
19898b3a7005SKris Katterjohn 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
19901da177e4SLinus Torvalds 		if (!newpsl) {
19911da177e4SLinus Torvalds 			err = -ENOBUFS;
19921da177e4SLinus Torvalds 			goto done;
19931da177e4SLinus Torvalds 		}
19941da177e4SLinus Torvalds 		newpsl->sl_max = count;
19951da177e4SLinus Torvalds 		newpsl->sl_count = count - IP_SFBLOCK;
19961da177e4SLinus Torvalds 		if (psl) {
19971da177e4SLinus Torvalds 			for (i=0; i<psl->sl_count; i++)
19981da177e4SLinus Torvalds 				newpsl->sl_addr[i] = psl->sl_addr[i];
1999c85bb41eSFlavio Leitner 			/* decrease mem now to avoid the memleak warning */
2000c85bb41eSFlavio Leitner 			atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
20017519cce4SLai Jiangshan 			kfree_rcu(psl, rcu);
20021da177e4SLinus Torvalds 		}
2003c85bb41eSFlavio Leitner 		rcu_assign_pointer(pmc->sflist, newpsl);
2004c85bb41eSFlavio Leitner 		psl = newpsl;
20051da177e4SLinus Torvalds 	}
20061da177e4SLinus Torvalds 	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
20071da177e4SLinus Torvalds 	for (i=0; i<psl->sl_count; i++) {
20081da177e4SLinus Torvalds 		rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
200963007727SAl Viro 			sizeof(__be32));
20101da177e4SLinus Torvalds 		if (rv == 0)
20111da177e4SLinus Torvalds 			break;
20121da177e4SLinus Torvalds 	}
20131da177e4SLinus Torvalds 	if (rv == 0)		/* address already there is an error */
20141da177e4SLinus Torvalds 		goto done;
20151da177e4SLinus Torvalds 	for (j=psl->sl_count-1; j>=i; j--)
20161da177e4SLinus Torvalds 		psl->sl_addr[j+1] = psl->sl_addr[j];
20171da177e4SLinus Torvalds 	psl->sl_addr[i] = mreqs->imr_sourceaddr;
20181da177e4SLinus Torvalds 	psl->sl_count++;
20191da177e4SLinus Torvalds 	err = 0;
20201da177e4SLinus Torvalds 	/* update the interface list */
20211da177e4SLinus Torvalds 	ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
20221da177e4SLinus Torvalds 		&mreqs->imr_sourceaddr, 1);
20231da177e4SLinus Torvalds done:
20246756ae4bSStephen Hemminger 	rtnl_unlock();
20258cdaaa15SDavid L Stevens 	if (leavegroup)
20268cdaaa15SDavid L Stevens 		return ip_mc_leave_group(sk, &imr);
20271da177e4SLinus Torvalds 	return err;
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
20311da177e4SLinus Torvalds {
20329951f036SDavid L Stevens 	int err = 0;
20331da177e4SLinus Torvalds 	struct ip_mreqn	imr;
203463007727SAl Viro 	__be32 addr = msf->imsf_multiaddr;
20351da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
20361da177e4SLinus Torvalds 	struct in_device *in_dev;
20371da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
20381da177e4SLinus Torvalds 	struct ip_sf_socklist *newpsl, *psl;
2039877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
20409951f036SDavid L Stevens 	int leavegroup = 0;
20411da177e4SLinus Torvalds 
2042f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
20431da177e4SLinus Torvalds 		return -EINVAL;
20441da177e4SLinus Torvalds 	if (msf->imsf_fmode != MCAST_INCLUDE &&
20451da177e4SLinus Torvalds 	    msf->imsf_fmode != MCAST_EXCLUDE)
20461da177e4SLinus Torvalds 		return -EINVAL;
20471da177e4SLinus Torvalds 
20486756ae4bSStephen Hemminger 	rtnl_lock();
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
20511da177e4SLinus Torvalds 	imr.imr_address.s_addr = msf->imsf_interface;
20521da177e4SLinus Torvalds 	imr.imr_ifindex = ifindex;
2053877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	if (!in_dev) {
20561da177e4SLinus Torvalds 		err = -ENODEV;
20571da177e4SLinus Torvalds 		goto done;
20581da177e4SLinus Torvalds 	}
20591da177e4SLinus Torvalds 
20609951f036SDavid L Stevens 	/* special case - (INCLUDE, empty) == LEAVE_GROUP */
20619951f036SDavid L Stevens 	if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
20629951f036SDavid L Stevens 		leavegroup = 1;
20639951f036SDavid L Stevens 		goto done;
20649951f036SDavid L Stevens 	}
20659951f036SDavid L Stevens 
20661d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
20671da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
20681da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
20691da177e4SLinus Torvalds 			break;
20701da177e4SLinus Torvalds 	}
2071917f2f10SDavid L Stevens 	if (!pmc) {		/* must have a prior join */
2072917f2f10SDavid L Stevens 		err = -EINVAL;
20731da177e4SLinus Torvalds 		goto done;
2074917f2f10SDavid L Stevens 	}
20751da177e4SLinus Torvalds 	if (msf->imsf_numsrc) {
20768b3a7005SKris Katterjohn 		newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
20778b3a7005SKris Katterjohn 							   GFP_KERNEL);
20781da177e4SLinus Torvalds 		if (!newpsl) {
20791da177e4SLinus Torvalds 			err = -ENOBUFS;
20801da177e4SLinus Torvalds 			goto done;
20811da177e4SLinus Torvalds 		}
20821da177e4SLinus Torvalds 		newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
20831da177e4SLinus Torvalds 		memcpy(newpsl->sl_addr, msf->imsf_slist,
20841da177e4SLinus Torvalds 			msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
20851da177e4SLinus Torvalds 		err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
20861da177e4SLinus Torvalds 			msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
20871da177e4SLinus Torvalds 		if (err) {
20881da177e4SLinus Torvalds 			sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
20891da177e4SLinus Torvalds 			goto done;
20901da177e4SLinus Torvalds 		}
20918713dbf0SYan Zheng 	} else {
20921da177e4SLinus Torvalds 		newpsl = NULL;
20938713dbf0SYan Zheng 		(void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
20948713dbf0SYan Zheng 				     msf->imsf_fmode, 0, NULL, 0);
20958713dbf0SYan Zheng 	}
20961d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
20971da177e4SLinus Torvalds 	if (psl) {
20981da177e4SLinus Torvalds 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
20991da177e4SLinus Torvalds 			psl->sl_count, psl->sl_addr, 0);
2100c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
2101c85bb41eSFlavio Leitner 		atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
21027519cce4SLai Jiangshan 		kfree_rcu(psl, rcu);
21031da177e4SLinus Torvalds 	} else
21041da177e4SLinus Torvalds 		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
21051da177e4SLinus Torvalds 			0, NULL, 0);
2106c85bb41eSFlavio Leitner 	rcu_assign_pointer(pmc->sflist, newpsl);
21071da177e4SLinus Torvalds 	pmc->sfmode = msf->imsf_fmode;
2108917f2f10SDavid L Stevens 	err = 0;
21091da177e4SLinus Torvalds done:
21106756ae4bSStephen Hemminger 	rtnl_unlock();
21119951f036SDavid L Stevens 	if (leavegroup)
21129951f036SDavid L Stevens 		err = ip_mc_leave_group(sk, &imr);
21131da177e4SLinus Torvalds 	return err;
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
21171da177e4SLinus Torvalds 	struct ip_msfilter __user *optval, int __user *optlen)
21181da177e4SLinus Torvalds {
21191da177e4SLinus Torvalds 	int err, len, count, copycount;
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 *psl;
2126877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
21271da177e4SLinus Torvalds 
2128f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
21291da177e4SLinus Torvalds 		return -EINVAL;
21301da177e4SLinus Torvalds 
21316756ae4bSStephen Hemminger 	rtnl_lock();
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
21341da177e4SLinus Torvalds 	imr.imr_address.s_addr = msf->imsf_interface;
21351da177e4SLinus Torvalds 	imr.imr_ifindex = 0;
2136877acedcSDaniel Lezcano 	in_dev = ip_mc_find_dev(net, &imr);
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	if (!in_dev) {
21391da177e4SLinus Torvalds 		err = -ENODEV;
21401da177e4SLinus Torvalds 		goto done;
21411da177e4SLinus Torvalds 	}
21421da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
21431da177e4SLinus Torvalds 
21441d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
21451da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
21461da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == imr.imr_ifindex)
21471da177e4SLinus Torvalds 			break;
21481da177e4SLinus Torvalds 	}
21491da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
21501da177e4SLinus Torvalds 		goto done;
21511da177e4SLinus Torvalds 	msf->imsf_fmode = pmc->sfmode;
21521d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
21536756ae4bSStephen Hemminger 	rtnl_unlock();
21541da177e4SLinus Torvalds 	if (!psl) {
21551da177e4SLinus Torvalds 		len = 0;
21561da177e4SLinus Torvalds 		count = 0;
21571da177e4SLinus Torvalds 	} else {
21581da177e4SLinus Torvalds 		count = psl->sl_count;
21591da177e4SLinus Torvalds 	}
21601da177e4SLinus Torvalds 	copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
21611da177e4SLinus Torvalds 	len = copycount * sizeof(psl->sl_addr[0]);
21621da177e4SLinus Torvalds 	msf->imsf_numsrc = count;
21631da177e4SLinus Torvalds 	if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
21641da177e4SLinus Torvalds 	    copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
21651da177e4SLinus Torvalds 		return -EFAULT;
21661da177e4SLinus Torvalds 	}
21671da177e4SLinus Torvalds 	if (len &&
21681da177e4SLinus Torvalds 	    copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
21691da177e4SLinus Torvalds 		return -EFAULT;
21701da177e4SLinus Torvalds 	return 0;
21711da177e4SLinus Torvalds done:
21726756ae4bSStephen Hemminger 	rtnl_unlock();
21731da177e4SLinus Torvalds 	return err;
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
21771da177e4SLinus Torvalds 	struct group_filter __user *optval, int __user *optlen)
21781da177e4SLinus Torvalds {
21791da177e4SLinus Torvalds 	int err, i, count, copycount;
21801da177e4SLinus Torvalds 	struct sockaddr_in *psin;
218163007727SAl Viro 	__be32 addr;
21821da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
21831da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
21841da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	psin = (struct sockaddr_in *)&gsf->gf_group;
21871da177e4SLinus Torvalds 	if (psin->sin_family != AF_INET)
21881da177e4SLinus Torvalds 		return -EINVAL;
21891da177e4SLinus Torvalds 	addr = psin->sin_addr.s_addr;
2190f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(addr))
21911da177e4SLinus Torvalds 		return -EINVAL;
21921da177e4SLinus Torvalds 
21936756ae4bSStephen Hemminger 	rtnl_lock();
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
21961da177e4SLinus Torvalds 
21971d7138deSEric Dumazet 	for_each_pmc_rtnl(inet, pmc) {
21981da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == addr &&
21991da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == gsf->gf_interface)
22001da177e4SLinus Torvalds 			break;
22011da177e4SLinus Torvalds 	}
22021da177e4SLinus Torvalds 	if (!pmc)		/* must have a prior join */
22031da177e4SLinus Torvalds 		goto done;
22041da177e4SLinus Torvalds 	gsf->gf_fmode = pmc->sfmode;
22051d7138deSEric Dumazet 	psl = rtnl_dereference(pmc->sflist);
22066756ae4bSStephen Hemminger 	rtnl_unlock();
22071da177e4SLinus Torvalds 	count = psl ? psl->sl_count : 0;
22081da177e4SLinus Torvalds 	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
22091da177e4SLinus Torvalds 	gsf->gf_numsrc = count;
22101da177e4SLinus Torvalds 	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
22111da177e4SLinus Torvalds 	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
22121da177e4SLinus Torvalds 		return -EFAULT;
22131da177e4SLinus Torvalds 	}
22141da177e4SLinus Torvalds 	for (i=0; i<copycount; i++) {
22151da177e4SLinus Torvalds 		struct sockaddr_storage ss;
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds 		psin = (struct sockaddr_in *)&ss;
22181da177e4SLinus Torvalds 		memset(&ss, 0, sizeof(ss));
22191da177e4SLinus Torvalds 		psin->sin_family = AF_INET;
22201da177e4SLinus Torvalds 		psin->sin_addr.s_addr = psl->sl_addr[i];
22211da177e4SLinus Torvalds 		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
22221da177e4SLinus Torvalds 			return -EFAULT;
22231da177e4SLinus Torvalds 	}
22241da177e4SLinus Torvalds 	return 0;
22251da177e4SLinus Torvalds done:
22266756ae4bSStephen Hemminger 	rtnl_unlock();
22271da177e4SLinus Torvalds 	return err;
22281da177e4SLinus Torvalds }
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds /*
22311da177e4SLinus Torvalds  * check if a multicast source filter allows delivery for a given <src,dst,intf>
22321da177e4SLinus Torvalds  */
2233c0cda068SAl Viro int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
22341da177e4SLinus Torvalds {
22351da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
22361da177e4SLinus Torvalds 	struct ip_mc_socklist *pmc;
22371da177e4SLinus Torvalds 	struct ip_sf_socklist *psl;
22381da177e4SLinus Torvalds 	int i;
2239c85bb41eSFlavio Leitner 	int ret;
22401da177e4SLinus Torvalds 
2241c85bb41eSFlavio Leitner 	ret = 1;
2242f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(loc_addr))
2243c85bb41eSFlavio Leitner 		goto out;
22441da177e4SLinus Torvalds 
2245c85bb41eSFlavio Leitner 	rcu_read_lock();
22461d7138deSEric Dumazet 	for_each_pmc_rcu(inet, pmc) {
22471da177e4SLinus Torvalds 		if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
22481da177e4SLinus Torvalds 		    pmc->multi.imr_ifindex == dif)
22491da177e4SLinus Torvalds 			break;
22501da177e4SLinus Torvalds 	}
2251c85bb41eSFlavio Leitner 	ret = inet->mc_all;
22521da177e4SLinus Torvalds 	if (!pmc)
2253c85bb41eSFlavio Leitner 		goto unlock;
22541d7138deSEric Dumazet 	psl = rcu_dereference(pmc->sflist);
2255c85bb41eSFlavio Leitner 	ret = (pmc->sfmode == MCAST_EXCLUDE);
22561da177e4SLinus Torvalds 	if (!psl)
2257c85bb41eSFlavio Leitner 		goto unlock;
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 	for (i=0; i<psl->sl_count; i++) {
22601da177e4SLinus Torvalds 		if (psl->sl_addr[i] == rmt_addr)
22611da177e4SLinus Torvalds 			break;
22621da177e4SLinus Torvalds 	}
2263c85bb41eSFlavio Leitner 	ret = 0;
22641da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2265c85bb41eSFlavio Leitner 		goto unlock;
22661da177e4SLinus Torvalds 	if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2267c85bb41eSFlavio Leitner 		goto unlock;
2268c85bb41eSFlavio Leitner 	ret = 1;
2269c85bb41eSFlavio Leitner unlock:
2270c85bb41eSFlavio Leitner 	rcu_read_unlock();
2271c85bb41eSFlavio Leitner out:
2272c85bb41eSFlavio Leitner 	return ret;
22731da177e4SLinus Torvalds }
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds /*
22761da177e4SLinus Torvalds  *	A socket is closing.
22771da177e4SLinus Torvalds  */
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk)
22801da177e4SLinus Torvalds {
22811da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
22821da177e4SLinus Torvalds 	struct ip_mc_socklist *iml;
2283877acedcSDaniel Lezcano 	struct net *net = sock_net(sk);
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds 	if (inet->mc_list == NULL)
22861da177e4SLinus Torvalds 		return;
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	rtnl_lock();
22891d7138deSEric Dumazet 	while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
22901da177e4SLinus Torvalds 		struct in_device *in_dev;
22911da177e4SLinus Torvalds 
22921d7138deSEric Dumazet 		inet->mc_list = iml->next_rcu;
2293877acedcSDaniel Lezcano 		in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
22941da177e4SLinus Torvalds 		(void) ip_mc_leave_src(sk, iml, in_dev);
229518943d29SEric Dumazet 		if (in_dev != NULL)
22961da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2297c85bb41eSFlavio Leitner 		/* decrease mem now to avoid the memleak warning */
2298c85bb41eSFlavio Leitner 		atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
229910d50e74SLai Jiangshan 		kfree_rcu(iml, rcu);
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 	rtnl_unlock();
23021da177e4SLinus Torvalds }
23031da177e4SLinus Torvalds 
2304dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */
2305dbdd9a52SDavid S. Miller int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
23061da177e4SLinus Torvalds {
23071da177e4SLinus Torvalds 	struct ip_mc_list *im;
23081da177e4SLinus Torvalds 	struct ip_sf_list *psf;
23091da177e4SLinus Torvalds 	int rv = 0;
23101da177e4SLinus Torvalds 
23111d7138deSEric Dumazet 	for_each_pmc_rcu(in_dev, im) {
23121da177e4SLinus Torvalds 		if (im->multiaddr == mc_addr)
23131da177e4SLinus Torvalds 			break;
23141da177e4SLinus Torvalds 	}
23151da177e4SLinus Torvalds 	if (im && proto == IPPROTO_IGMP) {
23161da177e4SLinus Torvalds 		rv = 1;
23171da177e4SLinus Torvalds 	} else if (im) {
23181da177e4SLinus Torvalds 		if (src_addr) {
23191da177e4SLinus Torvalds 			for (psf=im->sources; psf; psf=psf->sf_next) {
23201da177e4SLinus Torvalds 				if (psf->sf_inaddr == src_addr)
23211da177e4SLinus Torvalds 					break;
23221da177e4SLinus Torvalds 			}
23231da177e4SLinus Torvalds 			if (psf)
23241da177e4SLinus Torvalds 				rv = psf->sf_count[MCAST_INCLUDE] ||
23251da177e4SLinus Torvalds 					psf->sf_count[MCAST_EXCLUDE] !=
23261da177e4SLinus Torvalds 					im->sfcount[MCAST_EXCLUDE];
23271da177e4SLinus Torvalds 			else
23281da177e4SLinus Torvalds 				rv = im->sfcount[MCAST_EXCLUDE] != 0;
23291da177e4SLinus Torvalds 		} else
23301da177e4SLinus Torvalds 			rv = 1; /* unspecified source; tentatively allow */
23311da177e4SLinus Torvalds 	}
23321da177e4SLinus Torvalds 	return rv;
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS)
23361da177e4SLinus Torvalds struct igmp_mc_iter_state {
23377091e728SAlexey Dobriyan 	struct seq_net_private p;
23381da177e4SLinus Torvalds 	struct net_device *dev;
23391da177e4SLinus Torvalds 	struct in_device *in_dev;
23401da177e4SLinus Torvalds };
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds #define	igmp_mc_seq_private(seq)	((struct igmp_mc_iter_state *)(seq)->private)
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
23451da177e4SLinus Torvalds {
23467091e728SAlexey Dobriyan 	struct net *net = seq_file_net(seq);
23471da177e4SLinus Torvalds 	struct ip_mc_list *im = NULL;
23481da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
23491da177e4SLinus Torvalds 
23507562f876SPavel Emelianov 	state->in_dev = NULL;
235161fbab77Sstephen hemminger 	for_each_netdev_rcu(net, state->dev) {
23521da177e4SLinus Torvalds 		struct in_device *in_dev;
23536baff150SEric Dumazet 
23546baff150SEric Dumazet 		in_dev = __in_dev_get_rcu(state->dev);
23551da177e4SLinus Torvalds 		if (!in_dev)
23561da177e4SLinus Torvalds 			continue;
23571d7138deSEric Dumazet 		im = rcu_dereference(in_dev->mc_list);
23581da177e4SLinus Torvalds 		if (im) {
23591da177e4SLinus Torvalds 			state->in_dev = in_dev;
23601da177e4SLinus Torvalds 			break;
23611da177e4SLinus Torvalds 		}
23621da177e4SLinus Torvalds 	}
23631da177e4SLinus Torvalds 	return im;
23641da177e4SLinus Torvalds }
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
23671da177e4SLinus Torvalds {
23681da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
23696baff150SEric Dumazet 
23701d7138deSEric Dumazet 	im = rcu_dereference(im->next_rcu);
23711d7138deSEric Dumazet 	while (!im) {
23726baff150SEric Dumazet 		state->dev = next_net_device_rcu(state->dev);
23731da177e4SLinus Torvalds 		if (!state->dev) {
23741da177e4SLinus Torvalds 			state->in_dev = NULL;
23751da177e4SLinus Torvalds 			break;
23761da177e4SLinus Torvalds 		}
23776baff150SEric Dumazet 		state->in_dev = __in_dev_get_rcu(state->dev);
23781da177e4SLinus Torvalds 		if (!state->in_dev)
23791da177e4SLinus Torvalds 			continue;
23801d7138deSEric Dumazet 		im = rcu_dereference(state->in_dev->mc_list);
23811da177e4SLinus Torvalds 	}
23821da177e4SLinus Torvalds 	return im;
23831da177e4SLinus Torvalds }
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
23861da177e4SLinus Torvalds {
23871da177e4SLinus Torvalds 	struct ip_mc_list *im = igmp_mc_get_first(seq);
23881da177e4SLinus Torvalds 	if (im)
23891da177e4SLinus Torvalds 		while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
23901da177e4SLinus Torvalds 			--pos;
23911da177e4SLinus Torvalds 	return pos ? NULL : im;
23921da177e4SLinus Torvalds }
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
239561fbab77Sstephen hemminger 	__acquires(rcu)
23961da177e4SLinus Torvalds {
239761fbab77Sstephen hemminger 	rcu_read_lock();
23981da177e4SLinus Torvalds 	return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
23991da177e4SLinus Torvalds }
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	struct ip_mc_list *im;
24041da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
24051da177e4SLinus Torvalds 		im = igmp_mc_get_first(seq);
24061da177e4SLinus Torvalds 	else
24071da177e4SLinus Torvalds 		im = igmp_mc_get_next(seq, v);
24081da177e4SLinus Torvalds 	++*pos;
24091da177e4SLinus Torvalds 	return im;
24101da177e4SLinus Torvalds }
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
241361fbab77Sstephen hemminger 	__releases(rcu)
24141da177e4SLinus Torvalds {
24151da177e4SLinus Torvalds 	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
24161d7138deSEric Dumazet 
24171da177e4SLinus Torvalds 	state->in_dev = NULL;
24181da177e4SLinus Torvalds 	state->dev = NULL;
241961fbab77Sstephen hemminger 	rcu_read_unlock();
24201da177e4SLinus Torvalds }
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v)
24231da177e4SLinus Torvalds {
24241da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
24251da177e4SLinus Torvalds 		seq_puts(seq,
24261da177e4SLinus Torvalds 			 "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
24271da177e4SLinus Torvalds 	else {
24281da177e4SLinus Torvalds 		struct ip_mc_list *im = (struct ip_mc_list *)v;
24291da177e4SLinus Torvalds 		struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
24301da177e4SLinus Torvalds 		char   *querier;
24311da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
24321da177e4SLinus Torvalds 		querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
24331da177e4SLinus Torvalds 			  IGMP_V2_SEEN(state->in_dev) ? "V2" :
24341da177e4SLinus Torvalds 			  "V3";
24351da177e4SLinus Torvalds #else
24361da177e4SLinus Torvalds 		querier = "NONE";
24371da177e4SLinus Torvalds #endif
24381da177e4SLinus Torvalds 
24391d7138deSEric Dumazet 		if (rcu_dereference(state->in_dev->mc_list) == im) {
24401da177e4SLinus Torvalds 			seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2441b8bae41eSRami Rosen 				   state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
24421da177e4SLinus Torvalds 		}
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 		seq_printf(seq,
2445338fcf98SAlexey Dobriyan 			   "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
24461da177e4SLinus Torvalds 			   im->multiaddr, im->users,
24471da177e4SLinus Torvalds 			   im->tm_running, im->tm_running ?
24481da177e4SLinus Torvalds 			   jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
24491da177e4SLinus Torvalds 			   im->reporter);
24501da177e4SLinus Torvalds 	}
24511da177e4SLinus Torvalds 	return 0;
24521da177e4SLinus Torvalds }
24531da177e4SLinus Torvalds 
2454f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = {
24551da177e4SLinus Torvalds 	.start	=	igmp_mc_seq_start,
24561da177e4SLinus Torvalds 	.next	=	igmp_mc_seq_next,
24571da177e4SLinus Torvalds 	.stop	=	igmp_mc_seq_stop,
24581da177e4SLinus Torvalds 	.show	=	igmp_mc_seq_show,
24591da177e4SLinus Torvalds };
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file)
24621da177e4SLinus Torvalds {
24637091e728SAlexey Dobriyan 	return seq_open_net(inode, file, &igmp_mc_seq_ops,
2464cf7732e4SPavel Emelyanov 			sizeof(struct igmp_mc_iter_state));
24651da177e4SLinus Torvalds }
24661da177e4SLinus Torvalds 
24679a32144eSArjan van de Ven static const struct file_operations igmp_mc_seq_fops = {
24681da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
24691da177e4SLinus Torvalds 	.open		=	igmp_mc_seq_open,
24701da177e4SLinus Torvalds 	.read		=	seq_read,
24711da177e4SLinus Torvalds 	.llseek		=	seq_lseek,
24727091e728SAlexey Dobriyan 	.release	=	seq_release_net,
24731da177e4SLinus Torvalds };
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds struct igmp_mcf_iter_state {
24767091e728SAlexey Dobriyan 	struct seq_net_private p;
24771da177e4SLinus Torvalds 	struct net_device *dev;
24781da177e4SLinus Torvalds 	struct in_device *idev;
24791da177e4SLinus Torvalds 	struct ip_mc_list *im;
24801da177e4SLinus Torvalds };
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq)	((struct igmp_mcf_iter_state *)(seq)->private)
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
24851da177e4SLinus Torvalds {
24867091e728SAlexey Dobriyan 	struct net *net = seq_file_net(seq);
24871da177e4SLinus Torvalds 	struct ip_sf_list *psf = NULL;
24881da177e4SLinus Torvalds 	struct ip_mc_list *im = NULL;
24891da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
24901da177e4SLinus Torvalds 
24917562f876SPavel Emelianov 	state->idev = NULL;
24927562f876SPavel Emelianov 	state->im = NULL;
249361fbab77Sstephen hemminger 	for_each_netdev_rcu(net, state->dev) {
24941da177e4SLinus Torvalds 		struct in_device *idev;
24956baff150SEric Dumazet 		idev = __in_dev_get_rcu(state->dev);
24961da177e4SLinus Torvalds 		if (unlikely(idev == NULL))
24971da177e4SLinus Torvalds 			continue;
24981d7138deSEric Dumazet 		im = rcu_dereference(idev->mc_list);
24991da177e4SLinus Torvalds 		if (likely(im != NULL)) {
25001da177e4SLinus Torvalds 			spin_lock_bh(&im->lock);
25011da177e4SLinus Torvalds 			psf = im->sources;
25021da177e4SLinus Torvalds 			if (likely(psf != NULL)) {
25031da177e4SLinus Torvalds 				state->im = im;
25041da177e4SLinus Torvalds 				state->idev = idev;
25051da177e4SLinus Torvalds 				break;
25061da177e4SLinus Torvalds 			}
25071da177e4SLinus Torvalds 			spin_unlock_bh(&im->lock);
25081da177e4SLinus Torvalds 		}
25091da177e4SLinus Torvalds 	}
25101da177e4SLinus Torvalds 	return psf;
25111da177e4SLinus Torvalds }
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
25141da177e4SLinus Torvalds {
25151da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 	psf = psf->sf_next;
25181da177e4SLinus Torvalds 	while (!psf) {
25191da177e4SLinus Torvalds 		spin_unlock_bh(&state->im->lock);
25201da177e4SLinus Torvalds 		state->im = state->im->next;
25211da177e4SLinus Torvalds 		while (!state->im) {
25226baff150SEric Dumazet 			state->dev = next_net_device_rcu(state->dev);
25231da177e4SLinus Torvalds 			if (!state->dev) {
25241da177e4SLinus Torvalds 				state->idev = NULL;
25251da177e4SLinus Torvalds 				goto out;
25261da177e4SLinus Torvalds 			}
25276baff150SEric Dumazet 			state->idev = __in_dev_get_rcu(state->dev);
25281da177e4SLinus Torvalds 			if (!state->idev)
25291da177e4SLinus Torvalds 				continue;
25301d7138deSEric Dumazet 			state->im = rcu_dereference(state->idev->mc_list);
25311da177e4SLinus Torvalds 		}
25321da177e4SLinus Torvalds 		if (!state->im)
25331da177e4SLinus Torvalds 			break;
25341da177e4SLinus Torvalds 		spin_lock_bh(&state->im->lock);
25351da177e4SLinus Torvalds 		psf = state->im->sources;
25361da177e4SLinus Torvalds 	}
25371da177e4SLinus Torvalds out:
25381da177e4SLinus Torvalds 	return psf;
25391da177e4SLinus Torvalds }
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
25421da177e4SLinus Torvalds {
25431da177e4SLinus Torvalds 	struct ip_sf_list *psf = igmp_mcf_get_first(seq);
25441da177e4SLinus Torvalds 	if (psf)
25451da177e4SLinus Torvalds 		while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
25461da177e4SLinus Torvalds 			--pos;
25471da177e4SLinus Torvalds 	return pos ? NULL : psf;
25481da177e4SLinus Torvalds }
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
255161fbab77Sstephen hemminger 	__acquires(rcu)
25521da177e4SLinus Torvalds {
255361fbab77Sstephen hemminger 	rcu_read_lock();
25541da177e4SLinus Torvalds 	return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
25581da177e4SLinus Torvalds {
25591da177e4SLinus Torvalds 	struct ip_sf_list *psf;
25601da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
25611da177e4SLinus Torvalds 		psf = igmp_mcf_get_first(seq);
25621da177e4SLinus Torvalds 	else
25631da177e4SLinus Torvalds 		psf = igmp_mcf_get_next(seq, v);
25641da177e4SLinus Torvalds 	++*pos;
25651da177e4SLinus Torvalds 	return psf;
25661da177e4SLinus Torvalds }
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
256961fbab77Sstephen hemminger 	__releases(rcu)
25701da177e4SLinus Torvalds {
25711da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
25721da177e4SLinus Torvalds 	if (likely(state->im != NULL)) {
25731da177e4SLinus Torvalds 		spin_unlock_bh(&state->im->lock);
25741da177e4SLinus Torvalds 		state->im = NULL;
25751da177e4SLinus Torvalds 	}
25761da177e4SLinus Torvalds 	state->idev = NULL;
25771da177e4SLinus Torvalds 	state->dev = NULL;
257861fbab77Sstephen hemminger 	rcu_read_unlock();
25791da177e4SLinus Torvalds }
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
25821da177e4SLinus Torvalds {
25831da177e4SLinus Torvalds 	struct ip_sf_list *psf = (struct ip_sf_list *)v;
25841da177e4SLinus Torvalds 	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
25871da177e4SLinus Torvalds 		seq_printf(seq,
25881da177e4SLinus Torvalds 			   "%3s %6s "
25891da177e4SLinus Torvalds 			   "%10s %10s %6s %6s\n", "Idx",
25901da177e4SLinus Torvalds 			   "Device", "MCA",
25911da177e4SLinus Torvalds 			   "SRC", "INC", "EXC");
25921da177e4SLinus Torvalds 	} else {
25931da177e4SLinus Torvalds 		seq_printf(seq,
25941da177e4SLinus Torvalds 			   "%3d %6.6s 0x%08x "
25951da177e4SLinus Torvalds 			   "0x%08x %6lu %6lu\n",
25961da177e4SLinus Torvalds 			   state->dev->ifindex, state->dev->name,
25971da177e4SLinus Torvalds 			   ntohl(state->im->multiaddr),
25981da177e4SLinus Torvalds 			   ntohl(psf->sf_inaddr),
25991da177e4SLinus Torvalds 			   psf->sf_count[MCAST_INCLUDE],
26001da177e4SLinus Torvalds 			   psf->sf_count[MCAST_EXCLUDE]);
26011da177e4SLinus Torvalds 	}
26021da177e4SLinus Torvalds 	return 0;
26031da177e4SLinus Torvalds }
26041da177e4SLinus Torvalds 
2605f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = {
26061da177e4SLinus Torvalds 	.start	=	igmp_mcf_seq_start,
26071da177e4SLinus Torvalds 	.next	=	igmp_mcf_seq_next,
26081da177e4SLinus Torvalds 	.stop	=	igmp_mcf_seq_stop,
26091da177e4SLinus Torvalds 	.show	=	igmp_mcf_seq_show,
26101da177e4SLinus Torvalds };
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
26131da177e4SLinus Torvalds {
26147091e728SAlexey Dobriyan 	return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2615cf7732e4SPavel Emelyanov 			sizeof(struct igmp_mcf_iter_state));
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds 
26189a32144eSArjan van de Ven static const struct file_operations igmp_mcf_seq_fops = {
26191da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
26201da177e4SLinus Torvalds 	.open		=	igmp_mcf_seq_open,
26211da177e4SLinus Torvalds 	.read		=	seq_read,
26221da177e4SLinus Torvalds 	.llseek		=	seq_lseek,
26237091e728SAlexey Dobriyan 	.release	=	seq_release_net,
26247091e728SAlexey Dobriyan };
26257091e728SAlexey Dobriyan 
26262c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net)
26277091e728SAlexey Dobriyan {
26287091e728SAlexey Dobriyan 	struct proc_dir_entry *pde;
26297091e728SAlexey Dobriyan 
26307091e728SAlexey Dobriyan 	pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
26317091e728SAlexey Dobriyan 	if (!pde)
26327091e728SAlexey Dobriyan 		goto out_igmp;
26337091e728SAlexey Dobriyan 	pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
26347091e728SAlexey Dobriyan 	if (!pde)
26357091e728SAlexey Dobriyan 		goto out_mcfilter;
26367091e728SAlexey Dobriyan 	return 0;
26377091e728SAlexey Dobriyan 
26387091e728SAlexey Dobriyan out_mcfilter:
26397091e728SAlexey Dobriyan 	proc_net_remove(net, "igmp");
26407091e728SAlexey Dobriyan out_igmp:
26417091e728SAlexey Dobriyan 	return -ENOMEM;
26427091e728SAlexey Dobriyan }
26437091e728SAlexey Dobriyan 
26442c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net)
26457091e728SAlexey Dobriyan {
26467091e728SAlexey Dobriyan 	proc_net_remove(net, "mcfilter");
26477091e728SAlexey Dobriyan 	proc_net_remove(net, "igmp");
26487091e728SAlexey Dobriyan }
26497091e728SAlexey Dobriyan 
26507091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = {
26517091e728SAlexey Dobriyan 	.init = igmp_net_init,
26527091e728SAlexey Dobriyan 	.exit = igmp_net_exit,
26531da177e4SLinus Torvalds };
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds int __init igmp_mc_proc_init(void)
26561da177e4SLinus Torvalds {
26577091e728SAlexey Dobriyan 	return register_pernet_subsys(&igmp_net_ops);
26581da177e4SLinus Torvalds }
26591da177e4SLinus Torvalds #endif
2660