Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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