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); 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 8081da177e4SLinus Torvalds if (in_dev->mr_ifc_count) { 8091da177e4SLinus Torvalds in_dev->mr_ifc_count--; 810cab70040SWilliam Manley igmp_ifc_start_timer(in_dev, 811cab70040SWilliam Manley unsolicited_report_interval(in_dev)); 8121da177e4SLinus Torvalds } 813e2401654SSalam Noureddine in_dev_put(in_dev); 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 8171da177e4SLinus Torvalds { 818165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 8191da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 8201da177e4SLinus Torvalds return; 821165094afSNikolay Borisov in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 8221da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds 826e99e88a9SKees Cook static void igmp_timer_expire(struct timer_list *t) 8271da177e4SLinus Torvalds { 828e99e88a9SKees Cook struct ip_mc_list *im = from_timer(im, t, timer); 8291da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds spin_lock(&im->lock); 8321da177e4SLinus Torvalds im->tm_running = 0; 8331da177e4SLinus Torvalds 8344fb7253eSHangbin Liu if (im->unsolicit_count && --im->unsolicit_count) 835cab70040SWilliam Manley igmp_start_timer(im, unsolicited_report_interval(in_dev)); 8364fb7253eSHangbin Liu 8371da177e4SLinus Torvalds im->reporter = 1; 8381da177e4SLinus Torvalds spin_unlock(&im->lock); 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 8411da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 8421da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 8431da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 8441da177e4SLinus Torvalds else 8451da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds ip_ma_put(im); 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds 850ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 851ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds struct ip_sf_list *psf; 8541da177e4SLinus Torvalds int i, scount; 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds scount = 0; 8571da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 8581da177e4SLinus Torvalds if (scount == nsrcs) 8591da177e4SLinus Torvalds break; 860ad12583fSDavid L Stevens for (i = 0; i < nsrcs; i++) { 861ad12583fSDavid L Stevens /* skip inactive filters */ 862e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] || 863ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 864ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 865ce713ee5SRongQing.Li break; 866ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 867ad12583fSDavid L Stevens scount++; 868ad12583fSDavid L Stevens break; 869ad12583fSDavid L Stevens } 870ad12583fSDavid L Stevens } 871ad12583fSDavid L Stevens } 872ad12583fSDavid L Stevens pmc->gsquery = 0; 873ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 874ad12583fSDavid L Stevens return 0; 875ad12583fSDavid L Stevens return 1; 876ad12583fSDavid L Stevens } 877ad12583fSDavid L Stevens 878ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 879ad12583fSDavid L Stevens { 880ad12583fSDavid L Stevens struct ip_sf_list *psf; 881ad12583fSDavid L Stevens int i, scount; 882ad12583fSDavid L Stevens 883ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 884ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 885ad12583fSDavid L Stevens 886ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 887ad12583fSDavid L Stevens scount = 0; 888ad12583fSDavid L Stevens for (psf = pmc->sources; psf; psf = psf->sf_next) { 889ad12583fSDavid L Stevens if (scount == nsrcs) 890ad12583fSDavid L Stevens break; 8911da177e4SLinus Torvalds for (i = 0; i < nsrcs; i++) 8921da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 8931da177e4SLinus Torvalds psf->sf_gsresp = 1; 8941da177e4SLinus Torvalds scount++; 8951da177e4SLinus Torvalds break; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds } 898ad12583fSDavid L Stevens if (!scount) { 899ad12583fSDavid L Stevens pmc->gsquery = 0; 900ad12583fSDavid L Stevens return 0; 901ad12583fSDavid L Stevens } 902ad12583fSDavid L Stevens pmc->gsquery = 1; 903ad12583fSDavid L Stevens return 1; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 906d679c532SEric Dumazet /* return true if packet was dropped */ 907d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group) 9081da177e4SLinus Torvalds { 9091da177e4SLinus Torvalds struct ip_mc_list *im; 91087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 915d679c532SEric Dumazet return false; 91687a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 917df2cf4a7SPhilip Downey return false; 9181da177e4SLinus Torvalds 9191d7138deSEric Dumazet rcu_read_lock(); 9201d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 9211da177e4SLinus Torvalds if (im->multiaddr == group) { 9221da177e4SLinus Torvalds igmp_stop_timer(im); 9231da177e4SLinus Torvalds break; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds } 9261d7138deSEric Dumazet rcu_read_unlock(); 927d679c532SEric Dumazet return false; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 930d679c532SEric Dumazet /* return true if packet was dropped */ 931d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 9321da177e4SLinus Torvalds int len) 9331da177e4SLinus Torvalds { 934d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 935d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 9361da177e4SLinus Torvalds struct ip_mc_list *im; 93763007727SAl Viro __be32 group = ih->group; 9381da177e4SLinus Torvalds int max_delay; 9391da177e4SLinus Torvalds int mark = 0; 94087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds 9435b7c8406SDavid Stevens if (len == 8) { 9441da177e4SLinus Torvalds if (ih->code == 0) { 9451da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 9461da177e4SLinus Torvalds 947436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9481da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 949966c37f2SHangbin Liu (in_dev->mr_qrv * in_dev->mr_qi) + 950966c37f2SHangbin Liu in_dev->mr_qri; 9511da177e4SLinus Torvalds group = 0; 9521da177e4SLinus Torvalds } else { 9531da177e4SLinus Torvalds /* v2 router present */ 9541da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 9551da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 956966c37f2SHangbin Liu (in_dev->mr_qrv * in_dev->mr_qi) + 957966c37f2SHangbin Liu in_dev->mr_qri; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds /* cancel the interface change timer */ 9601da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 9611da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 9621da177e4SLinus Torvalds __in_dev_put(in_dev); 9631da177e4SLinus Torvalds /* clear deleted report items */ 9641da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 9651da177e4SLinus Torvalds } else if (len < 12) { 966d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */ 9675b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) { 9685b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */ 969436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9705b7c8406SDavid Stevens group = 0; 9715b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) { 9725b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present; 9735b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here. 9745b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a 9755b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely 9765b7c8406SDavid Stevens * to be intended in a v3 query. 9775b7c8406SDavid Stevens */ 9785b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 979a8c1f65cSBen Hutchings if (!max_delay) 980a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */ 9811da177e4SLinus Torvalds } else { /* v3 */ 9821da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 983d679c532SEric Dumazet return true; 9841da177e4SLinus Torvalds 985d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9861da177e4SLinus Torvalds if (ih3->nsrcs) { 9871da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 98863007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 989d679c532SEric Dumazet return true; 990d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 9941da177e4SLinus Torvalds if (!max_delay) 9951da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 9961da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 997966c37f2SHangbin Liu 998966c37f2SHangbin Liu /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently 999966c37f2SHangbin Liu * received value was zero, use the default or statically 1000966c37f2SHangbin Liu * configured value. 1001966c37f2SHangbin Liu */ 1002966c37f2SHangbin Liu in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv; 1003966c37f2SHangbin Liu in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL; 1004966c37f2SHangbin Liu 1005966c37f2SHangbin Liu /* RFC3376, 8.3. Query Response Interval: 1006966c37f2SHangbin Liu * The number of seconds represented by the [Query Response 1007966c37f2SHangbin Liu * Interval] must be less than the [Query Interval]. 1008966c37f2SHangbin Liu */ 1009966c37f2SHangbin Liu if (in_dev->mr_qri >= in_dev->mr_qi) 1010966c37f2SHangbin Liu in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ; 1011966c37f2SHangbin Liu 10121da177e4SLinus Torvalds if (!group) { /* general query */ 10131da177e4SLinus Torvalds if (ih3->nsrcs) 1014b47bd8d2SDaniel Borkmann return true; /* no sources allowed */ 10151da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 1016d679c532SEric Dumazet return false; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 10191da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds /* 10231da177e4SLinus Torvalds * - Start the timers in all of our membership records 10241da177e4SLinus Torvalds * that the query applies to for the interface on 10251da177e4SLinus Torvalds * which the query arrived excl. those that belong 10261da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 10271da177e4SLinus Torvalds * - For timers already running check if they need to 10281da177e4SLinus Torvalds * be reset. 10291da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 10301da177e4SLinus Torvalds * delay possible 10311da177e4SLinus Torvalds */ 10321d7138deSEric Dumazet rcu_read_lock(); 10331d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 1034ad12583fSDavid L Stevens int changed; 1035ad12583fSDavid L Stevens 10361da177e4SLinus Torvalds if (group && group != im->multiaddr) 10371da177e4SLinus Torvalds continue; 10381da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 10391da177e4SLinus Torvalds continue; 1040df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 104187a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1042df2cf4a7SPhilip Downey continue; 10431da177e4SLinus Torvalds spin_lock_bh(&im->lock); 10441da177e4SLinus Torvalds if (im->tm_running) 10451da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 10461da177e4SLinus Torvalds else 10471da177e4SLinus Torvalds im->gsquery = mark; 1048ad12583fSDavid L Stevens changed = !im->gsquery || 10491da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 10501da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 1051ad12583fSDavid L Stevens if (changed) 10521da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 10531da177e4SLinus Torvalds } 10541d7138deSEric Dumazet rcu_read_unlock(); 1055d679c532SEric Dumazet return false; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds 10589a57a9d2SEric Dumazet /* called in rcu_read_lock() section */ 10591da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 10601da177e4SLinus Torvalds { 10611da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 10621da177e4SLinus Torvalds struct igmphdr *ih; 1063c7b725beSDavid Ahern struct net_device *dev = skb->dev; 1064c7b725beSDavid Ahern struct in_device *in_dev; 10651da177e4SLinus Torvalds int len = skb->len; 1066d679c532SEric Dumazet bool dropped = true; 10671da177e4SLinus Torvalds 1068c7b725beSDavid Ahern if (netif_is_l3_master(dev)) { 1069c7b725beSDavid Ahern dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif); 1070c7b725beSDavid Ahern if (!dev) 1071c7b725beSDavid Ahern goto drop; 1072c7b725beSDavid Ahern } 1073c7b725beSDavid Ahern 1074c7b725beSDavid Ahern in_dev = __in_dev_get_rcu(dev); 107551456b29SIan Morris if (!in_dev) 1076cd557bc1SDenis V. Lunev goto drop; 10771da177e4SLinus Torvalds 1078fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 10799a57a9d2SEric Dumazet goto drop; 1080fb286bb2SHerbert Xu 1081de08dc1aSTom Herbert if (skb_checksum_simple_validate(skb)) 10829a57a9d2SEric Dumazet goto drop; 10831da177e4SLinus Torvalds 1084d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 10851da177e4SLinus Torvalds switch (ih->type) { 10861da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 1087d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len); 10881da177e4SLinus Torvalds break; 10891da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 10901da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 10911da177e4SLinus Torvalds /* Is it our report looped back? */ 1092c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) 10931da177e4SLinus Torvalds break; 109424c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 109524c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 109624c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 1097d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group); 10981da177e4SLinus Torvalds break; 10991da177e4SLinus Torvalds case IGMP_PIM: 11001da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 11011da177e4SLinus Torvalds return pim_rcv_v1(skb); 11021da177e4SLinus Torvalds #endif 1103c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 11041da177e4SLinus Torvalds case IGMP_DVMRP: 11051da177e4SLinus Torvalds case IGMP_TRACE: 11061da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 11071da177e4SLinus Torvalds case IGMP_MTRACE: 11081da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 11091da177e4SLinus Torvalds break; 11101da177e4SLinus Torvalds default: 1111dd1c1853SLinus Torvalds break; 11121da177e4SLinus Torvalds } 1113fb286bb2SHerbert Xu 1114cd557bc1SDenis V. Lunev drop: 1115d679c532SEric Dumazet if (dropped) 11161da177e4SLinus Torvalds kfree_skb(skb); 1117d679c532SEric Dumazet else 1118d679c532SEric Dumazet consume_skb(skb); 11191da177e4SLinus Torvalds return 0; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds #endif 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds /* 11261da177e4SLinus Torvalds * Add a filter to a device 11271da177e4SLinus Torvalds */ 11281da177e4SLinus Torvalds 112963007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 11301da177e4SLinus Torvalds { 11311da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 11321da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 11351da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 1136b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode 11371da177e4SLinus Torvalds routine. Something sort of: 11381da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 11391da177e4SLinus Torvalds --ANK 11401da177e4SLinus Torvalds */ 11411da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 114222bedad3SJiri Pirko dev_mc_add(dev, buf); 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds /* 11461da177e4SLinus Torvalds * Remove a filter from a device 11471da177e4SLinus Torvalds */ 11481da177e4SLinus Torvalds 114963007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 11501da177e4SLinus Torvalds { 11511da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 11521da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 115522bedad3SJiri Pirko dev_mc_del(dev, buf); 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11591da177e4SLinus Torvalds /* 11601da177e4SLinus Torvalds * deleted ip_mc_list manipulation 11611da177e4SLinus Torvalds */ 11629fb20801SFlorian Fainelli static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im, 11639fb20801SFlorian Fainelli gfp_t gfp) 11641da177e4SLinus Torvalds { 11651da177e4SLinus Torvalds struct ip_mc_list *pmc; 1166165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 11691da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 11701da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 11711da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 11721da177e4SLinus Torvalds * non-deleted or query-response MCA's. 11731da177e4SLinus Torvalds */ 11749fb20801SFlorian Fainelli pmc = kzalloc(sizeof(*pmc), gfp); 11751da177e4SLinus Torvalds if (!pmc) 11761da177e4SLinus Torvalds return; 1177b4846fc3SWANG Cong spin_lock_init(&pmc->lock); 11781da177e4SLinus Torvalds spin_lock_bh(&im->lock); 11791da177e4SLinus Torvalds pmc->interface = im->interface; 11801da177e4SLinus Torvalds in_dev_hold(in_dev); 11811da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 1182165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 11831da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 11841da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 11851da177e4SLinus Torvalds struct ip_sf_list *psf; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds pmc->tomb = im->tomb; 11881da177e4SLinus Torvalds pmc->sources = im->sources; 11891da177e4SLinus Torvalds im->tomb = im->sources = NULL; 11901da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 11911da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11961da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 11971da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 11981da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 120124803f38SHangbin Liu /* 120224803f38SHangbin Liu * restore ip_mc_list deleted records 120324803f38SHangbin Liu */ 120424803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im) 12051da177e4SLinus Torvalds { 12061da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 120724803f38SHangbin Liu struct ip_sf_list *psf; 120824803f38SHangbin Liu struct net *net = dev_net(in_dev->dev); 120924803f38SHangbin Liu __be32 multiaddr = im->multiaddr; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12121da177e4SLinus Torvalds pmc_prev = NULL; 12131da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) { 12141da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 12151da177e4SLinus Torvalds break; 12161da177e4SLinus Torvalds pmc_prev = pmc; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds if (pmc) { 12191da177e4SLinus Torvalds if (pmc_prev) 12201da177e4SLinus Torvalds pmc_prev->next = pmc->next; 12211da177e4SLinus Torvalds else 12221da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 122524803f38SHangbin Liu 122624803f38SHangbin Liu spin_lock_bh(&im->lock); 12271da177e4SLinus Torvalds if (pmc) { 122824803f38SHangbin Liu im->interface = pmc->interface; 122908d3ffccSHangbin Liu if (im->sfmode == MCAST_INCLUDE) { 1230e5b1c6c6SEric Dumazet swap(im->tomb, pmc->tomb); 1231e5b1c6c6SEric Dumazet swap(im->sources, pmc->sources); 123224803f38SHangbin Liu for (psf = im->sources; psf; psf = psf->sf_next) 12336e2059b5SHangbin Liu psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 12346e2059b5SHangbin Liu } else { 12356e2059b5SHangbin Liu im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds in_dev_put(pmc->interface); 12383580d04aSEric Dumazet kfree_pmc(pmc); 12391da177e4SLinus Torvalds } 124024803f38SHangbin Liu spin_unlock_bh(&im->lock); 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 124324803f38SHangbin Liu /* 124424803f38SHangbin Liu * flush ip_mc_list deleted records 124524803f38SHangbin Liu */ 12461da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 12471da177e4SLinus Torvalds { 12481da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12511da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 12521da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 12531da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 12561da177e4SLinus Torvalds nextpmc = pmc->next; 12571da177e4SLinus Torvalds ip_mc_clear_src(pmc); 12581da177e4SLinus Torvalds in_dev_put(pmc->interface); 12593580d04aSEric Dumazet kfree_pmc(pmc); 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds /* clear dead sources, too */ 12621d7138deSEric Dumazet rcu_read_lock(); 12631d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 12643580d04aSEric Dumazet struct ip_sf_list *psf; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 12671da177e4SLinus Torvalds psf = pmc->tomb; 12681da177e4SLinus Torvalds pmc->tomb = NULL; 12691da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 12703580d04aSEric Dumazet ip_sf_list_clear_all(psf); 12711da177e4SLinus Torvalds } 12721d7138deSEric Dumazet rcu_read_unlock(); 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds #endif 12751da177e4SLinus Torvalds 12769fb20801SFlorian Fainelli static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp) 12771da177e4SLinus Torvalds { 12781da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 12791da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 128087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 12811da177e4SLinus Torvalds int reporter; 12821da177e4SLinus Torvalds #endif 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (im->loaded) { 12851da177e4SLinus Torvalds im->loaded = 0; 12861da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12901da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 12911da177e4SLinus Torvalds return; 129287a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1293df2cf4a7SPhilip Downey return; 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds reporter = im->reporter; 12961da177e4SLinus Torvalds igmp_stop_timer(im); 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds if (!in_dev->dead) { 12991da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 130024cf3af3SVeaceslav Falico return; 13011da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 13021da177e4SLinus Torvalds if (reporter) 13031da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 130424cf3af3SVeaceslav Falico return; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds /* IGMPv3 */ 13079fb20801SFlorian Fainelli igmpv3_add_delrec(in_dev, im, gfp); 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds igmp_ifc_event(in_dev); 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds #endif 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13149fb20801SFlorian Fainelli static void igmp_group_dropped(struct ip_mc_list *im) 13159fb20801SFlorian Fainelli { 13169fb20801SFlorian Fainelli __igmp_group_dropped(im, GFP_KERNEL); 13179fb20801SFlorian Fainelli } 13189fb20801SFlorian Fainelli 13190ae0d60aSHangbin Liu static void igmp_group_added(struct ip_mc_list *im) 13201da177e4SLinus Torvalds { 13211da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 1322dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 132387a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1324dcd87999SNikolay Borisov #endif 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds if (im->loaded == 0) { 13271da177e4SLinus Torvalds im->loaded = 1; 13281da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13321da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 13331da177e4SLinus Torvalds return; 133487a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1335df2cf4a7SPhilip Downey return; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds if (in_dev->dead) 13381da177e4SLinus Torvalds return; 1339ff06525fSHangbin Liu 1340ff06525fSHangbin Liu im->unsolicit_count = net->ipv4.sysctl_igmp_qrv; 13411da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 13421da177e4SLinus Torvalds spin_lock_bh(&im->lock); 1343436f7c20SFabian Frederick igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY); 13441da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 13451da177e4SLinus Torvalds return; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds /* else, v3 */ 13481da177e4SLinus Torvalds 13496e2059b5SHangbin Liu /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should 13506e2059b5SHangbin Liu * not send filter-mode change record as the mode should be from 13516e2059b5SHangbin Liu * IN() to IN(A). 13526e2059b5SHangbin Liu */ 13530ae0d60aSHangbin Liu if (im->sfmode == MCAST_EXCLUDE) 1354165094afSNikolay Borisov im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 13556e2059b5SHangbin Liu 13561da177e4SLinus Torvalds igmp_ifc_event(in_dev); 13571da177e4SLinus Torvalds #endif 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds /* 13621da177e4SLinus Torvalds * Multicast list managers 13631da177e4SLinus Torvalds */ 13641da177e4SLinus Torvalds 1365e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im) 1366e9897071SEric Dumazet { 1367c70eba74SEric Dumazet return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); 1368e9897071SEric Dumazet } 1369e9897071SEric Dumazet 1370e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev, 1371e9897071SEric Dumazet struct ip_mc_list *im) 1372e9897071SEric Dumazet { 1373e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 1374e9897071SEric Dumazet u32 hash; 1375e9897071SEric Dumazet 1376e9897071SEric Dumazet mc_hash = rtnl_dereference(in_dev->mc_hash); 1377e9897071SEric Dumazet if (mc_hash) { 1378e9897071SEric Dumazet hash = ip_mc_hash(im); 1379c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1380e9897071SEric Dumazet rcu_assign_pointer(mc_hash[hash], im); 1381e9897071SEric Dumazet return; 1382e9897071SEric Dumazet } 1383e9897071SEric Dumazet 1384e9897071SEric Dumazet /* do not use a hash table for small number of items */ 1385e9897071SEric Dumazet if (in_dev->mc_count < 4) 1386e9897071SEric Dumazet return; 1387e9897071SEric Dumazet 1388e9897071SEric Dumazet mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG, 1389e9897071SEric Dumazet GFP_KERNEL); 1390e9897071SEric Dumazet if (!mc_hash) 1391e9897071SEric Dumazet return; 1392e9897071SEric Dumazet 1393e9897071SEric Dumazet for_each_pmc_rtnl(in_dev, im) { 1394e9897071SEric Dumazet hash = ip_mc_hash(im); 1395c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1396e9897071SEric Dumazet RCU_INIT_POINTER(mc_hash[hash], im); 1397e9897071SEric Dumazet } 1398e9897071SEric Dumazet 1399e9897071SEric Dumazet rcu_assign_pointer(in_dev->mc_hash, mc_hash); 1400e9897071SEric Dumazet } 1401e9897071SEric Dumazet 1402e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev, 1403e9897071SEric Dumazet struct ip_mc_list *im) 1404e9897071SEric Dumazet { 1405e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); 1406e9897071SEric Dumazet struct ip_mc_list *aux; 1407e9897071SEric Dumazet 1408e9897071SEric Dumazet if (!mc_hash) 1409e9897071SEric Dumazet return; 1410e9897071SEric Dumazet mc_hash += ip_mc_hash(im); 1411e9897071SEric Dumazet while ((aux = rtnl_dereference(*mc_hash)) != im) 1412e9897071SEric Dumazet mc_hash = &aux->next_hash; 1413e9897071SEric Dumazet *mc_hash = im->next_hash; 1414e9897071SEric Dumazet } 1415e9897071SEric Dumazet 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds /* 14181da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 14191da177e4SLinus Torvalds */ 14209fb20801SFlorian Fainelli static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, 14219fb20801SFlorian Fainelli unsigned int mode, gfp_t gfp) 14221da177e4SLinus Torvalds { 14231da177e4SLinus Torvalds struct ip_mc_list *im; 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds ASSERT_RTNL(); 14261da177e4SLinus Torvalds 14271d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) { 14281da177e4SLinus Torvalds if (im->multiaddr == addr) { 14291da177e4SLinus Torvalds im->users++; 14306e2059b5SHangbin Liu ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0); 14311da177e4SLinus Torvalds goto out; 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14359fb20801SFlorian Fainelli im = kzalloc(sizeof(*im), gfp); 14361da177e4SLinus Torvalds if (!im) 14371da177e4SLinus Torvalds goto out; 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds im->users = 1; 14401da177e4SLinus Torvalds im->interface = in_dev; 14411da177e4SLinus Torvalds in_dev_hold(in_dev); 14421da177e4SLinus Torvalds im->multiaddr = addr; 14431da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 14446e2059b5SHangbin Liu im->sfmode = mode; 14456e2059b5SHangbin Liu im->sfcount[mode] = 1; 14468851ab52SReshetova, Elena refcount_set(&im->refcnt, 1); 14471da177e4SLinus Torvalds spin_lock_init(&im->lock); 14481da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1449e99e88a9SKees Cook timer_setup(&im->timer, igmp_timer_expire, 0); 14501da177e4SLinus Torvalds #endif 14511d7138deSEric Dumazet 14521d7138deSEric Dumazet im->next_rcu = in_dev->mc_list; 1453b8bae41eSRami Rosen in_dev->mc_count++; 1454cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im); 14551d7138deSEric Dumazet 1456e9897071SEric Dumazet ip_mc_hash_add(in_dev, im); 1457e9897071SEric Dumazet 14581da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 145924803f38SHangbin Liu igmpv3_del_delrec(in_dev, im); 14601da177e4SLinus Torvalds #endif 14610ae0d60aSHangbin Liu igmp_group_added(im); 14621da177e4SLinus Torvalds if (!in_dev->dead) 14631da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 14641da177e4SLinus Torvalds out: 14651da177e4SLinus Torvalds return; 14661da177e4SLinus Torvalds } 14676e2059b5SHangbin Liu 14689fb20801SFlorian Fainelli void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp) 14699fb20801SFlorian Fainelli { 14709fb20801SFlorian Fainelli ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp); 14719fb20801SFlorian Fainelli } 14729fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_inc_group); 14739fb20801SFlorian Fainelli 14746e2059b5SHangbin Liu void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 14756e2059b5SHangbin Liu { 1476a1c4cd67SLi RongQing __ip_mc_inc_group(in_dev, addr, GFP_KERNEL); 14776e2059b5SHangbin Liu } 14784bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group); 14791da177e4SLinus Torvalds 14809afd85c9SLinus Lüssing static int ip_mc_check_iphdr(struct sk_buff *skb) 14819afd85c9SLinus Lüssing { 14829afd85c9SLinus Lüssing const struct iphdr *iph; 14839afd85c9SLinus Lüssing unsigned int len; 14849afd85c9SLinus Lüssing unsigned int offset = skb_network_offset(skb) + sizeof(*iph); 14859afd85c9SLinus Lüssing 14869afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset)) 14879afd85c9SLinus Lüssing return -EINVAL; 14889afd85c9SLinus Lüssing 14899afd85c9SLinus Lüssing iph = ip_hdr(skb); 14909afd85c9SLinus Lüssing 14919afd85c9SLinus Lüssing if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph)) 14929afd85c9SLinus Lüssing return -EINVAL; 14939afd85c9SLinus Lüssing 14949afd85c9SLinus Lüssing offset += ip_hdrlen(skb) - sizeof(*iph); 14959afd85c9SLinus Lüssing 14969afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset)) 14979afd85c9SLinus Lüssing return -EINVAL; 14989afd85c9SLinus Lüssing 14999afd85c9SLinus Lüssing iph = ip_hdr(skb); 15009afd85c9SLinus Lüssing 15019afd85c9SLinus Lüssing if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 15029afd85c9SLinus Lüssing return -EINVAL; 15039afd85c9SLinus Lüssing 15049afd85c9SLinus Lüssing len = skb_network_offset(skb) + ntohs(iph->tot_len); 15059afd85c9SLinus Lüssing if (skb->len < len || len < offset) 15069afd85c9SLinus Lüssing return -EINVAL; 15079afd85c9SLinus Lüssing 15089afd85c9SLinus Lüssing skb_set_transport_header(skb, offset); 15099afd85c9SLinus Lüssing 15109afd85c9SLinus Lüssing return 0; 15119afd85c9SLinus Lüssing } 15129afd85c9SLinus Lüssing 15139afd85c9SLinus Lüssing static int ip_mc_check_igmp_reportv3(struct sk_buff *skb) 15149afd85c9SLinus Lüssing { 15159afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb); 15169afd85c9SLinus Lüssing 15179afd85c9SLinus Lüssing len += sizeof(struct igmpv3_report); 15189afd85c9SLinus Lüssing 1519a2e2ca3bSLinus Lüssing return ip_mc_may_pull(skb, len) ? 0 : -EINVAL; 15209afd85c9SLinus Lüssing } 15219afd85c9SLinus Lüssing 15229afd85c9SLinus Lüssing static int ip_mc_check_igmp_query(struct sk_buff *skb) 15239afd85c9SLinus Lüssing { 1524a2e2ca3bSLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 1525a2e2ca3bSLinus Lüssing unsigned int len; 15269afd85c9SLinus Lüssing 15279afd85c9SLinus Lüssing /* IGMPv{1,2}? */ 1528a2e2ca3bSLinus Lüssing if (transport_len != sizeof(struct igmphdr)) { 15299afd85c9SLinus Lüssing /* or IGMPv3? */ 1530a2e2ca3bSLinus Lüssing if (transport_len < sizeof(struct igmpv3_query)) 1531a2e2ca3bSLinus Lüssing return -EINVAL; 1532a2e2ca3bSLinus Lüssing 1533a2e2ca3bSLinus Lüssing len = skb_transport_offset(skb) + sizeof(struct igmpv3_query); 1534a2e2ca3bSLinus Lüssing if (!ip_mc_may_pull(skb, len)) 15359afd85c9SLinus Lüssing return -EINVAL; 15369afd85c9SLinus Lüssing } 15379afd85c9SLinus Lüssing 15389afd85c9SLinus Lüssing /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer 15399afd85c9SLinus Lüssing * all-systems destination addresses (224.0.0.1) for general queries 15409afd85c9SLinus Lüssing */ 15419afd85c9SLinus Lüssing if (!igmp_hdr(skb)->group && 15429afd85c9SLinus Lüssing ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP)) 15439afd85c9SLinus Lüssing return -EINVAL; 15449afd85c9SLinus Lüssing 15459afd85c9SLinus Lüssing return 0; 15469afd85c9SLinus Lüssing } 15479afd85c9SLinus Lüssing 15489afd85c9SLinus Lüssing static int ip_mc_check_igmp_msg(struct sk_buff *skb) 15499afd85c9SLinus Lüssing { 15509afd85c9SLinus Lüssing switch (igmp_hdr(skb)->type) { 15519afd85c9SLinus Lüssing case IGMP_HOST_LEAVE_MESSAGE: 15529afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_REPORT: 15539afd85c9SLinus Lüssing case IGMPV2_HOST_MEMBERSHIP_REPORT: 15549afd85c9SLinus Lüssing return 0; 15559afd85c9SLinus Lüssing case IGMPV3_HOST_MEMBERSHIP_REPORT: 15569afd85c9SLinus Lüssing return ip_mc_check_igmp_reportv3(skb); 15579afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_QUERY: 15589afd85c9SLinus Lüssing return ip_mc_check_igmp_query(skb); 15599afd85c9SLinus Lüssing default: 15609afd85c9SLinus Lüssing return -ENOMSG; 15619afd85c9SLinus Lüssing } 15629afd85c9SLinus Lüssing } 15639afd85c9SLinus Lüssing 15645a43f697SAlexey Dobriyan static __sum16 ip_mc_validate_checksum(struct sk_buff *skb) 15659afd85c9SLinus Lüssing { 15669afd85c9SLinus Lüssing return skb_checksum_simple_validate(skb); 15679afd85c9SLinus Lüssing } 15689afd85c9SLinus Lüssing 1569a2e2ca3bSLinus Lüssing static int ip_mc_check_igmp_csum(struct sk_buff *skb) 15709afd85c9SLinus Lüssing { 15719afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr); 1572a2e2ca3bSLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 1573a2e2ca3bSLinus Lüssing struct sk_buff *skb_chk; 15749afd85c9SLinus Lüssing 1575a2e2ca3bSLinus Lüssing if (!ip_mc_may_pull(skb, len)) 1576a2e2ca3bSLinus Lüssing return -EINVAL; 15779afd85c9SLinus Lüssing 15789afd85c9SLinus Lüssing skb_chk = skb_checksum_trimmed(skb, transport_len, 15799afd85c9SLinus Lüssing ip_mc_validate_checksum); 15809afd85c9SLinus Lüssing if (!skb_chk) 1581a2e2ca3bSLinus Lüssing return -EINVAL; 15829afd85c9SLinus Lüssing 1583a2e2ca3bSLinus Lüssing if (skb_chk != skb) 1584a516993fSLinus Lüssing kfree_skb(skb_chk); 1585a516993fSLinus Lüssing 1586a2e2ca3bSLinus Lüssing return 0; 15879afd85c9SLinus Lüssing } 15889afd85c9SLinus Lüssing 15899afd85c9SLinus Lüssing /** 15909afd85c9SLinus Lüssing * ip_mc_check_igmp - checks whether this is a sane IGMP packet 15919afd85c9SLinus Lüssing * @skb: the skb to validate 15929afd85c9SLinus Lüssing * 15939afd85c9SLinus Lüssing * Checks whether an IPv4 packet is a valid IGMP packet. If so sets 1594a516993fSLinus Lüssing * skb transport header accordingly and returns zero. 15959afd85c9SLinus Lüssing * 15969afd85c9SLinus Lüssing * -EINVAL: A broken packet was detected, i.e. it violates some internet 15979afd85c9SLinus Lüssing * standard 15989afd85c9SLinus Lüssing * -ENOMSG: IP header validation succeeded but it is not an IGMP packet. 15999afd85c9SLinus Lüssing * -ENOMEM: A memory allocation failure happened. 16009afd85c9SLinus Lüssing * 1601a516993fSLinus Lüssing * Caller needs to set the skb network header and free any returned skb if it 1602a516993fSLinus Lüssing * differs from the provided skb. 16039afd85c9SLinus Lüssing */ 1604ba5ea614SLinus Lüssing int ip_mc_check_igmp(struct sk_buff *skb) 16059afd85c9SLinus Lüssing { 16069afd85c9SLinus Lüssing int ret = ip_mc_check_iphdr(skb); 16079afd85c9SLinus Lüssing 16089afd85c9SLinus Lüssing if (ret < 0) 16099afd85c9SLinus Lüssing return ret; 16109afd85c9SLinus Lüssing 16119afd85c9SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP) 16129afd85c9SLinus Lüssing return -ENOMSG; 16139afd85c9SLinus Lüssing 1614a2e2ca3bSLinus Lüssing ret = ip_mc_check_igmp_csum(skb); 1615a2e2ca3bSLinus Lüssing if (ret < 0) 1616a2e2ca3bSLinus Lüssing return ret; 1617a2e2ca3bSLinus Lüssing 1618a2e2ca3bSLinus Lüssing return ip_mc_check_igmp_msg(skb); 16199afd85c9SLinus Lüssing } 16209afd85c9SLinus Lüssing EXPORT_SYMBOL(ip_mc_check_igmp); 16219afd85c9SLinus Lüssing 16221da177e4SLinus Torvalds /* 16234aa5dee4SJiri Pirko * Resend IGMP JOIN report; used by netdev notifier. 1624a816c7c7SJay Vosburgh */ 16254aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev) 1626a816c7c7SJay Vosburgh { 162708882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1628866f3b25SEric Dumazet struct ip_mc_list *im; 1629866f3b25SEric Dumazet int type; 163087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1631a816c7c7SJay Vosburgh 16324aa5dee4SJiri Pirko ASSERT_RTNL(); 16334aa5dee4SJiri Pirko 16344aa5dee4SJiri Pirko for_each_pmc_rtnl(in_dev, im) { 1635a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1636866f3b25SEric Dumazet continue; 1637df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 163887a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1639df2cf4a7SPhilip Downey continue; 1640a816c7c7SJay Vosburgh 1641e12b4539SFlavio Leitner /* a failover is happening and switches 1642866f3b25SEric Dumazet * must be notified immediately 1643866f3b25SEric Dumazet */ 1644e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev)) 1645866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT; 1646e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev)) 1647866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT; 1648e12b4539SFlavio Leitner else 1649866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT; 1650866f3b25SEric Dumazet igmp_send_report(in_dev, im, type); 1651866f3b25SEric Dumazet } 1652a816c7c7SJay Vosburgh #endif 1653a816c7c7SJay Vosburgh } 1654a816c7c7SJay Vosburgh 1655a816c7c7SJay Vosburgh /* 16561da177e4SLinus Torvalds * A socket has left a multicast group on device dev 16571da177e4SLinus Torvalds */ 16581da177e4SLinus Torvalds 16599fb20801SFlorian Fainelli void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp) 16601da177e4SLinus Torvalds { 16611d7138deSEric Dumazet struct ip_mc_list *i; 16621d7138deSEric Dumazet struct ip_mc_list __rcu **ip; 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds ASSERT_RTNL(); 16651da177e4SLinus Torvalds 16661d7138deSEric Dumazet for (ip = &in_dev->mc_list; 16671d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL; 16681d7138deSEric Dumazet ip = &i->next_rcu) { 16691da177e4SLinus Torvalds if (i->multiaddr == addr) { 16701da177e4SLinus Torvalds if (--i->users == 0) { 1671e9897071SEric Dumazet ip_mc_hash_remove(in_dev, i); 16721d7138deSEric Dumazet *ip = i->next_rcu; 1673b8bae41eSRami Rosen in_dev->mc_count--; 16749fb20801SFlorian Fainelli __igmp_group_dropped(i, gfp); 167524cf3af3SVeaceslav Falico ip_mc_clear_src(i); 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds if (!in_dev->dead) 16781da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds ip_ma_put(i); 16811da177e4SLinus Torvalds return; 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds break; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds } 16879fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_dec_group); 16881da177e4SLinus Torvalds 168975c78500SMoni Shoua /* Device changing type */ 169075c78500SMoni Shoua 169175c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 169275c78500SMoni Shoua { 16931d7138deSEric Dumazet struct ip_mc_list *pmc; 169475c78500SMoni Shoua 169575c78500SMoni Shoua ASSERT_RTNL(); 169675c78500SMoni Shoua 16971d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 16981d7138deSEric Dumazet igmp_group_dropped(pmc); 169975c78500SMoni Shoua } 170075c78500SMoni Shoua 170175c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 170275c78500SMoni Shoua { 17031d7138deSEric Dumazet struct ip_mc_list *pmc; 170475c78500SMoni Shoua 170575c78500SMoni Shoua ASSERT_RTNL(); 170675c78500SMoni Shoua 170724803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) { 170824803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 170924803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc); 171024803f38SHangbin Liu #endif 17110ae0d60aSHangbin Liu igmp_group_added(pmc); 171275c78500SMoni Shoua } 171324803f38SHangbin Liu } 171475c78500SMoni Shoua 17151da177e4SLinus Torvalds /* Device going down */ 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 17181da177e4SLinus Torvalds { 17191d7138deSEric Dumazet struct ip_mc_list *pmc; 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds ASSERT_RTNL(); 17221da177e4SLinus Torvalds 17231d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 17241d7138deSEric Dumazet igmp_group_dropped(pmc); 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17271da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 17281da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 17291da177e4SLinus Torvalds __in_dev_put(in_dev); 17301da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 17311da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 17321da177e4SLinus Torvalds __in_dev_put(in_dev); 17331da177e4SLinus Torvalds #endif 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds 1738966c37f2SHangbin Liu #ifdef CONFIG_IP_MULTICAST 1739966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev) 1740966c37f2SHangbin Liu { 1741966c37f2SHangbin Liu struct net *net = dev_net(in_dev->dev); 1742966c37f2SHangbin Liu 1743966c37f2SHangbin Liu in_dev->mr_qi = IGMP_QUERY_INTERVAL; 1744966c37f2SHangbin Liu in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL; 1745966c37f2SHangbin Liu in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv; 1746966c37f2SHangbin Liu } 1747966c37f2SHangbin Liu #else 1748966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev) 1749966c37f2SHangbin Liu { 1750966c37f2SHangbin Liu } 1751966c37f2SHangbin Liu #endif 1752966c37f2SHangbin Liu 17531da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 17541da177e4SLinus Torvalds { 17551da177e4SLinus Torvalds ASSERT_RTNL(); 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1758e99e88a9SKees Cook timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0); 1759e99e88a9SKees Cook timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0); 17601da177e4SLinus Torvalds #endif 1761966c37f2SHangbin Liu ip_mc_reset(in_dev); 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds /* Device going up */ 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 17691da177e4SLinus Torvalds { 17701d7138deSEric Dumazet struct ip_mc_list *pmc; 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds ASSERT_RTNL(); 17731da177e4SLinus Torvalds 1774966c37f2SHangbin Liu ip_mc_reset(in_dev); 17751da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 17761da177e4SLinus Torvalds 177724803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) { 177824803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 177924803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc); 178024803f38SHangbin Liu #endif 17810ae0d60aSHangbin Liu igmp_group_added(pmc); 17821da177e4SLinus Torvalds } 178324803f38SHangbin Liu } 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds /* 17861da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 17871da177e4SLinus Torvalds */ 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 17901da177e4SLinus Torvalds { 17911da177e4SLinus Torvalds struct ip_mc_list *i; 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds ASSERT_RTNL(); 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds /* Deactivate timers */ 17961da177e4SLinus Torvalds ip_mc_down(in_dev); 179724803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 179824803f38SHangbin Liu igmpv3_clear_delrec(in_dev); 179924803f38SHangbin Liu #endif 18001da177e4SLinus Torvalds 18011d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { 18021d7138deSEric Dumazet in_dev->mc_list = i->next_rcu; 1803b8bae41eSRami Rosen in_dev->mc_count--; 1804*d8e29730SChengyang Fan ip_mc_clear_src(i); 18051da177e4SLinus Torvalds ip_ma_put(i); 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds 18099e917dcaSEric Dumazet /* RTNL is locked */ 1810877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 18111da177e4SLinus Torvalds { 18121da177e4SLinus Torvalds struct net_device *dev = NULL; 18131da177e4SLinus Torvalds struct in_device *idev = NULL; 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds if (imr->imr_ifindex) { 1816877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 18171da177e4SLinus Torvalds return idev; 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 18209e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false); 18211da177e4SLinus Torvalds if (!dev) 18221da177e4SLinus Torvalds return NULL; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 1825b23dd4feSDavid S. Miller if (!dev) { 182678fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, 182778fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr, 182878fbfd8aSDavid S. Miller 0, 0, 0); 1829b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1830d8d1f30bSChangli Gao dev = rt->dst.dev; 18311da177e4SLinus Torvalds ip_rt_put(rt); 18321da177e4SLinus Torvalds } 1833b23dd4feSDavid S. Miller } 18341da177e4SLinus Torvalds if (dev) { 18351da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1836e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds return idev; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds /* 18421da177e4SLinus Torvalds * Join a socket to a group 18431da177e4SLinus Torvalds */ 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 18468f935bbdSAl Viro __be32 *psfsrc) 18471da177e4SLinus Torvalds { 18481da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 18491da177e4SLinus Torvalds int rv = 0; 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds psf_prev = NULL; 18521da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 18531da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 18541da177e4SLinus Torvalds break; 18551da177e4SLinus Torvalds psf_prev = psf; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 18581da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 18591da177e4SLinus Torvalds return -ESRCH; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds psf->sf_count[sfmode]--; 18621da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 18631da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 18641da177e4SLinus Torvalds } 18651da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 18661da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18671da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 1868165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 18691da177e4SLinus Torvalds #endif 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds /* no more filters for this source */ 18721da177e4SLinus Torvalds if (psf_prev) 18731da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 18741da177e4SLinus Torvalds else 18751da177e4SLinus Torvalds pmc->sources = psf->sf_next; 18761da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18771da177e4SLinus Torvalds if (psf->sf_oldin && 18781da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 1879165094afSNikolay Borisov psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 18801da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 18811da177e4SLinus Torvalds pmc->tomb = psf; 18821da177e4SLinus Torvalds rv = 1; 18831da177e4SLinus Torvalds } else 18841da177e4SLinus Torvalds #endif 18851da177e4SLinus Torvalds kfree(psf); 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds return rv; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 18911da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 18921da177e4SLinus Torvalds #endif 18931da177e4SLinus Torvalds 18948f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 18958f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 18961da177e4SLinus Torvalds { 18971da177e4SLinus Torvalds struct ip_mc_list *pmc; 18981da177e4SLinus Torvalds int changerec = 0; 18991da177e4SLinus Torvalds int i, err; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds if (!in_dev) 19021da177e4SLinus Torvalds return -ENODEV; 19031d7138deSEric Dumazet rcu_read_lock(); 19041d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 19051da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 19061da177e4SLinus Torvalds break; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds if (!pmc) { 19091da177e4SLinus Torvalds /* MCA not found?? bug */ 19101d7138deSEric Dumazet rcu_read_unlock(); 19111da177e4SLinus Torvalds return -ESRCH; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 19141d7138deSEric Dumazet rcu_read_unlock(); 19151da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19161da177e4SLinus Torvalds sf_markstate(pmc); 19171da177e4SLinus Torvalds #endif 19181da177e4SLinus Torvalds if (!delta) { 19191da177e4SLinus Torvalds err = -EINVAL; 19201da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 19211da177e4SLinus Torvalds goto out_unlock; 19221da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds err = 0; 19251da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 19261da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds changerec |= rv > 0; 19291da177e4SLinus Torvalds if (!err && rv < 0) 19301da177e4SLinus Torvalds err = rv; 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 19331da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 19341da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 19351da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19361da177e4SLinus Torvalds struct ip_sf_list *psf; 1937165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 19381da177e4SLinus Torvalds #endif 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds /* filter mode change */ 19411da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 19421da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1943165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 19441da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 19451da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 19461da177e4SLinus Torvalds psf->sf_crcount = 0; 19471da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 19481da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 19491da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 19501da177e4SLinus Torvalds #endif 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds out_unlock: 19531da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 19541da177e4SLinus Torvalds return err; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds /* 19581da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 19591da177e4SLinus Torvalds */ 19601da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 19615eb81e89SJun Zhao __be32 *psfsrc) 19621da177e4SLinus Torvalds { 19631da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds psf_prev = NULL; 19661da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 19671da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 19681da177e4SLinus Torvalds break; 19691da177e4SLinus Torvalds psf_prev = psf; 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds if (!psf) { 19720da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 19731da177e4SLinus Torvalds if (!psf) 19741da177e4SLinus Torvalds return -ENOBUFS; 19751da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 19761da177e4SLinus Torvalds if (psf_prev) { 19771da177e4SLinus Torvalds psf_prev->sf_next = psf; 19781da177e4SLinus Torvalds } else 19791da177e4SLinus Torvalds pmc->sources = psf; 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds psf->sf_count[sfmode]++; 19821da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 19831da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds return 0; 19861da177e4SLinus Torvalds } 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19891da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 19901da177e4SLinus Torvalds { 19911da177e4SLinus Torvalds struct ip_sf_list *psf; 19921da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 19951da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 19961da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 19971da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 19981da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 19991da177e4SLinus Torvalds } else 20001da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 20041da177e4SLinus Torvalds { 2005ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 20061da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 20071da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 20081da177e4SLinus Torvalds int new_in, rv; 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds rv = 0; 20111da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 20121da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 20131da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 20141da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 20151da177e4SLinus Torvalds } else 20161da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 2017ad12583fSDavid L Stevens if (new_in) { 2018ad12583fSDavid L Stevens if (!psf->sf_oldin) { 201976edc605SAl Viro struct ip_sf_list *prev = NULL; 2020ad12583fSDavid L Stevens 2021ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) { 2022ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 2023ad12583fSDavid L Stevens break; 2024ad12583fSDavid L Stevens prev = dpsf; 2025ad12583fSDavid L Stevens } 2026ad12583fSDavid L Stevens if (dpsf) { 2027ad12583fSDavid L Stevens if (prev) 2028ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 2029ad12583fSDavid L Stevens else 2030ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 2031ad12583fSDavid L Stevens kfree(dpsf); 2032ad12583fSDavid L Stevens } 20331da177e4SLinus Torvalds psf->sf_crcount = qrv; 20341da177e4SLinus Torvalds rv++; 20351da177e4SLinus Torvalds } 2036ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 2037ad12583fSDavid L Stevens 2038ad12583fSDavid L Stevens psf->sf_crcount = 0; 2039ad12583fSDavid L Stevens /* 2040ad12583fSDavid L Stevens * add or update "delete" records if an active filter 2041ad12583fSDavid L Stevens * is now inactive 2042ad12583fSDavid L Stevens */ 2043ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) 2044ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 2045ad12583fSDavid L Stevens break; 2046ad12583fSDavid L Stevens if (!dpsf) { 20473ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); 2048ad12583fSDavid L Stevens if (!dpsf) 2049ad12583fSDavid L Stevens continue; 2050ad12583fSDavid L Stevens *dpsf = *psf; 2051ad12583fSDavid L Stevens /* pmc->lock held by callers */ 2052ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 2053ad12583fSDavid L Stevens pmc->tomb = dpsf; 2054ad12583fSDavid L Stevens } 2055ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 2056ad12583fSDavid L Stevens rv++; 2057ad12583fSDavid L Stevens } 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds return rv; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds #endif 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds /* 20641da177e4SLinus Torvalds * Add multicast source filter list to the interface list 20651da177e4SLinus Torvalds */ 20668f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 20678f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 20681da177e4SLinus Torvalds { 20691da177e4SLinus Torvalds struct ip_mc_list *pmc; 20701da177e4SLinus Torvalds int isexclude; 20711da177e4SLinus Torvalds int i, err; 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds if (!in_dev) 20741da177e4SLinus Torvalds return -ENODEV; 20751d7138deSEric Dumazet rcu_read_lock(); 20761d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 20771da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 20781da177e4SLinus Torvalds break; 20791da177e4SLinus Torvalds } 20801da177e4SLinus Torvalds if (!pmc) { 20811da177e4SLinus Torvalds /* MCA not found?? bug */ 20821d7138deSEric Dumazet rcu_read_unlock(); 20831da177e4SLinus Torvalds return -ESRCH; 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 20861d7138deSEric Dumazet rcu_read_unlock(); 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 20891da177e4SLinus Torvalds sf_markstate(pmc); 20901da177e4SLinus Torvalds #endif 20911da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 20921da177e4SLinus Torvalds if (!delta) 20931da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 20941da177e4SLinus Torvalds err = 0; 20951da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 20965eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); 20971da177e4SLinus Torvalds if (err) 20981da177e4SLinus Torvalds break; 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds if (err) { 21011da177e4SLinus Torvalds int j; 21021da177e4SLinus Torvalds 2103685f94e6SJun Zhao if (!delta) 21041da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 21051da177e4SLinus Torvalds for (j = 0; j < i; j++) 2106a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); 21071da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 21081da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 21091da177e4SLinus Torvalds struct ip_sf_list *psf; 2110165094afSNikolay Borisov struct net *net = dev_net(pmc->interface->dev); 2111cfcabdccSStephen Hemminger in_dev = pmc->interface; 21121da177e4SLinus Torvalds #endif 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds /* filter mode change */ 21151da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 21161da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 21171da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 21181da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 21191da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 21201da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 21211da177e4SLinus Torvalds 2122165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 21231da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 21241da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 21251da177e4SLinus Torvalds psf->sf_crcount = 0; 21261da177e4SLinus Torvalds igmp_ifc_event(in_dev); 21271da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 21281da177e4SLinus Torvalds igmp_ifc_event(in_dev); 21291da177e4SLinus Torvalds #endif 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 21321da177e4SLinus Torvalds return err; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 21361da177e4SLinus Torvalds { 21373580d04aSEric Dumazet struct ip_sf_list *tomb, *sources; 21381da177e4SLinus Torvalds 2139c38b7d32SWANG Cong spin_lock_bh(&pmc->lock); 2140c38b7d32SWANG Cong tomb = pmc->tomb; 21411da177e4SLinus Torvalds pmc->tomb = NULL; 2142c38b7d32SWANG Cong sources = pmc->sources; 21431da177e4SLinus Torvalds pmc->sources = NULL; 21441da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 2145de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 21461da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 2147c38b7d32SWANG Cong spin_unlock_bh(&pmc->lock); 2148c38b7d32SWANG Cong 21493580d04aSEric Dumazet ip_sf_list_clear_all(tomb); 21503580d04aSEric Dumazet ip_sf_list_clear_all(sources); 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds 215354ff9ef3SMarcelo Ricardo Leitner /* Join a multicast group 215454ff9ef3SMarcelo Ricardo Leitner */ 21556e2059b5SHangbin Liu static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr, 21566e2059b5SHangbin Liu unsigned int mode) 21571da177e4SLinus Torvalds { 21588f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 2159959d10f6SEric Dumazet struct ip_mc_socklist *iml, *i; 21601da177e4SLinus Torvalds struct in_device *in_dev; 21611da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2162877acedcSDaniel Lezcano struct net *net = sock_net(sk); 2163ca9b907dSDavid L Stevens int ifindex; 21641da177e4SLinus Torvalds int count = 0; 2165959d10f6SEric Dumazet int err; 2166959d10f6SEric Dumazet 2167959d10f6SEric Dumazet ASSERT_RTNL(); 21681da177e4SLinus Torvalds 2169f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21701da177e4SLinus Torvalds return -EINVAL; 21711da177e4SLinus Torvalds 2172877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds if (!in_dev) { 21751da177e4SLinus Torvalds err = -ENODEV; 21761da177e4SLinus Torvalds goto done; 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds err = -EADDRINUSE; 2180ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 21811d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 2182ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 2183ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 21841da177e4SLinus Torvalds goto done; 21851da177e4SLinus Torvalds count++; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds err = -ENOBUFS; 2188815c5270SNikolay Borisov if (count >= net->ipv4.sysctl_igmp_max_memberships) 21891da177e4SLinus Torvalds goto done; 21908b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 219151456b29SIan Morris if (!iml) 2192ca9b907dSDavid L Stevens goto done; 2193ca9b907dSDavid L Stevens 21941da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 21951d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 21961da177e4SLinus Torvalds iml->sflist = NULL; 21976e2059b5SHangbin Liu iml->sfmode = mode; 2198cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 2199a1c4cd67SLi RongQing ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL); 22001da177e4SLinus Torvalds err = 0; 22011da177e4SLinus Torvalds done: 22021da177e4SLinus Torvalds return err; 22031da177e4SLinus Torvalds } 22046e2059b5SHangbin Liu 22056e2059b5SHangbin Liu /* Join ASM (Any-Source Multicast) group 22066e2059b5SHangbin Liu */ 22076e2059b5SHangbin Liu int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr) 22086e2059b5SHangbin Liu { 22096e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE); 22106e2059b5SHangbin Liu } 22114bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 22121da177e4SLinus Torvalds 22136e2059b5SHangbin Liu /* Join SSM (Source-Specific Multicast) group 22146e2059b5SHangbin Liu */ 22156e2059b5SHangbin Liu int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr, 22166e2059b5SHangbin Liu unsigned int mode) 22176e2059b5SHangbin Liu { 22186e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, mode); 22196e2059b5SHangbin Liu } 22206e2059b5SHangbin Liu 22211da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 22221da177e4SLinus Torvalds struct in_device *in_dev) 22231da177e4SLinus Torvalds { 22241d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 22251da177e4SLinus Torvalds int err; 22261da177e4SLinus Torvalds 222751456b29SIan Morris if (!psf) { 22281da177e4SLinus Torvalds /* any-source empty exclude case */ 22291da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 22301da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 22311da177e4SLinus Torvalds } 22321da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 2233c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 2234a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 2235c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2236c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); 22377519cce4SLai Jiangshan kfree_rcu(psf, rcu); 22381da177e4SLinus Torvalds return err; 22391da177e4SLinus Torvalds } 22401da177e4SLinus Torvalds 224154ff9ef3SMarcelo Ricardo Leitner int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 22421da177e4SLinus Torvalds { 22431da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22441d7138deSEric Dumazet struct ip_mc_socklist *iml; 22451d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 224684b42baeSDavid L Stevens struct in_device *in_dev; 2247877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22488f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 224984b42baeSDavid L Stevens u32 ifindex; 2250acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 22511da177e4SLinus Torvalds 2252959d10f6SEric Dumazet ASSERT_RTNL(); 2253959d10f6SEric Dumazet 2254877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 22554eba7bb1SAndrew Lunn if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) { 225652ad353aSdingtianhong ret = -ENODEV; 225752ad353aSdingtianhong goto out; 225852ad353aSdingtianhong } 225984b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 22601d7138deSEric Dumazet for (imlp = &inet->mc_list; 22611d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 22621d7138deSEric Dumazet imlp = &iml->next_rcu) { 2263acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 2264acd6e00bSDavid L Stevens continue; 2265acd6e00bSDavid L Stevens if (ifindex) { 2266acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 2267acd6e00bSDavid L Stevens continue; 2268acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 2269acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 2270acd6e00bSDavid L Stevens continue; 2271acd6e00bSDavid L Stevens 22721da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 22731da177e4SLinus Torvalds 22741d7138deSEric Dumazet *imlp = iml->next_rcu; 22751da177e4SLinus Torvalds 22764eba7bb1SAndrew Lunn if (in_dev) 227784b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 2278959d10f6SEric Dumazet 2279c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2280c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 228110d50e74SLai Jiangshan kfree_rcu(iml, rcu); 22821da177e4SLinus Torvalds return 0; 22831da177e4SLinus Torvalds } 228452ad353aSdingtianhong out: 2285959d10f6SEric Dumazet return ret; 2286959d10f6SEric Dumazet } 2287193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 22901da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 22911da177e4SLinus Torvalds { 22921da177e4SLinus Torvalds int err; 22931da177e4SLinus Torvalds struct ip_mreqn imr; 229463007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 22951da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22961da177e4SLinus Torvalds struct in_device *in_dev = NULL; 22971da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22981da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2299877acedcSDaniel Lezcano struct net *net = sock_net(sk); 23008cdaaa15SDavid L Stevens int leavegroup = 0; 23011da177e4SLinus Torvalds int i, j, rv; 23021da177e4SLinus Torvalds 2303f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 23041da177e4SLinus Torvalds return -EINVAL; 23051da177e4SLinus Torvalds 230654ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 23091da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 23101da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2311877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds if (!in_dev) { 23141da177e4SLinus Torvalds err = -ENODEV; 23151da177e4SLinus Torvalds goto done; 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 23181da177e4SLinus Torvalds 23191d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 2320f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 2321f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 2322f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 23231da177e4SLinus Torvalds break; 23241da177e4SLinus Torvalds } 2325917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2326917f2f10SDavid L Stevens err = -EINVAL; 23271da177e4SLinus Torvalds goto done; 2328917f2f10SDavid L Stevens } 23291da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 23301da177e4SLinus Torvalds if (pmc->sflist) { 2331917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 2332917f2f10SDavid L Stevens err = -EINVAL; 23331da177e4SLinus Torvalds goto done; 2334917f2f10SDavid L Stevens } 23351da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 23361da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 23371da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 23381da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 23391da177e4SLinus Torvalds NULL, 0); 23401da177e4SLinus Torvalds pmc->sfmode = omode; 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 23431d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 23441da177e4SLinus Torvalds if (!add) { 23451da177e4SLinus Torvalds if (!psl) 2346917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 23471da177e4SLinus Torvalds rv = !0; 23481da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 23491da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 235063007727SAl Viro sizeof(__be32)); 23511da177e4SLinus Torvalds if (rv == 0) 23521da177e4SLinus Torvalds break; 23531da177e4SLinus Torvalds } 23541da177e4SLinus Torvalds if (rv) /* source not found */ 2355917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 23561da177e4SLinus Torvalds 23578cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 23588cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 23598cdaaa15SDavid L Stevens leavegroup = 1; 23608cdaaa15SDavid L Stevens goto done; 23618cdaaa15SDavid L Stevens } 23628cdaaa15SDavid L Stevens 23631da177e4SLinus Torvalds /* update the interface filter */ 23641da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 23651da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds for (j = i+1; j < psl->sl_count; j++) 23681da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 23691da177e4SLinus Torvalds psl->sl_count--; 23701da177e4SLinus Torvalds err = 0; 23711da177e4SLinus Torvalds goto done; 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds /* else, add a new source to the filter */ 23741da177e4SLinus Torvalds 2375166b6b2dSNikolay Borisov if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) { 23761da177e4SLinus Torvalds err = -ENOBUFS; 23771da177e4SLinus Torvalds goto done; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 23801da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 23811da177e4SLinus Torvalds int count = IP_SFBLOCK; 23821da177e4SLinus Torvalds 23831da177e4SLinus Torvalds if (psl) 23841da177e4SLinus Torvalds count += psl->sl_max; 23858b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 23861da177e4SLinus Torvalds if (!newpsl) { 23871da177e4SLinus Torvalds err = -ENOBUFS; 23881da177e4SLinus Torvalds goto done; 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds newpsl->sl_max = count; 23911da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 23921da177e4SLinus Torvalds if (psl) { 23931da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) 23941da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2395c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2396c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 23977519cce4SLai Jiangshan kfree_rcu(psl, rcu); 23981da177e4SLinus Torvalds } 2399cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2400c85bb41eSFlavio Leitner psl = newpsl; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 24031da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 24041da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 240563007727SAl Viro sizeof(__be32)); 24061da177e4SLinus Torvalds if (rv == 0) 24071da177e4SLinus Torvalds break; 24081da177e4SLinus Torvalds } 24091da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 24101da177e4SLinus Torvalds goto done; 24111da177e4SLinus Torvalds for (j = psl->sl_count-1; j >= i; j--) 24121da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 24131da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 24141da177e4SLinus Torvalds psl->sl_count++; 24151da177e4SLinus Torvalds err = 0; 24161da177e4SLinus Torvalds /* update the interface list */ 24171da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 24181da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 24191da177e4SLinus Torvalds done: 24208cdaaa15SDavid L Stevens if (leavegroup) 242154ff9ef3SMarcelo Ricardo Leitner err = ip_mc_leave_group(sk, &imr); 24221da177e4SLinus Torvalds return err; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 24261da177e4SLinus Torvalds { 24279951f036SDavid L Stevens int err = 0; 24281da177e4SLinus Torvalds struct ip_mreqn imr; 242963007727SAl Viro __be32 addr = msf->imsf_multiaddr; 24301da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 24311da177e4SLinus Torvalds struct in_device *in_dev; 24321da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 24331da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2434877acedcSDaniel Lezcano struct net *net = sock_net(sk); 24359951f036SDavid L Stevens int leavegroup = 0; 24361da177e4SLinus Torvalds 2437f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 24381da177e4SLinus Torvalds return -EINVAL; 24391da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 24401da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 24411da177e4SLinus Torvalds return -EINVAL; 24421da177e4SLinus Torvalds 244354ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 24461da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 24471da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2448877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds if (!in_dev) { 24511da177e4SLinus Torvalds err = -ENODEV; 24521da177e4SLinus Torvalds goto done; 24531da177e4SLinus Torvalds } 24541da177e4SLinus Torvalds 24559951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 24569951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 24579951f036SDavid L Stevens leavegroup = 1; 24589951f036SDavid L Stevens goto done; 24599951f036SDavid L Stevens } 24609951f036SDavid L Stevens 24611d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 24621da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 24631da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 24641da177e4SLinus Torvalds break; 24651da177e4SLinus Torvalds } 2466917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2467917f2f10SDavid L Stevens err = -EINVAL; 24681da177e4SLinus Torvalds goto done; 2469917f2f10SDavid L Stevens } 24701da177e4SLinus Torvalds if (msf->imsf_numsrc) { 24718b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 24728b3a7005SKris Katterjohn GFP_KERNEL); 24731da177e4SLinus Torvalds if (!newpsl) { 24741da177e4SLinus Torvalds err = -ENOBUFS; 24751da177e4SLinus Torvalds goto done; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 24781da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 24791da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 24801da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 24811da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 24821da177e4SLinus Torvalds if (err) { 24831da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 24841da177e4SLinus Torvalds goto done; 24851da177e4SLinus Torvalds } 24868713dbf0SYan Zheng } else { 24871da177e4SLinus Torvalds newpsl = NULL; 24888713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 24898713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 24908713dbf0SYan Zheng } 24911d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 24921da177e4SLinus Torvalds if (psl) { 24931da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 24941da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2495c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2496c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 24977519cce4SLai Jiangshan kfree_rcu(psl, rcu); 24981da177e4SLinus Torvalds } else 24991da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 25001da177e4SLinus Torvalds 0, NULL, 0); 2501cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 25021da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2503917f2f10SDavid L Stevens err = 0; 25041da177e4SLinus Torvalds done: 25059951f036SDavid L Stevens if (leavegroup) 25069951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 25071da177e4SLinus Torvalds return err; 25081da177e4SLinus Torvalds } 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 25111da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 25121da177e4SLinus Torvalds { 25131da177e4SLinus Torvalds int err, len, count, copycount; 25141da177e4SLinus Torvalds struct ip_mreqn imr; 251563007727SAl Viro __be32 addr = msf->imsf_multiaddr; 25161da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25171da177e4SLinus Torvalds struct in_device *in_dev; 25181da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25191da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2520877acedcSDaniel Lezcano struct net *net = sock_net(sk); 25211da177e4SLinus Torvalds 252287e9f031SWANG Cong ASSERT_RTNL(); 252387e9f031SWANG Cong 2524f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 25251da177e4SLinus Torvalds return -EINVAL; 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 25281da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 25291da177e4SLinus Torvalds imr.imr_ifindex = 0; 2530877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds if (!in_dev) { 25331da177e4SLinus Torvalds err = -ENODEV; 25341da177e4SLinus Torvalds goto done; 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 25371da177e4SLinus Torvalds 25381d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 25391da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 25401da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 25411da177e4SLinus Torvalds break; 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 25441da177e4SLinus Torvalds goto done; 25451da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 25461d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 25471da177e4SLinus Torvalds if (!psl) { 25481da177e4SLinus Torvalds len = 0; 25491da177e4SLinus Torvalds count = 0; 25501da177e4SLinus Torvalds } else { 25511da177e4SLinus Torvalds count = psl->sl_count; 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 25541da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 25551da177e4SLinus Torvalds msf->imsf_numsrc = count; 25561da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 25571da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 25581da177e4SLinus Torvalds return -EFAULT; 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds if (len && 25611da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 25621da177e4SLinus Torvalds return -EFAULT; 25631da177e4SLinus Torvalds return 0; 25641da177e4SLinus Torvalds done: 25651da177e4SLinus Torvalds return err; 25661da177e4SLinus Torvalds } 25671da177e4SLinus Torvalds 25681da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 2569931ca7abSAl Viro struct sockaddr_storage __user *p) 25701da177e4SLinus Torvalds { 2571931ca7abSAl Viro int i, count, copycount; 25721da177e4SLinus Torvalds struct sockaddr_in *psin; 257363007727SAl Viro __be32 addr; 25741da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25751da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25761da177e4SLinus Torvalds struct ip_sf_socklist *psl; 25771da177e4SLinus Torvalds 257887e9f031SWANG Cong ASSERT_RTNL(); 257987e9f031SWANG Cong 25801da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 25811da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 25821da177e4SLinus Torvalds return -EINVAL; 25831da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2584f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 25851da177e4SLinus Torvalds return -EINVAL; 25861da177e4SLinus Torvalds 25871d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 25881da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 25891da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 25901da177e4SLinus Torvalds break; 25911da177e4SLinus Torvalds } 25921da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 2593931ca7abSAl Viro return -EADDRNOTAVAIL; 25941da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 25951d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 25961da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 25971da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 25981da177e4SLinus Torvalds gsf->gf_numsrc = count; 2599931ca7abSAl Viro for (i = 0; i < copycount; i++, p++) { 26001da177e4SLinus Torvalds struct sockaddr_storage ss; 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 26031da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 26041da177e4SLinus Torvalds psin->sin_family = AF_INET; 26051da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 2606931ca7abSAl Viro if (copy_to_user(p, &ss, sizeof(ss))) 26071da177e4SLinus Torvalds return -EFAULT; 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds return 0; 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds /* 26131da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 26141da177e4SLinus Torvalds */ 261560d9b031SDavid Ahern int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, 261660d9b031SDavid Ahern int dif, int sdif) 26171da177e4SLinus Torvalds { 26181da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 26191da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 26201da177e4SLinus Torvalds struct ip_sf_socklist *psl; 26211da177e4SLinus Torvalds int i; 2622c85bb41eSFlavio Leitner int ret; 26231da177e4SLinus Torvalds 2624c85bb41eSFlavio Leitner ret = 1; 2625f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2626c85bb41eSFlavio Leitner goto out; 26271da177e4SLinus Torvalds 2628c85bb41eSFlavio Leitner rcu_read_lock(); 26291d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 26301da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 263160d9b031SDavid Ahern (pmc->multi.imr_ifindex == dif || 263260d9b031SDavid Ahern (sdif && pmc->multi.imr_ifindex == sdif))) 26331da177e4SLinus Torvalds break; 26341da177e4SLinus Torvalds } 2635c85bb41eSFlavio Leitner ret = inet->mc_all; 26361da177e4SLinus Torvalds if (!pmc) 2637c85bb41eSFlavio Leitner goto unlock; 26381d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2639c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 26401da177e4SLinus Torvalds if (!psl) 2641c85bb41eSFlavio Leitner goto unlock; 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 26441da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 26451da177e4SLinus Torvalds break; 26461da177e4SLinus Torvalds } 2647c85bb41eSFlavio Leitner ret = 0; 26481da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2649c85bb41eSFlavio Leitner goto unlock; 26501da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2651c85bb41eSFlavio Leitner goto unlock; 2652c85bb41eSFlavio Leitner ret = 1; 2653c85bb41eSFlavio Leitner unlock: 2654c85bb41eSFlavio Leitner rcu_read_unlock(); 2655c85bb41eSFlavio Leitner out: 2656c85bb41eSFlavio Leitner return ret; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds /* 26601da177e4SLinus Torvalds * A socket is closing. 26611da177e4SLinus Torvalds */ 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 26661da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2667877acedcSDaniel Lezcano struct net *net = sock_net(sk); 26681da177e4SLinus Torvalds 266951456b29SIan Morris if (!inet->mc_list) 26701da177e4SLinus Torvalds return; 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds rtnl_lock(); 26731d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 26741da177e4SLinus Torvalds struct in_device *in_dev; 26751da177e4SLinus Torvalds 26761d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2677877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 26781da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 267900db4124SIan Morris if (in_dev) 26801da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2681c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2682c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 268310d50e74SLai Jiangshan kfree_rcu(iml, rcu); 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds rtnl_unlock(); 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds 2688dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 26892094acbbSAlexander Duyck int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto) 26901da177e4SLinus Torvalds { 26911da177e4SLinus Torvalds struct ip_mc_list *im; 2692e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 26931da177e4SLinus Torvalds struct ip_sf_list *psf; 26941da177e4SLinus Torvalds int rv = 0; 26951da177e4SLinus Torvalds 2696e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash); 2697e9897071SEric Dumazet if (mc_hash) { 2698c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); 2699e9897071SEric Dumazet 2700e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]); 2701e9897071SEric Dumazet im != NULL; 2702e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) { 2703e9897071SEric Dumazet if (im->multiaddr == mc_addr) 2704e9897071SEric Dumazet break; 2705e9897071SEric Dumazet } 2706e9897071SEric Dumazet } else { 27071d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 27081da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 27091da177e4SLinus Torvalds break; 27101da177e4SLinus Torvalds } 2711e9897071SEric Dumazet } 27121da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 27131da177e4SLinus Torvalds rv = 1; 27141da177e4SLinus Torvalds } else if (im) { 27151da177e4SLinus Torvalds if (src_addr) { 27161da177e4SLinus Torvalds for (psf = im->sources; psf; psf = psf->sf_next) { 27171da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 27181da177e4SLinus Torvalds break; 27191da177e4SLinus Torvalds } 27201da177e4SLinus Torvalds if (psf) 27211da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 27221da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 27231da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 27241da177e4SLinus Torvalds else 27251da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 27261da177e4SLinus Torvalds } else 27271da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 27281da177e4SLinus Torvalds } 27291da177e4SLinus Torvalds return rv; 27301da177e4SLinus Torvalds } 27311da177e4SLinus Torvalds 27321da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 27331da177e4SLinus Torvalds struct igmp_mc_iter_state { 27347091e728SAlexey Dobriyan struct seq_net_private p; 27351da177e4SLinus Torvalds struct net_device *dev; 27361da177e4SLinus Torvalds struct in_device *in_dev; 27371da177e4SLinus Torvalds }; 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 27401da177e4SLinus Torvalds 27411da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 27421da177e4SLinus Torvalds { 27437091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 27441da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 27451da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27461da177e4SLinus Torvalds 27477562f876SPavel Emelianov state->in_dev = NULL; 274861fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 27491da177e4SLinus Torvalds struct in_device *in_dev; 27506baff150SEric Dumazet 27516baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 27521da177e4SLinus Torvalds if (!in_dev) 27531da177e4SLinus Torvalds continue; 27541d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 27551da177e4SLinus Torvalds if (im) { 27561da177e4SLinus Torvalds state->in_dev = in_dev; 27571da177e4SLinus Torvalds break; 27581da177e4SLinus Torvalds } 27591da177e4SLinus Torvalds } 27601da177e4SLinus Torvalds return im; 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 27641da177e4SLinus Torvalds { 27651da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27666baff150SEric Dumazet 27671d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 27681d7138deSEric Dumazet while (!im) { 27696baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 27701da177e4SLinus Torvalds if (!state->dev) { 27711da177e4SLinus Torvalds state->in_dev = NULL; 27721da177e4SLinus Torvalds break; 27731da177e4SLinus Torvalds } 27746baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 27751da177e4SLinus Torvalds if (!state->in_dev) 27761da177e4SLinus Torvalds continue; 27771d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds return im; 27801da177e4SLinus Torvalds } 27811da177e4SLinus Torvalds 27821da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 27831da177e4SLinus Torvalds { 27841da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 27851da177e4SLinus Torvalds if (im) 27861da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 27871da177e4SLinus Torvalds --pos; 27881da177e4SLinus Torvalds return pos ? NULL : im; 27891da177e4SLinus Torvalds } 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 279261fbab77Sstephen hemminger __acquires(rcu) 27931da177e4SLinus Torvalds { 279461fbab77Sstephen hemminger rcu_read_lock(); 27951da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds 27981da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 27991da177e4SLinus Torvalds { 28001da177e4SLinus Torvalds struct ip_mc_list *im; 28011da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 28021da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 28031da177e4SLinus Torvalds else 28041da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 28051da177e4SLinus Torvalds ++*pos; 28061da177e4SLinus Torvalds return im; 28071da177e4SLinus Torvalds } 28081da177e4SLinus Torvalds 28091da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 281061fbab77Sstephen hemminger __releases(rcu) 28111da177e4SLinus Torvalds { 28121da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 28131d7138deSEric Dumazet 28141da177e4SLinus Torvalds state->in_dev = NULL; 28151da177e4SLinus Torvalds state->dev = NULL; 281661fbab77Sstephen hemminger rcu_read_unlock(); 28171da177e4SLinus Torvalds } 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 28201da177e4SLinus Torvalds { 28211da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 28221da177e4SLinus Torvalds seq_puts(seq, 28231da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 28241da177e4SLinus Torvalds else { 28251da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 28261da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 28271da177e4SLinus Torvalds char *querier; 2828a399a805SEric Dumazet long delta; 2829a399a805SEric Dumazet 28301da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 28311da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 28321da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 28331da177e4SLinus Torvalds "V3"; 28341da177e4SLinus Torvalds #else 28351da177e4SLinus Torvalds querier = "NONE"; 28361da177e4SLinus Torvalds #endif 28371da177e4SLinus Torvalds 2838e6b68883SAndreea-Cristina Bernat if (rcu_access_pointer(state->in_dev->mc_list) == im) { 28391da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2840b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 28411da177e4SLinus Torvalds } 28421da177e4SLinus Torvalds 2843a399a805SEric Dumazet delta = im->timer.expires - jiffies; 28441da177e4SLinus Torvalds seq_printf(seq, 2845338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 28461da177e4SLinus Torvalds im->multiaddr, im->users, 2847a399a805SEric Dumazet im->tm_running, 2848a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 28491da177e4SLinus Torvalds im->reporter); 28501da177e4SLinus Torvalds } 28511da177e4SLinus Torvalds return 0; 28521da177e4SLinus Torvalds } 28531da177e4SLinus Torvalds 2854f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 28551da177e4SLinus Torvalds .start = igmp_mc_seq_start, 28561da177e4SLinus Torvalds .next = igmp_mc_seq_next, 28571da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 28581da177e4SLinus Torvalds .show = igmp_mc_seq_show, 28591da177e4SLinus Torvalds }; 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds struct igmp_mcf_iter_state { 28627091e728SAlexey Dobriyan struct seq_net_private p; 28631da177e4SLinus Torvalds struct net_device *dev; 28641da177e4SLinus Torvalds struct in_device *idev; 28651da177e4SLinus Torvalds struct ip_mc_list *im; 28661da177e4SLinus Torvalds }; 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 28711da177e4SLinus Torvalds { 28727091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 28731da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 28741da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 28751da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 28761da177e4SLinus Torvalds 28777562f876SPavel Emelianov state->idev = NULL; 28787562f876SPavel Emelianov state->im = NULL; 287961fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 28801da177e4SLinus Torvalds struct in_device *idev; 28816baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 288251456b29SIan Morris if (unlikely(!idev)) 28831da177e4SLinus Torvalds continue; 28841d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 288500db4124SIan Morris if (likely(im)) { 28861da177e4SLinus Torvalds spin_lock_bh(&im->lock); 28871da177e4SLinus Torvalds psf = im->sources; 288800db4124SIan Morris if (likely(psf)) { 28891da177e4SLinus Torvalds state->im = im; 28901da177e4SLinus Torvalds state->idev = idev; 28911da177e4SLinus Torvalds break; 28921da177e4SLinus Torvalds } 28931da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds } 28961da177e4SLinus Torvalds return psf; 28971da177e4SLinus Torvalds } 28981da177e4SLinus Torvalds 28991da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 29001da177e4SLinus Torvalds { 29011da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 29021da177e4SLinus Torvalds 29031da177e4SLinus Torvalds psf = psf->sf_next; 29041da177e4SLinus Torvalds while (!psf) { 29051da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 29061da177e4SLinus Torvalds state->im = state->im->next; 29071da177e4SLinus Torvalds while (!state->im) { 29086baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 29091da177e4SLinus Torvalds if (!state->dev) { 29101da177e4SLinus Torvalds state->idev = NULL; 29111da177e4SLinus Torvalds goto out; 29121da177e4SLinus Torvalds } 29136baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 29141da177e4SLinus Torvalds if (!state->idev) 29151da177e4SLinus Torvalds continue; 29161d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 29171da177e4SLinus Torvalds } 29181da177e4SLinus Torvalds if (!state->im) 29191da177e4SLinus Torvalds break; 29201da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 29211da177e4SLinus Torvalds psf = state->im->sources; 29221da177e4SLinus Torvalds } 29231da177e4SLinus Torvalds out: 29241da177e4SLinus Torvalds return psf; 29251da177e4SLinus Torvalds } 29261da177e4SLinus Torvalds 29271da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 29281da177e4SLinus Torvalds { 29291da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 29301da177e4SLinus Torvalds if (psf) 29311da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 29321da177e4SLinus Torvalds --pos; 29331da177e4SLinus Torvalds return pos ? NULL : psf; 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds 29361da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 293761fbab77Sstephen hemminger __acquires(rcu) 29381da177e4SLinus Torvalds { 293961fbab77Sstephen hemminger rcu_read_lock(); 29401da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 29411da177e4SLinus Torvalds } 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds struct ip_sf_list *psf; 29461da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 29471da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 29481da177e4SLinus Torvalds else 29491da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 29501da177e4SLinus Torvalds ++*pos; 29511da177e4SLinus Torvalds return psf; 29521da177e4SLinus Torvalds } 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 295561fbab77Sstephen hemminger __releases(rcu) 29561da177e4SLinus Torvalds { 29571da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 295800db4124SIan Morris if (likely(state->im)) { 29591da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 29601da177e4SLinus Torvalds state->im = NULL; 29611da177e4SLinus Torvalds } 29621da177e4SLinus Torvalds state->idev = NULL; 29631da177e4SLinus Torvalds state->dev = NULL; 296461fbab77Sstephen hemminger rcu_read_unlock(); 29651da177e4SLinus Torvalds } 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 29681da177e4SLinus Torvalds { 29691da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 29701da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 29711da177e4SLinus Torvalds 29721da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 29731744bea1SJoe Perches seq_puts(seq, "Idx Device MCA SRC INC EXC\n"); 29741da177e4SLinus Torvalds } else { 29751da177e4SLinus Torvalds seq_printf(seq, 29761da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 29771da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 29781da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 29791da177e4SLinus Torvalds ntohl(state->im->multiaddr), 29801da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 29811da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 29821da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 29831da177e4SLinus Torvalds } 29841da177e4SLinus Torvalds return 0; 29851da177e4SLinus Torvalds } 29861da177e4SLinus Torvalds 2987f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 29881da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 29891da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 29901da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 29911da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 29921da177e4SLinus Torvalds }; 29931da177e4SLinus Torvalds 29942c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 29957091e728SAlexey Dobriyan { 29967091e728SAlexey Dobriyan struct proc_dir_entry *pde; 299793a714d6SMadhu Challa int err; 29987091e728SAlexey Dobriyan 2999c3506372SChristoph Hellwig pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops, 3000c3506372SChristoph Hellwig sizeof(struct igmp_mc_iter_state)); 30017091e728SAlexey Dobriyan if (!pde) 30027091e728SAlexey Dobriyan goto out_igmp; 3003c3506372SChristoph Hellwig pde = proc_create_net("mcfilter", 0444, net->proc_net, 3004c3506372SChristoph Hellwig &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state)); 30057091e728SAlexey Dobriyan if (!pde) 30067091e728SAlexey Dobriyan goto out_mcfilter; 300793a714d6SMadhu Challa err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET, 300893a714d6SMadhu Challa SOCK_DGRAM, 0, net); 300993a714d6SMadhu Challa if (err < 0) { 301093a714d6SMadhu Challa pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n", 301193a714d6SMadhu Challa err); 301293a714d6SMadhu Challa goto out_sock; 301393a714d6SMadhu Challa } 301493a714d6SMadhu Challa 30157091e728SAlexey Dobriyan return 0; 30167091e728SAlexey Dobriyan 301793a714d6SMadhu Challa out_sock: 301893a714d6SMadhu Challa remove_proc_entry("mcfilter", net->proc_net); 30197091e728SAlexey Dobriyan out_mcfilter: 3020ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 30217091e728SAlexey Dobriyan out_igmp: 30227091e728SAlexey Dobriyan return -ENOMEM; 30237091e728SAlexey Dobriyan } 30247091e728SAlexey Dobriyan 30252c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 30267091e728SAlexey Dobriyan { 3027ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net); 3028ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 302993a714d6SMadhu Challa inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk); 30307091e728SAlexey Dobriyan } 30317091e728SAlexey Dobriyan 30327091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 30337091e728SAlexey Dobriyan .init = igmp_net_init, 30347091e728SAlexey Dobriyan .exit = igmp_net_exit, 30351da177e4SLinus Torvalds }; 303672c1d3bdSWANG Cong #endif 30371da177e4SLinus Torvalds 30384aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this, 30394aa5dee4SJiri Pirko unsigned long event, void *ptr) 30404aa5dee4SJiri Pirko { 30414aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 30424aa5dee4SJiri Pirko struct in_device *in_dev; 30434aa5dee4SJiri Pirko 30444aa5dee4SJiri Pirko switch (event) { 30454aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 30464aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev); 30474aa5dee4SJiri Pirko if (in_dev) 30484aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev); 30494aa5dee4SJiri Pirko break; 30504aa5dee4SJiri Pirko default: 30514aa5dee4SJiri Pirko break; 30524aa5dee4SJiri Pirko } 30534aa5dee4SJiri Pirko return NOTIFY_DONE; 30544aa5dee4SJiri Pirko } 30554aa5dee4SJiri Pirko 30564aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = { 30574aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event, 30584aa5dee4SJiri Pirko }; 30594aa5dee4SJiri Pirko 306072c1d3bdSWANG Cong int __init igmp_mc_init(void) 30611da177e4SLinus Torvalds { 306272c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS) 30634aa5dee4SJiri Pirko int err; 30644aa5dee4SJiri Pirko 30654aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops); 30664aa5dee4SJiri Pirko if (err) 30674aa5dee4SJiri Pirko return err; 30684aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier); 30694aa5dee4SJiri Pirko if (err) 30704aa5dee4SJiri Pirko goto reg_notif_fail; 30714aa5dee4SJiri Pirko return 0; 30724aa5dee4SJiri Pirko 30734aa5dee4SJiri Pirko reg_notif_fail: 30744aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops); 30754aa5dee4SJiri Pirko return err; 307672c1d3bdSWANG Cong #else 307772c1d3bdSWANG Cong return register_netdevice_notifier(&igmp_notifier); 30781da177e4SLinus Torvalds #endif 307972c1d3bdSWANG Cong } 3080