12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Linux NET3: Internet Group Management Protocol [IGMP]
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * This code implements the IGMP protocol as defined in RFC1112. There has
61da177e4SLinus Torvalds * been a further revision of this protocol since which is now supported.
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * If you have trouble with this module be careful what gcc you have used,
91da177e4SLinus Torvalds * the older version didn't come out right using gcc 2.5.8, the newer one
101da177e4SLinus Torvalds * seems to fall out with gcc 2.6.2.
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * Authors:
13113aa838SAlan Cox * Alan Cox <alan@lxorguk.ukuu.org.uk>
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * Fixes:
161da177e4SLinus Torvalds *
171da177e4SLinus Torvalds * Alan Cox : Added lots of __inline__ to optimise
181da177e4SLinus Torvalds * the memory usage of all the tiny little
191da177e4SLinus Torvalds * functions.
201da177e4SLinus Torvalds * Alan Cox : Dumped the header building experiment.
211da177e4SLinus Torvalds * Alan Cox : Minor tweaks ready for multicast routing
221da177e4SLinus Torvalds * and extended IGMP protocol.
231da177e4SLinus Torvalds * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
241da177e4SLinus Torvalds * writes utterly bogus code otherwise (sigh)
251da177e4SLinus Torvalds * fixed IGMP loopback to behave in the manner
261da177e4SLinus Torvalds * desired by mrouted, fixed the fact it has been
271da177e4SLinus Torvalds * broken since 1.3.6 and cleaned up a few minor
281da177e4SLinus Torvalds * points.
291da177e4SLinus Torvalds *
301da177e4SLinus Torvalds * Chih-Jen Chang : Tried to revise IGMP to Version 2
311da177e4SLinus Torvalds * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
321da177e4SLinus Torvalds * The enhancements are mainly based on Steve Deering's
331da177e4SLinus Torvalds * ipmulti-3.5 source code.
341da177e4SLinus Torvalds * Chih-Jen Chang : Added the igmp_get_mrouter_info and
351da177e4SLinus Torvalds * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
361da177e4SLinus Torvalds * the mrouted version on that device.
371da177e4SLinus Torvalds * Chih-Jen Chang : Added the max_resp_time parameter to
381da177e4SLinus Torvalds * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
391da177e4SLinus Torvalds * to identify the multicast router version
401da177e4SLinus Torvalds * and do what the IGMP version 2 specified.
411da177e4SLinus Torvalds * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
421da177e4SLinus Torvalds * Tsu-Sheng Tsao if the specified time expired.
431da177e4SLinus Torvalds * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
441da177e4SLinus Torvalds * Alan Cox : Use GFP_ATOMIC in the right places.
451da177e4SLinus Torvalds * Christian Daudt : igmp timer wasn't set for local group
461da177e4SLinus Torvalds * memberships but was being deleted,
471da177e4SLinus Torvalds * which caused a "del_timer() called
481da177e4SLinus Torvalds * from %p with timer not initialized\n"
491da177e4SLinus Torvalds * message (960131).
501da177e4SLinus Torvalds * Christian Daudt : removed del_timer from
511da177e4SLinus Torvalds * igmp_timer_expire function (960205).
521da177e4SLinus Torvalds * Christian Daudt : igmp_heard_report now only calls
531da177e4SLinus Torvalds * igmp_timer_expire if tm->running is
541da177e4SLinus Torvalds * true (960216).
551da177e4SLinus Torvalds * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
561da177e4SLinus Torvalds * igmp_heard_query never trigger. Expiry
571da177e4SLinus Torvalds * miscalculation fixed in igmp_heard_query
581da177e4SLinus Torvalds * and random() made to return unsigned to
591da177e4SLinus Torvalds * prevent negative expiry times.
601da177e4SLinus Torvalds * Alexey Kuznetsov: Wrong group leaving behaviour, backport
611da177e4SLinus Torvalds * fix from pending 2.1.x patches.
621da177e4SLinus Torvalds * Alan Cox: Forget to enable FDDI support earlier.
631da177e4SLinus Torvalds * Alexey Kuznetsov: Fixed leaving groups on device down.
641da177e4SLinus Torvalds * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
651da177e4SLinus Torvalds * David L Stevens: IGMPv3 support, with help from
661da177e4SLinus Torvalds * Vinay Kulkarni
671da177e4SLinus Torvalds */
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds #include <linux/module.h>
705a0e3ad6STejun Heo #include <linux/slab.h>
717c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
721da177e4SLinus Torvalds #include <linux/types.h>
731da177e4SLinus Torvalds #include <linux/kernel.h>
741da177e4SLinus Torvalds #include <linux/jiffies.h>
751da177e4SLinus Torvalds #include <linux/string.h>
761da177e4SLinus Torvalds #include <linux/socket.h>
771da177e4SLinus Torvalds #include <linux/sockios.h>
781da177e4SLinus Torvalds #include <linux/in.h>
791da177e4SLinus Torvalds #include <linux/inet.h>
801da177e4SLinus Torvalds #include <linux/netdevice.h>
811da177e4SLinus Torvalds #include <linux/skbuff.h>
821da177e4SLinus Torvalds #include <linux/inetdevice.h>
831da177e4SLinus Torvalds #include <linux/igmp.h>
841da177e4SLinus Torvalds #include <linux/if_arp.h>
851da177e4SLinus Torvalds #include <linux/rtnetlink.h>
861da177e4SLinus Torvalds #include <linux/times.h>
879d4a0314SHannes Frederic Sowa #include <linux/pkt_sched.h>
88a46182b0SKevin Cernekee #include <linux/byteorder/generic.h>
8914c85021SArnaldo Carvalho de Melo
90457c4cbcSEric W. Biederman #include <net/net_namespace.h>
9114c85021SArnaldo Carvalho de Melo #include <net/arp.h>
921da177e4SLinus Torvalds #include <net/ip.h>
931da177e4SLinus Torvalds #include <net/protocol.h>
941da177e4SLinus Torvalds #include <net/route.h>
951da177e4SLinus Torvalds #include <net/sock.h>
961da177e4SLinus Torvalds #include <net/checksum.h>
9793a714d6SMadhu Challa #include <net/inet_common.h>
981da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
991da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1001da177e4SLinus Torvalds #include <linux/mroute.h>
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
1031da177e4SLinus Torvalds #include <linux/proc_fs.h>
1041da177e4SLinus Torvalds #include <linux/seq_file.h>
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds
1071da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1081da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */
1091da177e4SLinus Torvalds
110966c37f2SHangbin Liu #define IGMP_QUERY_INTERVAL (125*HZ)
111436f7c20SFabian Frederick #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
1121da177e4SLinus Torvalds
113436f7c20SFabian Frederick #define IGMP_INITIAL_REPORT_DELAY (1)
1141da177e4SLinus Torvalds
115436f7c20SFabian Frederick /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
1161da177e4SLinus Torvalds * IGMP specs require to report membership immediately after
1171da177e4SLinus Torvalds * joining a group, but we delay the first report by a
1181da177e4SLinus Torvalds * small interval. It seems more natural and still does not
1191da177e4SLinus Torvalds * contradict to specs provided this delay is small enough.
1201da177e4SLinus Torvalds */
1211da177e4SLinus Torvalds
12242f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \
123c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
12442f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
1251da177e4SLinus Torvalds ((in_dev)->mr_v1_seen && \
1261da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v1_seen)))
12742f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \
128c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
12942f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
1301da177e4SLinus Torvalds ((in_dev)->mr_v2_seen && \
1311da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v2_seen)))
1321da177e4SLinus Torvalds
unsolicited_report_interval(struct in_device * in_dev)133cab70040SWilliam Manley static int unsolicited_report_interval(struct in_device *in_dev)
134cab70040SWilliam Manley {
1352690048cSWilliam Manley int interval_ms, interval_jiffies;
1362690048cSWilliam Manley
137cab70040SWilliam Manley if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
1382690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET(
1392690048cSWilliam Manley in_dev,
1402690048cSWilliam Manley IGMPV2_UNSOLICITED_REPORT_INTERVAL);
141cab70040SWilliam Manley else /* v3 */
1422690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET(
1432690048cSWilliam Manley in_dev,
1442690048cSWilliam Manley IGMPV3_UNSOLICITED_REPORT_INTERVAL);
1452690048cSWilliam Manley
1462690048cSWilliam Manley interval_jiffies = msecs_to_jiffies(interval_ms);
1472690048cSWilliam Manley
1482690048cSWilliam Manley /* _timer functions can't handle a delay of 0 jiffies so ensure
1492690048cSWilliam Manley * we always return a positive value.
1502690048cSWilliam Manley */
1512690048cSWilliam Manley if (interval_jiffies <= 0)
1522690048cSWilliam Manley interval_jiffies = 1;
1532690048cSWilliam Manley return interval_jiffies;
154cab70040SWilliam Manley }
155cab70040SWilliam Manley
1569fb20801SFlorian Fainelli static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
1579fb20801SFlorian Fainelli gfp_t gfp);
15824803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
1591da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev);
1601da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc);
1611da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc);
1621da177e4SLinus Torvalds #endif
1631da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc);
1648f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1658f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta);
1661da177e4SLinus Torvalds
ip_ma_put(struct ip_mc_list * im)1671da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im)
1681da177e4SLinus Torvalds {
1698851ab52SReshetova, Elena if (refcount_dec_and_test(&im->refcnt)) {
1701da177e4SLinus Torvalds in_dev_put(im->interface);
17142ea299dSLai Jiangshan kfree_rcu(im, rcu);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds
175d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc) \
176d9aa9380SDavid S. Miller for (pmc = rcu_dereference(in_dev->mc_list); \
177d9aa9380SDavid S. Miller pmc != NULL; \
178d9aa9380SDavid S. Miller pmc = rcu_dereference(pmc->next_rcu))
179d9aa9380SDavid S. Miller
180d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc) \
181d9aa9380SDavid S. Miller for (pmc = rtnl_dereference(in_dev->mc_list); \
182d9aa9380SDavid S. Miller pmc != NULL; \
183d9aa9380SDavid S. Miller pmc = rtnl_dereference(pmc->next_rcu))
184d9aa9380SDavid S. Miller
ip_sf_list_clear_all(struct ip_sf_list * psf)185903869bdSEric Dumazet static void ip_sf_list_clear_all(struct ip_sf_list *psf)
186903869bdSEric Dumazet {
187903869bdSEric Dumazet struct ip_sf_list *next;
188903869bdSEric Dumazet
189903869bdSEric Dumazet while (psf) {
190903869bdSEric Dumazet next = psf->sf_next;
191903869bdSEric Dumazet kfree(psf);
192903869bdSEric Dumazet psf = next;
193903869bdSEric Dumazet }
194903869bdSEric Dumazet }
195903869bdSEric Dumazet
1961da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds /*
1991da177e4SLinus Torvalds * Timer management
2001da177e4SLinus Torvalds */
2011da177e4SLinus Torvalds
igmp_stop_timer(struct ip_mc_list * im)2021d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds spin_lock_bh(&im->lock);
2051da177e4SLinus Torvalds if (del_timer(&im->timer))
2068851ab52SReshetova, Elena refcount_dec(&im->refcnt);
2071da177e4SLinus Torvalds im->tm_running = 0;
2081da177e4SLinus Torvalds im->reporter = 0;
2091da177e4SLinus Torvalds im->unsolicit_count = 0;
2101da177e4SLinus Torvalds spin_unlock_bh(&im->lock);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds
2131da177e4SLinus Torvalds /* It must be called with locked im->lock */
igmp_start_timer(struct ip_mc_list * im,int max_delay)2141da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
2151da177e4SLinus Torvalds {
2168032bf12SJason A. Donenfeld int tv = get_random_u32_below(max_delay);
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds im->tm_running = 1;
219*bf8601daSZhengchao Shao if (refcount_inc_not_zero(&im->refcnt)) {
220*bf8601daSZhengchao Shao if (mod_timer(&im->timer, jiffies + tv + 2))
221*bf8601daSZhengchao Shao ip_ma_put(im);
222*bf8601daSZhengchao Shao }
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds
igmp_gq_start_timer(struct in_device * in_dev)2251da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev)
2261da177e4SLinus Torvalds {
2278032bf12SJason A. Donenfeld int tv = get_random_u32_below(in_dev->mr_maxdelay);
2287ababb78SMichal Tesar unsigned long exp = jiffies + tv + 2;
2297ababb78SMichal Tesar
2307ababb78SMichal Tesar if (in_dev->mr_gq_running &&
2317ababb78SMichal Tesar time_after_eq(exp, (in_dev->mr_gq_timer).expires))
2327ababb78SMichal Tesar return;
2331da177e4SLinus Torvalds
2341da177e4SLinus Torvalds in_dev->mr_gq_running = 1;
2357ababb78SMichal Tesar if (!mod_timer(&in_dev->mr_gq_timer, exp))
2361da177e4SLinus Torvalds in_dev_hold(in_dev);
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds
igmp_ifc_start_timer(struct in_device * in_dev,int delay)2391da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
2401da177e4SLinus Torvalds {
2418032bf12SJason A. Donenfeld int tv = get_random_u32_below(delay);
2421da177e4SLinus Torvalds
2431da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
2441da177e4SLinus Torvalds in_dev_hold(in_dev);
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds
igmp_mod_timer(struct ip_mc_list * im,int max_delay)2471da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds spin_lock_bh(&im->lock);
2501da177e4SLinus Torvalds im->unsolicit_count = 0;
2511da177e4SLinus Torvalds if (del_timer(&im->timer)) {
2521da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) {
2531da177e4SLinus Torvalds add_timer(&im->timer);
2541da177e4SLinus Torvalds im->tm_running = 1;
2551da177e4SLinus Torvalds spin_unlock_bh(&im->lock);
2561da177e4SLinus Torvalds return;
2571da177e4SLinus Torvalds }
2588851ab52SReshetova, Elena refcount_dec(&im->refcnt);
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds igmp_start_timer(im, max_delay);
2611da177e4SLinus Torvalds spin_unlock_bh(&im->lock);
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds
2641da177e4SLinus Torvalds
2651da177e4SLinus Torvalds /*
2661da177e4SLinus Torvalds * Send an IGMP report.
2671da177e4SLinus Torvalds */
2681da177e4SLinus Torvalds
2691da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
2701da177e4SLinus Torvalds
2711da177e4SLinus Torvalds
is_in(struct ip_mc_list * pmc,struct ip_sf_list * psf,int type,int gdeleted,int sdeleted)2721da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
2731da177e4SLinus Torvalds int gdeleted, int sdeleted)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds switch (type) {
2761da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE:
2771da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE:
2781da177e4SLinus Torvalds if (gdeleted || sdeleted)
2791da177e4SLinus Torvalds return 0;
280ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) {
281ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE)
282ad12583fSDavid L Stevens return 1;
283ad12583fSDavid L Stevens /* don't include if this source is excluded
284ad12583fSDavid L Stevens * in all filters
285ad12583fSDavid L Stevens */
286ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE])
287ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE;
288ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] ==
289ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE];
290ad12583fSDavid L Stevens }
291ad12583fSDavid L Stevens return 0;
2921da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE:
2931da177e4SLinus Torvalds if (gdeleted || sdeleted)
2941da177e4SLinus Torvalds return 0;
2951da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0;
2961da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE:
2971da177e4SLinus Torvalds if (gdeleted || sdeleted)
2981da177e4SLinus Torvalds return 0;
2991da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
3001da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE])
3011da177e4SLinus Torvalds return 0;
3021da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] ==
3031da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE];
3041da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES:
3051da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount)
3061da177e4SLinus Torvalds return 0;
3071da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
3081da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES:
3091da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE)
3101da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted);
3111da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds return 0;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds
3161da177e4SLinus Torvalds static int
igmp_scount(struct ip_mc_list * pmc,int type,int gdeleted,int sdeleted)3171da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds struct ip_sf_list *psf;
3201da177e4SLinus Torvalds int scount = 0;
3211da177e4SLinus Torvalds
3221da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) {
3231da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted))
3241da177e4SLinus Torvalds continue;
3251da177e4SLinus Torvalds scount++;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds return scount;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds
330a46182b0SKevin Cernekee /* source address selection per RFC 3376 section 4.2.13 */
igmpv3_get_srcaddr(struct net_device * dev,const struct flowi4 * fl4)331a46182b0SKevin Cernekee static __be32 igmpv3_get_srcaddr(struct net_device *dev,
332a46182b0SKevin Cernekee const struct flowi4 *fl4)
333a46182b0SKevin Cernekee {
334a46182b0SKevin Cernekee struct in_device *in_dev = __in_dev_get_rcu(dev);
335cd5a411dSFlorian Westphal const struct in_ifaddr *ifa;
336a46182b0SKevin Cernekee
337a46182b0SKevin Cernekee if (!in_dev)
338a46182b0SKevin Cernekee return htonl(INADDR_ANY);
339a46182b0SKevin Cernekee
340cd5a411dSFlorian Westphal in_dev_for_each_ifa_rcu(ifa, in_dev) {
341ad23b750SFelix Fietkau if (fl4->saddr == ifa->ifa_local)
342a46182b0SKevin Cernekee return fl4->saddr;
343cd5a411dSFlorian Westphal }
344a46182b0SKevin Cernekee
345a46182b0SKevin Cernekee return htonl(INADDR_ANY);
346a46182b0SKevin Cernekee }
347a46182b0SKevin Cernekee
igmpv3_newpack(struct net_device * dev,unsigned int mtu)3484c672e4bSDaniel Borkmann static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds struct sk_buff *skb;
3511da177e4SLinus Torvalds struct rtable *rt;
3521da177e4SLinus Torvalds struct iphdr *pip;
3531da177e4SLinus Torvalds struct igmpv3_report *pig;
354877acedcSDaniel Lezcano struct net *net = dev_net(dev);
35531e4543dSDavid S. Miller struct flowi4 fl4;
35666088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev);
35766088243SHerbert Xu int tlen = dev->needed_tailroom;
358c3b704d4SEric Dumazet unsigned int size;
3591da177e4SLinus Torvalds
360c3b704d4SEric Dumazet size = min(mtu, IP_MAX_MTU);
36157e1ab6eSEric Dumazet while (1) {
36266088243SHerbert Xu skb = alloc_skb(size + hlen + tlen,
36357e1ab6eSEric Dumazet GFP_ATOMIC | __GFP_NOWARN);
36457e1ab6eSEric Dumazet if (skb)
36557e1ab6eSEric Dumazet break;
36657e1ab6eSEric Dumazet size >>= 1;
36757e1ab6eSEric Dumazet if (size < 256)
3681da177e4SLinus Torvalds return NULL;
36957e1ab6eSEric Dumazet }
3709d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL;
3711da177e4SLinus Torvalds
37231e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
37378fbfd8aSDavid S. Miller 0, 0,
37478fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex);
375b23dd4feSDavid S. Miller if (IS_ERR(rt)) {
3761da177e4SLinus Torvalds kfree_skb(skb);
3771da177e4SLinus Torvalds return NULL;
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds
380d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst);
3811da177e4SLinus Torvalds skb->dev = dev;
3821da177e4SLinus Torvalds
38366088243SHerbert Xu skb_reserve(skb, hlen);
3841837b2e2SBenjamin Poirier skb_tailroom_reserve(skb, mtu, tlen);
3851da177e4SLinus Torvalds
3867e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb);
387eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb);
3887e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4);
3891da177e4SLinus Torvalds
3901da177e4SLinus Torvalds pip->version = 4;
3911da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2;
3921da177e4SLinus Torvalds pip->tos = 0xc0;
3931da177e4SLinus Torvalds pip->frag_off = htons(IP_DF);
3941da177e4SLinus Torvalds pip->ttl = 1;
395492f64ceSDavid S. Miller pip->daddr = fl4.daddr;
396e7aadb27SEric Dumazet
397e7aadb27SEric Dumazet rcu_read_lock();
398a46182b0SKevin Cernekee pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
399e7aadb27SEric Dumazet rcu_read_unlock();
400e7aadb27SEric Dumazet
4011da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP;
4021da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */
403b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL);
4041da177e4SLinus Torvalds ((u8 *)&pip[1])[0] = IPOPT_RA;
4051da177e4SLinus Torvalds ((u8 *)&pip[1])[1] = 4;
4061da177e4SLinus Torvalds ((u8 *)&pip[1])[2] = 0;
4071da177e4SLinus Torvalds ((u8 *)&pip[1])[3] = 0;
4081da177e4SLinus Torvalds
409b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
410d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig));
411d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb);
4121da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
4131da177e4SLinus Torvalds pig->resv1 = 0;
4141da177e4SLinus Torvalds pig->csum = 0;
4151da177e4SLinus Torvalds pig->resv2 = 0;
4161da177e4SLinus Torvalds pig->ngrec = 0;
4171da177e4SLinus Torvalds return skb;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds
igmpv3_sendpack(struct sk_buff * skb)4201da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb)
4211da177e4SLinus Torvalds {
422d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb);
423f7c0c2aeSSimon Horman const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
4241da177e4SLinus Torvalds
425d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
4261da177e4SLinus Torvalds
42733224b16SEric W. Biederman return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds
grec_size(struct ip_mc_list * pmc,int type,int gdel,int sdel)4301da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds
add_grhead(struct sk_buff * skb,struct ip_mc_list * pmc,int type,struct igmpv3_grec ** ppgr,unsigned int mtu)4351da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
436b5476022SEric Dumazet int type, struct igmpv3_grec **ppgr, unsigned int mtu)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev;
4391da177e4SLinus Torvalds struct igmpv3_report *pih;
4401da177e4SLinus Torvalds struct igmpv3_grec *pgr;
4411da177e4SLinus Torvalds
442b5476022SEric Dumazet if (!skb) {
443b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu);
4441da177e4SLinus Torvalds if (!skb)
4451da177e4SLinus Torvalds return NULL;
446b5476022SEric Dumazet }
4474df864c1SJohannes Berg pgr = skb_put(skb, sizeof(struct igmpv3_grec));
4481da177e4SLinus Torvalds pgr->grec_type = type;
4491da177e4SLinus Torvalds pgr->grec_auxwords = 0;
4501da177e4SLinus Torvalds pgr->grec_nsrcs = 0;
4511da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr;
452d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb);
4531da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1);
4541da177e4SLinus Torvalds *ppgr = pgr;
4551da177e4SLinus Torvalds return skb;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds
4584c672e4bSDaniel Borkmann #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
4591da177e4SLinus Torvalds
add_grec(struct sk_buff * skb,struct ip_mc_list * pmc,int type,int gdeleted,int sdeleted)4601da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
4611da177e4SLinus Torvalds int type, int gdeleted, int sdeleted)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev;
46487a8a2aeSNikolay Borisov struct net *net = dev_net(dev);
4651da177e4SLinus Torvalds struct igmpv3_report *pih;
4661da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL;
4671da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
468ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate;
469b5476022SEric Dumazet unsigned int mtu;
4701da177e4SLinus Torvalds
4711da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS)
4721da177e4SLinus Torvalds return skb;
473f6da2267SKuniyuki Iwashima if (ipv4_is_local_multicast(pmc->multiaddr) &&
474f6da2267SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
475df2cf4a7SPhilip Downey return skb;
4761da177e4SLinus Torvalds
477b5476022SEric Dumazet mtu = READ_ONCE(dev->mtu);
478b5476022SEric Dumazet if (mtu < IPV4_MIN_MTU)
479b5476022SEric Dumazet return skb;
480b5476022SEric Dumazet
4811da177e4SLinus Torvalds isquery = type == IGMPV3_MODE_IS_INCLUDE ||
4821da177e4SLinus Torvalds type == IGMPV3_MODE_IS_EXCLUDE;
4831da177e4SLinus Torvalds truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
4841da177e4SLinus Torvalds type == IGMPV3_CHANGE_TO_EXCLUDE;
4851da177e4SLinus Torvalds
486ad12583fSDavid L Stevens stotal = scount = 0;
487ad12583fSDavid L Stevens
4881da177e4SLinus Torvalds psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
4891da177e4SLinus Torvalds
490ad12583fSDavid L Stevens if (!*psf_list)
491ad12583fSDavid L Stevens goto empty_source;
492ad12583fSDavid L Stevens
493d9edf9e2SArnaldo Carvalho de Melo pih = skb ? igmpv3_report_hdr(skb) : NULL;
4941da177e4SLinus Torvalds
4951da177e4SLinus Torvalds /* EX and TO_EX get a fresh packet, if needed */
4961da177e4SLinus Torvalds if (truncate) {
4971da177e4SLinus Torvalds if (pih && pih->ngrec &&
4981da177e4SLinus Torvalds AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
4991da177e4SLinus Torvalds if (skb)
5001da177e4SLinus Torvalds igmpv3_sendpack(skb);
501b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu);
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds first = 1;
5051da177e4SLinus Torvalds psf_prev = NULL;
5061da177e4SLinus Torvalds for (psf = *psf_list; psf; psf = psf_next) {
507ea4d9e72SAl Viro __be32 *psrc;
5081da177e4SLinus Torvalds
5091da177e4SLinus Torvalds psf_next = psf->sf_next;
5101da177e4SLinus Torvalds
5111da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
5121da177e4SLinus Torvalds psf_prev = psf;
5131da177e4SLinus Torvalds continue;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds
516a052517aSHangbin Liu /* Based on RFC3376 5.1. Should not send source-list change
517a052517aSHangbin Liu * records when there is a filter mode change.
518a052517aSHangbin Liu */
519a052517aSHangbin Liu if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
520a052517aSHangbin Liu (!gdeleted && pmc->crcount)) &&
521a052517aSHangbin Liu (type == IGMPV3_ALLOW_NEW_SOURCES ||
522a052517aSHangbin Liu type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
523a052517aSHangbin Liu goto decrease_sf_crcount;
524a052517aSHangbin Liu
5251da177e4SLinus Torvalds /* clear marks on query responses */
5261da177e4SLinus Torvalds if (isquery)
5271da177e4SLinus Torvalds psf->sf_gsresp = 0;
5281da177e4SLinus Torvalds
52963007727SAl Viro if (AVAILABLE(skb) < sizeof(__be32) +
5301da177e4SLinus Torvalds first*sizeof(struct igmpv3_grec)) {
5311da177e4SLinus Torvalds if (truncate && !first)
5321da177e4SLinus Torvalds break; /* truncate these */
5331da177e4SLinus Torvalds if (pgr)
5341da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount);
5351da177e4SLinus Torvalds if (skb)
5361da177e4SLinus Torvalds igmpv3_sendpack(skb);
537b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu);
5381da177e4SLinus Torvalds first = 1;
5391da177e4SLinus Torvalds scount = 0;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds if (first) {
542b5476022SEric Dumazet skb = add_grhead(skb, pmc, type, &pgr, mtu);
5431da177e4SLinus Torvalds first = 0;
5441da177e4SLinus Torvalds }
545cc63f70bSAlexey Dobriyan if (!skb)
546cc63f70bSAlexey Dobriyan return NULL;
5474df864c1SJohannes Berg psrc = skb_put(skb, sizeof(__be32));
5481da177e4SLinus Torvalds *psrc = psf->sf_inaddr;
549ad12583fSDavid L Stevens scount++; stotal++;
5501da177e4SLinus Torvalds if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
5511da177e4SLinus Torvalds type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
552a052517aSHangbin Liu decrease_sf_crcount:
5531da177e4SLinus Torvalds psf->sf_crcount--;
5541da177e4SLinus Torvalds if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
5551da177e4SLinus Torvalds if (psf_prev)
5561da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next;
5571da177e4SLinus Torvalds else
5581da177e4SLinus Torvalds *psf_list = psf->sf_next;
5591da177e4SLinus Torvalds kfree(psf);
5601da177e4SLinus Torvalds continue;
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds psf_prev = psf;
5641da177e4SLinus Torvalds }
565ad12583fSDavid L Stevens
566ad12583fSDavid L Stevens empty_source:
567ad12583fSDavid L Stevens if (!stotal) {
568ad12583fSDavid L Stevens if (type == IGMPV3_ALLOW_NEW_SOURCES ||
569ad12583fSDavid L Stevens type == IGMPV3_BLOCK_OLD_SOURCES)
570ad12583fSDavid L Stevens return skb;
571ad12583fSDavid L Stevens if (pmc->crcount || isquery) {
572ad12583fSDavid L Stevens /* make sure we have room for group header */
573ad12583fSDavid L Stevens if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
574ad12583fSDavid L Stevens igmpv3_sendpack(skb);
575ad12583fSDavid L Stevens skb = NULL; /* add_grhead will get a new one */
576ad12583fSDavid L Stevens }
577b5476022SEric Dumazet skb = add_grhead(skb, pmc, type, &pgr, mtu);
578ad12583fSDavid L Stevens }
579ad12583fSDavid L Stevens }
5801da177e4SLinus Torvalds if (pgr)
5811da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount);
5821da177e4SLinus Torvalds
5831da177e4SLinus Torvalds if (isquery)
5841da177e4SLinus Torvalds pmc->gsquery = 0; /* clear query state on report */
5851da177e4SLinus Torvalds return skb;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds
igmpv3_send_report(struct in_device * in_dev,struct ip_mc_list * pmc)5881da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
5891da177e4SLinus Torvalds {
5901da177e4SLinus Torvalds struct sk_buff *skb = NULL;
59187a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev);
5921da177e4SLinus Torvalds int type;
5931da177e4SLinus Torvalds
5941da177e4SLinus Torvalds if (!pmc) {
5951d7138deSEric Dumazet rcu_read_lock();
5961d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) {
5971da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS)
5981da177e4SLinus Torvalds continue;
599df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(pmc->multiaddr) &&
600f6da2267SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
601df2cf4a7SPhilip Downey continue;
6021da177e4SLinus Torvalds spin_lock_bh(&pmc->lock);
6031da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE])
6041da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE;
6051da177e4SLinus Torvalds else
6061da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE;
6071da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0);
6081da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock);
6091da177e4SLinus Torvalds }
6101d7138deSEric Dumazet rcu_read_unlock();
6111da177e4SLinus Torvalds } else {
6121da177e4SLinus Torvalds spin_lock_bh(&pmc->lock);
6131da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE])
6141da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE;
6151da177e4SLinus Torvalds else
6161da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE;
6171da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0);
6181da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock);
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds if (!skb)
6211da177e4SLinus Torvalds return 0;
6221da177e4SLinus Torvalds return igmpv3_sendpack(skb);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds
6251da177e4SLinus Torvalds /*
6261da177e4SLinus Torvalds * remove zero-count source records from a source filter list
6271da177e4SLinus Torvalds */
igmpv3_clear_zeros(struct ip_sf_list ** ppsf)6281da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
6291da177e4SLinus Torvalds {
6301da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf;
6311da177e4SLinus Torvalds
6321da177e4SLinus Torvalds psf_prev = NULL;
6331da177e4SLinus Torvalds for (psf = *ppsf; psf; psf = psf_next) {
6341da177e4SLinus Torvalds psf_next = psf->sf_next;
6351da177e4SLinus Torvalds if (psf->sf_crcount == 0) {
6361da177e4SLinus Torvalds if (psf_prev)
6371da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next;
6381da177e4SLinus Torvalds else
6391da177e4SLinus Torvalds *ppsf = psf->sf_next;
6401da177e4SLinus Torvalds kfree(psf);
6411da177e4SLinus Torvalds } else
6421da177e4SLinus Torvalds psf_prev = psf;
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds
kfree_pmc(struct ip_mc_list * pmc)6463580d04aSEric Dumazet static void kfree_pmc(struct ip_mc_list *pmc)
6473580d04aSEric Dumazet {
6483580d04aSEric Dumazet ip_sf_list_clear_all(pmc->sources);
6493580d04aSEric Dumazet ip_sf_list_clear_all(pmc->tomb);
6503580d04aSEric Dumazet kfree(pmc);
6513580d04aSEric Dumazet }
6523580d04aSEric Dumazet
igmpv3_send_cr(struct in_device * in_dev)6531da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev)
6541da177e4SLinus Torvalds {
6551da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
6561da177e4SLinus Torvalds struct sk_buff *skb = NULL;
6571da177e4SLinus Torvalds int type, dtype;
6581da177e4SLinus Torvalds
6591d7138deSEric Dumazet rcu_read_lock();
6601da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock);
6611da177e4SLinus Torvalds
6621da177e4SLinus Torvalds /* deleted MCA's */
6631da177e4SLinus Torvalds pmc_prev = NULL;
6641da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
6651da177e4SLinus Torvalds pmc_next = pmc->next;
6661da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) {
6671da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES;
6681da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES;
6691da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0);
6701da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1);
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds if (pmc->crcount) {
6731da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) {
6741da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE;
6751da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0);
6761da177e4SLinus Torvalds }
677ad12583fSDavid L Stevens pmc->crcount--;
6781da177e4SLinus Torvalds if (pmc->crcount == 0) {
6791da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb);
6801da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
6841da177e4SLinus Torvalds if (pmc_prev)
6851da177e4SLinus Torvalds pmc_prev->next = pmc_next;
6861da177e4SLinus Torvalds else
6871da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next;
6881da177e4SLinus Torvalds in_dev_put(pmc->interface);
6893580d04aSEric Dumazet kfree_pmc(pmc);
6901da177e4SLinus Torvalds } else
6911da177e4SLinus Torvalds pmc_prev = pmc;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock);
6941da177e4SLinus Torvalds
6951da177e4SLinus Torvalds /* change recs */
6961d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) {
6971da177e4SLinus Torvalds spin_lock_bh(&pmc->lock);
6981da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) {
6991da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES;
7001da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES;
7011da177e4SLinus Torvalds } else {
7021da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES;
7031da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0);
7061da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
7071da177e4SLinus Torvalds
7081da177e4SLinus Torvalds /* filter mode changes */
7091da177e4SLinus Torvalds if (pmc->crcount) {
7101da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE)
7111da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE;
7121da177e4SLinus Torvalds else
7131da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE;
7141da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0);
715ad12583fSDavid L Stevens pmc->crcount--;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock);
7181da177e4SLinus Torvalds }
7191d7138deSEric Dumazet rcu_read_unlock();
7201da177e4SLinus Torvalds
7211da177e4SLinus Torvalds if (!skb)
7221da177e4SLinus Torvalds return;
7231da177e4SLinus Torvalds (void) igmpv3_sendpack(skb);
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds
igmp_send_report(struct in_device * in_dev,struct ip_mc_list * pmc,int type)7261da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
7271da177e4SLinus Torvalds int type)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds struct sk_buff *skb;
7301da177e4SLinus Torvalds struct iphdr *iph;
7311da177e4SLinus Torvalds struct igmphdr *ih;
7321da177e4SLinus Torvalds struct rtable *rt;
7331da177e4SLinus Torvalds struct net_device *dev = in_dev->dev;
734877acedcSDaniel Lezcano struct net *net = dev_net(dev);
73563007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0;
73631e4543dSDavid S. Miller struct flowi4 fl4;
73763007727SAl Viro __be32 dst;
73866088243SHerbert Xu int hlen, tlen;
7391da177e4SLinus Torvalds
7401da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
7411da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc);
742df2cf4a7SPhilip Downey
743f6da2267SKuniyuki Iwashima if (ipv4_is_local_multicast(group) &&
744f6da2267SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
745df2cf4a7SPhilip Downey return 0;
746df2cf4a7SPhilip Downey
747df2cf4a7SPhilip Downey if (type == IGMP_HOST_LEAVE_MESSAGE)
7481da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER;
7491da177e4SLinus Torvalds else
7501da177e4SLinus Torvalds dst = group;
7511da177e4SLinus Torvalds
75231e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
75378fbfd8aSDavid S. Miller 0, 0,
75478fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex);
755b23dd4feSDavid S. Miller if (IS_ERR(rt))
7561da177e4SLinus Torvalds return -1;
75778fbfd8aSDavid S. Miller
75866088243SHerbert Xu hlen = LL_RESERVED_SPACE(dev);
75966088243SHerbert Xu tlen = dev->needed_tailroom;
76066088243SHerbert Xu skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
76151456b29SIan Morris if (!skb) {
7621da177e4SLinus Torvalds ip_rt_put(rt);
7631da177e4SLinus Torvalds return -1;
7641da177e4SLinus Torvalds }
7659d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL;
7661da177e4SLinus Torvalds
767d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst);
7681da177e4SLinus Torvalds
76966088243SHerbert Xu skb_reserve(skb, hlen);
7701da177e4SLinus Torvalds
7717e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb);
772eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb);
7737e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4);
7741da177e4SLinus Torvalds
7751da177e4SLinus Torvalds iph->version = 4;
7761da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2;
7771da177e4SLinus Torvalds iph->tos = 0xc0;
7781da177e4SLinus Torvalds iph->frag_off = htons(IP_DF);
7791da177e4SLinus Torvalds iph->ttl = 1;
7801da177e4SLinus Torvalds iph->daddr = dst;
781492f64ceSDavid S. Miller iph->saddr = fl4.saddr;
7821da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP;
783b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL);
7841da177e4SLinus Torvalds ((u8 *)&iph[1])[0] = IPOPT_RA;
7851da177e4SLinus Torvalds ((u8 *)&iph[1])[1] = 4;
7861da177e4SLinus Torvalds ((u8 *)&iph[1])[2] = 0;
7871da177e4SLinus Torvalds ((u8 *)&iph[1])[3] = 0;
7881da177e4SLinus Torvalds
7894df864c1SJohannes Berg ih = skb_put(skb, sizeof(struct igmphdr));
7901da177e4SLinus Torvalds ih->type = type;
7911da177e4SLinus Torvalds ih->code = 0;
7921da177e4SLinus Torvalds ih->csum = 0;
7931da177e4SLinus Torvalds ih->group = group;
7941da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
7951da177e4SLinus Torvalds
79633224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb);
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds
igmp_gq_timer_expire(struct timer_list * t)799e99e88a9SKees Cook static void igmp_gq_timer_expire(struct timer_list *t)
8001da177e4SLinus Torvalds {
801e99e88a9SKees Cook struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
8021da177e4SLinus Torvalds
8031da177e4SLinus Torvalds in_dev->mr_gq_running = 0;
8041da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL);
805e2401654SSalam Noureddine in_dev_put(in_dev);
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds
igmp_ifc_timer_expire(struct timer_list * t)808e99e88a9SKees Cook static void igmp_ifc_timer_expire(struct timer_list *t)
8091da177e4SLinus Torvalds {
810e99e88a9SKees Cook struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
811b69dd5b3SEric Dumazet u32 mr_ifc_count;
8121da177e4SLinus Torvalds
8131da177e4SLinus Torvalds igmpv3_send_cr(in_dev);
8144a2b285eSEric Dumazet restart:
8154a2b285eSEric Dumazet mr_ifc_count = READ_ONCE(in_dev->mr_ifc_count);
8164a2b285eSEric Dumazet
8174a2b285eSEric Dumazet if (mr_ifc_count) {
8184a2b285eSEric Dumazet if (cmpxchg(&in_dev->mr_ifc_count,
8194a2b285eSEric Dumazet mr_ifc_count,
8204a2b285eSEric Dumazet mr_ifc_count - 1) != mr_ifc_count)
8214a2b285eSEric Dumazet goto restart;
822cab70040SWilliam Manley igmp_ifc_start_timer(in_dev,
823cab70040SWilliam Manley unsolicited_report_interval(in_dev));
8241da177e4SLinus Torvalds }
825e2401654SSalam Noureddine in_dev_put(in_dev);
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds
igmp_ifc_event(struct in_device * in_dev)8281da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev)
8291da177e4SLinus Torvalds {
830165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev);
8311da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
8321da177e4SLinus Torvalds return;
8338ebcc62cSKuniyuki Iwashima WRITE_ONCE(in_dev->mr_ifc_count, in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv));
8341da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1);
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds
8371da177e4SLinus Torvalds
igmp_timer_expire(struct timer_list * t)838e99e88a9SKees Cook static void igmp_timer_expire(struct timer_list *t)
8391da177e4SLinus Torvalds {
840e99e88a9SKees Cook struct ip_mc_list *im = from_timer(im, t, timer);
8411da177e4SLinus Torvalds struct in_device *in_dev = im->interface;
8421da177e4SLinus Torvalds
8431da177e4SLinus Torvalds spin_lock(&im->lock);
8441da177e4SLinus Torvalds im->tm_running = 0;
8451da177e4SLinus Torvalds
8464fb7253eSHangbin Liu if (im->unsolicit_count && --im->unsolicit_count)
847cab70040SWilliam Manley igmp_start_timer(im, unsolicited_report_interval(in_dev));
8484fb7253eSHangbin Liu
8491da177e4SLinus Torvalds im->reporter = 1;
8501da177e4SLinus Torvalds spin_unlock(&im->lock);
8511da177e4SLinus Torvalds
8521da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev))
8531da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
8541da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev))
8551da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
8561da177e4SLinus Torvalds else
8571da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
8581da177e4SLinus Torvalds
8591da177e4SLinus Torvalds ip_ma_put(im);
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds
862ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */
igmp_xmarksources(struct ip_mc_list * pmc,int nsrcs,__be32 * srcs)863ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
8641da177e4SLinus Torvalds {
8651da177e4SLinus Torvalds struct ip_sf_list *psf;
8661da177e4SLinus Torvalds int i, scount;
8671da177e4SLinus Torvalds
8681da177e4SLinus Torvalds scount = 0;
8691da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) {
8701da177e4SLinus Torvalds if (scount == nsrcs)
8711da177e4SLinus Torvalds break;
872ad12583fSDavid L Stevens for (i = 0; i < nsrcs; i++) {
873ad12583fSDavid L Stevens /* skip inactive filters */
874e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] ||
875ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] !=
876ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE])
877ce713ee5SRongQing.Li break;
878ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) {
879ad12583fSDavid L Stevens scount++;
880ad12583fSDavid L Stevens break;
881ad12583fSDavid L Stevens }
882ad12583fSDavid L Stevens }
883ad12583fSDavid L Stevens }
884ad12583fSDavid L Stevens pmc->gsquery = 0;
885ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */
886ad12583fSDavid L Stevens return 0;
887ad12583fSDavid L Stevens return 1;
888ad12583fSDavid L Stevens }
889ad12583fSDavid L Stevens
igmp_marksources(struct ip_mc_list * pmc,int nsrcs,__be32 * srcs)890ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
891ad12583fSDavid L Stevens {
892ad12583fSDavid L Stevens struct ip_sf_list *psf;
893ad12583fSDavid L Stevens int i, scount;
894ad12583fSDavid L Stevens
895ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE)
896ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs);
897ad12583fSDavid L Stevens
898ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */
899ad12583fSDavid L Stevens scount = 0;
900ad12583fSDavid L Stevens for (psf = pmc->sources; psf; psf = psf->sf_next) {
901ad12583fSDavid L Stevens if (scount == nsrcs)
902ad12583fSDavid L Stevens break;
9031da177e4SLinus Torvalds for (i = 0; i < nsrcs; i++)
9041da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) {
9051da177e4SLinus Torvalds psf->sf_gsresp = 1;
9061da177e4SLinus Torvalds scount++;
9071da177e4SLinus Torvalds break;
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds }
910ad12583fSDavid L Stevens if (!scount) {
911ad12583fSDavid L Stevens pmc->gsquery = 0;
912ad12583fSDavid L Stevens return 0;
913ad12583fSDavid L Stevens }
914ad12583fSDavid L Stevens pmc->gsquery = 1;
915ad12583fSDavid L Stevens return 1;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds
918d679c532SEric Dumazet /* return true if packet was dropped */
igmp_heard_report(struct in_device * in_dev,__be32 group)919d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds struct ip_mc_list *im;
92287a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev);
9231da177e4SLinus Torvalds
9241da177e4SLinus Torvalds /* Timers are only set for non-local groups */
9251da177e4SLinus Torvalds
9261da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS)
927d679c532SEric Dumazet return false;
928f6da2267SKuniyuki Iwashima if (ipv4_is_local_multicast(group) &&
929f6da2267SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
930df2cf4a7SPhilip Downey return false;
9311da177e4SLinus Torvalds
9321d7138deSEric Dumazet rcu_read_lock();
9331d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) {
9341da177e4SLinus Torvalds if (im->multiaddr == group) {
9351da177e4SLinus Torvalds igmp_stop_timer(im);
9361da177e4SLinus Torvalds break;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds }
9391d7138deSEric Dumazet rcu_read_unlock();
940d679c532SEric Dumazet return false;
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds
943d679c532SEric Dumazet /* return true if packet was dropped */
igmp_heard_query(struct in_device * in_dev,struct sk_buff * skb,int len)944d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
9451da177e4SLinus Torvalds int len)
9461da177e4SLinus Torvalds {
947d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb);
948d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
9491da177e4SLinus Torvalds struct ip_mc_list *im;
95063007727SAl Viro __be32 group = ih->group;
9511da177e4SLinus Torvalds int max_delay;
9521da177e4SLinus Torvalds int mark = 0;
95387a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev);
9541da177e4SLinus Torvalds
9551da177e4SLinus Torvalds
9565b7c8406SDavid Stevens if (len == 8) {
9571da177e4SLinus Torvalds if (ih->code == 0) {
9581da177e4SLinus Torvalds /* Alas, old v1 router presents here. */
9591da177e4SLinus Torvalds
960436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
9611da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies +
962966c37f2SHangbin Liu (in_dev->mr_qrv * in_dev->mr_qi) +
963966c37f2SHangbin Liu in_dev->mr_qri;
9641da177e4SLinus Torvalds group = 0;
9651da177e4SLinus Torvalds } else {
9661da177e4SLinus Torvalds /* v2 router present */
9671da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
9681da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies +
969966c37f2SHangbin Liu (in_dev->mr_qrv * in_dev->mr_qi) +
970966c37f2SHangbin Liu in_dev->mr_qri;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds /* cancel the interface change timer */
9734a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, 0);
9741da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer))
9751da177e4SLinus Torvalds __in_dev_put(in_dev);
9761da177e4SLinus Torvalds /* clear deleted report items */
9771da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev);
9781da177e4SLinus Torvalds } else if (len < 12) {
979d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */
9805b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) {
9815b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */
982436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
9835b7c8406SDavid Stevens group = 0;
9845b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) {
9855b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present;
9865b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here.
9875b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a
9885b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely
9895b7c8406SDavid Stevens * to be intended in a v3 query.
9905b7c8406SDavid Stevens */
9915b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
992a8c1f65cSBen Hutchings if (!max_delay)
993a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */
9941da177e4SLinus Torvalds } else { /* v3 */
9951da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
996d679c532SEric Dumazet return true;
9971da177e4SLinus Torvalds
998d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb);
9991da177e4SLinus Torvalds if (ih3->nsrcs) {
10001da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
100163007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32)))
1002d679c532SEric Dumazet return true;
1003d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb);
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds
10061da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
10071da177e4SLinus Torvalds if (!max_delay)
10081da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */
10091da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay;
1010966c37f2SHangbin Liu
1011966c37f2SHangbin Liu /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
1012966c37f2SHangbin Liu * received value was zero, use the default or statically
1013966c37f2SHangbin Liu * configured value.
1014966c37f2SHangbin Liu */
10158ebcc62cSKuniyuki Iwashima in_dev->mr_qrv = ih3->qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1016966c37f2SHangbin Liu in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
1017966c37f2SHangbin Liu
1018966c37f2SHangbin Liu /* RFC3376, 8.3. Query Response Interval:
1019966c37f2SHangbin Liu * The number of seconds represented by the [Query Response
1020966c37f2SHangbin Liu * Interval] must be less than the [Query Interval].
1021966c37f2SHangbin Liu */
1022966c37f2SHangbin Liu if (in_dev->mr_qri >= in_dev->mr_qi)
1023966c37f2SHangbin Liu in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
1024966c37f2SHangbin Liu
10251da177e4SLinus Torvalds if (!group) { /* general query */
10261da177e4SLinus Torvalds if (ih3->nsrcs)
1027b47bd8d2SDaniel Borkmann return true; /* no sources allowed */
10281da177e4SLinus Torvalds igmp_gq_start_timer(in_dev);
1029d679c532SEric Dumazet return false;
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */
10321da177e4SLinus Torvalds mark = ih3->nsrcs != 0;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds
10351da177e4SLinus Torvalds /*
10361da177e4SLinus Torvalds * - Start the timers in all of our membership records
10371da177e4SLinus Torvalds * that the query applies to for the interface on
10381da177e4SLinus Torvalds * which the query arrived excl. those that belong
10391da177e4SLinus Torvalds * to a "local" group (224.0.0.X)
10401da177e4SLinus Torvalds * - For timers already running check if they need to
10411da177e4SLinus Torvalds * be reset.
10421da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum
10431da177e4SLinus Torvalds * delay possible
10441da177e4SLinus Torvalds */
10451d7138deSEric Dumazet rcu_read_lock();
10461d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) {
1047ad12583fSDavid L Stevens int changed;
1048ad12583fSDavid L Stevens
10491da177e4SLinus Torvalds if (group && group != im->multiaddr)
10501da177e4SLinus Torvalds continue;
10511da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS)
10521da177e4SLinus Torvalds continue;
1053df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) &&
1054f6da2267SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1055df2cf4a7SPhilip Downey continue;
10561da177e4SLinus Torvalds spin_lock_bh(&im->lock);
10571da177e4SLinus Torvalds if (im->tm_running)
10581da177e4SLinus Torvalds im->gsquery = im->gsquery && mark;
10591da177e4SLinus Torvalds else
10601da177e4SLinus Torvalds im->gsquery = mark;
1061ad12583fSDavid L Stevens changed = !im->gsquery ||
10621da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
10631da177e4SLinus Torvalds spin_unlock_bh(&im->lock);
1064ad12583fSDavid L Stevens if (changed)
10651da177e4SLinus Torvalds igmp_mod_timer(im, max_delay);
10661da177e4SLinus Torvalds }
10671d7138deSEric Dumazet rcu_read_unlock();
1068d679c532SEric Dumazet return false;
10691da177e4SLinus Torvalds }
10701da177e4SLinus Torvalds
10719a57a9d2SEric Dumazet /* called in rcu_read_lock() section */
igmp_rcv(struct sk_buff * skb)10721da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */
10751da177e4SLinus Torvalds struct igmphdr *ih;
1076c7b725beSDavid Ahern struct net_device *dev = skb->dev;
1077c7b725beSDavid Ahern struct in_device *in_dev;
10781da177e4SLinus Torvalds int len = skb->len;
1079d679c532SEric Dumazet bool dropped = true;
10801da177e4SLinus Torvalds
1081c7b725beSDavid Ahern if (netif_is_l3_master(dev)) {
1082c7b725beSDavid Ahern dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1083c7b725beSDavid Ahern if (!dev)
1084c7b725beSDavid Ahern goto drop;
1085c7b725beSDavid Ahern }
1086c7b725beSDavid Ahern
1087c7b725beSDavid Ahern in_dev = __in_dev_get_rcu(dev);
108851456b29SIan Morris if (!in_dev)
1089cd557bc1SDenis V. Lunev goto drop;
10901da177e4SLinus Torvalds
1091fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
10929a57a9d2SEric Dumazet goto drop;
1093fb286bb2SHerbert Xu
1094de08dc1aSTom Herbert if (skb_checksum_simple_validate(skb))
10959a57a9d2SEric Dumazet goto drop;
10961da177e4SLinus Torvalds
1097d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb);
10981da177e4SLinus Torvalds switch (ih->type) {
10991da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY:
1100d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len);
11011da177e4SLinus Torvalds break;
11021da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT:
11031da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT:
11041da177e4SLinus Torvalds /* Is it our report looped back? */
1105c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb)))
11061da177e4SLinus Torvalds break;
110724c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */
110824c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST ||
110924c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST)
1110d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group);
11111da177e4SLinus Torvalds break;
11121da177e4SLinus Torvalds case IGMP_PIM:
11131da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1
11141da177e4SLinus Torvalds return pim_rcv_v1(skb);
11151da177e4SLinus Torvalds #endif
1116c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT:
11171da177e4SLinus Torvalds case IGMP_DVMRP:
11181da177e4SLinus Torvalds case IGMP_TRACE:
11191da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE:
11201da177e4SLinus Torvalds case IGMP_MTRACE:
11211da177e4SLinus Torvalds case IGMP_MTRACE_RESP:
11221da177e4SLinus Torvalds break;
11231da177e4SLinus Torvalds default:
1124dd1c1853SLinus Torvalds break;
11251da177e4SLinus Torvalds }
1126fb286bb2SHerbert Xu
1127cd557bc1SDenis V. Lunev drop:
1128d679c532SEric Dumazet if (dropped)
11291da177e4SLinus Torvalds kfree_skb(skb);
1130d679c532SEric Dumazet else
1131d679c532SEric Dumazet consume_skb(skb);
11321da177e4SLinus Torvalds return 0;
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds
11351da177e4SLinus Torvalds #endif
11361da177e4SLinus Torvalds
11371da177e4SLinus Torvalds
11381da177e4SLinus Torvalds /*
11391da177e4SLinus Torvalds * Add a filter to a device
11401da177e4SLinus Torvalds */
11411da177e4SLinus Torvalds
ip_mc_filter_add(struct in_device * in_dev,__be32 addr)114263007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
11431da177e4SLinus Torvalds {
11441da177e4SLinus Torvalds char buf[MAX_ADDR_LEN];
11451da177e4SLinus Torvalds struct net_device *dev = in_dev->dev;
11461da177e4SLinus Torvalds
11471da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
11481da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST
1149b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode
11501da177e4SLinus Torvalds routine. Something sort of:
11511da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
11521da177e4SLinus Torvalds --ANK
11531da177e4SLinus Torvalds */
11541da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0)
115522bedad3SJiri Pirko dev_mc_add(dev, buf);
11561da177e4SLinus Torvalds }
11571da177e4SLinus Torvalds
11581da177e4SLinus Torvalds /*
11591da177e4SLinus Torvalds * Remove a filter from a device
11601da177e4SLinus Torvalds */
11611da177e4SLinus Torvalds
ip_mc_filter_del(struct in_device * in_dev,__be32 addr)116263007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
11631da177e4SLinus Torvalds {
11641da177e4SLinus Torvalds char buf[MAX_ADDR_LEN];
11651da177e4SLinus Torvalds struct net_device *dev = in_dev->dev;
11661da177e4SLinus Torvalds
11671da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0)
116822bedad3SJiri Pirko dev_mc_del(dev, buf);
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds
11711da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
11721da177e4SLinus Torvalds /*
11731da177e4SLinus Torvalds * deleted ip_mc_list manipulation
11741da177e4SLinus Torvalds */
igmpv3_add_delrec(struct in_device * in_dev,struct ip_mc_list * im,gfp_t gfp)11759fb20801SFlorian Fainelli static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
11769fb20801SFlorian Fainelli gfp_t gfp)
11771da177e4SLinus Torvalds {
11781da177e4SLinus Torvalds struct ip_mc_list *pmc;
1179165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev);
11801da177e4SLinus Torvalds
11811da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below
11821da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not
11831da177e4SLinus Torvalds * used for management of the delete list. Using the same structure
11841da177e4SLinus Torvalds * for deleted items allows change reports to use common code with
11851da177e4SLinus Torvalds * non-deleted or query-response MCA's.
11861da177e4SLinus Torvalds */
11879fb20801SFlorian Fainelli pmc = kzalloc(sizeof(*pmc), gfp);
11881da177e4SLinus Torvalds if (!pmc)
11891da177e4SLinus Torvalds return;
1190b4846fc3SWANG Cong spin_lock_init(&pmc->lock);
11911da177e4SLinus Torvalds spin_lock_bh(&im->lock);
11921da177e4SLinus Torvalds pmc->interface = im->interface;
11931da177e4SLinus Torvalds in_dev_hold(in_dev);
11941da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr;
11958ebcc62cSKuniyuki Iwashima pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
11961da177e4SLinus Torvalds pmc->sfmode = im->sfmode;
11971da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) {
11981da177e4SLinus Torvalds struct ip_sf_list *psf;
11991da177e4SLinus Torvalds
12001da177e4SLinus Torvalds pmc->tomb = im->tomb;
12011da177e4SLinus Torvalds pmc->sources = im->sources;
12021da177e4SLinus Torvalds im->tomb = im->sources = NULL;
12031da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next)
12041da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds spin_unlock_bh(&im->lock);
12071da177e4SLinus Torvalds
12081da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock);
12091da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb;
12101da177e4SLinus Torvalds in_dev->mc_tomb = pmc;
12111da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock);
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds
121424803f38SHangbin Liu /*
121524803f38SHangbin Liu * restore ip_mc_list deleted records
121624803f38SHangbin Liu */
igmpv3_del_delrec(struct in_device * in_dev,struct ip_mc_list * im)121724803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
12181da177e4SLinus Torvalds {
12191da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev;
122024803f38SHangbin Liu struct ip_sf_list *psf;
122124803f38SHangbin Liu struct net *net = dev_net(in_dev->dev);
122224803f38SHangbin Liu __be32 multiaddr = im->multiaddr;
12231da177e4SLinus Torvalds
12241da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock);
12251da177e4SLinus Torvalds pmc_prev = NULL;
12261da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
12271da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr)
12281da177e4SLinus Torvalds break;
12291da177e4SLinus Torvalds pmc_prev = pmc;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds if (pmc) {
12321da177e4SLinus Torvalds if (pmc_prev)
12331da177e4SLinus Torvalds pmc_prev->next = pmc->next;
12341da177e4SLinus Torvalds else
12351da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock);
123824803f38SHangbin Liu
123924803f38SHangbin Liu spin_lock_bh(&im->lock);
12401da177e4SLinus Torvalds if (pmc) {
124124803f38SHangbin Liu im->interface = pmc->interface;
124208d3ffccSHangbin Liu if (im->sfmode == MCAST_INCLUDE) {
1243e5b1c6c6SEric Dumazet swap(im->tomb, pmc->tomb);
1244e5b1c6c6SEric Dumazet swap(im->sources, pmc->sources);
124524803f38SHangbin Liu for (psf = im->sources; psf; psf = psf->sf_next)
12468ebcc62cSKuniyuki Iwashima psf->sf_crcount = in_dev->mr_qrv ?:
12478ebcc62cSKuniyuki Iwashima READ_ONCE(net->ipv4.sysctl_igmp_qrv);
12486e2059b5SHangbin Liu } else {
12498ebcc62cSKuniyuki Iwashima im->crcount = in_dev->mr_qrv ?:
12508ebcc62cSKuniyuki Iwashima READ_ONCE(net->ipv4.sysctl_igmp_qrv);
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds in_dev_put(pmc->interface);
12533580d04aSEric Dumazet kfree_pmc(pmc);
12541da177e4SLinus Torvalds }
125524803f38SHangbin Liu spin_unlock_bh(&im->lock);
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds
125824803f38SHangbin Liu /*
125924803f38SHangbin Liu * flush ip_mc_list deleted records
126024803f38SHangbin Liu */
igmpv3_clear_delrec(struct in_device * in_dev)12611da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev)
12621da177e4SLinus Torvalds {
12631da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc;
12641da177e4SLinus Torvalds
12651da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock);
12661da177e4SLinus Torvalds pmc = in_dev->mc_tomb;
12671da177e4SLinus Torvalds in_dev->mc_tomb = NULL;
12681da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock);
12691da177e4SLinus Torvalds
12701da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) {
12711da177e4SLinus Torvalds nextpmc = pmc->next;
12721da177e4SLinus Torvalds ip_mc_clear_src(pmc);
12731da177e4SLinus Torvalds in_dev_put(pmc->interface);
12743580d04aSEric Dumazet kfree_pmc(pmc);
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds /* clear dead sources, too */
12771d7138deSEric Dumazet rcu_read_lock();
12781d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) {
12793580d04aSEric Dumazet struct ip_sf_list *psf;
12801da177e4SLinus Torvalds
12811da177e4SLinus Torvalds spin_lock_bh(&pmc->lock);
12821da177e4SLinus Torvalds psf = pmc->tomb;
12831da177e4SLinus Torvalds pmc->tomb = NULL;
12841da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock);
12853580d04aSEric Dumazet ip_sf_list_clear_all(psf);
12861da177e4SLinus Torvalds }
12871d7138deSEric Dumazet rcu_read_unlock();
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds #endif
12901da177e4SLinus Torvalds
__igmp_group_dropped(struct ip_mc_list * im,gfp_t gfp)12919fb20801SFlorian Fainelli static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
12921da177e4SLinus Torvalds {
12931da177e4SLinus Torvalds struct in_device *in_dev = im->interface;
12941da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
129587a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev);
12961da177e4SLinus Torvalds int reporter;
12971da177e4SLinus Torvalds #endif
12981da177e4SLinus Torvalds
12991da177e4SLinus Torvalds if (im->loaded) {
13001da177e4SLinus Torvalds im->loaded = 0;
13011da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr);
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds
13041da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
13051da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS)
13061da177e4SLinus Torvalds return;
1307f6da2267SKuniyuki Iwashima if (ipv4_is_local_multicast(im->multiaddr) &&
1308f6da2267SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1309df2cf4a7SPhilip Downey return;
13101da177e4SLinus Torvalds
13111da177e4SLinus Torvalds reporter = im->reporter;
13121da177e4SLinus Torvalds igmp_stop_timer(im);
13131da177e4SLinus Torvalds
13141da177e4SLinus Torvalds if (!in_dev->dead) {
13151da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev))
131624cf3af3SVeaceslav Falico return;
13171da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) {
13181da177e4SLinus Torvalds if (reporter)
13191da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
132024cf3af3SVeaceslav Falico return;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds /* IGMPv3 */
13239fb20801SFlorian Fainelli igmpv3_add_delrec(in_dev, im, gfp);
13241da177e4SLinus Torvalds
13251da177e4SLinus Torvalds igmp_ifc_event(in_dev);
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds #endif
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds
igmp_group_dropped(struct ip_mc_list * im)13309fb20801SFlorian Fainelli static void igmp_group_dropped(struct ip_mc_list *im)
13319fb20801SFlorian Fainelli {
13329fb20801SFlorian Fainelli __igmp_group_dropped(im, GFP_KERNEL);
13339fb20801SFlorian Fainelli }
13349fb20801SFlorian Fainelli
igmp_group_added(struct ip_mc_list * im)13350ae0d60aSHangbin Liu static void igmp_group_added(struct ip_mc_list *im)
13361da177e4SLinus Torvalds {
13371da177e4SLinus Torvalds struct in_device *in_dev = im->interface;
1338dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST
133987a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev);
1340dcd87999SNikolay Borisov #endif
13411da177e4SLinus Torvalds
13421da177e4SLinus Torvalds if (im->loaded == 0) {
13431da177e4SLinus Torvalds im->loaded = 1;
13441da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr);
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds
13471da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
13481da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS)
13491da177e4SLinus Torvalds return;
1350f6da2267SKuniyuki Iwashima if (ipv4_is_local_multicast(im->multiaddr) &&
1351f6da2267SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1352df2cf4a7SPhilip Downey return;
13531da177e4SLinus Torvalds
13541da177e4SLinus Torvalds if (in_dev->dead)
13551da177e4SLinus Torvalds return;
1356ff06525fSHangbin Liu
13578ebcc62cSKuniyuki Iwashima im->unsolicit_count = READ_ONCE(net->ipv4.sysctl_igmp_qrv);
13581da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
13591da177e4SLinus Torvalds spin_lock_bh(&im->lock);
1360436f7c20SFabian Frederick igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
13611da177e4SLinus Torvalds spin_unlock_bh(&im->lock);
13621da177e4SLinus Torvalds return;
13631da177e4SLinus Torvalds }
13641da177e4SLinus Torvalds /* else, v3 */
13651da177e4SLinus Torvalds
13666e2059b5SHangbin Liu /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
13676e2059b5SHangbin Liu * not send filter-mode change record as the mode should be from
13686e2059b5SHangbin Liu * IN() to IN(A).
13696e2059b5SHangbin Liu */
13700ae0d60aSHangbin Liu if (im->sfmode == MCAST_EXCLUDE)
13718ebcc62cSKuniyuki Iwashima im->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
13726e2059b5SHangbin Liu
13731da177e4SLinus Torvalds igmp_ifc_event(in_dev);
13741da177e4SLinus Torvalds #endif
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds
13771da177e4SLinus Torvalds
13781da177e4SLinus Torvalds /*
13791da177e4SLinus Torvalds * Multicast list managers
13801da177e4SLinus Torvalds */
13811da177e4SLinus Torvalds
ip_mc_hash(const struct ip_mc_list * im)1382e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im)
1383e9897071SEric Dumazet {
1384c70eba74SEric Dumazet return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1385e9897071SEric Dumazet }
1386e9897071SEric Dumazet
ip_mc_hash_add(struct in_device * in_dev,struct ip_mc_list * im)1387e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev,
1388e9897071SEric Dumazet struct ip_mc_list *im)
1389e9897071SEric Dumazet {
1390e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash;
1391e9897071SEric Dumazet u32 hash;
1392e9897071SEric Dumazet
1393e9897071SEric Dumazet mc_hash = rtnl_dereference(in_dev->mc_hash);
1394e9897071SEric Dumazet if (mc_hash) {
1395e9897071SEric Dumazet hash = ip_mc_hash(im);
1396c70eba74SEric Dumazet im->next_hash = mc_hash[hash];
1397e9897071SEric Dumazet rcu_assign_pointer(mc_hash[hash], im);
1398e9897071SEric Dumazet return;
1399e9897071SEric Dumazet }
1400e9897071SEric Dumazet
1401e9897071SEric Dumazet /* do not use a hash table for small number of items */
1402e9897071SEric Dumazet if (in_dev->mc_count < 4)
1403e9897071SEric Dumazet return;
1404e9897071SEric Dumazet
1405e9897071SEric Dumazet mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1406e9897071SEric Dumazet GFP_KERNEL);
1407e9897071SEric Dumazet if (!mc_hash)
1408e9897071SEric Dumazet return;
1409e9897071SEric Dumazet
1410e9897071SEric Dumazet for_each_pmc_rtnl(in_dev, im) {
1411e9897071SEric Dumazet hash = ip_mc_hash(im);
1412c70eba74SEric Dumazet im->next_hash = mc_hash[hash];
1413e9897071SEric Dumazet RCU_INIT_POINTER(mc_hash[hash], im);
1414e9897071SEric Dumazet }
1415e9897071SEric Dumazet
1416e9897071SEric Dumazet rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1417e9897071SEric Dumazet }
1418e9897071SEric Dumazet
ip_mc_hash_remove(struct in_device * in_dev,struct ip_mc_list * im)1419e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev,
1420e9897071SEric Dumazet struct ip_mc_list *im)
1421e9897071SEric Dumazet {
1422e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1423e9897071SEric Dumazet struct ip_mc_list *aux;
1424e9897071SEric Dumazet
1425e9897071SEric Dumazet if (!mc_hash)
1426e9897071SEric Dumazet return;
1427e9897071SEric Dumazet mc_hash += ip_mc_hash(im);
1428e9897071SEric Dumazet while ((aux = rtnl_dereference(*mc_hash)) != im)
1429e9897071SEric Dumazet mc_hash = &aux->next_hash;
1430e9897071SEric Dumazet *mc_hash = im->next_hash;
1431e9897071SEric Dumazet }
1432e9897071SEric Dumazet
14331da177e4SLinus Torvalds
14341da177e4SLinus Torvalds /*
14351da177e4SLinus Torvalds * A socket has joined a multicast group on device dev.
14361da177e4SLinus Torvalds */
____ip_mc_inc_group(struct in_device * in_dev,__be32 addr,unsigned int mode,gfp_t gfp)14379fb20801SFlorian Fainelli static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
14389fb20801SFlorian Fainelli unsigned int mode, gfp_t gfp)
14391da177e4SLinus Torvalds {
14401da177e4SLinus Torvalds struct ip_mc_list *im;
14411da177e4SLinus Torvalds
14421da177e4SLinus Torvalds ASSERT_RTNL();
14431da177e4SLinus Torvalds
14441d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) {
14451da177e4SLinus Torvalds if (im->multiaddr == addr) {
14461da177e4SLinus Torvalds im->users++;
14476e2059b5SHangbin Liu ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
14481da177e4SLinus Torvalds goto out;
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds }
14511da177e4SLinus Torvalds
14529fb20801SFlorian Fainelli im = kzalloc(sizeof(*im), gfp);
14531da177e4SLinus Torvalds if (!im)
14541da177e4SLinus Torvalds goto out;
14551da177e4SLinus Torvalds
14561da177e4SLinus Torvalds im->users = 1;
14571da177e4SLinus Torvalds im->interface = in_dev;
14581da177e4SLinus Torvalds in_dev_hold(in_dev);
14591da177e4SLinus Torvalds im->multiaddr = addr;
14601da177e4SLinus Torvalds /* initial mode is (EX, empty) */
14616e2059b5SHangbin Liu im->sfmode = mode;
14626e2059b5SHangbin Liu im->sfcount[mode] = 1;
14638851ab52SReshetova, Elena refcount_set(&im->refcnt, 1);
14641da177e4SLinus Torvalds spin_lock_init(&im->lock);
14651da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1466e99e88a9SKees Cook timer_setup(&im->timer, igmp_timer_expire, 0);
14671da177e4SLinus Torvalds #endif
14681d7138deSEric Dumazet
14691d7138deSEric Dumazet im->next_rcu = in_dev->mc_list;
1470b8bae41eSRami Rosen in_dev->mc_count++;
1471cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im);
14721d7138deSEric Dumazet
1473e9897071SEric Dumazet ip_mc_hash_add(in_dev, im);
1474e9897071SEric Dumazet
14751da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
147624803f38SHangbin Liu igmpv3_del_delrec(in_dev, im);
14771da177e4SLinus Torvalds #endif
14780ae0d60aSHangbin Liu igmp_group_added(im);
14791da177e4SLinus Torvalds if (!in_dev->dead)
14801da177e4SLinus Torvalds ip_rt_multicast_event(in_dev);
14811da177e4SLinus Torvalds out:
14821da177e4SLinus Torvalds return;
14831da177e4SLinus Torvalds }
14846e2059b5SHangbin Liu
__ip_mc_inc_group(struct in_device * in_dev,__be32 addr,gfp_t gfp)14859fb20801SFlorian Fainelli void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
14869fb20801SFlorian Fainelli {
14879fb20801SFlorian Fainelli ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
14889fb20801SFlorian Fainelli }
14899fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_inc_group);
14909fb20801SFlorian Fainelli
ip_mc_inc_group(struct in_device * in_dev,__be32 addr)14916e2059b5SHangbin Liu void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
14926e2059b5SHangbin Liu {
1493a1c4cd67SLi RongQing __ip_mc_inc_group(in_dev, addr, GFP_KERNEL);
14946e2059b5SHangbin Liu }
14954bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group);
14961da177e4SLinus Torvalds
ip_mc_check_iphdr(struct sk_buff * skb)14979afd85c9SLinus Lüssing static int ip_mc_check_iphdr(struct sk_buff *skb)
14989afd85c9SLinus Lüssing {
14999afd85c9SLinus Lüssing const struct iphdr *iph;
15009afd85c9SLinus Lüssing unsigned int len;
15019afd85c9SLinus Lüssing unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
15029afd85c9SLinus Lüssing
15039afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset))
15049afd85c9SLinus Lüssing return -EINVAL;
15059afd85c9SLinus Lüssing
15069afd85c9SLinus Lüssing iph = ip_hdr(skb);
15079afd85c9SLinus Lüssing
15089afd85c9SLinus Lüssing if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
15099afd85c9SLinus Lüssing return -EINVAL;
15109afd85c9SLinus Lüssing
15119afd85c9SLinus Lüssing offset += ip_hdrlen(skb) - sizeof(*iph);
15129afd85c9SLinus Lüssing
15139afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset))
15149afd85c9SLinus Lüssing return -EINVAL;
15159afd85c9SLinus Lüssing
15169afd85c9SLinus Lüssing iph = ip_hdr(skb);
15179afd85c9SLinus Lüssing
15189afd85c9SLinus Lüssing if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
15199afd85c9SLinus Lüssing return -EINVAL;
15209afd85c9SLinus Lüssing
15219afd85c9SLinus Lüssing len = skb_network_offset(skb) + ntohs(iph->tot_len);
15229afd85c9SLinus Lüssing if (skb->len < len || len < offset)
15239afd85c9SLinus Lüssing return -EINVAL;
15249afd85c9SLinus Lüssing
15259afd85c9SLinus Lüssing skb_set_transport_header(skb, offset);
15269afd85c9SLinus Lüssing
15279afd85c9SLinus Lüssing return 0;
15289afd85c9SLinus Lüssing }
15299afd85c9SLinus Lüssing
ip_mc_check_igmp_reportv3(struct sk_buff * skb)15309afd85c9SLinus Lüssing static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
15319afd85c9SLinus Lüssing {
15329afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb);
15339afd85c9SLinus Lüssing
15349afd85c9SLinus Lüssing len += sizeof(struct igmpv3_report);
15359afd85c9SLinus Lüssing
1536a2e2ca3bSLinus Lüssing return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
15379afd85c9SLinus Lüssing }
15389afd85c9SLinus Lüssing
ip_mc_check_igmp_query(struct sk_buff * skb)15399afd85c9SLinus Lüssing static int ip_mc_check_igmp_query(struct sk_buff *skb)
15409afd85c9SLinus Lüssing {
1541a2e2ca3bSLinus Lüssing unsigned int transport_len = ip_transport_len(skb);
1542a2e2ca3bSLinus Lüssing unsigned int len;
15439afd85c9SLinus Lüssing
15449afd85c9SLinus Lüssing /* IGMPv{1,2}? */
1545a2e2ca3bSLinus Lüssing if (transport_len != sizeof(struct igmphdr)) {
15469afd85c9SLinus Lüssing /* or IGMPv3? */
1547a2e2ca3bSLinus Lüssing if (transport_len < sizeof(struct igmpv3_query))
1548a2e2ca3bSLinus Lüssing return -EINVAL;
1549a2e2ca3bSLinus Lüssing
1550a2e2ca3bSLinus Lüssing len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1551a2e2ca3bSLinus Lüssing if (!ip_mc_may_pull(skb, len))
15529afd85c9SLinus Lüssing return -EINVAL;
15539afd85c9SLinus Lüssing }
15549afd85c9SLinus Lüssing
15559afd85c9SLinus Lüssing /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
15569afd85c9SLinus Lüssing * all-systems destination addresses (224.0.0.1) for general queries
15579afd85c9SLinus Lüssing */
15589afd85c9SLinus Lüssing if (!igmp_hdr(skb)->group &&
15599afd85c9SLinus Lüssing ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
15609afd85c9SLinus Lüssing return -EINVAL;
15619afd85c9SLinus Lüssing
15629afd85c9SLinus Lüssing return 0;
15639afd85c9SLinus Lüssing }
15649afd85c9SLinus Lüssing
ip_mc_check_igmp_msg(struct sk_buff * skb)15659afd85c9SLinus Lüssing static int ip_mc_check_igmp_msg(struct sk_buff *skb)
15669afd85c9SLinus Lüssing {
15679afd85c9SLinus Lüssing switch (igmp_hdr(skb)->type) {
15689afd85c9SLinus Lüssing case IGMP_HOST_LEAVE_MESSAGE:
15699afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_REPORT:
15709afd85c9SLinus Lüssing case IGMPV2_HOST_MEMBERSHIP_REPORT:
15719afd85c9SLinus Lüssing return 0;
15729afd85c9SLinus Lüssing case IGMPV3_HOST_MEMBERSHIP_REPORT:
15739afd85c9SLinus Lüssing return ip_mc_check_igmp_reportv3(skb);
15749afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_QUERY:
15759afd85c9SLinus Lüssing return ip_mc_check_igmp_query(skb);
15769afd85c9SLinus Lüssing default:
15779afd85c9SLinus Lüssing return -ENOMSG;
15789afd85c9SLinus Lüssing }
15799afd85c9SLinus Lüssing }
15809afd85c9SLinus Lüssing
ip_mc_validate_checksum(struct sk_buff * skb)15815a43f697SAlexey Dobriyan static __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
15829afd85c9SLinus Lüssing {
15839afd85c9SLinus Lüssing return skb_checksum_simple_validate(skb);
15849afd85c9SLinus Lüssing }
15859afd85c9SLinus Lüssing
ip_mc_check_igmp_csum(struct sk_buff * skb)1586a2e2ca3bSLinus Lüssing static int ip_mc_check_igmp_csum(struct sk_buff *skb)
15879afd85c9SLinus Lüssing {
15889afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1589a2e2ca3bSLinus Lüssing unsigned int transport_len = ip_transport_len(skb);
1590a2e2ca3bSLinus Lüssing struct sk_buff *skb_chk;
15919afd85c9SLinus Lüssing
1592a2e2ca3bSLinus Lüssing if (!ip_mc_may_pull(skb, len))
1593a2e2ca3bSLinus Lüssing return -EINVAL;
15949afd85c9SLinus Lüssing
15959afd85c9SLinus Lüssing skb_chk = skb_checksum_trimmed(skb, transport_len,
15969afd85c9SLinus Lüssing ip_mc_validate_checksum);
15979afd85c9SLinus Lüssing if (!skb_chk)
1598a2e2ca3bSLinus Lüssing return -EINVAL;
15999afd85c9SLinus Lüssing
1600a2e2ca3bSLinus Lüssing if (skb_chk != skb)
1601a516993fSLinus Lüssing kfree_skb(skb_chk);
1602a516993fSLinus Lüssing
1603a2e2ca3bSLinus Lüssing return 0;
16049afd85c9SLinus Lüssing }
16059afd85c9SLinus Lüssing
16069afd85c9SLinus Lüssing /**
16079afd85c9SLinus Lüssing * ip_mc_check_igmp - checks whether this is a sane IGMP packet
16089afd85c9SLinus Lüssing * @skb: the skb to validate
16099afd85c9SLinus Lüssing *
16109afd85c9SLinus Lüssing * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1611a516993fSLinus Lüssing * skb transport header accordingly and returns zero.
16129afd85c9SLinus Lüssing *
16139afd85c9SLinus Lüssing * -EINVAL: A broken packet was detected, i.e. it violates some internet
16149afd85c9SLinus Lüssing * standard
16159afd85c9SLinus Lüssing * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
16169afd85c9SLinus Lüssing * -ENOMEM: A memory allocation failure happened.
16179afd85c9SLinus Lüssing *
1618a516993fSLinus Lüssing * Caller needs to set the skb network header and free any returned skb if it
1619a516993fSLinus Lüssing * differs from the provided skb.
16209afd85c9SLinus Lüssing */
ip_mc_check_igmp(struct sk_buff * skb)1621ba5ea614SLinus Lüssing int ip_mc_check_igmp(struct sk_buff *skb)
16229afd85c9SLinus Lüssing {
16239afd85c9SLinus Lüssing int ret = ip_mc_check_iphdr(skb);
16249afd85c9SLinus Lüssing
16259afd85c9SLinus Lüssing if (ret < 0)
16269afd85c9SLinus Lüssing return ret;
16279afd85c9SLinus Lüssing
16289afd85c9SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
16299afd85c9SLinus Lüssing return -ENOMSG;
16309afd85c9SLinus Lüssing
1631a2e2ca3bSLinus Lüssing ret = ip_mc_check_igmp_csum(skb);
1632a2e2ca3bSLinus Lüssing if (ret < 0)
1633a2e2ca3bSLinus Lüssing return ret;
1634a2e2ca3bSLinus Lüssing
1635a2e2ca3bSLinus Lüssing return ip_mc_check_igmp_msg(skb);
16369afd85c9SLinus Lüssing }
16379afd85c9SLinus Lüssing EXPORT_SYMBOL(ip_mc_check_igmp);
16389afd85c9SLinus Lüssing
16391da177e4SLinus Torvalds /*
16404aa5dee4SJiri Pirko * Resend IGMP JOIN report; used by netdev notifier.
1641a816c7c7SJay Vosburgh */
ip_mc_rejoin_groups(struct in_device * in_dev)16424aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev)
1643a816c7c7SJay Vosburgh {
164408882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST
1645866f3b25SEric Dumazet struct ip_mc_list *im;
1646866f3b25SEric Dumazet int type;
164787a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev);
1648a816c7c7SJay Vosburgh
16494aa5dee4SJiri Pirko ASSERT_RTNL();
16504aa5dee4SJiri Pirko
16514aa5dee4SJiri Pirko for_each_pmc_rtnl(in_dev, im) {
1652a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS)
1653866f3b25SEric Dumazet continue;
1654df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) &&
1655f6da2267SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1656df2cf4a7SPhilip Downey continue;
1657a816c7c7SJay Vosburgh
1658e12b4539SFlavio Leitner /* a failover is happening and switches
1659866f3b25SEric Dumazet * must be notified immediately
1660866f3b25SEric Dumazet */
1661e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev))
1662866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT;
1663e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev))
1664866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1665e12b4539SFlavio Leitner else
1666866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1667866f3b25SEric Dumazet igmp_send_report(in_dev, im, type);
1668866f3b25SEric Dumazet }
1669a816c7c7SJay Vosburgh #endif
1670a816c7c7SJay Vosburgh }
1671a816c7c7SJay Vosburgh
1672a816c7c7SJay Vosburgh /*
16731da177e4SLinus Torvalds * A socket has left a multicast group on device dev
16741da177e4SLinus Torvalds */
16751da177e4SLinus Torvalds
__ip_mc_dec_group(struct in_device * in_dev,__be32 addr,gfp_t gfp)16769fb20801SFlorian Fainelli void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
16771da177e4SLinus Torvalds {
16781d7138deSEric Dumazet struct ip_mc_list *i;
16791d7138deSEric Dumazet struct ip_mc_list __rcu **ip;
16801da177e4SLinus Torvalds
16811da177e4SLinus Torvalds ASSERT_RTNL();
16821da177e4SLinus Torvalds
16831d7138deSEric Dumazet for (ip = &in_dev->mc_list;
16841d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL;
16851d7138deSEric Dumazet ip = &i->next_rcu) {
16861da177e4SLinus Torvalds if (i->multiaddr == addr) {
16871da177e4SLinus Torvalds if (--i->users == 0) {
1688e9897071SEric Dumazet ip_mc_hash_remove(in_dev, i);
16891d7138deSEric Dumazet *ip = i->next_rcu;
1690b8bae41eSRami Rosen in_dev->mc_count--;
16919fb20801SFlorian Fainelli __igmp_group_dropped(i, gfp);
169224cf3af3SVeaceslav Falico ip_mc_clear_src(i);
16931da177e4SLinus Torvalds
16941da177e4SLinus Torvalds if (!in_dev->dead)
16951da177e4SLinus Torvalds ip_rt_multicast_event(in_dev);
16961da177e4SLinus Torvalds
16971da177e4SLinus Torvalds ip_ma_put(i);
16981da177e4SLinus Torvalds return;
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds break;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds }
17049fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_dec_group);
17051da177e4SLinus Torvalds
170675c78500SMoni Shoua /* Device changing type */
170775c78500SMoni Shoua
ip_mc_unmap(struct in_device * in_dev)170875c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev)
170975c78500SMoni Shoua {
17101d7138deSEric Dumazet struct ip_mc_list *pmc;
171175c78500SMoni Shoua
171275c78500SMoni Shoua ASSERT_RTNL();
171375c78500SMoni Shoua
17141d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc)
17151d7138deSEric Dumazet igmp_group_dropped(pmc);
171675c78500SMoni Shoua }
171775c78500SMoni Shoua
ip_mc_remap(struct in_device * in_dev)171875c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev)
171975c78500SMoni Shoua {
17201d7138deSEric Dumazet struct ip_mc_list *pmc;
172175c78500SMoni Shoua
172275c78500SMoni Shoua ASSERT_RTNL();
172375c78500SMoni Shoua
172424803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) {
172524803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST
172624803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc);
172724803f38SHangbin Liu #endif
17280ae0d60aSHangbin Liu igmp_group_added(pmc);
172975c78500SMoni Shoua }
173024803f38SHangbin Liu }
173175c78500SMoni Shoua
17321da177e4SLinus Torvalds /* Device going down */
17331da177e4SLinus Torvalds
ip_mc_down(struct in_device * in_dev)17341da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev)
17351da177e4SLinus Torvalds {
17361d7138deSEric Dumazet struct ip_mc_list *pmc;
17371da177e4SLinus Torvalds
17381da177e4SLinus Torvalds ASSERT_RTNL();
17391da177e4SLinus Torvalds
17401d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc)
17411d7138deSEric Dumazet igmp_group_dropped(pmc);
17421da177e4SLinus Torvalds
17431da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
17444a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, 0);
17451da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer))
17461da177e4SLinus Torvalds __in_dev_put(in_dev);
17471da177e4SLinus Torvalds in_dev->mr_gq_running = 0;
17481da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer))
17491da177e4SLinus Torvalds __in_dev_put(in_dev);
17501da177e4SLinus Torvalds #endif
17511da177e4SLinus Torvalds
17521da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
17531da177e4SLinus Torvalds }
17541da177e4SLinus Torvalds
1755966c37f2SHangbin Liu #ifdef CONFIG_IP_MULTICAST
ip_mc_reset(struct in_device * in_dev)1756966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev)
1757966c37f2SHangbin Liu {
1758966c37f2SHangbin Liu struct net *net = dev_net(in_dev->dev);
1759966c37f2SHangbin Liu
1760966c37f2SHangbin Liu in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1761966c37f2SHangbin Liu in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
17628ebcc62cSKuniyuki Iwashima in_dev->mr_qrv = READ_ONCE(net->ipv4.sysctl_igmp_qrv);
1763966c37f2SHangbin Liu }
1764966c37f2SHangbin Liu #else
ip_mc_reset(struct in_device * in_dev)1765966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev)
1766966c37f2SHangbin Liu {
1767966c37f2SHangbin Liu }
1768966c37f2SHangbin Liu #endif
1769966c37f2SHangbin Liu
ip_mc_init_dev(struct in_device * in_dev)17701da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev)
17711da177e4SLinus Torvalds {
17721da177e4SLinus Torvalds ASSERT_RTNL();
17731da177e4SLinus Torvalds
17741da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
1775e99e88a9SKees Cook timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1776e99e88a9SKees Cook timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
17771da177e4SLinus Torvalds #endif
1778966c37f2SHangbin Liu ip_mc_reset(in_dev);
17791da177e4SLinus Torvalds
17801da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock);
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds
17831da177e4SLinus Torvalds /* Device going up */
17841da177e4SLinus Torvalds
ip_mc_up(struct in_device * in_dev)17851da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev)
17861da177e4SLinus Torvalds {
17871d7138deSEric Dumazet struct ip_mc_list *pmc;
17881da177e4SLinus Torvalds
17891da177e4SLinus Torvalds ASSERT_RTNL();
17901da177e4SLinus Torvalds
1791966c37f2SHangbin Liu ip_mc_reset(in_dev);
17921da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
17931da177e4SLinus Torvalds
179424803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) {
179524803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST
179624803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc);
179724803f38SHangbin Liu #endif
17980ae0d60aSHangbin Liu igmp_group_added(pmc);
17991da177e4SLinus Torvalds }
180024803f38SHangbin Liu }
18011da177e4SLinus Torvalds
18021da177e4SLinus Torvalds /*
18031da177e4SLinus Torvalds * Device is about to be destroyed: clean up.
18041da177e4SLinus Torvalds */
18051da177e4SLinus Torvalds
ip_mc_destroy_dev(struct in_device * in_dev)18061da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev)
18071da177e4SLinus Torvalds {
18081da177e4SLinus Torvalds struct ip_mc_list *i;
18091da177e4SLinus Torvalds
18101da177e4SLinus Torvalds ASSERT_RTNL();
18111da177e4SLinus Torvalds
18121da177e4SLinus Torvalds /* Deactivate timers */
18131da177e4SLinus Torvalds ip_mc_down(in_dev);
181424803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST
181524803f38SHangbin Liu igmpv3_clear_delrec(in_dev);
181624803f38SHangbin Liu #endif
18171da177e4SLinus Torvalds
18181d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
18191d7138deSEric Dumazet in_dev->mc_list = i->next_rcu;
1820b8bae41eSRami Rosen in_dev->mc_count--;
1821d8e29730SChengyang Fan ip_mc_clear_src(i);
18221da177e4SLinus Torvalds ip_ma_put(i);
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds }
18251da177e4SLinus Torvalds
18269e917dcaSEric Dumazet /* RTNL is locked */
ip_mc_find_dev(struct net * net,struct ip_mreqn * imr)1827877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
18281da177e4SLinus Torvalds {
18291da177e4SLinus Torvalds struct net_device *dev = NULL;
18301da177e4SLinus Torvalds struct in_device *idev = NULL;
18311da177e4SLinus Torvalds
18321da177e4SLinus Torvalds if (imr->imr_ifindex) {
1833877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex);
18341da177e4SLinus Torvalds return idev;
18351da177e4SLinus Torvalds }
18361da177e4SLinus Torvalds if (imr->imr_address.s_addr) {
18379e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
18381da177e4SLinus Torvalds if (!dev)
18391da177e4SLinus Torvalds return NULL;
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds
1842b23dd4feSDavid S. Miller if (!dev) {
184378fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net,
184478fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr,
184578fbfd8aSDavid S. Miller 0, 0, 0);
1846b23dd4feSDavid S. Miller if (!IS_ERR(rt)) {
1847d8d1f30bSChangli Gao dev = rt->dst.dev;
18481da177e4SLinus Torvalds ip_rt_put(rt);
18491da177e4SLinus Torvalds }
1850b23dd4feSDavid S. Miller }
18511da177e4SLinus Torvalds if (dev) {
18521da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex;
1853e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev);
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds return idev;
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds
18581da177e4SLinus Torvalds /*
18591da177e4SLinus Torvalds * Join a socket to a group
18601da177e4SLinus Torvalds */
18611da177e4SLinus Torvalds
ip_mc_del1_src(struct ip_mc_list * pmc,int sfmode,__be32 * psfsrc)18621da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
18638f935bbdSAl Viro __be32 *psfsrc)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev;
18661da177e4SLinus Torvalds int rv = 0;
18671da177e4SLinus Torvalds
18681da177e4SLinus Torvalds psf_prev = NULL;
18691da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) {
18701da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc)
18711da177e4SLinus Torvalds break;
18721da177e4SLinus Torvalds psf_prev = psf;
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) {
18751da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */
18761da177e4SLinus Torvalds return -ESRCH;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds psf->sf_count[sfmode]--;
18791da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) {
18801da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface);
18811da177e4SLinus Torvalds }
18821da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
18831da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
18841da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface;
1885165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev);
18861da177e4SLinus Torvalds #endif
18871da177e4SLinus Torvalds
18881da177e4SLinus Torvalds /* no more filters for this source */
18891da177e4SLinus Torvalds if (psf_prev)
18901da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next;
18911da177e4SLinus Torvalds else
18921da177e4SLinus Torvalds pmc->sources = psf->sf_next;
18931da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
18941da177e4SLinus Torvalds if (psf->sf_oldin &&
18951da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
18968ebcc62cSKuniyuki Iwashima psf->sf_crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
18971da177e4SLinus Torvalds psf->sf_next = pmc->tomb;
18981da177e4SLinus Torvalds pmc->tomb = psf;
18991da177e4SLinus Torvalds rv = 1;
19001da177e4SLinus Torvalds } else
19011da177e4SLinus Torvalds #endif
19021da177e4SLinus Torvalds kfree(psf);
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds return rv;
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds
19071da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST
19081da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0)
19091da177e4SLinus Torvalds #endif
19101da177e4SLinus Torvalds
ip_mc_del_src(struct in_device * in_dev,__be32 * pmca,int sfmode,int sfcount,__be32 * psfsrc,int delta)19118f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
19128f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta)
19131da177e4SLinus Torvalds {
19141da177e4SLinus Torvalds struct ip_mc_list *pmc;
19151da177e4SLinus Torvalds int changerec = 0;
19161da177e4SLinus Torvalds int i, err;
19171da177e4SLinus Torvalds
19181da177e4SLinus Torvalds if (!in_dev)
19191da177e4SLinus Torvalds return -ENODEV;
19201d7138deSEric Dumazet rcu_read_lock();
19211d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) {
19221da177e4SLinus Torvalds if (*pmca == pmc->multiaddr)
19231da177e4SLinus Torvalds break;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds if (!pmc) {
19261da177e4SLinus Torvalds /* MCA not found?? bug */
19271d7138deSEric Dumazet rcu_read_unlock();
19281da177e4SLinus Torvalds return -ESRCH;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds spin_lock_bh(&pmc->lock);
19311d7138deSEric Dumazet rcu_read_unlock();
19321da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
19331da177e4SLinus Torvalds sf_markstate(pmc);
19341da177e4SLinus Torvalds #endif
19351da177e4SLinus Torvalds if (!delta) {
19361da177e4SLinus Torvalds err = -EINVAL;
19371da177e4SLinus Torvalds if (!pmc->sfcount[sfmode])
19381da177e4SLinus Torvalds goto out_unlock;
19391da177e4SLinus Torvalds pmc->sfcount[sfmode]--;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds err = 0;
19421da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) {
19431da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
19441da177e4SLinus Torvalds
19451da177e4SLinus Torvalds changerec |= rv > 0;
19461da177e4SLinus Torvalds if (!err && rv < 0)
19471da177e4SLinus Torvalds err = rv;
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE &&
19501da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 &&
19511da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) {
19521da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
19531da177e4SLinus Torvalds struct ip_sf_list *psf;
1954165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev);
19551da177e4SLinus Torvalds #endif
19561da177e4SLinus Torvalds
19571da177e4SLinus Torvalds /* filter mode change */
19581da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE;
19591da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
19608ebcc62cSKuniyuki Iwashima pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
19614a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
19621da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next)
19631da177e4SLinus Torvalds psf->sf_crcount = 0;
19641da177e4SLinus Torvalds igmp_ifc_event(pmc->interface);
19651da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) {
19661da177e4SLinus Torvalds igmp_ifc_event(pmc->interface);
19671da177e4SLinus Torvalds #endif
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds out_unlock:
19701da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock);
19711da177e4SLinus Torvalds return err;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds
19741da177e4SLinus Torvalds /*
19751da177e4SLinus Torvalds * Add multicast single-source filter to the interface list
19761da177e4SLinus Torvalds */
ip_mc_add1_src(struct ip_mc_list * pmc,int sfmode,__be32 * psfsrc)19771da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
19785eb81e89SJun Zhao __be32 *psfsrc)
19791da177e4SLinus Torvalds {
19801da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev;
19811da177e4SLinus Torvalds
19821da177e4SLinus Torvalds psf_prev = NULL;
19831da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) {
19841da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc)
19851da177e4SLinus Torvalds break;
19861da177e4SLinus Torvalds psf_prev = psf;
19871da177e4SLinus Torvalds }
19881da177e4SLinus Torvalds if (!psf) {
19890da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
19901da177e4SLinus Torvalds if (!psf)
19911da177e4SLinus Torvalds return -ENOBUFS;
19921da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc;
19931da177e4SLinus Torvalds if (psf_prev) {
19941da177e4SLinus Torvalds psf_prev->sf_next = psf;
19951da177e4SLinus Torvalds } else
19961da177e4SLinus Torvalds pmc->sources = psf;
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds psf->sf_count[sfmode]++;
19991da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) {
20001da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface);
20011da177e4SLinus Torvalds }
20021da177e4SLinus Torvalds return 0;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds
20051da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
sf_markstate(struct ip_mc_list * pmc)20061da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc)
20071da177e4SLinus Torvalds {
20081da177e4SLinus Torvalds struct ip_sf_list *psf;
20091da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
20101da177e4SLinus Torvalds
20111da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next)
20121da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) {
20131da177e4SLinus Torvalds psf->sf_oldin = mca_xcount ==
20141da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] &&
20151da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE];
20161da177e4SLinus Torvalds } else
20171da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
20181da177e4SLinus Torvalds }
20191da177e4SLinus Torvalds
sf_setstate(struct ip_mc_list * pmc)20201da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc)
20211da177e4SLinus Torvalds {
2022ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf;
20231da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
20241da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv;
20251da177e4SLinus Torvalds int new_in, rv;
20261da177e4SLinus Torvalds
20271da177e4SLinus Torvalds rv = 0;
20281da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) {
20291da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) {
20301da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
20311da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE];
20321da177e4SLinus Torvalds } else
20331da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2034ad12583fSDavid L Stevens if (new_in) {
2035ad12583fSDavid L Stevens if (!psf->sf_oldin) {
203676edc605SAl Viro struct ip_sf_list *prev = NULL;
2037ad12583fSDavid L Stevens
2038ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2039ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr)
2040ad12583fSDavid L Stevens break;
2041ad12583fSDavid L Stevens prev = dpsf;
2042ad12583fSDavid L Stevens }
2043ad12583fSDavid L Stevens if (dpsf) {
2044ad12583fSDavid L Stevens if (prev)
2045ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next;
2046ad12583fSDavid L Stevens else
2047ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next;
2048ad12583fSDavid L Stevens kfree(dpsf);
2049ad12583fSDavid L Stevens }
20501da177e4SLinus Torvalds psf->sf_crcount = qrv;
20511da177e4SLinus Torvalds rv++;
20521da177e4SLinus Torvalds }
2053ad12583fSDavid L Stevens } else if (psf->sf_oldin) {
2054ad12583fSDavid L Stevens
2055ad12583fSDavid L Stevens psf->sf_crcount = 0;
2056ad12583fSDavid L Stevens /*
2057ad12583fSDavid L Stevens * add or update "delete" records if an active filter
2058ad12583fSDavid L Stevens * is now inactive
2059ad12583fSDavid L Stevens */
2060ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2061ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr)
2062ad12583fSDavid L Stevens break;
2063ad12583fSDavid L Stevens if (!dpsf) {
20643ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2065ad12583fSDavid L Stevens if (!dpsf)
2066ad12583fSDavid L Stevens continue;
2067ad12583fSDavid L Stevens *dpsf = *psf;
2068ad12583fSDavid L Stevens /* pmc->lock held by callers */
2069ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb;
2070ad12583fSDavid L Stevens pmc->tomb = dpsf;
2071ad12583fSDavid L Stevens }
2072ad12583fSDavid L Stevens dpsf->sf_crcount = qrv;
2073ad12583fSDavid L Stevens rv++;
2074ad12583fSDavid L Stevens }
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds return rv;
20771da177e4SLinus Torvalds }
20781da177e4SLinus Torvalds #endif
20791da177e4SLinus Torvalds
20801da177e4SLinus Torvalds /*
20811da177e4SLinus Torvalds * Add multicast source filter list to the interface list
20821da177e4SLinus Torvalds */
ip_mc_add_src(struct in_device * in_dev,__be32 * pmca,int sfmode,int sfcount,__be32 * psfsrc,int delta)20838f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
20848f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta)
20851da177e4SLinus Torvalds {
20861da177e4SLinus Torvalds struct ip_mc_list *pmc;
20871da177e4SLinus Torvalds int isexclude;
20881da177e4SLinus Torvalds int i, err;
20891da177e4SLinus Torvalds
20901da177e4SLinus Torvalds if (!in_dev)
20911da177e4SLinus Torvalds return -ENODEV;
20921d7138deSEric Dumazet rcu_read_lock();
20931d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) {
20941da177e4SLinus Torvalds if (*pmca == pmc->multiaddr)
20951da177e4SLinus Torvalds break;
20961da177e4SLinus Torvalds }
20971da177e4SLinus Torvalds if (!pmc) {
20981da177e4SLinus Torvalds /* MCA not found?? bug */
20991d7138deSEric Dumazet rcu_read_unlock();
21001da177e4SLinus Torvalds return -ESRCH;
21011da177e4SLinus Torvalds }
21021da177e4SLinus Torvalds spin_lock_bh(&pmc->lock);
21031d7138deSEric Dumazet rcu_read_unlock();
21041da177e4SLinus Torvalds
21051da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
21061da177e4SLinus Torvalds sf_markstate(pmc);
21071da177e4SLinus Torvalds #endif
21081da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE;
21091da177e4SLinus Torvalds if (!delta)
21101da177e4SLinus Torvalds pmc->sfcount[sfmode]++;
21111da177e4SLinus Torvalds err = 0;
21121da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) {
21135eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
21141da177e4SLinus Torvalds if (err)
21151da177e4SLinus Torvalds break;
21161da177e4SLinus Torvalds }
21171da177e4SLinus Torvalds if (err) {
21181da177e4SLinus Torvalds int j;
21191da177e4SLinus Torvalds
2120685f94e6SJun Zhao if (!delta)
21211da177e4SLinus Torvalds pmc->sfcount[sfmode]--;
21221da177e4SLinus Torvalds for (j = 0; j < i; j++)
2123a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
21241da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
21251da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
21261da177e4SLinus Torvalds struct ip_sf_list *psf;
2127165094afSNikolay Borisov struct net *net = dev_net(pmc->interface->dev);
2128cfcabdccSStephen Hemminger in_dev = pmc->interface;
21291da177e4SLinus Torvalds #endif
21301da177e4SLinus Torvalds
21311da177e4SLinus Torvalds /* filter mode change */
21321da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE])
21331da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE;
21341da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE])
21351da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE;
21361da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
21371da177e4SLinus Torvalds /* else no filters; keep old mode for reports */
21381da177e4SLinus Torvalds
21398ebcc62cSKuniyuki Iwashima pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
21404a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
21411da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next)
21421da177e4SLinus Torvalds psf->sf_crcount = 0;
21431da177e4SLinus Torvalds igmp_ifc_event(in_dev);
21441da177e4SLinus Torvalds } else if (sf_setstate(pmc)) {
21451da177e4SLinus Torvalds igmp_ifc_event(in_dev);
21461da177e4SLinus Torvalds #endif
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock);
21491da177e4SLinus Torvalds return err;
21501da177e4SLinus Torvalds }
21511da177e4SLinus Torvalds
ip_mc_clear_src(struct ip_mc_list * pmc)21521da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc)
21531da177e4SLinus Torvalds {
21543580d04aSEric Dumazet struct ip_sf_list *tomb, *sources;
21551da177e4SLinus Torvalds
2156c38b7d32SWANG Cong spin_lock_bh(&pmc->lock);
2157c38b7d32SWANG Cong tomb = pmc->tomb;
21581da177e4SLinus Torvalds pmc->tomb = NULL;
2159c38b7d32SWANG Cong sources = pmc->sources;
21601da177e4SLinus Torvalds pmc->sources = NULL;
21611da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE;
2162de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0;
21631da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1;
2164c38b7d32SWANG Cong spin_unlock_bh(&pmc->lock);
2165c38b7d32SWANG Cong
21663580d04aSEric Dumazet ip_sf_list_clear_all(tomb);
21673580d04aSEric Dumazet ip_sf_list_clear_all(sources);
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds
217054ff9ef3SMarcelo Ricardo Leitner /* Join a multicast group
217154ff9ef3SMarcelo Ricardo Leitner */
__ip_mc_join_group(struct sock * sk,struct ip_mreqn * imr,unsigned int mode)21726e2059b5SHangbin Liu static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
21736e2059b5SHangbin Liu unsigned int mode)
21741da177e4SLinus Torvalds {
21758f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr;
2176959d10f6SEric Dumazet struct ip_mc_socklist *iml, *i;
21771da177e4SLinus Torvalds struct in_device *in_dev;
21781da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
2179877acedcSDaniel Lezcano struct net *net = sock_net(sk);
2180ca9b907dSDavid L Stevens int ifindex;
21811da177e4SLinus Torvalds int count = 0;
2182959d10f6SEric Dumazet int err;
2183959d10f6SEric Dumazet
2184959d10f6SEric Dumazet ASSERT_RTNL();
21851da177e4SLinus Torvalds
2186f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr))
21871da177e4SLinus Torvalds return -EINVAL;
21881da177e4SLinus Torvalds
2189877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr);
21901da177e4SLinus Torvalds
21911da177e4SLinus Torvalds if (!in_dev) {
21921da177e4SLinus Torvalds err = -ENODEV;
21931da177e4SLinus Torvalds goto done;
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds
21961da177e4SLinus Torvalds err = -EADDRINUSE;
2197ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex;
21981d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) {
2199ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr &&
2200ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex)
22011da177e4SLinus Torvalds goto done;
22021da177e4SLinus Torvalds count++;
22031da177e4SLinus Torvalds }
22041da177e4SLinus Torvalds err = -ENOBUFS;
22056305d821SKuniyuki Iwashima if (count >= READ_ONCE(net->ipv4.sysctl_igmp_max_memberships))
22061da177e4SLinus Torvalds goto done;
22078b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
220851456b29SIan Morris if (!iml)
2209ca9b907dSDavid L Stevens goto done;
2210ca9b907dSDavid L Stevens
22111da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr));
22121d7138deSEric Dumazet iml->next_rcu = inet->mc_list;
22131da177e4SLinus Torvalds iml->sflist = NULL;
22146e2059b5SHangbin Liu iml->sfmode = mode;
2215cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml);
2216a1c4cd67SLi RongQing ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL);
22171da177e4SLinus Torvalds err = 0;
22181da177e4SLinus Torvalds done:
22191da177e4SLinus Torvalds return err;
22201da177e4SLinus Torvalds }
22216e2059b5SHangbin Liu
22226e2059b5SHangbin Liu /* Join ASM (Any-Source Multicast) group
22236e2059b5SHangbin Liu */
ip_mc_join_group(struct sock * sk,struct ip_mreqn * imr)22246e2059b5SHangbin Liu int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
22256e2059b5SHangbin Liu {
22266e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
22276e2059b5SHangbin Liu }
22284bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group);
22291da177e4SLinus Torvalds
22306e2059b5SHangbin Liu /* Join SSM (Source-Specific Multicast) group
22316e2059b5SHangbin Liu */
ip_mc_join_group_ssm(struct sock * sk,struct ip_mreqn * imr,unsigned int mode)22326e2059b5SHangbin Liu int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
22336e2059b5SHangbin Liu unsigned int mode)
22346e2059b5SHangbin Liu {
22356e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, mode);
22366e2059b5SHangbin Liu }
22376e2059b5SHangbin Liu
ip_mc_leave_src(struct sock * sk,struct ip_mc_socklist * iml,struct in_device * in_dev)22381da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
22391da177e4SLinus Torvalds struct in_device *in_dev)
22401da177e4SLinus Torvalds {
22411d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
22421da177e4SLinus Torvalds int err;
22431da177e4SLinus Torvalds
224451456b29SIan Morris if (!psf) {
22451da177e4SLinus Torvalds /* any-source empty exclude case */
22461da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
22471da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0);
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2250c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2251a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL);
2252c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */
2253e6a1f7e0SGustavo A. R. Silva atomic_sub(struct_size(psf, sl_addr, psf->sl_max), &sk->sk_omem_alloc);
22547519cce4SLai Jiangshan kfree_rcu(psf, rcu);
22551da177e4SLinus Torvalds return err;
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds
ip_mc_leave_group(struct sock * sk,struct ip_mreqn * imr)225854ff9ef3SMarcelo Ricardo Leitner int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
22591da177e4SLinus Torvalds {
22601da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
22611d7138deSEric Dumazet struct ip_mc_socklist *iml;
22621d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp;
226384b42baeSDavid L Stevens struct in_device *in_dev;
2264877acedcSDaniel Lezcano struct net *net = sock_net(sk);
22658f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr;
226684b42baeSDavid L Stevens u32 ifindex;
2267acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL;
22681da177e4SLinus Torvalds
2269959d10f6SEric Dumazet ASSERT_RTNL();
2270959d10f6SEric Dumazet
2271877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr);
22724eba7bb1SAndrew Lunn if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
227352ad353aSdingtianhong ret = -ENODEV;
227452ad353aSdingtianhong goto out;
227552ad353aSdingtianhong }
227684b42baeSDavid L Stevens ifindex = imr->imr_ifindex;
22771d7138deSEric Dumazet for (imlp = &inet->mc_list;
22781d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL;
22791d7138deSEric Dumazet imlp = &iml->next_rcu) {
2280acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group)
2281acd6e00bSDavid L Stevens continue;
2282acd6e00bSDavid L Stevens if (ifindex) {
2283acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex)
2284acd6e00bSDavid L Stevens continue;
2285acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2286acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr)
2287acd6e00bSDavid L Stevens continue;
2288acd6e00bSDavid L Stevens
22891da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev);
22901da177e4SLinus Torvalds
22911d7138deSEric Dumazet *imlp = iml->next_rcu;
22921da177e4SLinus Torvalds
22934eba7bb1SAndrew Lunn if (in_dev)
229484b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group);
2295959d10f6SEric Dumazet
2296c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */
2297c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
229810d50e74SLai Jiangshan kfree_rcu(iml, rcu);
22991da177e4SLinus Torvalds return 0;
23001da177e4SLinus Torvalds }
230152ad353aSdingtianhong out:
2302959d10f6SEric Dumazet return ret;
2303959d10f6SEric Dumazet }
2304193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group);
23051da177e4SLinus Torvalds
ip_mc_source(int add,int omode,struct sock * sk,struct ip_mreq_source * mreqs,int ifindex)23061da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct
23071da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex)
23081da177e4SLinus Torvalds {
23091da177e4SLinus Torvalds int err;
23101da177e4SLinus Torvalds struct ip_mreqn imr;
231163007727SAl Viro __be32 addr = mreqs->imr_multiaddr;
23121da177e4SLinus Torvalds struct ip_mc_socklist *pmc;
23131da177e4SLinus Torvalds struct in_device *in_dev = NULL;
23141da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
23151da177e4SLinus Torvalds struct ip_sf_socklist *psl;
2316877acedcSDaniel Lezcano struct net *net = sock_net(sk);
23178cdaaa15SDavid L Stevens int leavegroup = 0;
23181da177e4SLinus Torvalds int i, j, rv;
23191da177e4SLinus Torvalds
2320f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr))
23211da177e4SLinus Torvalds return -EINVAL;
23221da177e4SLinus Torvalds
232354ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL();
23241da177e4SLinus Torvalds
23251da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
23261da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface;
23271da177e4SLinus Torvalds imr.imr_ifindex = ifindex;
2328877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr);
23291da177e4SLinus Torvalds
23301da177e4SLinus Torvalds if (!in_dev) {
23311da177e4SLinus Torvalds err = -ENODEV;
23321da177e4SLinus Torvalds goto done;
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds err = -EADDRNOTAVAIL;
23351da177e4SLinus Torvalds
23361d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) {
2337f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr ==
2338f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) &&
2339f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex))
23401da177e4SLinus Torvalds break;
23411da177e4SLinus Torvalds }
2342917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */
2343917f2f10SDavid L Stevens err = -EINVAL;
23441da177e4SLinus Torvalds goto done;
2345917f2f10SDavid L Stevens }
23461da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */
23471da177e4SLinus Torvalds if (pmc->sflist) {
2348917f2f10SDavid L Stevens if (pmc->sfmode != omode) {
2349917f2f10SDavid L Stevens err = -EINVAL;
23501da177e4SLinus Torvalds goto done;
2351917f2f10SDavid L Stevens }
23521da177e4SLinus Torvalds } else if (pmc->sfmode != omode) {
23531da177e4SLinus Torvalds /* allow mode switches for empty-set filters */
23541da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
23551da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
23561da177e4SLinus Torvalds NULL, 0);
23571da177e4SLinus Torvalds pmc->sfmode = omode;
23581da177e4SLinus Torvalds }
23591da177e4SLinus Torvalds
23601d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist);
23611da177e4SLinus Torvalds if (!add) {
23621da177e4SLinus Torvalds if (!psl)
2363917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */
23641da177e4SLinus Torvalds rv = !0;
23651da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) {
23661da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
236763007727SAl Viro sizeof(__be32));
23681da177e4SLinus Torvalds if (rv == 0)
23691da177e4SLinus Torvalds break;
23701da177e4SLinus Torvalds }
23711da177e4SLinus Torvalds if (rv) /* source not found */
2372917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */
23731da177e4SLinus Torvalds
23748cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */
23758cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
23768cdaaa15SDavid L Stevens leavegroup = 1;
23778cdaaa15SDavid L Stevens goto done;
23788cdaaa15SDavid L Stevens }
23798cdaaa15SDavid L Stevens
23801da177e4SLinus Torvalds /* update the interface filter */
23811da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
23821da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1);
23831da177e4SLinus Torvalds
23841da177e4SLinus Torvalds for (j = i+1; j < psl->sl_count; j++)
23851da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j];
23861da177e4SLinus Torvalds psl->sl_count--;
23871da177e4SLinus Torvalds err = 0;
23881da177e4SLinus Torvalds goto done;
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds /* else, add a new source to the filter */
23911da177e4SLinus Torvalds
23926ae0f2e5SKuniyuki Iwashima if (psl && psl->sl_count >= READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) {
23931da177e4SLinus Torvalds err = -ENOBUFS;
23941da177e4SLinus Torvalds goto done;
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) {
23971da177e4SLinus Torvalds struct ip_sf_socklist *newpsl;
23981da177e4SLinus Torvalds int count = IP_SFBLOCK;
23991da177e4SLinus Torvalds
24001da177e4SLinus Torvalds if (psl)
24011da177e4SLinus Torvalds count += psl->sl_max;
2402e6a1f7e0SGustavo A. R. Silva newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, count),
2403e6a1f7e0SGustavo A. R. Silva GFP_KERNEL);
24041da177e4SLinus Torvalds if (!newpsl) {
24051da177e4SLinus Torvalds err = -ENOBUFS;
24061da177e4SLinus Torvalds goto done;
24071da177e4SLinus Torvalds }
24081da177e4SLinus Torvalds newpsl->sl_max = count;
24091da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK;
24101da177e4SLinus Torvalds if (psl) {
24111da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++)
24121da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i];
2413c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */
2414e6a1f7e0SGustavo A. R. Silva atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
2415e6a1f7e0SGustavo A. R. Silva &sk->sk_omem_alloc);
24161da177e4SLinus Torvalds }
2417cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl);
2418dba5bdd5SEric Dumazet if (psl)
2419dba5bdd5SEric Dumazet kfree_rcu(psl, rcu);
2420c85bb41eSFlavio Leitner psl = newpsl;
24211da177e4SLinus Torvalds }
24221da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */
24231da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) {
24241da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
242563007727SAl Viro sizeof(__be32));
24261da177e4SLinus Torvalds if (rv == 0)
24271da177e4SLinus Torvalds break;
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */
24301da177e4SLinus Torvalds goto done;
24311da177e4SLinus Torvalds for (j = psl->sl_count-1; j >= i; j--)
24321da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j];
24331da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr;
24341da177e4SLinus Torvalds psl->sl_count++;
24351da177e4SLinus Torvalds err = 0;
24361da177e4SLinus Torvalds /* update the interface list */
24371da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
24381da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1);
24391da177e4SLinus Torvalds done:
24408cdaaa15SDavid L Stevens if (leavegroup)
244154ff9ef3SMarcelo Ricardo Leitner err = ip_mc_leave_group(sk, &imr);
24421da177e4SLinus Torvalds return err;
24431da177e4SLinus Torvalds }
24441da177e4SLinus Torvalds
ip_mc_msfilter(struct sock * sk,struct ip_msfilter * msf,int ifindex)24451da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
24461da177e4SLinus Torvalds {
24479951f036SDavid L Stevens int err = 0;
24481da177e4SLinus Torvalds struct ip_mreqn imr;
244963007727SAl Viro __be32 addr = msf->imsf_multiaddr;
24501da177e4SLinus Torvalds struct ip_mc_socklist *pmc;
24511da177e4SLinus Torvalds struct in_device *in_dev;
24521da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
24531da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl;
2454877acedcSDaniel Lezcano struct net *net = sock_net(sk);
24559951f036SDavid L Stevens int leavegroup = 0;
24561da177e4SLinus Torvalds
2457f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr))
24581da177e4SLinus Torvalds return -EINVAL;
24591da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE &&
24601da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE)
24611da177e4SLinus Torvalds return -EINVAL;
24621da177e4SLinus Torvalds
246354ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL();
24641da177e4SLinus Torvalds
24651da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
24661da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface;
24671da177e4SLinus Torvalds imr.imr_ifindex = ifindex;
2468877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr);
24691da177e4SLinus Torvalds
24701da177e4SLinus Torvalds if (!in_dev) {
24711da177e4SLinus Torvalds err = -ENODEV;
24721da177e4SLinus Torvalds goto done;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds
24759951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */
24769951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
24779951f036SDavid L Stevens leavegroup = 1;
24789951f036SDavid L Stevens goto done;
24799951f036SDavid L Stevens }
24809951f036SDavid L Stevens
24811d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) {
24821da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
24831da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex)
24841da177e4SLinus Torvalds break;
24851da177e4SLinus Torvalds }
2486917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */
2487917f2f10SDavid L Stevens err = -EINVAL;
24881da177e4SLinus Torvalds goto done;
2489917f2f10SDavid L Stevens }
24901da177e4SLinus Torvalds if (msf->imsf_numsrc) {
2491e6a1f7e0SGustavo A. R. Silva newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr,
2492e6a1f7e0SGustavo A. R. Silva msf->imsf_numsrc),
24938b3a7005SKris Katterjohn GFP_KERNEL);
24941da177e4SLinus Torvalds if (!newpsl) {
24951da177e4SLinus Torvalds err = -ENOBUFS;
24961da177e4SLinus Torvalds goto done;
24971da177e4SLinus Torvalds }
24981da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
24992d3e5cafSGustavo A. R. Silva memcpy(newpsl->sl_addr, msf->imsf_slist_flex,
25002d3e5cafSGustavo A. R. Silva flex_array_size(msf, imsf_slist_flex, msf->imsf_numsrc));
25011da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
25021da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
25031da177e4SLinus Torvalds if (err) {
2504e6a1f7e0SGustavo A. R. Silva sock_kfree_s(sk, newpsl,
2505e6a1f7e0SGustavo A. R. Silva struct_size(newpsl, sl_addr,
2506e6a1f7e0SGustavo A. R. Silva newpsl->sl_max));
25071da177e4SLinus Torvalds goto done;
25081da177e4SLinus Torvalds }
25098713dbf0SYan Zheng } else {
25101da177e4SLinus Torvalds newpsl = NULL;
25118713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
25128713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0);
25138713dbf0SYan Zheng }
25141d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist);
25151da177e4SLinus Torvalds if (psl) {
25161da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
25171da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0);
2518c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */
2519e6a1f7e0SGustavo A. R. Silva atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
2520e6a1f7e0SGustavo A. R. Silva &sk->sk_omem_alloc);
2521dba5bdd5SEric Dumazet } else {
25221da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
25231da177e4SLinus Torvalds 0, NULL, 0);
2524dba5bdd5SEric Dumazet }
2525cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl);
2526dba5bdd5SEric Dumazet if (psl)
2527dba5bdd5SEric Dumazet kfree_rcu(psl, rcu);
25281da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode;
2529917f2f10SDavid L Stevens err = 0;
25301da177e4SLinus Torvalds done:
25319951f036SDavid L Stevens if (leavegroup)
25329951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr);
25331da177e4SLinus Torvalds return err;
25341da177e4SLinus Torvalds }
ip_mc_msfget(struct sock * sk,struct ip_msfilter * msf,sockptr_t optval,sockptr_t optlen)25351da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2536728f064cSMartin KaFai Lau sockptr_t optval, sockptr_t optlen)
25371da177e4SLinus Torvalds {
2538728f064cSMartin KaFai Lau int err, len, count, copycount, msf_size;
25391da177e4SLinus Torvalds struct ip_mreqn imr;
254063007727SAl Viro __be32 addr = msf->imsf_multiaddr;
25411da177e4SLinus Torvalds struct ip_mc_socklist *pmc;
25421da177e4SLinus Torvalds struct in_device *in_dev;
25431da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
25441da177e4SLinus Torvalds struct ip_sf_socklist *psl;
2545877acedcSDaniel Lezcano struct net *net = sock_net(sk);
25461da177e4SLinus Torvalds
254787e9f031SWANG Cong ASSERT_RTNL();
254887e9f031SWANG Cong
2549f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr))
25501da177e4SLinus Torvalds return -EINVAL;
25511da177e4SLinus Torvalds
25521da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
25531da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface;
25541da177e4SLinus Torvalds imr.imr_ifindex = 0;
2555877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr);
25561da177e4SLinus Torvalds
25571da177e4SLinus Torvalds if (!in_dev) {
25581da177e4SLinus Torvalds err = -ENODEV;
25591da177e4SLinus Torvalds goto done;
25601da177e4SLinus Torvalds }
25611da177e4SLinus Torvalds err = -EADDRNOTAVAIL;
25621da177e4SLinus Torvalds
25631d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) {
25641da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
25651da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex)
25661da177e4SLinus Torvalds break;
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds if (!pmc) /* must have a prior join */
25691da177e4SLinus Torvalds goto done;
25701da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode;
25711d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist);
25721da177e4SLinus Torvalds if (!psl) {
25731da177e4SLinus Torvalds count = 0;
25741da177e4SLinus Torvalds } else {
25751da177e4SLinus Torvalds count = psl->sl_count;
25761da177e4SLinus Torvalds }
25771da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
25782d3e5cafSGustavo A. R. Silva len = flex_array_size(psl, sl_addr, copycount);
25791da177e4SLinus Torvalds msf->imsf_numsrc = count;
2580728f064cSMartin KaFai Lau msf_size = IP_MSFILTER_SIZE(copycount);
2581728f064cSMartin KaFai Lau if (copy_to_sockptr(optlen, &msf_size, sizeof(int)) ||
2582728f064cSMartin KaFai Lau copy_to_sockptr(optval, msf, IP_MSFILTER_SIZE(0))) {
25831da177e4SLinus Torvalds return -EFAULT;
25841da177e4SLinus Torvalds }
25851da177e4SLinus Torvalds if (len &&
2586728f064cSMartin KaFai Lau copy_to_sockptr_offset(optval,
2587728f064cSMartin KaFai Lau offsetof(struct ip_msfilter, imsf_slist_flex),
2588728f064cSMartin KaFai Lau psl->sl_addr, len))
25891da177e4SLinus Torvalds return -EFAULT;
25901da177e4SLinus Torvalds return 0;
25911da177e4SLinus Torvalds done:
25921da177e4SLinus Torvalds return err;
25931da177e4SLinus Torvalds }
25941da177e4SLinus Torvalds
ip_mc_gsfget(struct sock * sk,struct group_filter * gsf,sockptr_t optval,size_t ss_offset)25951da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2596728f064cSMartin KaFai Lau sockptr_t optval, size_t ss_offset)
25971da177e4SLinus Torvalds {
2598931ca7abSAl Viro int i, count, copycount;
25991da177e4SLinus Torvalds struct sockaddr_in *psin;
260063007727SAl Viro __be32 addr;
26011da177e4SLinus Torvalds struct ip_mc_socklist *pmc;
26021da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
26031da177e4SLinus Torvalds struct ip_sf_socklist *psl;
26041da177e4SLinus Torvalds
260587e9f031SWANG Cong ASSERT_RTNL();
260687e9f031SWANG Cong
26071da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group;
26081da177e4SLinus Torvalds if (psin->sin_family != AF_INET)
26091da177e4SLinus Torvalds return -EINVAL;
26101da177e4SLinus Torvalds addr = psin->sin_addr.s_addr;
2611f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr))
26121da177e4SLinus Torvalds return -EINVAL;
26131da177e4SLinus Torvalds
26141d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) {
26151da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr &&
26161da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface)
26171da177e4SLinus Torvalds break;
26181da177e4SLinus Torvalds }
26191da177e4SLinus Torvalds if (!pmc) /* must have a prior join */
2620931ca7abSAl Viro return -EADDRNOTAVAIL;
26211da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode;
26221d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist);
26231da177e4SLinus Torvalds count = psl ? psl->sl_count : 0;
26241da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
26251da177e4SLinus Torvalds gsf->gf_numsrc = count;
2626728f064cSMartin KaFai Lau for (i = 0; i < copycount; i++) {
26271da177e4SLinus Torvalds struct sockaddr_storage ss;
26281da177e4SLinus Torvalds
26291da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss;
26301da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss));
26311da177e4SLinus Torvalds psin->sin_family = AF_INET;
26321da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i];
2633728f064cSMartin KaFai Lau if (copy_to_sockptr_offset(optval, ss_offset,
2634728f064cSMartin KaFai Lau &ss, sizeof(ss)))
26351da177e4SLinus Torvalds return -EFAULT;
2636728f064cSMartin KaFai Lau ss_offset += sizeof(ss);
26371da177e4SLinus Torvalds }
26381da177e4SLinus Torvalds return 0;
26391da177e4SLinus Torvalds }
26401da177e4SLinus Torvalds
26411da177e4SLinus Torvalds /*
26421da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf>
26431da177e4SLinus Torvalds */
ip_mc_sf_allow(const struct sock * sk,__be32 loc_addr,__be32 rmt_addr,int dif,int sdif)264433e972bdSEric Dumazet int ip_mc_sf_allow(const struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
264560d9b031SDavid Ahern int dif, int sdif)
26461da177e4SLinus Torvalds {
264733e972bdSEric Dumazet const struct inet_sock *inet = inet_sk(sk);
26481da177e4SLinus Torvalds struct ip_mc_socklist *pmc;
26491da177e4SLinus Torvalds struct ip_sf_socklist *psl;
26501da177e4SLinus Torvalds int i;
2651c85bb41eSFlavio Leitner int ret;
26521da177e4SLinus Torvalds
2653c85bb41eSFlavio Leitner ret = 1;
2654f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr))
2655c85bb41eSFlavio Leitner goto out;
26561da177e4SLinus Torvalds
2657c85bb41eSFlavio Leitner rcu_read_lock();
26581d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) {
26591da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
266060d9b031SDavid Ahern (pmc->multi.imr_ifindex == dif ||
266160d9b031SDavid Ahern (sdif && pmc->multi.imr_ifindex == sdif)))
26621da177e4SLinus Torvalds break;
26631da177e4SLinus Torvalds }
2664307b4ac6SEric Dumazet ret = inet_test_bit(MC_ALL, sk);
26651da177e4SLinus Torvalds if (!pmc)
2666c85bb41eSFlavio Leitner goto unlock;
26671d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist);
2668c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE);
26691da177e4SLinus Torvalds if (!psl)
2670c85bb41eSFlavio Leitner goto unlock;
26711da177e4SLinus Torvalds
26721da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) {
26731da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr)
26741da177e4SLinus Torvalds break;
26751da177e4SLinus Torvalds }
2676c85bb41eSFlavio Leitner ret = 0;
26771da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2678c85bb41eSFlavio Leitner goto unlock;
26791da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2680c85bb41eSFlavio Leitner goto unlock;
2681c85bb41eSFlavio Leitner ret = 1;
2682c85bb41eSFlavio Leitner unlock:
2683c85bb41eSFlavio Leitner rcu_read_unlock();
2684c85bb41eSFlavio Leitner out:
2685c85bb41eSFlavio Leitner return ret;
26861da177e4SLinus Torvalds }
26871da177e4SLinus Torvalds
26881da177e4SLinus Torvalds /*
26891da177e4SLinus Torvalds * A socket is closing.
26901da177e4SLinus Torvalds */
26911da177e4SLinus Torvalds
ip_mc_drop_socket(struct sock * sk)26921da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk)
26931da177e4SLinus Torvalds {
26941da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
26951da177e4SLinus Torvalds struct ip_mc_socklist *iml;
2696877acedcSDaniel Lezcano struct net *net = sock_net(sk);
26971da177e4SLinus Torvalds
269851456b29SIan Morris if (!inet->mc_list)
26991da177e4SLinus Torvalds return;
27001da177e4SLinus Torvalds
27011da177e4SLinus Torvalds rtnl_lock();
27021d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
27031da177e4SLinus Torvalds struct in_device *in_dev;
27041da177e4SLinus Torvalds
27051d7138deSEric Dumazet inet->mc_list = iml->next_rcu;
2706877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
27071da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev);
270800db4124SIan Morris if (in_dev)
27091da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2710c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */
2711c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
271210d50e74SLai Jiangshan kfree_rcu(iml, rcu);
27131da177e4SLinus Torvalds }
27141da177e4SLinus Torvalds rtnl_unlock();
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds
2717dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */
ip_check_mc_rcu(struct in_device * in_dev,__be32 mc_addr,__be32 src_addr,u8 proto)27182094acbbSAlexander Duyck int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
27191da177e4SLinus Torvalds {
27201da177e4SLinus Torvalds struct ip_mc_list *im;
2721e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash;
27221da177e4SLinus Torvalds struct ip_sf_list *psf;
27231da177e4SLinus Torvalds int rv = 0;
27241da177e4SLinus Torvalds
2725e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash);
2726e9897071SEric Dumazet if (mc_hash) {
2727c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2728e9897071SEric Dumazet
2729e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]);
2730e9897071SEric Dumazet im != NULL;
2731e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) {
2732e9897071SEric Dumazet if (im->multiaddr == mc_addr)
2733e9897071SEric Dumazet break;
2734e9897071SEric Dumazet }
2735e9897071SEric Dumazet } else {
27361d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) {
27371da177e4SLinus Torvalds if (im->multiaddr == mc_addr)
27381da177e4SLinus Torvalds break;
27391da177e4SLinus Torvalds }
2740e9897071SEric Dumazet }
27411da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) {
27421da177e4SLinus Torvalds rv = 1;
27431da177e4SLinus Torvalds } else if (im) {
27441da177e4SLinus Torvalds if (src_addr) {
274523d2b940SLiu Jian spin_lock_bh(&im->lock);
27461da177e4SLinus Torvalds for (psf = im->sources; psf; psf = psf->sf_next) {
27471da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr)
27481da177e4SLinus Torvalds break;
27491da177e4SLinus Torvalds }
27501da177e4SLinus Torvalds if (psf)
27511da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] ||
27521da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] !=
27531da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE];
27541da177e4SLinus Torvalds else
27551da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0;
275623d2b940SLiu Jian spin_unlock_bh(&im->lock);
27571da177e4SLinus Torvalds } else
27581da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */
27591da177e4SLinus Torvalds }
27601da177e4SLinus Torvalds return rv;
27611da177e4SLinus Torvalds }
27621da177e4SLinus Torvalds
27631da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS)
27641da177e4SLinus Torvalds struct igmp_mc_iter_state {
27657091e728SAlexey Dobriyan struct seq_net_private p;
27661da177e4SLinus Torvalds struct net_device *dev;
27671da177e4SLinus Torvalds struct in_device *in_dev;
27681da177e4SLinus Torvalds };
27691da177e4SLinus Torvalds
27701da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
27711da177e4SLinus Torvalds
igmp_mc_get_first(struct seq_file * seq)27721da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
27731da177e4SLinus Torvalds {
27747091e728SAlexey Dobriyan struct net *net = seq_file_net(seq);
27751da177e4SLinus Torvalds struct ip_mc_list *im = NULL;
27761da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
27771da177e4SLinus Torvalds
27787562f876SPavel Emelianov state->in_dev = NULL;
277961fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) {
27801da177e4SLinus Torvalds struct in_device *in_dev;
27816baff150SEric Dumazet
27826baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev);
27831da177e4SLinus Torvalds if (!in_dev)
27841da177e4SLinus Torvalds continue;
27851d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list);
27861da177e4SLinus Torvalds if (im) {
27871da177e4SLinus Torvalds state->in_dev = in_dev;
27881da177e4SLinus Torvalds break;
27891da177e4SLinus Torvalds }
27901da177e4SLinus Torvalds }
27911da177e4SLinus Torvalds return im;
27921da177e4SLinus Torvalds }
27931da177e4SLinus Torvalds
igmp_mc_get_next(struct seq_file * seq,struct ip_mc_list * im)27941da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
27951da177e4SLinus Torvalds {
27961da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
27976baff150SEric Dumazet
27981d7138deSEric Dumazet im = rcu_dereference(im->next_rcu);
27991d7138deSEric Dumazet while (!im) {
28006baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev);
28011da177e4SLinus Torvalds if (!state->dev) {
28021da177e4SLinus Torvalds state->in_dev = NULL;
28031da177e4SLinus Torvalds break;
28041da177e4SLinus Torvalds }
28056baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev);
28061da177e4SLinus Torvalds if (!state->in_dev)
28071da177e4SLinus Torvalds continue;
28081d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list);
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds return im;
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds
igmp_mc_get_idx(struct seq_file * seq,loff_t pos)28131da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
28141da177e4SLinus Torvalds {
28151da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq);
28161da177e4SLinus Torvalds if (im)
28171da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
28181da177e4SLinus Torvalds --pos;
28191da177e4SLinus Torvalds return pos ? NULL : im;
28201da177e4SLinus Torvalds }
28211da177e4SLinus Torvalds
igmp_mc_seq_start(struct seq_file * seq,loff_t * pos)28221da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
282361fbab77Sstephen hemminger __acquires(rcu)
28241da177e4SLinus Torvalds {
282561fbab77Sstephen hemminger rcu_read_lock();
28261da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
28271da177e4SLinus Torvalds }
28281da177e4SLinus Torvalds
igmp_mc_seq_next(struct seq_file * seq,void * v,loff_t * pos)28291da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
28301da177e4SLinus Torvalds {
28311da177e4SLinus Torvalds struct ip_mc_list *im;
28321da177e4SLinus Torvalds if (v == SEQ_START_TOKEN)
28331da177e4SLinus Torvalds im = igmp_mc_get_first(seq);
28341da177e4SLinus Torvalds else
28351da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v);
28361da177e4SLinus Torvalds ++*pos;
28371da177e4SLinus Torvalds return im;
28381da177e4SLinus Torvalds }
28391da177e4SLinus Torvalds
igmp_mc_seq_stop(struct seq_file * seq,void * v)28401da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
284161fbab77Sstephen hemminger __releases(rcu)
28421da177e4SLinus Torvalds {
28431da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
28441d7138deSEric Dumazet
28451da177e4SLinus Torvalds state->in_dev = NULL;
28461da177e4SLinus Torvalds state->dev = NULL;
284761fbab77Sstephen hemminger rcu_read_unlock();
28481da177e4SLinus Torvalds }
28491da177e4SLinus Torvalds
igmp_mc_seq_show(struct seq_file * seq,void * v)28501da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v)
28511da177e4SLinus Torvalds {
28521da177e4SLinus Torvalds if (v == SEQ_START_TOKEN)
28531da177e4SLinus Torvalds seq_puts(seq,
28541da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
28551da177e4SLinus Torvalds else {
28562e47eeceSYu Zhe struct ip_mc_list *im = v;
28571da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
28581da177e4SLinus Torvalds char *querier;
2859a399a805SEric Dumazet long delta;
2860a399a805SEric Dumazet
28611da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
28621da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
28631da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" :
28641da177e4SLinus Torvalds "V3";
28651da177e4SLinus Torvalds #else
28661da177e4SLinus Torvalds querier = "NONE";
28671da177e4SLinus Torvalds #endif
28681da177e4SLinus Torvalds
2869e6b68883SAndreea-Cristina Bernat if (rcu_access_pointer(state->in_dev->mc_list) == im) {
28701da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2871b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
28721da177e4SLinus Torvalds }
28731da177e4SLinus Torvalds
2874a399a805SEric Dumazet delta = im->timer.expires - jiffies;
28751da177e4SLinus Torvalds seq_printf(seq,
2876338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
28771da177e4SLinus Torvalds im->multiaddr, im->users,
2878a399a805SEric Dumazet im->tm_running,
2879a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
28801da177e4SLinus Torvalds im->reporter);
28811da177e4SLinus Torvalds }
28821da177e4SLinus Torvalds return 0;
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds
2885f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = {
28861da177e4SLinus Torvalds .start = igmp_mc_seq_start,
28871da177e4SLinus Torvalds .next = igmp_mc_seq_next,
28881da177e4SLinus Torvalds .stop = igmp_mc_seq_stop,
28891da177e4SLinus Torvalds .show = igmp_mc_seq_show,
28901da177e4SLinus Torvalds };
28911da177e4SLinus Torvalds
28921da177e4SLinus Torvalds struct igmp_mcf_iter_state {
28937091e728SAlexey Dobriyan struct seq_net_private p;
28941da177e4SLinus Torvalds struct net_device *dev;
28951da177e4SLinus Torvalds struct in_device *idev;
28961da177e4SLinus Torvalds struct ip_mc_list *im;
28971da177e4SLinus Torvalds };
28981da177e4SLinus Torvalds
28991da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
29001da177e4SLinus Torvalds
igmp_mcf_get_first(struct seq_file * seq)29011da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
29021da177e4SLinus Torvalds {
29037091e728SAlexey Dobriyan struct net *net = seq_file_net(seq);
29041da177e4SLinus Torvalds struct ip_sf_list *psf = NULL;
29051da177e4SLinus Torvalds struct ip_mc_list *im = NULL;
29061da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
29071da177e4SLinus Torvalds
29087562f876SPavel Emelianov state->idev = NULL;
29097562f876SPavel Emelianov state->im = NULL;
291061fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) {
29111da177e4SLinus Torvalds struct in_device *idev;
29126baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev);
291351456b29SIan Morris if (unlikely(!idev))
29141da177e4SLinus Torvalds continue;
29151d7138deSEric Dumazet im = rcu_dereference(idev->mc_list);
291600db4124SIan Morris if (likely(im)) {
29171da177e4SLinus Torvalds spin_lock_bh(&im->lock);
29181da177e4SLinus Torvalds psf = im->sources;
291900db4124SIan Morris if (likely(psf)) {
29201da177e4SLinus Torvalds state->im = im;
29211da177e4SLinus Torvalds state->idev = idev;
29221da177e4SLinus Torvalds break;
29231da177e4SLinus Torvalds }
29241da177e4SLinus Torvalds spin_unlock_bh(&im->lock);
29251da177e4SLinus Torvalds }
29261da177e4SLinus Torvalds }
29271da177e4SLinus Torvalds return psf;
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds
igmp_mcf_get_next(struct seq_file * seq,struct ip_sf_list * psf)29301da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
29311da177e4SLinus Torvalds {
29321da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
29331da177e4SLinus Torvalds
29341da177e4SLinus Torvalds psf = psf->sf_next;
29351da177e4SLinus Torvalds while (!psf) {
29361da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock);
29371da177e4SLinus Torvalds state->im = state->im->next;
29381da177e4SLinus Torvalds while (!state->im) {
29396baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev);
29401da177e4SLinus Torvalds if (!state->dev) {
29411da177e4SLinus Torvalds state->idev = NULL;
29421da177e4SLinus Torvalds goto out;
29431da177e4SLinus Torvalds }
29446baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev);
29451da177e4SLinus Torvalds if (!state->idev)
29461da177e4SLinus Torvalds continue;
29471d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list);
29481da177e4SLinus Torvalds }
29491da177e4SLinus Torvalds if (!state->im)
29501da177e4SLinus Torvalds break;
29511da177e4SLinus Torvalds spin_lock_bh(&state->im->lock);
29521da177e4SLinus Torvalds psf = state->im->sources;
29531da177e4SLinus Torvalds }
29541da177e4SLinus Torvalds out:
29551da177e4SLinus Torvalds return psf;
29561da177e4SLinus Torvalds }
29571da177e4SLinus Torvalds
igmp_mcf_get_idx(struct seq_file * seq,loff_t pos)29581da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
29591da177e4SLinus Torvalds {
29601da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq);
29611da177e4SLinus Torvalds if (psf)
29621da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
29631da177e4SLinus Torvalds --pos;
29641da177e4SLinus Torvalds return pos ? NULL : psf;
29651da177e4SLinus Torvalds }
29661da177e4SLinus Torvalds
igmp_mcf_seq_start(struct seq_file * seq,loff_t * pos)29671da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
296861fbab77Sstephen hemminger __acquires(rcu)
29691da177e4SLinus Torvalds {
297061fbab77Sstephen hemminger rcu_read_lock();
29711da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
29721da177e4SLinus Torvalds }
29731da177e4SLinus Torvalds
igmp_mcf_seq_next(struct seq_file * seq,void * v,loff_t * pos)29741da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
29751da177e4SLinus Torvalds {
29761da177e4SLinus Torvalds struct ip_sf_list *psf;
29771da177e4SLinus Torvalds if (v == SEQ_START_TOKEN)
29781da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq);
29791da177e4SLinus Torvalds else
29801da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v);
29811da177e4SLinus Torvalds ++*pos;
29821da177e4SLinus Torvalds return psf;
29831da177e4SLinus Torvalds }
29841da177e4SLinus Torvalds
igmp_mcf_seq_stop(struct seq_file * seq,void * v)29851da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
298661fbab77Sstephen hemminger __releases(rcu)
29871da177e4SLinus Torvalds {
29881da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
298900db4124SIan Morris if (likely(state->im)) {
29901da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock);
29911da177e4SLinus Torvalds state->im = NULL;
29921da177e4SLinus Torvalds }
29931da177e4SLinus Torvalds state->idev = NULL;
29941da177e4SLinus Torvalds state->dev = NULL;
299561fbab77Sstephen hemminger rcu_read_unlock();
29961da177e4SLinus Torvalds }
29971da177e4SLinus Torvalds
igmp_mcf_seq_show(struct seq_file * seq,void * v)29981da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
29991da177e4SLinus Torvalds {
30002e47eeceSYu Zhe struct ip_sf_list *psf = v;
30011da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
30021da177e4SLinus Torvalds
30031da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) {
30041744bea1SJoe Perches seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
30051da177e4SLinus Torvalds } else {
30061da177e4SLinus Torvalds seq_printf(seq,
30071da177e4SLinus Torvalds "%3d %6.6s 0x%08x "
30081da177e4SLinus Torvalds "0x%08x %6lu %6lu\n",
30091da177e4SLinus Torvalds state->dev->ifindex, state->dev->name,
30101da177e4SLinus Torvalds ntohl(state->im->multiaddr),
30111da177e4SLinus Torvalds ntohl(psf->sf_inaddr),
30121da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE],
30131da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]);
30141da177e4SLinus Torvalds }
30151da177e4SLinus Torvalds return 0;
30161da177e4SLinus Torvalds }
30171da177e4SLinus Torvalds
3018f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = {
30191da177e4SLinus Torvalds .start = igmp_mcf_seq_start,
30201da177e4SLinus Torvalds .next = igmp_mcf_seq_next,
30211da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop,
30221da177e4SLinus Torvalds .show = igmp_mcf_seq_show,
30231da177e4SLinus Torvalds };
30241da177e4SLinus Torvalds
igmp_net_init(struct net * net)30252c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net)
30267091e728SAlexey Dobriyan {
30277091e728SAlexey Dobriyan struct proc_dir_entry *pde;
302893a714d6SMadhu Challa int err;
30297091e728SAlexey Dobriyan
3030c3506372SChristoph Hellwig pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3031c3506372SChristoph Hellwig sizeof(struct igmp_mc_iter_state));
30327091e728SAlexey Dobriyan if (!pde)
30337091e728SAlexey Dobriyan goto out_igmp;
3034c3506372SChristoph Hellwig pde = proc_create_net("mcfilter", 0444, net->proc_net,
3035c3506372SChristoph Hellwig &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
30367091e728SAlexey Dobriyan if (!pde)
30377091e728SAlexey Dobriyan goto out_mcfilter;
303893a714d6SMadhu Challa err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
303993a714d6SMadhu Challa SOCK_DGRAM, 0, net);
304093a714d6SMadhu Challa if (err < 0) {
304193a714d6SMadhu Challa pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
304293a714d6SMadhu Challa err);
304393a714d6SMadhu Challa goto out_sock;
304493a714d6SMadhu Challa }
304593a714d6SMadhu Challa
30467091e728SAlexey Dobriyan return 0;
30477091e728SAlexey Dobriyan
304893a714d6SMadhu Challa out_sock:
304993a714d6SMadhu Challa remove_proc_entry("mcfilter", net->proc_net);
30507091e728SAlexey Dobriyan out_mcfilter:
3051ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net);
30527091e728SAlexey Dobriyan out_igmp:
30537091e728SAlexey Dobriyan return -ENOMEM;
30547091e728SAlexey Dobriyan }
30557091e728SAlexey Dobriyan
igmp_net_exit(struct net * net)30562c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net)
30577091e728SAlexey Dobriyan {
3058ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net);
3059ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net);
306093a714d6SMadhu Challa inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
30617091e728SAlexey Dobriyan }
30627091e728SAlexey Dobriyan
30637091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = {
30647091e728SAlexey Dobriyan .init = igmp_net_init,
30657091e728SAlexey Dobriyan .exit = igmp_net_exit,
30661da177e4SLinus Torvalds };
306772c1d3bdSWANG Cong #endif
30681da177e4SLinus Torvalds
igmp_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)30694aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this,
30704aa5dee4SJiri Pirko unsigned long event, void *ptr)
30714aa5dee4SJiri Pirko {
30724aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr);
30734aa5dee4SJiri Pirko struct in_device *in_dev;
30744aa5dee4SJiri Pirko
30754aa5dee4SJiri Pirko switch (event) {
30764aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP:
30774aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev);
30784aa5dee4SJiri Pirko if (in_dev)
30794aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev);
30804aa5dee4SJiri Pirko break;
30814aa5dee4SJiri Pirko default:
30824aa5dee4SJiri Pirko break;
30834aa5dee4SJiri Pirko }
30844aa5dee4SJiri Pirko return NOTIFY_DONE;
30854aa5dee4SJiri Pirko }
30864aa5dee4SJiri Pirko
30874aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = {
30884aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event,
30894aa5dee4SJiri Pirko };
30904aa5dee4SJiri Pirko
igmp_mc_init(void)309172c1d3bdSWANG Cong int __init igmp_mc_init(void)
30921da177e4SLinus Torvalds {
309372c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS)
30944aa5dee4SJiri Pirko int err;
30954aa5dee4SJiri Pirko
30964aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops);
30974aa5dee4SJiri Pirko if (err)
30984aa5dee4SJiri Pirko return err;
30994aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier);
31004aa5dee4SJiri Pirko if (err)
31014aa5dee4SJiri Pirko goto reg_notif_fail;
31024aa5dee4SJiri Pirko return 0;
31034aa5dee4SJiri Pirko
31044aa5dee4SJiri Pirko reg_notif_fail:
31054aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops);
31064aa5dee4SJiri Pirko return err;
310772c1d3bdSWANG Cong #else
310872c1d3bdSWANG Cong return register_netdevice_notifier(&igmp_notifier);
31091da177e4SLinus Torvalds #endif
311072c1d3bdSWANG Cong }
3111