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 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 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 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 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 */ 2141da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay) 2151da177e4SLinus Torvalds { 21663862b5bSAruna-Hewapathirane int tv = prandom_u32() % max_delay; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds im->tm_running = 1; 2191da177e4SLinus Torvalds if (!mod_timer(&im->timer, jiffies+tv+2)) 2208851ab52SReshetova, Elena refcount_inc(&im->refcnt); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev) 2241da177e4SLinus Torvalds { 22563862b5bSAruna-Hewapathirane int tv = prandom_u32() % in_dev->mr_maxdelay; 2267ababb78SMichal Tesar unsigned long exp = jiffies + tv + 2; 2277ababb78SMichal Tesar 2287ababb78SMichal Tesar if (in_dev->mr_gq_running && 2297ababb78SMichal Tesar time_after_eq(exp, (in_dev->mr_gq_timer).expires)) 2307ababb78SMichal Tesar return; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds in_dev->mr_gq_running = 1; 2337ababb78SMichal Tesar if (!mod_timer(&in_dev->mr_gq_timer, exp)) 2341da177e4SLinus Torvalds in_dev_hold(in_dev); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) 2381da177e4SLinus Torvalds { 23963862b5bSAruna-Hewapathirane int tv = prandom_u32() % delay; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) 2421da177e4SLinus Torvalds in_dev_hold(in_dev); 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) 2461da177e4SLinus Torvalds { 2471da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2481da177e4SLinus Torvalds im->unsolicit_count = 0; 2491da177e4SLinus Torvalds if (del_timer(&im->timer)) { 2501da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) { 2511da177e4SLinus Torvalds add_timer(&im->timer); 2521da177e4SLinus Torvalds im->tm_running = 1; 2531da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2541da177e4SLinus Torvalds return; 2551da177e4SLinus Torvalds } 2568851ab52SReshetova, Elena refcount_dec(&im->refcnt); 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds igmp_start_timer(im, max_delay); 2591da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds /* 2641da177e4SLinus Torvalds * Send an IGMP report. 2651da177e4SLinus Torvalds */ 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, 2711da177e4SLinus Torvalds int gdeleted, int sdeleted) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds switch (type) { 2741da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE: 2751da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE: 2761da177e4SLinus Torvalds if (gdeleted || sdeleted) 2771da177e4SLinus Torvalds return 0; 278ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) { 279ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE) 280ad12583fSDavid L Stevens return 1; 281ad12583fSDavid L Stevens /* don't include if this source is excluded 282ad12583fSDavid L Stevens * in all filters 283ad12583fSDavid L Stevens */ 284ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE]) 285ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE; 286ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] == 287ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]; 288ad12583fSDavid L Stevens } 289ad12583fSDavid L Stevens return 0; 2901da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE: 2911da177e4SLinus Torvalds if (gdeleted || sdeleted) 2921da177e4SLinus Torvalds return 0; 2931da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0; 2941da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE: 2951da177e4SLinus Torvalds if (gdeleted || sdeleted) 2961da177e4SLinus Torvalds return 0; 2971da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 || 2981da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE]) 2991da177e4SLinus Torvalds return 0; 3001da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] == 3011da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]; 3021da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES: 3031da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount) 3041da177e4SLinus Torvalds return 0; 3051da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; 3061da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES: 3071da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) 3081da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted); 3091da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds return 0; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds static int 3151da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) 3161da177e4SLinus Torvalds { 3171da177e4SLinus Torvalds struct ip_sf_list *psf; 3181da177e4SLinus Torvalds int scount = 0; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 3211da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 3221da177e4SLinus Torvalds continue; 3231da177e4SLinus Torvalds scount++; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds return scount; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 328a46182b0SKevin Cernekee /* source address selection per RFC 3376 section 4.2.13 */ 329a46182b0SKevin Cernekee static __be32 igmpv3_get_srcaddr(struct net_device *dev, 330a46182b0SKevin Cernekee const struct flowi4 *fl4) 331a46182b0SKevin Cernekee { 332a46182b0SKevin Cernekee struct in_device *in_dev = __in_dev_get_rcu(dev); 333cd5a411dSFlorian Westphal const struct in_ifaddr *ifa; 334a46182b0SKevin Cernekee 335a46182b0SKevin Cernekee if (!in_dev) 336a46182b0SKevin Cernekee return htonl(INADDR_ANY); 337a46182b0SKevin Cernekee 338cd5a411dSFlorian Westphal in_dev_for_each_ifa_rcu(ifa, in_dev) { 339ad23b750SFelix Fietkau if (fl4->saddr == ifa->ifa_local) 340a46182b0SKevin Cernekee return fl4->saddr; 341cd5a411dSFlorian Westphal } 342a46182b0SKevin Cernekee 343a46182b0SKevin Cernekee return htonl(INADDR_ANY); 344a46182b0SKevin Cernekee } 345a46182b0SKevin Cernekee 3464c672e4bSDaniel Borkmann static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds struct sk_buff *skb; 3491da177e4SLinus Torvalds struct rtable *rt; 3501da177e4SLinus Torvalds struct iphdr *pip; 3511da177e4SLinus Torvalds struct igmpv3_report *pig; 352877acedcSDaniel Lezcano struct net *net = dev_net(dev); 35331e4543dSDavid S. Miller struct flowi4 fl4; 35466088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 35566088243SHerbert Xu int tlen = dev->needed_tailroom; 3564c672e4bSDaniel Borkmann unsigned int size = mtu; 3571da177e4SLinus Torvalds 35857e1ab6eSEric Dumazet while (1) { 35966088243SHerbert Xu skb = alloc_skb(size + hlen + tlen, 36057e1ab6eSEric Dumazet GFP_ATOMIC | __GFP_NOWARN); 36157e1ab6eSEric Dumazet if (skb) 36257e1ab6eSEric Dumazet break; 36357e1ab6eSEric Dumazet size >>= 1; 36457e1ab6eSEric Dumazet if (size < 256) 3651da177e4SLinus Torvalds return NULL; 36657e1ab6eSEric Dumazet } 3679d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 3681da177e4SLinus Torvalds 36931e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, 37078fbfd8aSDavid S. Miller 0, 0, 37178fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 372b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 3731da177e4SLinus Torvalds kfree_skb(skb); 3741da177e4SLinus Torvalds return NULL; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 377d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 3781da177e4SLinus Torvalds skb->dev = dev; 3791da177e4SLinus Torvalds 38066088243SHerbert Xu skb_reserve(skb, hlen); 3811837b2e2SBenjamin Poirier skb_tailroom_reserve(skb, mtu, tlen); 3821da177e4SLinus Torvalds 3837e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 384eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb); 3857e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds pip->version = 4; 3881da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2; 3891da177e4SLinus Torvalds pip->tos = 0xc0; 3901da177e4SLinus Torvalds pip->frag_off = htons(IP_DF); 3911da177e4SLinus Torvalds pip->ttl = 1; 392492f64ceSDavid S. Miller pip->daddr = fl4.daddr; 393e7aadb27SEric Dumazet 394e7aadb27SEric Dumazet rcu_read_lock(); 395a46182b0SKevin Cernekee pip->saddr = igmpv3_get_srcaddr(dev, &fl4); 396e7aadb27SEric Dumazet rcu_read_unlock(); 397e7aadb27SEric Dumazet 3981da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP; 3991da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */ 400b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 4011da177e4SLinus Torvalds ((u8 *)&pip[1])[0] = IPOPT_RA; 4021da177e4SLinus Torvalds ((u8 *)&pip[1])[1] = 4; 4031da177e4SLinus Torvalds ((u8 *)&pip[1])[2] = 0; 4041da177e4SLinus Torvalds ((u8 *)&pip[1])[3] = 0; 4051da177e4SLinus Torvalds 406b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; 407d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig)); 408d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb); 4091da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; 4101da177e4SLinus Torvalds pig->resv1 = 0; 4111da177e4SLinus Torvalds pig->csum = 0; 4121da177e4SLinus Torvalds pig->resv2 = 0; 4131da177e4SLinus Torvalds pig->ngrec = 0; 4141da177e4SLinus Torvalds return skb; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb) 4181da177e4SLinus Torvalds { 419d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb); 420f7c0c2aeSSimon Horman const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb); 4211da177e4SLinus Torvalds 422d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); 4231da177e4SLinus Torvalds 42433224b16SEric W. Biederman return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, 433b5476022SEric Dumazet int type, struct igmpv3_grec **ppgr, unsigned int mtu) 4341da177e4SLinus Torvalds { 4351da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4361da177e4SLinus Torvalds struct igmpv3_report *pih; 4371da177e4SLinus Torvalds struct igmpv3_grec *pgr; 4381da177e4SLinus Torvalds 439b5476022SEric Dumazet if (!skb) { 440b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 4411da177e4SLinus Torvalds if (!skb) 4421da177e4SLinus Torvalds return NULL; 443b5476022SEric Dumazet } 4444df864c1SJohannes Berg pgr = skb_put(skb, sizeof(struct igmpv3_grec)); 4451da177e4SLinus Torvalds pgr->grec_type = type; 4461da177e4SLinus Torvalds pgr->grec_auxwords = 0; 4471da177e4SLinus Torvalds pgr->grec_nsrcs = 0; 4481da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr; 449d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb); 4501da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1); 4511da177e4SLinus Torvalds *ppgr = pgr; 4521da177e4SLinus Torvalds return skb; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4554c672e4bSDaniel Borkmann #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0) 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, 4581da177e4SLinus Torvalds int type, int gdeleted, int sdeleted) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 46187a8a2aeSNikolay Borisov struct net *net = dev_net(dev); 4621da177e4SLinus Torvalds struct igmpv3_report *pih; 4631da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL; 4641da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; 465ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate; 466b5476022SEric Dumazet unsigned int mtu; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 4691da177e4SLinus Torvalds return skb; 47087a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 471df2cf4a7SPhilip Downey return skb; 4721da177e4SLinus Torvalds 473b5476022SEric Dumazet mtu = READ_ONCE(dev->mtu); 474b5476022SEric Dumazet if (mtu < IPV4_MIN_MTU) 475b5476022SEric Dumazet return skb; 476b5476022SEric Dumazet 4771da177e4SLinus Torvalds isquery = type == IGMPV3_MODE_IS_INCLUDE || 4781da177e4SLinus Torvalds type == IGMPV3_MODE_IS_EXCLUDE; 4791da177e4SLinus Torvalds truncate = type == IGMPV3_MODE_IS_EXCLUDE || 4801da177e4SLinus Torvalds type == IGMPV3_CHANGE_TO_EXCLUDE; 4811da177e4SLinus Torvalds 482ad12583fSDavid L Stevens stotal = scount = 0; 483ad12583fSDavid L Stevens 4841da177e4SLinus Torvalds psf_list = sdeleted ? &pmc->tomb : &pmc->sources; 4851da177e4SLinus Torvalds 486ad12583fSDavid L Stevens if (!*psf_list) 487ad12583fSDavid L Stevens goto empty_source; 488ad12583fSDavid L Stevens 489d9edf9e2SArnaldo Carvalho de Melo pih = skb ? igmpv3_report_hdr(skb) : NULL; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* EX and TO_EX get a fresh packet, if needed */ 4921da177e4SLinus Torvalds if (truncate) { 4931da177e4SLinus Torvalds if (pih && pih->ngrec && 4941da177e4SLinus Torvalds AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { 4951da177e4SLinus Torvalds if (skb) 4961da177e4SLinus Torvalds igmpv3_sendpack(skb); 497b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds first = 1; 5011da177e4SLinus Torvalds psf_prev = NULL; 5021da177e4SLinus Torvalds for (psf = *psf_list; psf; psf = psf_next) { 503ea4d9e72SAl Viro __be32 *psrc; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds psf_next = psf->sf_next; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { 5081da177e4SLinus Torvalds psf_prev = psf; 5091da177e4SLinus Torvalds continue; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 512a052517aSHangbin Liu /* Based on RFC3376 5.1. Should not send source-list change 513a052517aSHangbin Liu * records when there is a filter mode change. 514a052517aSHangbin Liu */ 515a052517aSHangbin Liu if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) || 516a052517aSHangbin Liu (!gdeleted && pmc->crcount)) && 517a052517aSHangbin Liu (type == IGMPV3_ALLOW_NEW_SOURCES || 518a052517aSHangbin Liu type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) 519a052517aSHangbin Liu goto decrease_sf_crcount; 520a052517aSHangbin Liu 5211da177e4SLinus Torvalds /* clear marks on query responses */ 5221da177e4SLinus Torvalds if (isquery) 5231da177e4SLinus Torvalds psf->sf_gsresp = 0; 5241da177e4SLinus Torvalds 52563007727SAl Viro if (AVAILABLE(skb) < sizeof(__be32) + 5261da177e4SLinus Torvalds first*sizeof(struct igmpv3_grec)) { 5271da177e4SLinus Torvalds if (truncate && !first) 5281da177e4SLinus Torvalds break; /* truncate these */ 5291da177e4SLinus Torvalds if (pgr) 5301da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5311da177e4SLinus Torvalds if (skb) 5321da177e4SLinus Torvalds igmpv3_sendpack(skb); 533b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 5341da177e4SLinus Torvalds first = 1; 5351da177e4SLinus Torvalds scount = 0; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds if (first) { 538b5476022SEric Dumazet skb = add_grhead(skb, pmc, type, &pgr, mtu); 5391da177e4SLinus Torvalds first = 0; 5401da177e4SLinus Torvalds } 541cc63f70bSAlexey Dobriyan if (!skb) 542cc63f70bSAlexey Dobriyan return NULL; 5434df864c1SJohannes Berg psrc = skb_put(skb, sizeof(__be32)); 5441da177e4SLinus Torvalds *psrc = psf->sf_inaddr; 545ad12583fSDavid L Stevens scount++; stotal++; 5461da177e4SLinus Torvalds if ((type == IGMPV3_ALLOW_NEW_SOURCES || 5471da177e4SLinus Torvalds type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { 548a052517aSHangbin Liu decrease_sf_crcount: 5491da177e4SLinus Torvalds psf->sf_crcount--; 5501da177e4SLinus Torvalds if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { 5511da177e4SLinus Torvalds if (psf_prev) 5521da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5531da177e4SLinus Torvalds else 5541da177e4SLinus Torvalds *psf_list = psf->sf_next; 5551da177e4SLinus Torvalds kfree(psf); 5561da177e4SLinus Torvalds continue; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds psf_prev = psf; 5601da177e4SLinus Torvalds } 561ad12583fSDavid L Stevens 562ad12583fSDavid L Stevens empty_source: 563ad12583fSDavid L Stevens if (!stotal) { 564ad12583fSDavid L Stevens if (type == IGMPV3_ALLOW_NEW_SOURCES || 565ad12583fSDavid L Stevens type == IGMPV3_BLOCK_OLD_SOURCES) 566ad12583fSDavid L Stevens return skb; 567ad12583fSDavid L Stevens if (pmc->crcount || isquery) { 568ad12583fSDavid L Stevens /* make sure we have room for group header */ 569ad12583fSDavid L Stevens if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) { 570ad12583fSDavid L Stevens igmpv3_sendpack(skb); 571ad12583fSDavid L Stevens skb = NULL; /* add_grhead will get a new one */ 572ad12583fSDavid L Stevens } 573b5476022SEric Dumazet skb = add_grhead(skb, pmc, type, &pgr, mtu); 574ad12583fSDavid L Stevens } 575ad12583fSDavid L Stevens } 5761da177e4SLinus Torvalds if (pgr) 5771da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds if (isquery) 5801da177e4SLinus Torvalds pmc->gsquery = 0; /* clear query state on report */ 5811da177e4SLinus Torvalds return skb; 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) 5851da177e4SLinus Torvalds { 5861da177e4SLinus Torvalds struct sk_buff *skb = NULL; 58787a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 5881da177e4SLinus Torvalds int type; 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds if (!pmc) { 5911d7138deSEric Dumazet rcu_read_lock(); 5921d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 5931da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 5941da177e4SLinus Torvalds continue; 595df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(pmc->multiaddr) && 59687a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 597df2cf4a7SPhilip Downey continue; 5981da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5991da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 6001da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 6011da177e4SLinus Torvalds else 6021da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 6031da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6041da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6051da177e4SLinus Torvalds } 6061d7138deSEric Dumazet rcu_read_unlock(); 6071da177e4SLinus Torvalds } else { 6081da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6091da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 6101da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 6111da177e4SLinus Torvalds else 6121da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 6131da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6141da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds if (!skb) 6171da177e4SLinus Torvalds return 0; 6181da177e4SLinus Torvalds return igmpv3_sendpack(skb); 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* 6221da177e4SLinus Torvalds * remove zero-count source records from a source filter list 6231da177e4SLinus Torvalds */ 6241da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds psf_prev = NULL; 6291da177e4SLinus Torvalds for (psf = *ppsf; psf; psf = psf_next) { 6301da177e4SLinus Torvalds psf_next = psf->sf_next; 6311da177e4SLinus Torvalds if (psf->sf_crcount == 0) { 6321da177e4SLinus Torvalds if (psf_prev) 6331da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 6341da177e4SLinus Torvalds else 6351da177e4SLinus Torvalds *ppsf = psf->sf_next; 6361da177e4SLinus Torvalds kfree(psf); 6371da177e4SLinus Torvalds } else 6381da177e4SLinus Torvalds psf_prev = psf; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6423580d04aSEric Dumazet static void kfree_pmc(struct ip_mc_list *pmc) 6433580d04aSEric Dumazet { 6443580d04aSEric Dumazet ip_sf_list_clear_all(pmc->sources); 6453580d04aSEric Dumazet ip_sf_list_clear_all(pmc->tomb); 6463580d04aSEric Dumazet kfree(pmc); 6473580d04aSEric Dumazet } 6483580d04aSEric Dumazet 6491da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev) 6501da177e4SLinus Torvalds { 6511da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next; 6521da177e4SLinus Torvalds struct sk_buff *skb = NULL; 6531da177e4SLinus Torvalds int type, dtype; 6541da177e4SLinus Torvalds 6551d7138deSEric Dumazet rcu_read_lock(); 6561da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* deleted MCA's */ 6591da177e4SLinus Torvalds pmc_prev = NULL; 6601da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) { 6611da177e4SLinus Torvalds pmc_next = pmc->next; 6621da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 6631da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6641da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6651da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6661da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1); 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds if (pmc->crcount) { 6691da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) { 6701da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6711da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6721da177e4SLinus Torvalds } 673ad12583fSDavid L Stevens pmc->crcount--; 6741da177e4SLinus Torvalds if (pmc->crcount == 0) { 6751da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb); 6761da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources); 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { 6801da177e4SLinus Torvalds if (pmc_prev) 6811da177e4SLinus Torvalds pmc_prev->next = pmc_next; 6821da177e4SLinus Torvalds else 6831da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next; 6841da177e4SLinus Torvalds in_dev_put(pmc->interface); 6853580d04aSEric Dumazet kfree_pmc(pmc); 6861da177e4SLinus Torvalds } else 6871da177e4SLinus Torvalds pmc_prev = pmc; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds /* change recs */ 6921d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 6931da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6941da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 6951da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6961da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES; 6971da177e4SLinus Torvalds } else { 6981da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES; 6991da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 7021da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds /* filter mode changes */ 7051da177e4SLinus Torvalds if (pmc->crcount) { 7061da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) 7071da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE; 7081da177e4SLinus Torvalds else 7091da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 7101da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 711ad12583fSDavid L Stevens pmc->crcount--; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 7141da177e4SLinus Torvalds } 7151d7138deSEric Dumazet rcu_read_unlock(); 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds if (!skb) 7181da177e4SLinus Torvalds return; 7191da177e4SLinus Torvalds (void) igmpv3_sendpack(skb); 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, 7231da177e4SLinus Torvalds int type) 7241da177e4SLinus Torvalds { 7251da177e4SLinus Torvalds struct sk_buff *skb; 7261da177e4SLinus Torvalds struct iphdr *iph; 7271da177e4SLinus Torvalds struct igmphdr *ih; 7281da177e4SLinus Torvalds struct rtable *rt; 7291da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 730877acedcSDaniel Lezcano struct net *net = dev_net(dev); 73163007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0; 73231e4543dSDavid S. Miller struct flowi4 fl4; 73363007727SAl Viro __be32 dst; 73466088243SHerbert Xu int hlen, tlen; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) 7371da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc); 738df2cf4a7SPhilip Downey 73987a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 740df2cf4a7SPhilip Downey return 0; 741df2cf4a7SPhilip Downey 742df2cf4a7SPhilip Downey if (type == IGMP_HOST_LEAVE_MESSAGE) 7431da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER; 7441da177e4SLinus Torvalds else 7451da177e4SLinus Torvalds dst = group; 7461da177e4SLinus Torvalds 74731e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, 74878fbfd8aSDavid S. Miller 0, 0, 74978fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 750b23dd4feSDavid S. Miller if (IS_ERR(rt)) 7511da177e4SLinus Torvalds return -1; 75278fbfd8aSDavid S. Miller 75366088243SHerbert Xu hlen = LL_RESERVED_SPACE(dev); 75466088243SHerbert Xu tlen = dev->needed_tailroom; 75566088243SHerbert Xu skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); 75651456b29SIan Morris if (!skb) { 7571da177e4SLinus Torvalds ip_rt_put(rt); 7581da177e4SLinus Torvalds return -1; 7591da177e4SLinus Torvalds } 7609d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 7611da177e4SLinus Torvalds 762d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 7631da177e4SLinus Torvalds 76466088243SHerbert Xu skb_reserve(skb, hlen); 7651da177e4SLinus Torvalds 7667e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 767eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 7687e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds iph->version = 4; 7711da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2; 7721da177e4SLinus Torvalds iph->tos = 0xc0; 7731da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 7741da177e4SLinus Torvalds iph->ttl = 1; 7751da177e4SLinus Torvalds iph->daddr = dst; 776492f64ceSDavid S. Miller iph->saddr = fl4.saddr; 7771da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP; 778b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 7791da177e4SLinus Torvalds ((u8 *)&iph[1])[0] = IPOPT_RA; 7801da177e4SLinus Torvalds ((u8 *)&iph[1])[1] = 4; 7811da177e4SLinus Torvalds ((u8 *)&iph[1])[2] = 0; 7821da177e4SLinus Torvalds ((u8 *)&iph[1])[3] = 0; 7831da177e4SLinus Torvalds 7844df864c1SJohannes Berg ih = skb_put(skb, sizeof(struct igmphdr)); 7851da177e4SLinus Torvalds ih->type = type; 7861da177e4SLinus Torvalds ih->code = 0; 7871da177e4SLinus Torvalds ih->csum = 0; 7881da177e4SLinus Torvalds ih->group = group; 7891da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 7901da177e4SLinus Torvalds 79133224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb); 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 794e99e88a9SKees Cook static void igmp_gq_timer_expire(struct timer_list *t) 7951da177e4SLinus Torvalds { 796e99e88a9SKees Cook struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer); 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 7991da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL); 800e2401654SSalam Noureddine in_dev_put(in_dev); 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 803e99e88a9SKees Cook static void igmp_ifc_timer_expire(struct timer_list *t) 8041da177e4SLinus Torvalds { 805e99e88a9SKees Cook struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer); 806b69dd5b3SEric Dumazet u32 mr_ifc_count; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 8094a2b285eSEric Dumazet restart: 8104a2b285eSEric Dumazet mr_ifc_count = READ_ONCE(in_dev->mr_ifc_count); 8114a2b285eSEric Dumazet 8124a2b285eSEric Dumazet if (mr_ifc_count) { 8134a2b285eSEric Dumazet if (cmpxchg(&in_dev->mr_ifc_count, 8144a2b285eSEric Dumazet mr_ifc_count, 8154a2b285eSEric Dumazet mr_ifc_count - 1) != mr_ifc_count) 8164a2b285eSEric Dumazet goto restart; 817cab70040SWilliam Manley igmp_ifc_start_timer(in_dev, 818cab70040SWilliam Manley unsolicited_report_interval(in_dev)); 8191da177e4SLinus Torvalds } 820e2401654SSalam Noureddine in_dev_put(in_dev); 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 8241da177e4SLinus Torvalds { 825165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 8261da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 8271da177e4SLinus Torvalds return; 8284a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv); 8291da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds 833e99e88a9SKees Cook static void igmp_timer_expire(struct timer_list *t) 8341da177e4SLinus Torvalds { 835e99e88a9SKees Cook struct ip_mc_list *im = from_timer(im, t, timer); 8361da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds spin_lock(&im->lock); 8391da177e4SLinus Torvalds im->tm_running = 0; 8401da177e4SLinus Torvalds 8414fb7253eSHangbin Liu if (im->unsolicit_count && --im->unsolicit_count) 842cab70040SWilliam Manley igmp_start_timer(im, unsolicited_report_interval(in_dev)); 8434fb7253eSHangbin Liu 8441da177e4SLinus Torvalds im->reporter = 1; 8451da177e4SLinus Torvalds spin_unlock(&im->lock); 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 8481da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 8491da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 8501da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 8511da177e4SLinus Torvalds else 8521da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds ip_ma_put(im); 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 857ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 858ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds struct ip_sf_list *psf; 8611da177e4SLinus Torvalds int i, scount; 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds scount = 0; 8641da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 8651da177e4SLinus Torvalds if (scount == nsrcs) 8661da177e4SLinus Torvalds break; 867ad12583fSDavid L Stevens for (i = 0; i < nsrcs; i++) { 868ad12583fSDavid L Stevens /* skip inactive filters */ 869e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] || 870ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 871ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 872ce713ee5SRongQing.Li break; 873ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 874ad12583fSDavid L Stevens scount++; 875ad12583fSDavid L Stevens break; 876ad12583fSDavid L Stevens } 877ad12583fSDavid L Stevens } 878ad12583fSDavid L Stevens } 879ad12583fSDavid L Stevens pmc->gsquery = 0; 880ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 881ad12583fSDavid L Stevens return 0; 882ad12583fSDavid L Stevens return 1; 883ad12583fSDavid L Stevens } 884ad12583fSDavid L Stevens 885ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 886ad12583fSDavid L Stevens { 887ad12583fSDavid L Stevens struct ip_sf_list *psf; 888ad12583fSDavid L Stevens int i, scount; 889ad12583fSDavid L Stevens 890ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 891ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 892ad12583fSDavid L Stevens 893ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 894ad12583fSDavid L Stevens scount = 0; 895ad12583fSDavid L Stevens for (psf = pmc->sources; psf; psf = psf->sf_next) { 896ad12583fSDavid L Stevens if (scount == nsrcs) 897ad12583fSDavid L Stevens break; 8981da177e4SLinus Torvalds for (i = 0; i < nsrcs; i++) 8991da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 9001da177e4SLinus Torvalds psf->sf_gsresp = 1; 9011da177e4SLinus Torvalds scount++; 9021da177e4SLinus Torvalds break; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds } 905ad12583fSDavid L Stevens if (!scount) { 906ad12583fSDavid L Stevens pmc->gsquery = 0; 907ad12583fSDavid L Stevens return 0; 908ad12583fSDavid L Stevens } 909ad12583fSDavid L Stevens pmc->gsquery = 1; 910ad12583fSDavid L Stevens return 1; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 913d679c532SEric Dumazet /* return true if packet was dropped */ 914d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group) 9151da177e4SLinus Torvalds { 9161da177e4SLinus Torvalds struct ip_mc_list *im; 91787a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 922d679c532SEric Dumazet return false; 92387a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 924df2cf4a7SPhilip Downey return false; 9251da177e4SLinus Torvalds 9261d7138deSEric Dumazet rcu_read_lock(); 9271d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 9281da177e4SLinus Torvalds if (im->multiaddr == group) { 9291da177e4SLinus Torvalds igmp_stop_timer(im); 9301da177e4SLinus Torvalds break; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds } 9331d7138deSEric Dumazet rcu_read_unlock(); 934d679c532SEric Dumazet return false; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 937d679c532SEric Dumazet /* return true if packet was dropped */ 938d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 9391da177e4SLinus Torvalds int len) 9401da177e4SLinus Torvalds { 941d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 942d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 9431da177e4SLinus Torvalds struct ip_mc_list *im; 94463007727SAl Viro __be32 group = ih->group; 9451da177e4SLinus Torvalds int max_delay; 9461da177e4SLinus Torvalds int mark = 0; 94787a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds 9505b7c8406SDavid Stevens if (len == 8) { 9511da177e4SLinus Torvalds if (ih->code == 0) { 9521da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 9531da177e4SLinus Torvalds 954436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9551da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 956966c37f2SHangbin Liu (in_dev->mr_qrv * in_dev->mr_qi) + 957966c37f2SHangbin Liu in_dev->mr_qri; 9581da177e4SLinus Torvalds group = 0; 9591da177e4SLinus Torvalds } else { 9601da177e4SLinus Torvalds /* v2 router present */ 9611da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 9621da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 963966c37f2SHangbin Liu (in_dev->mr_qrv * in_dev->mr_qi) + 964966c37f2SHangbin Liu in_dev->mr_qri; 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds /* cancel the interface change timer */ 9674a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, 0); 9681da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 9691da177e4SLinus Torvalds __in_dev_put(in_dev); 9701da177e4SLinus Torvalds /* clear deleted report items */ 9711da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 9721da177e4SLinus Torvalds } else if (len < 12) { 973d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */ 9745b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) { 9755b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */ 976436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9775b7c8406SDavid Stevens group = 0; 9785b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) { 9795b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present; 9805b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here. 9815b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a 9825b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely 9835b7c8406SDavid Stevens * to be intended in a v3 query. 9845b7c8406SDavid Stevens */ 9855b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 986a8c1f65cSBen Hutchings if (!max_delay) 987a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */ 9881da177e4SLinus Torvalds } else { /* v3 */ 9891da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 990d679c532SEric Dumazet return true; 9911da177e4SLinus Torvalds 992d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9931da177e4SLinus Torvalds if (ih3->nsrcs) { 9941da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 99563007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 996d679c532SEric Dumazet return true; 997d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 10011da177e4SLinus Torvalds if (!max_delay) 10021da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 10031da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 1004966c37f2SHangbin Liu 1005966c37f2SHangbin Liu /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently 1006966c37f2SHangbin Liu * received value was zero, use the default or statically 1007966c37f2SHangbin Liu * configured value. 1008966c37f2SHangbin Liu */ 1009966c37f2SHangbin Liu in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv; 1010966c37f2SHangbin Liu in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL; 1011966c37f2SHangbin Liu 1012966c37f2SHangbin Liu /* RFC3376, 8.3. Query Response Interval: 1013966c37f2SHangbin Liu * The number of seconds represented by the [Query Response 1014966c37f2SHangbin Liu * Interval] must be less than the [Query Interval]. 1015966c37f2SHangbin Liu */ 1016966c37f2SHangbin Liu if (in_dev->mr_qri >= in_dev->mr_qi) 1017966c37f2SHangbin Liu in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ; 1018966c37f2SHangbin Liu 10191da177e4SLinus Torvalds if (!group) { /* general query */ 10201da177e4SLinus Torvalds if (ih3->nsrcs) 1021b47bd8d2SDaniel Borkmann return true; /* no sources allowed */ 10221da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 1023d679c532SEric Dumazet return false; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 10261da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds /* 10301da177e4SLinus Torvalds * - Start the timers in all of our membership records 10311da177e4SLinus Torvalds * that the query applies to for the interface on 10321da177e4SLinus Torvalds * which the query arrived excl. those that belong 10331da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 10341da177e4SLinus Torvalds * - For timers already running check if they need to 10351da177e4SLinus Torvalds * be reset. 10361da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 10371da177e4SLinus Torvalds * delay possible 10381da177e4SLinus Torvalds */ 10391d7138deSEric Dumazet rcu_read_lock(); 10401d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 1041ad12583fSDavid L Stevens int changed; 1042ad12583fSDavid L Stevens 10431da177e4SLinus Torvalds if (group && group != im->multiaddr) 10441da177e4SLinus Torvalds continue; 10451da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 10461da177e4SLinus Torvalds continue; 1047df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 104887a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1049df2cf4a7SPhilip Downey continue; 10501da177e4SLinus Torvalds spin_lock_bh(&im->lock); 10511da177e4SLinus Torvalds if (im->tm_running) 10521da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 10531da177e4SLinus Torvalds else 10541da177e4SLinus Torvalds im->gsquery = mark; 1055ad12583fSDavid L Stevens changed = !im->gsquery || 10561da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 10571da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 1058ad12583fSDavid L Stevens if (changed) 10591da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 10601da177e4SLinus Torvalds } 10611d7138deSEric Dumazet rcu_read_unlock(); 1062d679c532SEric Dumazet return false; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10659a57a9d2SEric Dumazet /* called in rcu_read_lock() section */ 10661da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 10671da177e4SLinus Torvalds { 10681da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 10691da177e4SLinus Torvalds struct igmphdr *ih; 1070c7b725beSDavid Ahern struct net_device *dev = skb->dev; 1071c7b725beSDavid Ahern struct in_device *in_dev; 10721da177e4SLinus Torvalds int len = skb->len; 1073d679c532SEric Dumazet bool dropped = true; 10741da177e4SLinus Torvalds 1075c7b725beSDavid Ahern if (netif_is_l3_master(dev)) { 1076c7b725beSDavid Ahern dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif); 1077c7b725beSDavid Ahern if (!dev) 1078c7b725beSDavid Ahern goto drop; 1079c7b725beSDavid Ahern } 1080c7b725beSDavid Ahern 1081c7b725beSDavid Ahern in_dev = __in_dev_get_rcu(dev); 108251456b29SIan Morris if (!in_dev) 1083cd557bc1SDenis V. Lunev goto drop; 10841da177e4SLinus Torvalds 1085fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 10869a57a9d2SEric Dumazet goto drop; 1087fb286bb2SHerbert Xu 1088de08dc1aSTom Herbert if (skb_checksum_simple_validate(skb)) 10899a57a9d2SEric Dumazet goto drop; 10901da177e4SLinus Torvalds 1091d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 10921da177e4SLinus Torvalds switch (ih->type) { 10931da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 1094d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len); 10951da177e4SLinus Torvalds break; 10961da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 10971da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 10981da177e4SLinus Torvalds /* Is it our report looped back? */ 1099c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) 11001da177e4SLinus Torvalds break; 110124c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 110224c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 110324c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 1104d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group); 11051da177e4SLinus Torvalds break; 11061da177e4SLinus Torvalds case IGMP_PIM: 11071da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 11081da177e4SLinus Torvalds return pim_rcv_v1(skb); 11091da177e4SLinus Torvalds #endif 1110c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 11111da177e4SLinus Torvalds case IGMP_DVMRP: 11121da177e4SLinus Torvalds case IGMP_TRACE: 11131da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 11141da177e4SLinus Torvalds case IGMP_MTRACE: 11151da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 11161da177e4SLinus Torvalds break; 11171da177e4SLinus Torvalds default: 1118dd1c1853SLinus Torvalds break; 11191da177e4SLinus Torvalds } 1120fb286bb2SHerbert Xu 1121cd557bc1SDenis V. Lunev drop: 1122d679c532SEric Dumazet if (dropped) 11231da177e4SLinus Torvalds kfree_skb(skb); 1124d679c532SEric Dumazet else 1125d679c532SEric Dumazet consume_skb(skb); 11261da177e4SLinus Torvalds return 0; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds #endif 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds /* 11331da177e4SLinus Torvalds * Add a filter to a device 11341da177e4SLinus Torvalds */ 11351da177e4SLinus Torvalds 113663007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 11371da177e4SLinus Torvalds { 11381da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 11391da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 11421da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 1143b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode 11441da177e4SLinus Torvalds routine. Something sort of: 11451da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 11461da177e4SLinus Torvalds --ANK 11471da177e4SLinus Torvalds */ 11481da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 114922bedad3SJiri Pirko dev_mc_add(dev, buf); 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds /* 11531da177e4SLinus Torvalds * Remove a filter from a device 11541da177e4SLinus Torvalds */ 11551da177e4SLinus Torvalds 115663007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 11571da177e4SLinus Torvalds { 11581da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 11591da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 116222bedad3SJiri Pirko dev_mc_del(dev, buf); 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11661da177e4SLinus Torvalds /* 11671da177e4SLinus Torvalds * deleted ip_mc_list manipulation 11681da177e4SLinus Torvalds */ 11699fb20801SFlorian Fainelli static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im, 11709fb20801SFlorian Fainelli gfp_t gfp) 11711da177e4SLinus Torvalds { 11721da177e4SLinus Torvalds struct ip_mc_list *pmc; 1173165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 11761da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 11771da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 11781da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 11791da177e4SLinus Torvalds * non-deleted or query-response MCA's. 11801da177e4SLinus Torvalds */ 11819fb20801SFlorian Fainelli pmc = kzalloc(sizeof(*pmc), gfp); 11821da177e4SLinus Torvalds if (!pmc) 11831da177e4SLinus Torvalds return; 1184b4846fc3SWANG Cong spin_lock_init(&pmc->lock); 11851da177e4SLinus Torvalds spin_lock_bh(&im->lock); 11861da177e4SLinus Torvalds pmc->interface = im->interface; 11871da177e4SLinus Torvalds in_dev_hold(in_dev); 11881da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 1189165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 11901da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 11911da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 11921da177e4SLinus Torvalds struct ip_sf_list *psf; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds pmc->tomb = im->tomb; 11951da177e4SLinus Torvalds pmc->sources = im->sources; 11961da177e4SLinus Torvalds im->tomb = im->sources = NULL; 11971da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 11981da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12031da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 12041da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 12051da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 120824803f38SHangbin Liu /* 120924803f38SHangbin Liu * restore ip_mc_list deleted records 121024803f38SHangbin Liu */ 121124803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im) 12121da177e4SLinus Torvalds { 12131da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 121424803f38SHangbin Liu struct ip_sf_list *psf; 121524803f38SHangbin Liu struct net *net = dev_net(in_dev->dev); 121624803f38SHangbin Liu __be32 multiaddr = im->multiaddr; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12191da177e4SLinus Torvalds pmc_prev = NULL; 12201da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) { 12211da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 12221da177e4SLinus Torvalds break; 12231da177e4SLinus Torvalds pmc_prev = pmc; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds if (pmc) { 12261da177e4SLinus Torvalds if (pmc_prev) 12271da177e4SLinus Torvalds pmc_prev->next = pmc->next; 12281da177e4SLinus Torvalds else 12291da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 123224803f38SHangbin Liu 123324803f38SHangbin Liu spin_lock_bh(&im->lock); 12341da177e4SLinus Torvalds if (pmc) { 123524803f38SHangbin Liu im->interface = pmc->interface; 123608d3ffccSHangbin Liu if (im->sfmode == MCAST_INCLUDE) { 1237e5b1c6c6SEric Dumazet swap(im->tomb, pmc->tomb); 1238e5b1c6c6SEric Dumazet swap(im->sources, pmc->sources); 123924803f38SHangbin Liu for (psf = im->sources; psf; psf = psf->sf_next) 12406e2059b5SHangbin Liu psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 12416e2059b5SHangbin Liu } else { 12426e2059b5SHangbin Liu im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds in_dev_put(pmc->interface); 12453580d04aSEric Dumazet kfree_pmc(pmc); 12461da177e4SLinus Torvalds } 124724803f38SHangbin Liu spin_unlock_bh(&im->lock); 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 125024803f38SHangbin Liu /* 125124803f38SHangbin Liu * flush ip_mc_list deleted records 125224803f38SHangbin Liu */ 12531da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12581da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 12591da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 12601da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 12631da177e4SLinus Torvalds nextpmc = pmc->next; 12641da177e4SLinus Torvalds ip_mc_clear_src(pmc); 12651da177e4SLinus Torvalds in_dev_put(pmc->interface); 12663580d04aSEric Dumazet kfree_pmc(pmc); 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds /* clear dead sources, too */ 12691d7138deSEric Dumazet rcu_read_lock(); 12701d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 12713580d04aSEric Dumazet struct ip_sf_list *psf; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 12741da177e4SLinus Torvalds psf = pmc->tomb; 12751da177e4SLinus Torvalds pmc->tomb = NULL; 12761da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 12773580d04aSEric Dumazet ip_sf_list_clear_all(psf); 12781da177e4SLinus Torvalds } 12791d7138deSEric Dumazet rcu_read_unlock(); 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds #endif 12821da177e4SLinus Torvalds 12839fb20801SFlorian Fainelli static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp) 12841da177e4SLinus Torvalds { 12851da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 12861da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 128787a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 12881da177e4SLinus Torvalds int reporter; 12891da177e4SLinus Torvalds #endif 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds if (im->loaded) { 12921da177e4SLinus Torvalds im->loaded = 0; 12931da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12971da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 12981da177e4SLinus Torvalds return; 129987a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1300df2cf4a7SPhilip Downey return; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds reporter = im->reporter; 13031da177e4SLinus Torvalds igmp_stop_timer(im); 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds if (!in_dev->dead) { 13061da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 130724cf3af3SVeaceslav Falico return; 13081da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 13091da177e4SLinus Torvalds if (reporter) 13101da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 131124cf3af3SVeaceslav Falico return; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds /* IGMPv3 */ 13149fb20801SFlorian Fainelli igmpv3_add_delrec(in_dev, im, gfp); 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds igmp_ifc_event(in_dev); 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds #endif 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13219fb20801SFlorian Fainelli static void igmp_group_dropped(struct ip_mc_list *im) 13229fb20801SFlorian Fainelli { 13239fb20801SFlorian Fainelli __igmp_group_dropped(im, GFP_KERNEL); 13249fb20801SFlorian Fainelli } 13259fb20801SFlorian Fainelli 13260ae0d60aSHangbin Liu static void igmp_group_added(struct ip_mc_list *im) 13271da177e4SLinus Torvalds { 13281da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 1329dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 133087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1331dcd87999SNikolay Borisov #endif 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds if (im->loaded == 0) { 13341da177e4SLinus Torvalds im->loaded = 1; 13351da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13391da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 13401da177e4SLinus Torvalds return; 134187a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1342df2cf4a7SPhilip Downey return; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds if (in_dev->dead) 13451da177e4SLinus Torvalds return; 1346ff06525fSHangbin Liu 1347ff06525fSHangbin Liu im->unsolicit_count = net->ipv4.sysctl_igmp_qrv; 13481da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 13491da177e4SLinus Torvalds spin_lock_bh(&im->lock); 1350436f7c20SFabian Frederick igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY); 13511da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 13521da177e4SLinus Torvalds return; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds /* else, v3 */ 13551da177e4SLinus Torvalds 13566e2059b5SHangbin Liu /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should 13576e2059b5SHangbin Liu * not send filter-mode change record as the mode should be from 13586e2059b5SHangbin Liu * IN() to IN(A). 13596e2059b5SHangbin Liu */ 13600ae0d60aSHangbin Liu if (im->sfmode == MCAST_EXCLUDE) 1361165094afSNikolay Borisov im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 13626e2059b5SHangbin Liu 13631da177e4SLinus Torvalds igmp_ifc_event(in_dev); 13641da177e4SLinus Torvalds #endif 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds /* 13691da177e4SLinus Torvalds * Multicast list managers 13701da177e4SLinus Torvalds */ 13711da177e4SLinus Torvalds 1372e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im) 1373e9897071SEric Dumazet { 1374c70eba74SEric Dumazet return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); 1375e9897071SEric Dumazet } 1376e9897071SEric Dumazet 1377e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev, 1378e9897071SEric Dumazet struct ip_mc_list *im) 1379e9897071SEric Dumazet { 1380e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 1381e9897071SEric Dumazet u32 hash; 1382e9897071SEric Dumazet 1383e9897071SEric Dumazet mc_hash = rtnl_dereference(in_dev->mc_hash); 1384e9897071SEric Dumazet if (mc_hash) { 1385e9897071SEric Dumazet hash = ip_mc_hash(im); 1386c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1387e9897071SEric Dumazet rcu_assign_pointer(mc_hash[hash], im); 1388e9897071SEric Dumazet return; 1389e9897071SEric Dumazet } 1390e9897071SEric Dumazet 1391e9897071SEric Dumazet /* do not use a hash table for small number of items */ 1392e9897071SEric Dumazet if (in_dev->mc_count < 4) 1393e9897071SEric Dumazet return; 1394e9897071SEric Dumazet 1395e9897071SEric Dumazet mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG, 1396e9897071SEric Dumazet GFP_KERNEL); 1397e9897071SEric Dumazet if (!mc_hash) 1398e9897071SEric Dumazet return; 1399e9897071SEric Dumazet 1400e9897071SEric Dumazet for_each_pmc_rtnl(in_dev, im) { 1401e9897071SEric Dumazet hash = ip_mc_hash(im); 1402c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1403e9897071SEric Dumazet RCU_INIT_POINTER(mc_hash[hash], im); 1404e9897071SEric Dumazet } 1405e9897071SEric Dumazet 1406e9897071SEric Dumazet rcu_assign_pointer(in_dev->mc_hash, mc_hash); 1407e9897071SEric Dumazet } 1408e9897071SEric Dumazet 1409e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev, 1410e9897071SEric Dumazet struct ip_mc_list *im) 1411e9897071SEric Dumazet { 1412e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); 1413e9897071SEric Dumazet struct ip_mc_list *aux; 1414e9897071SEric Dumazet 1415e9897071SEric Dumazet if (!mc_hash) 1416e9897071SEric Dumazet return; 1417e9897071SEric Dumazet mc_hash += ip_mc_hash(im); 1418e9897071SEric Dumazet while ((aux = rtnl_dereference(*mc_hash)) != im) 1419e9897071SEric Dumazet mc_hash = &aux->next_hash; 1420e9897071SEric Dumazet *mc_hash = im->next_hash; 1421e9897071SEric Dumazet } 1422e9897071SEric Dumazet 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds /* 14251da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 14261da177e4SLinus Torvalds */ 14279fb20801SFlorian Fainelli static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, 14289fb20801SFlorian Fainelli unsigned int mode, gfp_t gfp) 14291da177e4SLinus Torvalds { 14301da177e4SLinus Torvalds struct ip_mc_list *im; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds ASSERT_RTNL(); 14331da177e4SLinus Torvalds 14341d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) { 14351da177e4SLinus Torvalds if (im->multiaddr == addr) { 14361da177e4SLinus Torvalds im->users++; 14376e2059b5SHangbin Liu ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0); 14381da177e4SLinus Torvalds goto out; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds 14429fb20801SFlorian Fainelli im = kzalloc(sizeof(*im), gfp); 14431da177e4SLinus Torvalds if (!im) 14441da177e4SLinus Torvalds goto out; 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds im->users = 1; 14471da177e4SLinus Torvalds im->interface = in_dev; 14481da177e4SLinus Torvalds in_dev_hold(in_dev); 14491da177e4SLinus Torvalds im->multiaddr = addr; 14501da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 14516e2059b5SHangbin Liu im->sfmode = mode; 14526e2059b5SHangbin Liu im->sfcount[mode] = 1; 14538851ab52SReshetova, Elena refcount_set(&im->refcnt, 1); 14541da177e4SLinus Torvalds spin_lock_init(&im->lock); 14551da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1456e99e88a9SKees Cook timer_setup(&im->timer, igmp_timer_expire, 0); 14571da177e4SLinus Torvalds #endif 14581d7138deSEric Dumazet 14591d7138deSEric Dumazet im->next_rcu = in_dev->mc_list; 1460b8bae41eSRami Rosen in_dev->mc_count++; 1461cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im); 14621d7138deSEric Dumazet 1463e9897071SEric Dumazet ip_mc_hash_add(in_dev, im); 1464e9897071SEric Dumazet 14651da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 146624803f38SHangbin Liu igmpv3_del_delrec(in_dev, im); 14671da177e4SLinus Torvalds #endif 14680ae0d60aSHangbin Liu igmp_group_added(im); 14691da177e4SLinus Torvalds if (!in_dev->dead) 14701da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 14711da177e4SLinus Torvalds out: 14721da177e4SLinus Torvalds return; 14731da177e4SLinus Torvalds } 14746e2059b5SHangbin Liu 14759fb20801SFlorian Fainelli void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp) 14769fb20801SFlorian Fainelli { 14779fb20801SFlorian Fainelli ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp); 14789fb20801SFlorian Fainelli } 14799fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_inc_group); 14809fb20801SFlorian Fainelli 14816e2059b5SHangbin Liu void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 14826e2059b5SHangbin Liu { 1483a1c4cd67SLi RongQing __ip_mc_inc_group(in_dev, addr, GFP_KERNEL); 14846e2059b5SHangbin Liu } 14854bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group); 14861da177e4SLinus Torvalds 14879afd85c9SLinus Lüssing static int ip_mc_check_iphdr(struct sk_buff *skb) 14889afd85c9SLinus Lüssing { 14899afd85c9SLinus Lüssing const struct iphdr *iph; 14909afd85c9SLinus Lüssing unsigned int len; 14919afd85c9SLinus Lüssing unsigned int offset = skb_network_offset(skb) + sizeof(*iph); 14929afd85c9SLinus Lüssing 14939afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset)) 14949afd85c9SLinus Lüssing return -EINVAL; 14959afd85c9SLinus Lüssing 14969afd85c9SLinus Lüssing iph = ip_hdr(skb); 14979afd85c9SLinus Lüssing 14989afd85c9SLinus Lüssing if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph)) 14999afd85c9SLinus Lüssing return -EINVAL; 15009afd85c9SLinus Lüssing 15019afd85c9SLinus Lüssing offset += ip_hdrlen(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 (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 15099afd85c9SLinus Lüssing return -EINVAL; 15109afd85c9SLinus Lüssing 15119afd85c9SLinus Lüssing len = skb_network_offset(skb) + ntohs(iph->tot_len); 15129afd85c9SLinus Lüssing if (skb->len < len || len < offset) 15139afd85c9SLinus Lüssing return -EINVAL; 15149afd85c9SLinus Lüssing 15159afd85c9SLinus Lüssing skb_set_transport_header(skb, offset); 15169afd85c9SLinus Lüssing 15179afd85c9SLinus Lüssing return 0; 15189afd85c9SLinus Lüssing } 15199afd85c9SLinus Lüssing 15209afd85c9SLinus Lüssing static int ip_mc_check_igmp_reportv3(struct sk_buff *skb) 15219afd85c9SLinus Lüssing { 15229afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb); 15239afd85c9SLinus Lüssing 15249afd85c9SLinus Lüssing len += sizeof(struct igmpv3_report); 15259afd85c9SLinus Lüssing 1526a2e2ca3bSLinus Lüssing return ip_mc_may_pull(skb, len) ? 0 : -EINVAL; 15279afd85c9SLinus Lüssing } 15289afd85c9SLinus Lüssing 15299afd85c9SLinus Lüssing static int ip_mc_check_igmp_query(struct sk_buff *skb) 15309afd85c9SLinus Lüssing { 1531a2e2ca3bSLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 1532a2e2ca3bSLinus Lüssing unsigned int len; 15339afd85c9SLinus Lüssing 15349afd85c9SLinus Lüssing /* IGMPv{1,2}? */ 1535a2e2ca3bSLinus Lüssing if (transport_len != sizeof(struct igmphdr)) { 15369afd85c9SLinus Lüssing /* or IGMPv3? */ 1537a2e2ca3bSLinus Lüssing if (transport_len < sizeof(struct igmpv3_query)) 1538a2e2ca3bSLinus Lüssing return -EINVAL; 1539a2e2ca3bSLinus Lüssing 1540a2e2ca3bSLinus Lüssing len = skb_transport_offset(skb) + sizeof(struct igmpv3_query); 1541a2e2ca3bSLinus Lüssing if (!ip_mc_may_pull(skb, len)) 15429afd85c9SLinus Lüssing return -EINVAL; 15439afd85c9SLinus Lüssing } 15449afd85c9SLinus Lüssing 15459afd85c9SLinus Lüssing /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer 15469afd85c9SLinus Lüssing * all-systems destination addresses (224.0.0.1) for general queries 15479afd85c9SLinus Lüssing */ 15489afd85c9SLinus Lüssing if (!igmp_hdr(skb)->group && 15499afd85c9SLinus Lüssing ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP)) 15509afd85c9SLinus Lüssing return -EINVAL; 15519afd85c9SLinus Lüssing 15529afd85c9SLinus Lüssing return 0; 15539afd85c9SLinus Lüssing } 15549afd85c9SLinus Lüssing 15559afd85c9SLinus Lüssing static int ip_mc_check_igmp_msg(struct sk_buff *skb) 15569afd85c9SLinus Lüssing { 15579afd85c9SLinus Lüssing switch (igmp_hdr(skb)->type) { 15589afd85c9SLinus Lüssing case IGMP_HOST_LEAVE_MESSAGE: 15599afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_REPORT: 15609afd85c9SLinus Lüssing case IGMPV2_HOST_MEMBERSHIP_REPORT: 15619afd85c9SLinus Lüssing return 0; 15629afd85c9SLinus Lüssing case IGMPV3_HOST_MEMBERSHIP_REPORT: 15639afd85c9SLinus Lüssing return ip_mc_check_igmp_reportv3(skb); 15649afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_QUERY: 15659afd85c9SLinus Lüssing return ip_mc_check_igmp_query(skb); 15669afd85c9SLinus Lüssing default: 15679afd85c9SLinus Lüssing return -ENOMSG; 15689afd85c9SLinus Lüssing } 15699afd85c9SLinus Lüssing } 15709afd85c9SLinus Lüssing 15715a43f697SAlexey Dobriyan static __sum16 ip_mc_validate_checksum(struct sk_buff *skb) 15729afd85c9SLinus Lüssing { 15739afd85c9SLinus Lüssing return skb_checksum_simple_validate(skb); 15749afd85c9SLinus Lüssing } 15759afd85c9SLinus Lüssing 1576a2e2ca3bSLinus Lüssing static int ip_mc_check_igmp_csum(struct sk_buff *skb) 15779afd85c9SLinus Lüssing { 15789afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr); 1579a2e2ca3bSLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 1580a2e2ca3bSLinus Lüssing struct sk_buff *skb_chk; 15819afd85c9SLinus Lüssing 1582a2e2ca3bSLinus Lüssing if (!ip_mc_may_pull(skb, len)) 1583a2e2ca3bSLinus Lüssing return -EINVAL; 15849afd85c9SLinus Lüssing 15859afd85c9SLinus Lüssing skb_chk = skb_checksum_trimmed(skb, transport_len, 15869afd85c9SLinus Lüssing ip_mc_validate_checksum); 15879afd85c9SLinus Lüssing if (!skb_chk) 1588a2e2ca3bSLinus Lüssing return -EINVAL; 15899afd85c9SLinus Lüssing 1590a2e2ca3bSLinus Lüssing if (skb_chk != skb) 1591a516993fSLinus Lüssing kfree_skb(skb_chk); 1592a516993fSLinus Lüssing 1593a2e2ca3bSLinus Lüssing return 0; 15949afd85c9SLinus Lüssing } 15959afd85c9SLinus Lüssing 15969afd85c9SLinus Lüssing /** 15979afd85c9SLinus Lüssing * ip_mc_check_igmp - checks whether this is a sane IGMP packet 15989afd85c9SLinus Lüssing * @skb: the skb to validate 15999afd85c9SLinus Lüssing * 16009afd85c9SLinus Lüssing * Checks whether an IPv4 packet is a valid IGMP packet. If so sets 1601a516993fSLinus Lüssing * skb transport header accordingly and returns zero. 16029afd85c9SLinus Lüssing * 16039afd85c9SLinus Lüssing * -EINVAL: A broken packet was detected, i.e. it violates some internet 16049afd85c9SLinus Lüssing * standard 16059afd85c9SLinus Lüssing * -ENOMSG: IP header validation succeeded but it is not an IGMP packet. 16069afd85c9SLinus Lüssing * -ENOMEM: A memory allocation failure happened. 16079afd85c9SLinus Lüssing * 1608a516993fSLinus Lüssing * Caller needs to set the skb network header and free any returned skb if it 1609a516993fSLinus Lüssing * differs from the provided skb. 16109afd85c9SLinus Lüssing */ 1611ba5ea614SLinus Lüssing int ip_mc_check_igmp(struct sk_buff *skb) 16129afd85c9SLinus Lüssing { 16139afd85c9SLinus Lüssing int ret = ip_mc_check_iphdr(skb); 16149afd85c9SLinus Lüssing 16159afd85c9SLinus Lüssing if (ret < 0) 16169afd85c9SLinus Lüssing return ret; 16179afd85c9SLinus Lüssing 16189afd85c9SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP) 16199afd85c9SLinus Lüssing return -ENOMSG; 16209afd85c9SLinus Lüssing 1621a2e2ca3bSLinus Lüssing ret = ip_mc_check_igmp_csum(skb); 1622a2e2ca3bSLinus Lüssing if (ret < 0) 1623a2e2ca3bSLinus Lüssing return ret; 1624a2e2ca3bSLinus Lüssing 1625a2e2ca3bSLinus Lüssing return ip_mc_check_igmp_msg(skb); 16269afd85c9SLinus Lüssing } 16279afd85c9SLinus Lüssing EXPORT_SYMBOL(ip_mc_check_igmp); 16289afd85c9SLinus Lüssing 16291da177e4SLinus Torvalds /* 16304aa5dee4SJiri Pirko * Resend IGMP JOIN report; used by netdev notifier. 1631a816c7c7SJay Vosburgh */ 16324aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev) 1633a816c7c7SJay Vosburgh { 163408882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1635866f3b25SEric Dumazet struct ip_mc_list *im; 1636866f3b25SEric Dumazet int type; 163787a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1638a816c7c7SJay Vosburgh 16394aa5dee4SJiri Pirko ASSERT_RTNL(); 16404aa5dee4SJiri Pirko 16414aa5dee4SJiri Pirko for_each_pmc_rtnl(in_dev, im) { 1642a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1643866f3b25SEric Dumazet continue; 1644df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 164587a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1646df2cf4a7SPhilip Downey continue; 1647a816c7c7SJay Vosburgh 1648e12b4539SFlavio Leitner /* a failover is happening and switches 1649866f3b25SEric Dumazet * must be notified immediately 1650866f3b25SEric Dumazet */ 1651e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev)) 1652866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT; 1653e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev)) 1654866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT; 1655e12b4539SFlavio Leitner else 1656866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT; 1657866f3b25SEric Dumazet igmp_send_report(in_dev, im, type); 1658866f3b25SEric Dumazet } 1659a816c7c7SJay Vosburgh #endif 1660a816c7c7SJay Vosburgh } 1661a816c7c7SJay Vosburgh 1662a816c7c7SJay Vosburgh /* 16631da177e4SLinus Torvalds * A socket has left a multicast group on device dev 16641da177e4SLinus Torvalds */ 16651da177e4SLinus Torvalds 16669fb20801SFlorian Fainelli void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp) 16671da177e4SLinus Torvalds { 16681d7138deSEric Dumazet struct ip_mc_list *i; 16691d7138deSEric Dumazet struct ip_mc_list __rcu **ip; 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds ASSERT_RTNL(); 16721da177e4SLinus Torvalds 16731d7138deSEric Dumazet for (ip = &in_dev->mc_list; 16741d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL; 16751d7138deSEric Dumazet ip = &i->next_rcu) { 16761da177e4SLinus Torvalds if (i->multiaddr == addr) { 16771da177e4SLinus Torvalds if (--i->users == 0) { 1678e9897071SEric Dumazet ip_mc_hash_remove(in_dev, i); 16791d7138deSEric Dumazet *ip = i->next_rcu; 1680b8bae41eSRami Rosen in_dev->mc_count--; 16819fb20801SFlorian Fainelli __igmp_group_dropped(i, gfp); 168224cf3af3SVeaceslav Falico ip_mc_clear_src(i); 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds if (!in_dev->dead) 16851da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds ip_ma_put(i); 16881da177e4SLinus Torvalds return; 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds break; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds } 16949fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_dec_group); 16951da177e4SLinus Torvalds 169675c78500SMoni Shoua /* Device changing type */ 169775c78500SMoni Shoua 169875c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 169975c78500SMoni Shoua { 17001d7138deSEric Dumazet struct ip_mc_list *pmc; 170175c78500SMoni Shoua 170275c78500SMoni Shoua ASSERT_RTNL(); 170375c78500SMoni Shoua 17041d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 17051d7138deSEric Dumazet igmp_group_dropped(pmc); 170675c78500SMoni Shoua } 170775c78500SMoni Shoua 170875c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 170975c78500SMoni Shoua { 17101d7138deSEric Dumazet struct ip_mc_list *pmc; 171175c78500SMoni Shoua 171275c78500SMoni Shoua ASSERT_RTNL(); 171375c78500SMoni Shoua 171424803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) { 171524803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 171624803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc); 171724803f38SHangbin Liu #endif 17180ae0d60aSHangbin Liu igmp_group_added(pmc); 171975c78500SMoni Shoua } 172024803f38SHangbin Liu } 172175c78500SMoni Shoua 17221da177e4SLinus Torvalds /* Device going down */ 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 17251da177e4SLinus Torvalds { 17261d7138deSEric Dumazet struct ip_mc_list *pmc; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds ASSERT_RTNL(); 17291da177e4SLinus Torvalds 17301d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 17311d7138deSEric Dumazet igmp_group_dropped(pmc); 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17344a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, 0); 17351da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 17361da177e4SLinus Torvalds __in_dev_put(in_dev); 17371da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 17381da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 17391da177e4SLinus Torvalds __in_dev_put(in_dev); 17401da177e4SLinus Torvalds #endif 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 1745966c37f2SHangbin Liu #ifdef CONFIG_IP_MULTICAST 1746966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev) 1747966c37f2SHangbin Liu { 1748966c37f2SHangbin Liu struct net *net = dev_net(in_dev->dev); 1749966c37f2SHangbin Liu 1750966c37f2SHangbin Liu in_dev->mr_qi = IGMP_QUERY_INTERVAL; 1751966c37f2SHangbin Liu in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL; 1752966c37f2SHangbin Liu in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv; 1753966c37f2SHangbin Liu } 1754966c37f2SHangbin Liu #else 1755966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev) 1756966c37f2SHangbin Liu { 1757966c37f2SHangbin Liu } 1758966c37f2SHangbin Liu #endif 1759966c37f2SHangbin Liu 17601da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 17611da177e4SLinus Torvalds { 17621da177e4SLinus Torvalds ASSERT_RTNL(); 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1765e99e88a9SKees Cook timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0); 1766e99e88a9SKees Cook timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0); 17671da177e4SLinus Torvalds #endif 1768966c37f2SHangbin Liu ip_mc_reset(in_dev); 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds /* Device going up */ 17741da177e4SLinus Torvalds 17751da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 17761da177e4SLinus Torvalds { 17771d7138deSEric Dumazet struct ip_mc_list *pmc; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds ASSERT_RTNL(); 17801da177e4SLinus Torvalds 1781966c37f2SHangbin Liu ip_mc_reset(in_dev); 17821da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 17831da177e4SLinus Torvalds 178424803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) { 178524803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 178624803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc); 178724803f38SHangbin Liu #endif 17880ae0d60aSHangbin Liu igmp_group_added(pmc); 17891da177e4SLinus Torvalds } 179024803f38SHangbin Liu } 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds /* 17931da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 17941da177e4SLinus Torvalds */ 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 17971da177e4SLinus Torvalds { 17981da177e4SLinus Torvalds struct ip_mc_list *i; 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds ASSERT_RTNL(); 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds /* Deactivate timers */ 18031da177e4SLinus Torvalds ip_mc_down(in_dev); 180424803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 180524803f38SHangbin Liu igmpv3_clear_delrec(in_dev); 180624803f38SHangbin Liu #endif 18071da177e4SLinus Torvalds 18081d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { 18091d7138deSEric Dumazet in_dev->mc_list = i->next_rcu; 1810b8bae41eSRami Rosen in_dev->mc_count--; 1811d8e29730SChengyang Fan ip_mc_clear_src(i); 18121da177e4SLinus Torvalds ip_ma_put(i); 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 18169e917dcaSEric Dumazet /* RTNL is locked */ 1817877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 18181da177e4SLinus Torvalds { 18191da177e4SLinus Torvalds struct net_device *dev = NULL; 18201da177e4SLinus Torvalds struct in_device *idev = NULL; 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds if (imr->imr_ifindex) { 1823877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 18241da177e4SLinus Torvalds return idev; 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 18279e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false); 18281da177e4SLinus Torvalds if (!dev) 18291da177e4SLinus Torvalds return NULL; 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 1832b23dd4feSDavid S. Miller if (!dev) { 183378fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, 183478fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr, 183578fbfd8aSDavid S. Miller 0, 0, 0); 1836b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1837d8d1f30bSChangli Gao dev = rt->dst.dev; 18381da177e4SLinus Torvalds ip_rt_put(rt); 18391da177e4SLinus Torvalds } 1840b23dd4feSDavid S. Miller } 18411da177e4SLinus Torvalds if (dev) { 18421da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1843e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 18441da177e4SLinus Torvalds } 18451da177e4SLinus Torvalds return idev; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds /* 18491da177e4SLinus Torvalds * Join a socket to a group 18501da177e4SLinus Torvalds */ 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 18538f935bbdSAl Viro __be32 *psfsrc) 18541da177e4SLinus Torvalds { 18551da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 18561da177e4SLinus Torvalds int rv = 0; 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds psf_prev = NULL; 18591da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 18601da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 18611da177e4SLinus Torvalds break; 18621da177e4SLinus Torvalds psf_prev = psf; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 18651da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 18661da177e4SLinus Torvalds return -ESRCH; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds psf->sf_count[sfmode]--; 18691da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 18701da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 18731da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18741da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 1875165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 18761da177e4SLinus Torvalds #endif 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds /* no more filters for this source */ 18791da177e4SLinus Torvalds if (psf_prev) 18801da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 18811da177e4SLinus Torvalds else 18821da177e4SLinus Torvalds pmc->sources = psf->sf_next; 18831da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18841da177e4SLinus Torvalds if (psf->sf_oldin && 18851da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 1886165094afSNikolay Borisov psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 18871da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 18881da177e4SLinus Torvalds pmc->tomb = psf; 18891da177e4SLinus Torvalds rv = 1; 18901da177e4SLinus Torvalds } else 18911da177e4SLinus Torvalds #endif 18921da177e4SLinus Torvalds kfree(psf); 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds return rv; 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 18981da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 18991da177e4SLinus Torvalds #endif 19001da177e4SLinus Torvalds 19018f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 19028f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 19031da177e4SLinus Torvalds { 19041da177e4SLinus Torvalds struct ip_mc_list *pmc; 19051da177e4SLinus Torvalds int changerec = 0; 19061da177e4SLinus Torvalds int i, err; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds if (!in_dev) 19091da177e4SLinus Torvalds return -ENODEV; 19101d7138deSEric Dumazet rcu_read_lock(); 19111d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 19121da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 19131da177e4SLinus Torvalds break; 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds if (!pmc) { 19161da177e4SLinus Torvalds /* MCA not found?? bug */ 19171d7138deSEric Dumazet rcu_read_unlock(); 19181da177e4SLinus Torvalds return -ESRCH; 19191da177e4SLinus Torvalds } 19201da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 19211d7138deSEric Dumazet rcu_read_unlock(); 19221da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19231da177e4SLinus Torvalds sf_markstate(pmc); 19241da177e4SLinus Torvalds #endif 19251da177e4SLinus Torvalds if (!delta) { 19261da177e4SLinus Torvalds err = -EINVAL; 19271da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 19281da177e4SLinus Torvalds goto out_unlock; 19291da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 19301da177e4SLinus Torvalds } 19311da177e4SLinus Torvalds err = 0; 19321da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 19331da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds changerec |= rv > 0; 19361da177e4SLinus Torvalds if (!err && rv < 0) 19371da177e4SLinus Torvalds err = rv; 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 19401da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 19411da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 19421da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19431da177e4SLinus Torvalds struct ip_sf_list *psf; 1944165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 19451da177e4SLinus Torvalds #endif 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* filter mode change */ 19481da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 19491da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1950165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 19514a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount); 19521da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 19531da177e4SLinus Torvalds psf->sf_crcount = 0; 19541da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 19551da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 19561da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 19571da177e4SLinus Torvalds #endif 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds out_unlock: 19601da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 19611da177e4SLinus Torvalds return err; 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds /* 19651da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 19661da177e4SLinus Torvalds */ 19671da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 19685eb81e89SJun Zhao __be32 *psfsrc) 19691da177e4SLinus Torvalds { 19701da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds psf_prev = NULL; 19731da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 19741da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 19751da177e4SLinus Torvalds break; 19761da177e4SLinus Torvalds psf_prev = psf; 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds if (!psf) { 19790da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 19801da177e4SLinus Torvalds if (!psf) 19811da177e4SLinus Torvalds return -ENOBUFS; 19821da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 19831da177e4SLinus Torvalds if (psf_prev) { 19841da177e4SLinus Torvalds psf_prev->sf_next = psf; 19851da177e4SLinus Torvalds } else 19861da177e4SLinus Torvalds pmc->sources = psf; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds psf->sf_count[sfmode]++; 19891da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 19901da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds return 0; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19961da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 19971da177e4SLinus Torvalds { 19981da177e4SLinus Torvalds struct ip_sf_list *psf; 19991da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 20021da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 20031da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 20041da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 20051da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 20061da177e4SLinus Torvalds } else 20071da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 20111da177e4SLinus Torvalds { 2012ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 20131da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 20141da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 20151da177e4SLinus Torvalds int new_in, rv; 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds rv = 0; 20181da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 20191da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 20201da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 20211da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 20221da177e4SLinus Torvalds } else 20231da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 2024ad12583fSDavid L Stevens if (new_in) { 2025ad12583fSDavid L Stevens if (!psf->sf_oldin) { 202676edc605SAl Viro struct ip_sf_list *prev = NULL; 2027ad12583fSDavid L Stevens 2028ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) { 2029ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 2030ad12583fSDavid L Stevens break; 2031ad12583fSDavid L Stevens prev = dpsf; 2032ad12583fSDavid L Stevens } 2033ad12583fSDavid L Stevens if (dpsf) { 2034ad12583fSDavid L Stevens if (prev) 2035ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 2036ad12583fSDavid L Stevens else 2037ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 2038ad12583fSDavid L Stevens kfree(dpsf); 2039ad12583fSDavid L Stevens } 20401da177e4SLinus Torvalds psf->sf_crcount = qrv; 20411da177e4SLinus Torvalds rv++; 20421da177e4SLinus Torvalds } 2043ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 2044ad12583fSDavid L Stevens 2045ad12583fSDavid L Stevens psf->sf_crcount = 0; 2046ad12583fSDavid L Stevens /* 2047ad12583fSDavid L Stevens * add or update "delete" records if an active filter 2048ad12583fSDavid L Stevens * is now inactive 2049ad12583fSDavid L Stevens */ 2050ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) 2051ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 2052ad12583fSDavid L Stevens break; 2053ad12583fSDavid L Stevens if (!dpsf) { 20543ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); 2055ad12583fSDavid L Stevens if (!dpsf) 2056ad12583fSDavid L Stevens continue; 2057ad12583fSDavid L Stevens *dpsf = *psf; 2058ad12583fSDavid L Stevens /* pmc->lock held by callers */ 2059ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 2060ad12583fSDavid L Stevens pmc->tomb = dpsf; 2061ad12583fSDavid L Stevens } 2062ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 2063ad12583fSDavid L Stevens rv++; 2064ad12583fSDavid L Stevens } 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds return rv; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds #endif 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds /* 20711da177e4SLinus Torvalds * Add multicast source filter list to the interface list 20721da177e4SLinus Torvalds */ 20738f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 20748f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 20751da177e4SLinus Torvalds { 20761da177e4SLinus Torvalds struct ip_mc_list *pmc; 20771da177e4SLinus Torvalds int isexclude; 20781da177e4SLinus Torvalds int i, err; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds if (!in_dev) 20811da177e4SLinus Torvalds return -ENODEV; 20821d7138deSEric Dumazet rcu_read_lock(); 20831d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 20841da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 20851da177e4SLinus Torvalds break; 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds if (!pmc) { 20881da177e4SLinus Torvalds /* MCA not found?? bug */ 20891d7138deSEric Dumazet rcu_read_unlock(); 20901da177e4SLinus Torvalds return -ESRCH; 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 20931d7138deSEric Dumazet rcu_read_unlock(); 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 20961da177e4SLinus Torvalds sf_markstate(pmc); 20971da177e4SLinus Torvalds #endif 20981da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 20991da177e4SLinus Torvalds if (!delta) 21001da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 21011da177e4SLinus Torvalds err = 0; 21021da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 21035eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); 21041da177e4SLinus Torvalds if (err) 21051da177e4SLinus Torvalds break; 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds if (err) { 21081da177e4SLinus Torvalds int j; 21091da177e4SLinus Torvalds 2110685f94e6SJun Zhao if (!delta) 21111da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 21121da177e4SLinus Torvalds for (j = 0; j < i; j++) 2113a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); 21141da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 21151da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 21161da177e4SLinus Torvalds struct ip_sf_list *psf; 2117165094afSNikolay Borisov struct net *net = dev_net(pmc->interface->dev); 2118cfcabdccSStephen Hemminger in_dev = pmc->interface; 21191da177e4SLinus Torvalds #endif 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds /* filter mode change */ 21221da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 21231da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 21241da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 21251da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 21261da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 21271da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 21281da177e4SLinus Torvalds 2129165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 21304a2b285eSEric Dumazet WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount); 21311da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 21321da177e4SLinus Torvalds psf->sf_crcount = 0; 21331da177e4SLinus Torvalds igmp_ifc_event(in_dev); 21341da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 21351da177e4SLinus Torvalds igmp_ifc_event(in_dev); 21361da177e4SLinus Torvalds #endif 21371da177e4SLinus Torvalds } 21381da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 21391da177e4SLinus Torvalds return err; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 21431da177e4SLinus Torvalds { 21443580d04aSEric Dumazet struct ip_sf_list *tomb, *sources; 21451da177e4SLinus Torvalds 2146c38b7d32SWANG Cong spin_lock_bh(&pmc->lock); 2147c38b7d32SWANG Cong tomb = pmc->tomb; 21481da177e4SLinus Torvalds pmc->tomb = NULL; 2149c38b7d32SWANG Cong sources = pmc->sources; 21501da177e4SLinus Torvalds pmc->sources = NULL; 21511da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 2152de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 21531da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 2154c38b7d32SWANG Cong spin_unlock_bh(&pmc->lock); 2155c38b7d32SWANG Cong 21563580d04aSEric Dumazet ip_sf_list_clear_all(tomb); 21573580d04aSEric Dumazet ip_sf_list_clear_all(sources); 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 216054ff9ef3SMarcelo Ricardo Leitner /* Join a multicast group 216154ff9ef3SMarcelo Ricardo Leitner */ 21626e2059b5SHangbin Liu static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr, 21636e2059b5SHangbin Liu unsigned int mode) 21641da177e4SLinus Torvalds { 21658f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 2166959d10f6SEric Dumazet struct ip_mc_socklist *iml, *i; 21671da177e4SLinus Torvalds struct in_device *in_dev; 21681da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2169877acedcSDaniel Lezcano struct net *net = sock_net(sk); 2170ca9b907dSDavid L Stevens int ifindex; 21711da177e4SLinus Torvalds int count = 0; 2172959d10f6SEric Dumazet int err; 2173959d10f6SEric Dumazet 2174959d10f6SEric Dumazet ASSERT_RTNL(); 21751da177e4SLinus Torvalds 2176f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21771da177e4SLinus Torvalds return -EINVAL; 21781da177e4SLinus Torvalds 2179877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds if (!in_dev) { 21821da177e4SLinus Torvalds err = -ENODEV; 21831da177e4SLinus Torvalds goto done; 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds err = -EADDRINUSE; 2187ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 21881d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 2189ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 2190ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 21911da177e4SLinus Torvalds goto done; 21921da177e4SLinus Torvalds count++; 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds err = -ENOBUFS; 2195815c5270SNikolay Borisov if (count >= net->ipv4.sysctl_igmp_max_memberships) 21961da177e4SLinus Torvalds goto done; 21978b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 219851456b29SIan Morris if (!iml) 2199ca9b907dSDavid L Stevens goto done; 2200ca9b907dSDavid L Stevens 22011da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 22021d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 22031da177e4SLinus Torvalds iml->sflist = NULL; 22046e2059b5SHangbin Liu iml->sfmode = mode; 2205cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 2206a1c4cd67SLi RongQing ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL); 22071da177e4SLinus Torvalds err = 0; 22081da177e4SLinus Torvalds done: 22091da177e4SLinus Torvalds return err; 22101da177e4SLinus Torvalds } 22116e2059b5SHangbin Liu 22126e2059b5SHangbin Liu /* Join ASM (Any-Source Multicast) group 22136e2059b5SHangbin Liu */ 22146e2059b5SHangbin Liu int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr) 22156e2059b5SHangbin Liu { 22166e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE); 22176e2059b5SHangbin Liu } 22184bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 22191da177e4SLinus Torvalds 22206e2059b5SHangbin Liu /* Join SSM (Source-Specific Multicast) group 22216e2059b5SHangbin Liu */ 22226e2059b5SHangbin Liu int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr, 22236e2059b5SHangbin Liu unsigned int mode) 22246e2059b5SHangbin Liu { 22256e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, mode); 22266e2059b5SHangbin Liu } 22276e2059b5SHangbin Liu 22281da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 22291da177e4SLinus Torvalds struct in_device *in_dev) 22301da177e4SLinus Torvalds { 22311d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 22321da177e4SLinus Torvalds int err; 22331da177e4SLinus Torvalds 223451456b29SIan Morris if (!psf) { 22351da177e4SLinus Torvalds /* any-source empty exclude case */ 22361da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 22371da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 22381da177e4SLinus Torvalds } 22391da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 2240c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 2241a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 2242c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2243e6a1f7e0SGustavo A. R. Silva atomic_sub(struct_size(psf, sl_addr, psf->sl_max), &sk->sk_omem_alloc); 22447519cce4SLai Jiangshan kfree_rcu(psf, rcu); 22451da177e4SLinus Torvalds return err; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 224854ff9ef3SMarcelo Ricardo Leitner int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 22491da177e4SLinus Torvalds { 22501da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22511d7138deSEric Dumazet struct ip_mc_socklist *iml; 22521d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 225384b42baeSDavid L Stevens struct in_device *in_dev; 2254877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22558f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 225684b42baeSDavid L Stevens u32 ifindex; 2257acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 22581da177e4SLinus Torvalds 2259959d10f6SEric Dumazet ASSERT_RTNL(); 2260959d10f6SEric Dumazet 2261877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 22624eba7bb1SAndrew Lunn if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) { 226352ad353aSdingtianhong ret = -ENODEV; 226452ad353aSdingtianhong goto out; 226552ad353aSdingtianhong } 226684b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 22671d7138deSEric Dumazet for (imlp = &inet->mc_list; 22681d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 22691d7138deSEric Dumazet imlp = &iml->next_rcu) { 2270acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 2271acd6e00bSDavid L Stevens continue; 2272acd6e00bSDavid L Stevens if (ifindex) { 2273acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 2274acd6e00bSDavid L Stevens continue; 2275acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 2276acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 2277acd6e00bSDavid L Stevens continue; 2278acd6e00bSDavid L Stevens 22791da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 22801da177e4SLinus Torvalds 22811d7138deSEric Dumazet *imlp = iml->next_rcu; 22821da177e4SLinus Torvalds 22834eba7bb1SAndrew Lunn if (in_dev) 228484b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 2285959d10f6SEric Dumazet 2286c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2287c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 228810d50e74SLai Jiangshan kfree_rcu(iml, rcu); 22891da177e4SLinus Torvalds return 0; 22901da177e4SLinus Torvalds } 229152ad353aSdingtianhong out: 2292959d10f6SEric Dumazet return ret; 2293959d10f6SEric Dumazet } 2294193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 22971da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 22981da177e4SLinus Torvalds { 22991da177e4SLinus Torvalds int err; 23001da177e4SLinus Torvalds struct ip_mreqn imr; 230163007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 23021da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 23031da177e4SLinus Torvalds struct in_device *in_dev = NULL; 23041da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23051da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2306877acedcSDaniel Lezcano struct net *net = sock_net(sk); 23078cdaaa15SDavid L Stevens int leavegroup = 0; 23081da177e4SLinus Torvalds int i, j, rv; 23091da177e4SLinus Torvalds 2310f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 23111da177e4SLinus Torvalds return -EINVAL; 23121da177e4SLinus Torvalds 231354ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 23141da177e4SLinus Torvalds 23151da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 23161da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 23171da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2318877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 23191da177e4SLinus Torvalds 23201da177e4SLinus Torvalds if (!in_dev) { 23211da177e4SLinus Torvalds err = -ENODEV; 23221da177e4SLinus Torvalds goto done; 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 23251da177e4SLinus Torvalds 23261d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 2327f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 2328f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 2329f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 23301da177e4SLinus Torvalds break; 23311da177e4SLinus Torvalds } 2332917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2333917f2f10SDavid L Stevens err = -EINVAL; 23341da177e4SLinus Torvalds goto done; 2335917f2f10SDavid L Stevens } 23361da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 23371da177e4SLinus Torvalds if (pmc->sflist) { 2338917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 2339917f2f10SDavid L Stevens err = -EINVAL; 23401da177e4SLinus Torvalds goto done; 2341917f2f10SDavid L Stevens } 23421da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 23431da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 23441da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 23451da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 23461da177e4SLinus Torvalds NULL, 0); 23471da177e4SLinus Torvalds pmc->sfmode = omode; 23481da177e4SLinus Torvalds } 23491da177e4SLinus Torvalds 23501d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 23511da177e4SLinus Torvalds if (!add) { 23521da177e4SLinus Torvalds if (!psl) 2353917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 23541da177e4SLinus Torvalds rv = !0; 23551da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 23561da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 235763007727SAl Viro sizeof(__be32)); 23581da177e4SLinus Torvalds if (rv == 0) 23591da177e4SLinus Torvalds break; 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds if (rv) /* source not found */ 2362917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 23631da177e4SLinus Torvalds 23648cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 23658cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 23668cdaaa15SDavid L Stevens leavegroup = 1; 23678cdaaa15SDavid L Stevens goto done; 23688cdaaa15SDavid L Stevens } 23698cdaaa15SDavid L Stevens 23701da177e4SLinus Torvalds /* update the interface filter */ 23711da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 23721da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds for (j = i+1; j < psl->sl_count; j++) 23751da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 23761da177e4SLinus Torvalds psl->sl_count--; 23771da177e4SLinus Torvalds err = 0; 23781da177e4SLinus Torvalds goto done; 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds /* else, add a new source to the filter */ 23811da177e4SLinus Torvalds 2382166b6b2dSNikolay Borisov if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) { 23831da177e4SLinus Torvalds err = -ENOBUFS; 23841da177e4SLinus Torvalds goto done; 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 23871da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 23881da177e4SLinus Torvalds int count = IP_SFBLOCK; 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds if (psl) 23911da177e4SLinus Torvalds count += psl->sl_max; 2392e6a1f7e0SGustavo A. R. Silva newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, count), 2393e6a1f7e0SGustavo A. R. Silva GFP_KERNEL); 23941da177e4SLinus Torvalds if (!newpsl) { 23951da177e4SLinus Torvalds err = -ENOBUFS; 23961da177e4SLinus Torvalds goto done; 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds newpsl->sl_max = count; 23991da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 24001da177e4SLinus Torvalds if (psl) { 24011da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) 24021da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2403c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2404e6a1f7e0SGustavo A. R. Silva atomic_sub(struct_size(psl, sl_addr, psl->sl_max), 2405e6a1f7e0SGustavo A. R. Silva &sk->sk_omem_alloc); 24067519cce4SLai Jiangshan kfree_rcu(psl, rcu); 24071da177e4SLinus Torvalds } 2408cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2409c85bb41eSFlavio Leitner psl = newpsl; 24101da177e4SLinus Torvalds } 24111da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 24121da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 24131da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 241463007727SAl Viro sizeof(__be32)); 24151da177e4SLinus Torvalds if (rv == 0) 24161da177e4SLinus Torvalds break; 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 24191da177e4SLinus Torvalds goto done; 24201da177e4SLinus Torvalds for (j = psl->sl_count-1; j >= i; j--) 24211da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 24221da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 24231da177e4SLinus Torvalds psl->sl_count++; 24241da177e4SLinus Torvalds err = 0; 24251da177e4SLinus Torvalds /* update the interface list */ 24261da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 24271da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 24281da177e4SLinus Torvalds done: 24298cdaaa15SDavid L Stevens if (leavegroup) 243054ff9ef3SMarcelo Ricardo Leitner err = ip_mc_leave_group(sk, &imr); 24311da177e4SLinus Torvalds return err; 24321da177e4SLinus Torvalds } 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 24351da177e4SLinus Torvalds { 24369951f036SDavid L Stevens int err = 0; 24371da177e4SLinus Torvalds struct ip_mreqn imr; 243863007727SAl Viro __be32 addr = msf->imsf_multiaddr; 24391da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 24401da177e4SLinus Torvalds struct in_device *in_dev; 24411da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 24421da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2443877acedcSDaniel Lezcano struct net *net = sock_net(sk); 24449951f036SDavid L Stevens int leavegroup = 0; 24451da177e4SLinus Torvalds 2446f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 24471da177e4SLinus Torvalds return -EINVAL; 24481da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 24491da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 24501da177e4SLinus Torvalds return -EINVAL; 24511da177e4SLinus Torvalds 245254ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 24531da177e4SLinus Torvalds 24541da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 24551da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 24561da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2457877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds if (!in_dev) { 24601da177e4SLinus Torvalds err = -ENODEV; 24611da177e4SLinus Torvalds goto done; 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds 24649951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 24659951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 24669951f036SDavid L Stevens leavegroup = 1; 24679951f036SDavid L Stevens goto done; 24689951f036SDavid L Stevens } 24699951f036SDavid L Stevens 24701d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 24711da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 24721da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 24731da177e4SLinus Torvalds break; 24741da177e4SLinus Torvalds } 2475917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2476917f2f10SDavid L Stevens err = -EINVAL; 24771da177e4SLinus Torvalds goto done; 2478917f2f10SDavid L Stevens } 24791da177e4SLinus Torvalds if (msf->imsf_numsrc) { 2480e6a1f7e0SGustavo A. R. Silva newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, 2481e6a1f7e0SGustavo A. R. Silva msf->imsf_numsrc), 24828b3a7005SKris Katterjohn GFP_KERNEL); 24831da177e4SLinus Torvalds if (!newpsl) { 24841da177e4SLinus Torvalds err = -ENOBUFS; 24851da177e4SLinus Torvalds goto done; 24861da177e4SLinus Torvalds } 24871da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 24882d3e5cafSGustavo A. R. Silva memcpy(newpsl->sl_addr, msf->imsf_slist_flex, 24892d3e5cafSGustavo A. R. Silva flex_array_size(msf, imsf_slist_flex, msf->imsf_numsrc)); 24901da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 24911da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 24921da177e4SLinus Torvalds if (err) { 2493e6a1f7e0SGustavo A. R. Silva sock_kfree_s(sk, newpsl, 2494e6a1f7e0SGustavo A. R. Silva struct_size(newpsl, sl_addr, 2495e6a1f7e0SGustavo A. R. Silva newpsl->sl_max)); 24961da177e4SLinus Torvalds goto done; 24971da177e4SLinus Torvalds } 24988713dbf0SYan Zheng } else { 24991da177e4SLinus Torvalds newpsl = NULL; 25008713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 25018713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 25028713dbf0SYan Zheng } 25031d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 25041da177e4SLinus Torvalds if (psl) { 25051da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 25061da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2507c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2508e6a1f7e0SGustavo A. R. Silva atomic_sub(struct_size(psl, sl_addr, psl->sl_max), 2509e6a1f7e0SGustavo A. R. Silva &sk->sk_omem_alloc); 25107519cce4SLai Jiangshan kfree_rcu(psl, rcu); 25111da177e4SLinus Torvalds } else 25121da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 25131da177e4SLinus Torvalds 0, NULL, 0); 2514cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 25151da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2516917f2f10SDavid L Stevens err = 0; 25171da177e4SLinus Torvalds done: 25189951f036SDavid L Stevens if (leavegroup) 25199951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 25201da177e4SLinus Torvalds return err; 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 25241da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 25251da177e4SLinus Torvalds { 25261da177e4SLinus Torvalds int err, len, count, copycount; 25271da177e4SLinus Torvalds struct ip_mreqn imr; 252863007727SAl Viro __be32 addr = msf->imsf_multiaddr; 25291da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25301da177e4SLinus Torvalds struct in_device *in_dev; 25311da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25321da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2533877acedcSDaniel Lezcano struct net *net = sock_net(sk); 25341da177e4SLinus Torvalds 253587e9f031SWANG Cong ASSERT_RTNL(); 253687e9f031SWANG Cong 2537f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 25381da177e4SLinus Torvalds return -EINVAL; 25391da177e4SLinus Torvalds 25401da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 25411da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 25421da177e4SLinus Torvalds imr.imr_ifindex = 0; 2543877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds if (!in_dev) { 25461da177e4SLinus Torvalds err = -ENODEV; 25471da177e4SLinus Torvalds goto done; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 25501da177e4SLinus Torvalds 25511d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 25521da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 25531da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 25541da177e4SLinus Torvalds break; 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 25571da177e4SLinus Torvalds goto done; 25581da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 25591d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 25601da177e4SLinus Torvalds if (!psl) { 25611da177e4SLinus Torvalds count = 0; 25621da177e4SLinus Torvalds } else { 25631da177e4SLinus Torvalds count = psl->sl_count; 25641da177e4SLinus Torvalds } 25651da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 25662d3e5cafSGustavo A. R. Silva len = flex_array_size(psl, sl_addr, copycount); 25671da177e4SLinus Torvalds msf->imsf_numsrc = count; 25684167a960SGustavo A. R. Silva if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 25694167a960SGustavo A. R. Silva copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 25701da177e4SLinus Torvalds return -EFAULT; 25711da177e4SLinus Torvalds } 25721da177e4SLinus Torvalds if (len && 25732d3e5cafSGustavo A. R. Silva copy_to_user(&optval->imsf_slist_flex[0], psl->sl_addr, len)) 25741da177e4SLinus Torvalds return -EFAULT; 25751da177e4SLinus Torvalds return 0; 25761da177e4SLinus Torvalds done: 25771da177e4SLinus Torvalds return err; 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 2581931ca7abSAl Viro struct sockaddr_storage __user *p) 25821da177e4SLinus Torvalds { 2583931ca7abSAl Viro int i, count, copycount; 25841da177e4SLinus Torvalds struct sockaddr_in *psin; 258563007727SAl Viro __be32 addr; 25861da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25871da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25881da177e4SLinus Torvalds struct ip_sf_socklist *psl; 25891da177e4SLinus Torvalds 259087e9f031SWANG Cong ASSERT_RTNL(); 259187e9f031SWANG Cong 25921da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 25931da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 25941da177e4SLinus Torvalds return -EINVAL; 25951da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2596f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 25971da177e4SLinus Torvalds return -EINVAL; 25981da177e4SLinus Torvalds 25991d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 26001da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 26011da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 26021da177e4SLinus Torvalds break; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 2605931ca7abSAl Viro return -EADDRNOTAVAIL; 26061da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 26071d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 26081da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 26091da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 26101da177e4SLinus Torvalds gsf->gf_numsrc = count; 2611931ca7abSAl Viro for (i = 0; i < copycount; i++, p++) { 26121da177e4SLinus Torvalds struct sockaddr_storage ss; 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 26151da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 26161da177e4SLinus Torvalds psin->sin_family = AF_INET; 26171da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 2618931ca7abSAl Viro if (copy_to_user(p, &ss, sizeof(ss))) 26191da177e4SLinus Torvalds return -EFAULT; 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds return 0; 26221da177e4SLinus Torvalds } 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds /* 26251da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 26261da177e4SLinus Torvalds */ 262760d9b031SDavid Ahern int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, 262860d9b031SDavid Ahern int dif, int sdif) 26291da177e4SLinus Torvalds { 26301da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 26311da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 26321da177e4SLinus Torvalds struct ip_sf_socklist *psl; 26331da177e4SLinus Torvalds int i; 2634c85bb41eSFlavio Leitner int ret; 26351da177e4SLinus Torvalds 2636c85bb41eSFlavio Leitner ret = 1; 2637f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2638c85bb41eSFlavio Leitner goto out; 26391da177e4SLinus Torvalds 2640c85bb41eSFlavio Leitner rcu_read_lock(); 26411d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 26421da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 264360d9b031SDavid Ahern (pmc->multi.imr_ifindex == dif || 264460d9b031SDavid Ahern (sdif && pmc->multi.imr_ifindex == sdif))) 26451da177e4SLinus Torvalds break; 26461da177e4SLinus Torvalds } 2647c85bb41eSFlavio Leitner ret = inet->mc_all; 26481da177e4SLinus Torvalds if (!pmc) 2649c85bb41eSFlavio Leitner goto unlock; 26501d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2651c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 26521da177e4SLinus Torvalds if (!psl) 2653c85bb41eSFlavio Leitner goto unlock; 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 26561da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 26571da177e4SLinus Torvalds break; 26581da177e4SLinus Torvalds } 2659c85bb41eSFlavio Leitner ret = 0; 26601da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2661c85bb41eSFlavio Leitner goto unlock; 26621da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2663c85bb41eSFlavio Leitner goto unlock; 2664c85bb41eSFlavio Leitner ret = 1; 2665c85bb41eSFlavio Leitner unlock: 2666c85bb41eSFlavio Leitner rcu_read_unlock(); 2667c85bb41eSFlavio Leitner out: 2668c85bb41eSFlavio Leitner return ret; 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds /* 26721da177e4SLinus Torvalds * A socket is closing. 26731da177e4SLinus Torvalds */ 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 26761da177e4SLinus Torvalds { 26771da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 26781da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2679877acedcSDaniel Lezcano struct net *net = sock_net(sk); 26801da177e4SLinus Torvalds 268151456b29SIan Morris if (!inet->mc_list) 26821da177e4SLinus Torvalds return; 26831da177e4SLinus Torvalds 26841da177e4SLinus Torvalds rtnl_lock(); 26851d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 26861da177e4SLinus Torvalds struct in_device *in_dev; 26871da177e4SLinus Torvalds 26881d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2689877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 26901da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 269100db4124SIan Morris if (in_dev) 26921da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2693c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2694c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 269510d50e74SLai Jiangshan kfree_rcu(iml, rcu); 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds rtnl_unlock(); 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds 2700dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 27012094acbbSAlexander Duyck int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto) 27021da177e4SLinus Torvalds { 27031da177e4SLinus Torvalds struct ip_mc_list *im; 2704e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 27051da177e4SLinus Torvalds struct ip_sf_list *psf; 27061da177e4SLinus Torvalds int rv = 0; 27071da177e4SLinus Torvalds 2708e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash); 2709e9897071SEric Dumazet if (mc_hash) { 2710c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); 2711e9897071SEric Dumazet 2712e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]); 2713e9897071SEric Dumazet im != NULL; 2714e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) { 2715e9897071SEric Dumazet if (im->multiaddr == mc_addr) 2716e9897071SEric Dumazet break; 2717e9897071SEric Dumazet } 2718e9897071SEric Dumazet } else { 27191d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 27201da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 27211da177e4SLinus Torvalds break; 27221da177e4SLinus Torvalds } 2723e9897071SEric Dumazet } 27241da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 27251da177e4SLinus Torvalds rv = 1; 27261da177e4SLinus Torvalds } else if (im) { 27271da177e4SLinus Torvalds if (src_addr) { 272823d2b940SLiu Jian spin_lock_bh(&im->lock); 27291da177e4SLinus Torvalds for (psf = im->sources; psf; psf = psf->sf_next) { 27301da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 27311da177e4SLinus Torvalds break; 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds if (psf) 27341da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 27351da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 27361da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 27371da177e4SLinus Torvalds else 27381da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 273923d2b940SLiu Jian spin_unlock_bh(&im->lock); 27401da177e4SLinus Torvalds } else 27411da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 27421da177e4SLinus Torvalds } 27431da177e4SLinus Torvalds return rv; 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 27471da177e4SLinus Torvalds struct igmp_mc_iter_state { 27487091e728SAlexey Dobriyan struct seq_net_private p; 27491da177e4SLinus Torvalds struct net_device *dev; 27501da177e4SLinus Torvalds struct in_device *in_dev; 27511da177e4SLinus Torvalds }; 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 27561da177e4SLinus Torvalds { 27577091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 27581da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 27591da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27601da177e4SLinus Torvalds 27617562f876SPavel Emelianov state->in_dev = NULL; 276261fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 27631da177e4SLinus Torvalds struct in_device *in_dev; 27646baff150SEric Dumazet 27656baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 27661da177e4SLinus Torvalds if (!in_dev) 27671da177e4SLinus Torvalds continue; 27681d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 27691da177e4SLinus Torvalds if (im) { 27701da177e4SLinus Torvalds state->in_dev = in_dev; 27711da177e4SLinus Torvalds break; 27721da177e4SLinus Torvalds } 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds return im; 27751da177e4SLinus Torvalds } 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 27781da177e4SLinus Torvalds { 27791da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27806baff150SEric Dumazet 27811d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 27821d7138deSEric Dumazet while (!im) { 27836baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 27841da177e4SLinus Torvalds if (!state->dev) { 27851da177e4SLinus Torvalds state->in_dev = NULL; 27861da177e4SLinus Torvalds break; 27871da177e4SLinus Torvalds } 27886baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 27891da177e4SLinus Torvalds if (!state->in_dev) 27901da177e4SLinus Torvalds continue; 27911d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 27921da177e4SLinus Torvalds } 27931da177e4SLinus Torvalds return im; 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds 27961da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 27971da177e4SLinus Torvalds { 27981da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 27991da177e4SLinus Torvalds if (im) 28001da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 28011da177e4SLinus Torvalds --pos; 28021da177e4SLinus Torvalds return pos ? NULL : im; 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 280661fbab77Sstephen hemminger __acquires(rcu) 28071da177e4SLinus Torvalds { 280861fbab77Sstephen hemminger rcu_read_lock(); 28091da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 28101da177e4SLinus Torvalds } 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 28131da177e4SLinus Torvalds { 28141da177e4SLinus Torvalds struct ip_mc_list *im; 28151da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 28161da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 28171da177e4SLinus Torvalds else 28181da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 28191da177e4SLinus Torvalds ++*pos; 28201da177e4SLinus Torvalds return im; 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 282461fbab77Sstephen hemminger __releases(rcu) 28251da177e4SLinus Torvalds { 28261da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 28271d7138deSEric Dumazet 28281da177e4SLinus Torvalds state->in_dev = NULL; 28291da177e4SLinus Torvalds state->dev = NULL; 283061fbab77Sstephen hemminger rcu_read_unlock(); 28311da177e4SLinus Torvalds } 28321da177e4SLinus Torvalds 28331da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 28341da177e4SLinus Torvalds { 28351da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 28361da177e4SLinus Torvalds seq_puts(seq, 28371da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 28381da177e4SLinus Torvalds else { 2839*2e47eeceSYu Zhe struct ip_mc_list *im = v; 28401da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 28411da177e4SLinus Torvalds char *querier; 2842a399a805SEric Dumazet long delta; 2843a399a805SEric Dumazet 28441da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 28451da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 28461da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 28471da177e4SLinus Torvalds "V3"; 28481da177e4SLinus Torvalds #else 28491da177e4SLinus Torvalds querier = "NONE"; 28501da177e4SLinus Torvalds #endif 28511da177e4SLinus Torvalds 2852e6b68883SAndreea-Cristina Bernat if (rcu_access_pointer(state->in_dev->mc_list) == im) { 28531da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2854b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 28551da177e4SLinus Torvalds } 28561da177e4SLinus Torvalds 2857a399a805SEric Dumazet delta = im->timer.expires - jiffies; 28581da177e4SLinus Torvalds seq_printf(seq, 2859338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 28601da177e4SLinus Torvalds im->multiaddr, im->users, 2861a399a805SEric Dumazet im->tm_running, 2862a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 28631da177e4SLinus Torvalds im->reporter); 28641da177e4SLinus Torvalds } 28651da177e4SLinus Torvalds return 0; 28661da177e4SLinus Torvalds } 28671da177e4SLinus Torvalds 2868f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 28691da177e4SLinus Torvalds .start = igmp_mc_seq_start, 28701da177e4SLinus Torvalds .next = igmp_mc_seq_next, 28711da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 28721da177e4SLinus Torvalds .show = igmp_mc_seq_show, 28731da177e4SLinus Torvalds }; 28741da177e4SLinus Torvalds 28751da177e4SLinus Torvalds struct igmp_mcf_iter_state { 28767091e728SAlexey Dobriyan struct seq_net_private p; 28771da177e4SLinus Torvalds struct net_device *dev; 28781da177e4SLinus Torvalds struct in_device *idev; 28791da177e4SLinus Torvalds struct ip_mc_list *im; 28801da177e4SLinus Torvalds }; 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 28831da177e4SLinus Torvalds 28841da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 28851da177e4SLinus Torvalds { 28867091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 28871da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 28881da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 28891da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 28901da177e4SLinus Torvalds 28917562f876SPavel Emelianov state->idev = NULL; 28927562f876SPavel Emelianov state->im = NULL; 289361fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 28941da177e4SLinus Torvalds struct in_device *idev; 28956baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 289651456b29SIan Morris if (unlikely(!idev)) 28971da177e4SLinus Torvalds continue; 28981d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 289900db4124SIan Morris if (likely(im)) { 29001da177e4SLinus Torvalds spin_lock_bh(&im->lock); 29011da177e4SLinus Torvalds psf = im->sources; 290200db4124SIan Morris if (likely(psf)) { 29031da177e4SLinus Torvalds state->im = im; 29041da177e4SLinus Torvalds state->idev = idev; 29051da177e4SLinus Torvalds break; 29061da177e4SLinus Torvalds } 29071da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 29081da177e4SLinus Torvalds } 29091da177e4SLinus Torvalds } 29101da177e4SLinus Torvalds return psf; 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds 29131da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 29141da177e4SLinus Torvalds { 29151da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 29161da177e4SLinus Torvalds 29171da177e4SLinus Torvalds psf = psf->sf_next; 29181da177e4SLinus Torvalds while (!psf) { 29191da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 29201da177e4SLinus Torvalds state->im = state->im->next; 29211da177e4SLinus Torvalds while (!state->im) { 29226baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 29231da177e4SLinus Torvalds if (!state->dev) { 29241da177e4SLinus Torvalds state->idev = NULL; 29251da177e4SLinus Torvalds goto out; 29261da177e4SLinus Torvalds } 29276baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 29281da177e4SLinus Torvalds if (!state->idev) 29291da177e4SLinus Torvalds continue; 29301d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds if (!state->im) 29331da177e4SLinus Torvalds break; 29341da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 29351da177e4SLinus Torvalds psf = state->im->sources; 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds out: 29381da177e4SLinus Torvalds return psf; 29391da177e4SLinus Torvalds } 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 29421da177e4SLinus Torvalds { 29431da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 29441da177e4SLinus Torvalds if (psf) 29451da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 29461da177e4SLinus Torvalds --pos; 29471da177e4SLinus Torvalds return pos ? NULL : psf; 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 29501da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 295161fbab77Sstephen hemminger __acquires(rcu) 29521da177e4SLinus Torvalds { 295361fbab77Sstephen hemminger rcu_read_lock(); 29541da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 29551da177e4SLinus Torvalds } 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 29581da177e4SLinus Torvalds { 29591da177e4SLinus Torvalds struct ip_sf_list *psf; 29601da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 29611da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 29621da177e4SLinus Torvalds else 29631da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 29641da177e4SLinus Torvalds ++*pos; 29651da177e4SLinus Torvalds return psf; 29661da177e4SLinus Torvalds } 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 296961fbab77Sstephen hemminger __releases(rcu) 29701da177e4SLinus Torvalds { 29711da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 297200db4124SIan Morris if (likely(state->im)) { 29731da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 29741da177e4SLinus Torvalds state->im = NULL; 29751da177e4SLinus Torvalds } 29761da177e4SLinus Torvalds state->idev = NULL; 29771da177e4SLinus Torvalds state->dev = NULL; 297861fbab77Sstephen hemminger rcu_read_unlock(); 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds 29811da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 29821da177e4SLinus Torvalds { 2983*2e47eeceSYu Zhe struct ip_sf_list *psf = v; 29841da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 29851da177e4SLinus Torvalds 29861da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 29871744bea1SJoe Perches seq_puts(seq, "Idx Device MCA SRC INC EXC\n"); 29881da177e4SLinus Torvalds } else { 29891da177e4SLinus Torvalds seq_printf(seq, 29901da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 29911da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 29921da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 29931da177e4SLinus Torvalds ntohl(state->im->multiaddr), 29941da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 29951da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 29961da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds return 0; 29991da177e4SLinus Torvalds } 30001da177e4SLinus Torvalds 3001f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 30021da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 30031da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 30041da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 30051da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 30061da177e4SLinus Torvalds }; 30071da177e4SLinus Torvalds 30082c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 30097091e728SAlexey Dobriyan { 30107091e728SAlexey Dobriyan struct proc_dir_entry *pde; 301193a714d6SMadhu Challa int err; 30127091e728SAlexey Dobriyan 3013c3506372SChristoph Hellwig pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops, 3014c3506372SChristoph Hellwig sizeof(struct igmp_mc_iter_state)); 30157091e728SAlexey Dobriyan if (!pde) 30167091e728SAlexey Dobriyan goto out_igmp; 3017c3506372SChristoph Hellwig pde = proc_create_net("mcfilter", 0444, net->proc_net, 3018c3506372SChristoph Hellwig &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state)); 30197091e728SAlexey Dobriyan if (!pde) 30207091e728SAlexey Dobriyan goto out_mcfilter; 302193a714d6SMadhu Challa err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET, 302293a714d6SMadhu Challa SOCK_DGRAM, 0, net); 302393a714d6SMadhu Challa if (err < 0) { 302493a714d6SMadhu Challa pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n", 302593a714d6SMadhu Challa err); 302693a714d6SMadhu Challa goto out_sock; 302793a714d6SMadhu Challa } 302893a714d6SMadhu Challa 30297091e728SAlexey Dobriyan return 0; 30307091e728SAlexey Dobriyan 303193a714d6SMadhu Challa out_sock: 303293a714d6SMadhu Challa remove_proc_entry("mcfilter", net->proc_net); 30337091e728SAlexey Dobriyan out_mcfilter: 3034ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 30357091e728SAlexey Dobriyan out_igmp: 30367091e728SAlexey Dobriyan return -ENOMEM; 30377091e728SAlexey Dobriyan } 30387091e728SAlexey Dobriyan 30392c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 30407091e728SAlexey Dobriyan { 3041ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net); 3042ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 304393a714d6SMadhu Challa inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk); 30447091e728SAlexey Dobriyan } 30457091e728SAlexey Dobriyan 30467091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 30477091e728SAlexey Dobriyan .init = igmp_net_init, 30487091e728SAlexey Dobriyan .exit = igmp_net_exit, 30491da177e4SLinus Torvalds }; 305072c1d3bdSWANG Cong #endif 30511da177e4SLinus Torvalds 30524aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this, 30534aa5dee4SJiri Pirko unsigned long event, void *ptr) 30544aa5dee4SJiri Pirko { 30554aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 30564aa5dee4SJiri Pirko struct in_device *in_dev; 30574aa5dee4SJiri Pirko 30584aa5dee4SJiri Pirko switch (event) { 30594aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 30604aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev); 30614aa5dee4SJiri Pirko if (in_dev) 30624aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev); 30634aa5dee4SJiri Pirko break; 30644aa5dee4SJiri Pirko default: 30654aa5dee4SJiri Pirko break; 30664aa5dee4SJiri Pirko } 30674aa5dee4SJiri Pirko return NOTIFY_DONE; 30684aa5dee4SJiri Pirko } 30694aa5dee4SJiri Pirko 30704aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = { 30714aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event, 30724aa5dee4SJiri Pirko }; 30734aa5dee4SJiri Pirko 307472c1d3bdSWANG Cong int __init igmp_mc_init(void) 30751da177e4SLinus Torvalds { 307672c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS) 30774aa5dee4SJiri Pirko int err; 30784aa5dee4SJiri Pirko 30794aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops); 30804aa5dee4SJiri Pirko if (err) 30814aa5dee4SJiri Pirko return err; 30824aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier); 30834aa5dee4SJiri Pirko if (err) 30844aa5dee4SJiri Pirko goto reg_notif_fail; 30854aa5dee4SJiri Pirko return 0; 30864aa5dee4SJiri Pirko 30874aa5dee4SJiri Pirko reg_notif_fail: 30884aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops); 30894aa5dee4SJiri Pirko return err; 309072c1d3bdSWANG Cong #else 309172c1d3bdSWANG Cong return register_netdevice_notifier(&igmp_notifier); 30921da177e4SLinus Torvalds #endif 309372c1d3bdSWANG Cong } 3094