11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux NET3: Internet Group Management Protocol [IGMP] 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This code implements the IGMP protocol as defined in RFC1112. There has 51da177e4SLinus Torvalds * been a further revision of this protocol since which is now supported. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * If you have trouble with this module be careful what gcc you have used, 81da177e4SLinus Torvalds * the older version didn't come out right using gcc 2.5.8, the newer one 91da177e4SLinus Torvalds * seems to fall out with gcc 2.6.2. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Authors: 12113aa838SAlan Cox * Alan Cox <alan@lxorguk.ukuu.org.uk> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 151da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 161da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 171da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Fixes: 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Alan Cox : Added lots of __inline__ to optimise 221da177e4SLinus Torvalds * the memory usage of all the tiny little 231da177e4SLinus Torvalds * functions. 241da177e4SLinus Torvalds * Alan Cox : Dumped the header building experiment. 251da177e4SLinus Torvalds * Alan Cox : Minor tweaks ready for multicast routing 261da177e4SLinus Torvalds * and extended IGMP protocol. 271da177e4SLinus Torvalds * Alan Cox : Removed a load of inline directives. Gcc 2.5.8 281da177e4SLinus Torvalds * writes utterly bogus code otherwise (sigh) 291da177e4SLinus Torvalds * fixed IGMP loopback to behave in the manner 301da177e4SLinus Torvalds * desired by mrouted, fixed the fact it has been 311da177e4SLinus Torvalds * broken since 1.3.6 and cleaned up a few minor 321da177e4SLinus Torvalds * points. 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Chih-Jen Chang : Tried to revise IGMP to Version 2 351da177e4SLinus Torvalds * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu 361da177e4SLinus Torvalds * The enhancements are mainly based on Steve Deering's 371da177e4SLinus Torvalds * ipmulti-3.5 source code. 381da177e4SLinus Torvalds * Chih-Jen Chang : Added the igmp_get_mrouter_info and 391da177e4SLinus Torvalds * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of 401da177e4SLinus Torvalds * the mrouted version on that device. 411da177e4SLinus Torvalds * Chih-Jen Chang : Added the max_resp_time parameter to 421da177e4SLinus Torvalds * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter 431da177e4SLinus Torvalds * to identify the multicast router version 441da177e4SLinus Torvalds * and do what the IGMP version 2 specified. 451da177e4SLinus Torvalds * Chih-Jen Chang : Added a timer to revert to IGMP V2 router 461da177e4SLinus Torvalds * Tsu-Sheng Tsao if the specified time expired. 471da177e4SLinus Torvalds * Alan Cox : Stop IGMP from 0.0.0.0 being accepted. 481da177e4SLinus Torvalds * Alan Cox : Use GFP_ATOMIC in the right places. 491da177e4SLinus Torvalds * Christian Daudt : igmp timer wasn't set for local group 501da177e4SLinus Torvalds * memberships but was being deleted, 511da177e4SLinus Torvalds * which caused a "del_timer() called 521da177e4SLinus Torvalds * from %p with timer not initialized\n" 531da177e4SLinus Torvalds * message (960131). 541da177e4SLinus Torvalds * Christian Daudt : removed del_timer from 551da177e4SLinus Torvalds * igmp_timer_expire function (960205). 561da177e4SLinus Torvalds * Christian Daudt : igmp_heard_report now only calls 571da177e4SLinus Torvalds * igmp_timer_expire if tm->running is 581da177e4SLinus Torvalds * true (960216). 591da177e4SLinus Torvalds * Malcolm Beattie : ttl comparison wrong in igmp_rcv made 601da177e4SLinus Torvalds * igmp_heard_query never trigger. Expiry 611da177e4SLinus Torvalds * miscalculation fixed in igmp_heard_query 621da177e4SLinus Torvalds * and random() made to return unsigned to 631da177e4SLinus Torvalds * prevent negative expiry times. 641da177e4SLinus Torvalds * Alexey Kuznetsov: Wrong group leaving behaviour, backport 651da177e4SLinus Torvalds * fix from pending 2.1.x patches. 661da177e4SLinus Torvalds * Alan Cox: Forget to enable FDDI support earlier. 671da177e4SLinus Torvalds * Alexey Kuznetsov: Fixed leaving groups on device down. 681da177e4SLinus Torvalds * Alexey Kuznetsov: Accordance to igmp-v2-06 draft. 691da177e4SLinus Torvalds * David L Stevens: IGMPv3 support, with help from 701da177e4SLinus Torvalds * Vinay Kulkarni 711da177e4SLinus Torvalds */ 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds #include <linux/module.h> 745a0e3ad6STejun Heo #include <linux/slab.h> 757c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 761da177e4SLinus Torvalds #include <linux/types.h> 771da177e4SLinus Torvalds #include <linux/kernel.h> 781da177e4SLinus Torvalds #include <linux/jiffies.h> 791da177e4SLinus Torvalds #include <linux/string.h> 801da177e4SLinus Torvalds #include <linux/socket.h> 811da177e4SLinus Torvalds #include <linux/sockios.h> 821da177e4SLinus Torvalds #include <linux/in.h> 831da177e4SLinus Torvalds #include <linux/inet.h> 841da177e4SLinus Torvalds #include <linux/netdevice.h> 851da177e4SLinus Torvalds #include <linux/skbuff.h> 861da177e4SLinus Torvalds #include <linux/inetdevice.h> 871da177e4SLinus Torvalds #include <linux/igmp.h> 881da177e4SLinus Torvalds #include <linux/if_arp.h> 891da177e4SLinus Torvalds #include <linux/rtnetlink.h> 901da177e4SLinus Torvalds #include <linux/times.h> 919d4a0314SHannes Frederic Sowa #include <linux/pkt_sched.h> 92a46182b0SKevin Cernekee #include <linux/byteorder/generic.h> 9314c85021SArnaldo Carvalho de Melo 94457c4cbcSEric W. Biederman #include <net/net_namespace.h> 9514c85021SArnaldo Carvalho de Melo #include <net/arp.h> 961da177e4SLinus Torvalds #include <net/ip.h> 971da177e4SLinus Torvalds #include <net/protocol.h> 981da177e4SLinus Torvalds #include <net/route.h> 991da177e4SLinus Torvalds #include <net/sock.h> 1001da177e4SLinus Torvalds #include <net/checksum.h> 10193a714d6SMadhu Challa #include <net/inet_common.h> 1021da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 1031da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1041da177e4SLinus Torvalds #include <linux/mroute.h> 1051da177e4SLinus Torvalds #endif 1061da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1071da177e4SLinus Torvalds #include <linux/proc_fs.h> 1081da177e4SLinus Torvalds #include <linux/seq_file.h> 1091da177e4SLinus Torvalds #endif 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1121da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */ 1131da177e4SLinus Torvalds 114436f7c20SFabian Frederick #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ) 115436f7c20SFabian Frederick #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ) 116966c37f2SHangbin Liu #define IGMP_QUERY_INTERVAL (125*HZ) 117436f7c20SFabian Frederick #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ) 1181da177e4SLinus Torvalds 119436f7c20SFabian Frederick #define IGMP_INITIAL_REPORT_DELAY (1) 1201da177e4SLinus Torvalds 121436f7c20SFabian Frederick /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs! 1221da177e4SLinus Torvalds * IGMP specs require to report membership immediately after 1231da177e4SLinus Torvalds * joining a group, but we delay the first report by a 1241da177e4SLinus Torvalds * small interval. It seems more natural and still does not 1251da177e4SLinus Torvalds * contradict to specs provided this delay is small enough. 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds 12842f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \ 129c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ 13042f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ 1311da177e4SLinus Torvalds ((in_dev)->mr_v1_seen && \ 1321da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v1_seen))) 13342f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \ 134c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ 13542f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ 1361da177e4SLinus Torvalds ((in_dev)->mr_v2_seen && \ 1371da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v2_seen))) 1381da177e4SLinus Torvalds 139cab70040SWilliam Manley static int unsolicited_report_interval(struct in_device *in_dev) 140cab70040SWilliam Manley { 1412690048cSWilliam Manley int interval_ms, interval_jiffies; 1422690048cSWilliam Manley 143cab70040SWilliam Manley if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 1442690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1452690048cSWilliam Manley in_dev, 1462690048cSWilliam Manley IGMPV2_UNSOLICITED_REPORT_INTERVAL); 147cab70040SWilliam Manley else /* v3 */ 1482690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1492690048cSWilliam Manley in_dev, 1502690048cSWilliam Manley IGMPV3_UNSOLICITED_REPORT_INTERVAL); 1512690048cSWilliam Manley 1522690048cSWilliam Manley interval_jiffies = msecs_to_jiffies(interval_ms); 1532690048cSWilliam Manley 1542690048cSWilliam Manley /* _timer functions can't handle a delay of 0 jiffies so ensure 1552690048cSWilliam Manley * we always return a positive value. 1562690048cSWilliam Manley */ 1572690048cSWilliam Manley if (interval_jiffies <= 0) 1582690048cSWilliam Manley interval_jiffies = 1; 1592690048cSWilliam Manley return interval_jiffies; 160cab70040SWilliam Manley } 161cab70040SWilliam Manley 1629fb20801SFlorian Fainelli static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im, 1639fb20801SFlorian Fainelli gfp_t gfp); 16424803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im); 1651da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev); 1661da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc); 1671da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc); 1681da177e4SLinus Torvalds #endif 1691da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc); 1708f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 1718f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im) 1741da177e4SLinus Torvalds { 1758851ab52SReshetova, Elena if (refcount_dec_and_test(&im->refcnt)) { 1761da177e4SLinus Torvalds in_dev_put(im->interface); 17742ea299dSLai Jiangshan kfree_rcu(im, rcu); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 181d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc) \ 182d9aa9380SDavid S. Miller for (pmc = rcu_dereference(in_dev->mc_list); \ 183d9aa9380SDavid S. Miller pmc != NULL; \ 184d9aa9380SDavid S. Miller pmc = rcu_dereference(pmc->next_rcu)) 185d9aa9380SDavid S. Miller 186d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc) \ 187d9aa9380SDavid S. Miller for (pmc = rtnl_dereference(in_dev->mc_list); \ 188d9aa9380SDavid S. Miller pmc != NULL; \ 189d9aa9380SDavid S. Miller pmc = rtnl_dereference(pmc->next_rcu)) 190d9aa9380SDavid S. Miller 191903869bdSEric Dumazet static void ip_sf_list_clear_all(struct ip_sf_list *psf) 192903869bdSEric Dumazet { 193903869bdSEric Dumazet struct ip_sf_list *next; 194903869bdSEric Dumazet 195903869bdSEric Dumazet while (psf) { 196903869bdSEric Dumazet next = psf->sf_next; 197903869bdSEric Dumazet kfree(psf); 198903869bdSEric Dumazet psf = next; 199903869bdSEric Dumazet } 200903869bdSEric Dumazet } 201903869bdSEric Dumazet 2021da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds /* 2051da177e4SLinus Torvalds * Timer management 2061da177e4SLinus Torvalds */ 2071da177e4SLinus Torvalds 2081d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2111da177e4SLinus Torvalds if (del_timer(&im->timer)) 2128851ab52SReshetova, Elena refcount_dec(&im->refcnt); 2131da177e4SLinus Torvalds im->tm_running = 0; 2141da177e4SLinus Torvalds im->reporter = 0; 2151da177e4SLinus Torvalds im->unsolicit_count = 0; 2161da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds /* It must be called with locked im->lock */ 2201da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay) 2211da177e4SLinus Torvalds { 22263862b5bSAruna-Hewapathirane int tv = prandom_u32() % max_delay; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds im->tm_running = 1; 2251da177e4SLinus Torvalds if (!mod_timer(&im->timer, jiffies+tv+2)) 2268851ab52SReshetova, Elena refcount_inc(&im->refcnt); 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev) 2301da177e4SLinus Torvalds { 23163862b5bSAruna-Hewapathirane int tv = prandom_u32() % in_dev->mr_maxdelay; 2327ababb78SMichal Tesar unsigned long exp = jiffies + tv + 2; 2337ababb78SMichal Tesar 2347ababb78SMichal Tesar if (in_dev->mr_gq_running && 2357ababb78SMichal Tesar time_after_eq(exp, (in_dev->mr_gq_timer).expires)) 2367ababb78SMichal Tesar return; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds in_dev->mr_gq_running = 1; 2397ababb78SMichal Tesar if (!mod_timer(&in_dev->mr_gq_timer, exp)) 2401da177e4SLinus Torvalds in_dev_hold(in_dev); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) 2441da177e4SLinus Torvalds { 24563862b5bSAruna-Hewapathirane int tv = prandom_u32() % delay; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) 2481da177e4SLinus Torvalds in_dev_hold(in_dev); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2541da177e4SLinus Torvalds im->unsolicit_count = 0; 2551da177e4SLinus Torvalds if (del_timer(&im->timer)) { 2561da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) { 2571da177e4SLinus Torvalds add_timer(&im->timer); 2581da177e4SLinus Torvalds im->tm_running = 1; 2591da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2601da177e4SLinus Torvalds return; 2611da177e4SLinus Torvalds } 2628851ab52SReshetova, Elena refcount_dec(&im->refcnt); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds igmp_start_timer(im, max_delay); 2651da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* 2701da177e4SLinus Torvalds * Send an IGMP report. 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, 2771da177e4SLinus Torvalds int gdeleted, int sdeleted) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds switch (type) { 2801da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE: 2811da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE: 2821da177e4SLinus Torvalds if (gdeleted || sdeleted) 2831da177e4SLinus Torvalds return 0; 284ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) { 285ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE) 286ad12583fSDavid L Stevens return 1; 287ad12583fSDavid L Stevens /* don't include if this source is excluded 288ad12583fSDavid L Stevens * in all filters 289ad12583fSDavid L Stevens */ 290ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE]) 291ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE; 292ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] == 293ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]; 294ad12583fSDavid L Stevens } 295ad12583fSDavid L Stevens return 0; 2961da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE: 2971da177e4SLinus Torvalds if (gdeleted || sdeleted) 2981da177e4SLinus Torvalds return 0; 2991da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0; 3001da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE: 3011da177e4SLinus Torvalds if (gdeleted || sdeleted) 3021da177e4SLinus Torvalds return 0; 3031da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 || 3041da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE]) 3051da177e4SLinus Torvalds return 0; 3061da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] == 3071da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]; 3081da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES: 3091da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount) 3101da177e4SLinus Torvalds return 0; 3111da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; 3121da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES: 3131da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) 3141da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted); 3151da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds return 0; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds static int 3211da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds struct ip_sf_list *psf; 3241da177e4SLinus Torvalds int scount = 0; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 3271da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 3281da177e4SLinus Torvalds continue; 3291da177e4SLinus Torvalds scount++; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds return scount; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 334a46182b0SKevin Cernekee /* source address selection per RFC 3376 section 4.2.13 */ 335a46182b0SKevin Cernekee static __be32 igmpv3_get_srcaddr(struct net_device *dev, 336a46182b0SKevin Cernekee const struct flowi4 *fl4) 337a46182b0SKevin Cernekee { 338a46182b0SKevin Cernekee struct in_device *in_dev = __in_dev_get_rcu(dev); 339*cd5a411dSFlorian Westphal const struct in_ifaddr *ifa; 340a46182b0SKevin Cernekee 341a46182b0SKevin Cernekee if (!in_dev) 342a46182b0SKevin Cernekee return htonl(INADDR_ANY); 343a46182b0SKevin Cernekee 344*cd5a411dSFlorian Westphal in_dev_for_each_ifa_rcu(ifa, in_dev) { 345ad23b750SFelix Fietkau if (fl4->saddr == ifa->ifa_local) 346a46182b0SKevin Cernekee return fl4->saddr; 347*cd5a411dSFlorian Westphal } 348a46182b0SKevin Cernekee 349a46182b0SKevin Cernekee return htonl(INADDR_ANY); 350a46182b0SKevin Cernekee } 351a46182b0SKevin Cernekee 3524c672e4bSDaniel Borkmann static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu) 3531da177e4SLinus Torvalds { 3541da177e4SLinus Torvalds struct sk_buff *skb; 3551da177e4SLinus Torvalds struct rtable *rt; 3561da177e4SLinus Torvalds struct iphdr *pip; 3571da177e4SLinus Torvalds struct igmpv3_report *pig; 358877acedcSDaniel Lezcano struct net *net = dev_net(dev); 35931e4543dSDavid S. Miller struct flowi4 fl4; 36066088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 36166088243SHerbert Xu int tlen = dev->needed_tailroom; 3624c672e4bSDaniel Borkmann unsigned int size = mtu; 3631da177e4SLinus Torvalds 36457e1ab6eSEric Dumazet while (1) { 36566088243SHerbert Xu skb = alloc_skb(size + hlen + tlen, 36657e1ab6eSEric Dumazet GFP_ATOMIC | __GFP_NOWARN); 36757e1ab6eSEric Dumazet if (skb) 36857e1ab6eSEric Dumazet break; 36957e1ab6eSEric Dumazet size >>= 1; 37057e1ab6eSEric Dumazet if (size < 256) 3711da177e4SLinus Torvalds return NULL; 37257e1ab6eSEric Dumazet } 3739d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 3741da177e4SLinus Torvalds 37531e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, 37678fbfd8aSDavid S. Miller 0, 0, 37778fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 378b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 3791da177e4SLinus Torvalds kfree_skb(skb); 3801da177e4SLinus Torvalds return NULL; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 383d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 3841da177e4SLinus Torvalds skb->dev = dev; 3851da177e4SLinus Torvalds 38666088243SHerbert Xu skb_reserve(skb, hlen); 3871837b2e2SBenjamin Poirier skb_tailroom_reserve(skb, mtu, tlen); 3881da177e4SLinus Torvalds 3897e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 390eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb); 3917e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds pip->version = 4; 3941da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2; 3951da177e4SLinus Torvalds pip->tos = 0xc0; 3961da177e4SLinus Torvalds pip->frag_off = htons(IP_DF); 3971da177e4SLinus Torvalds pip->ttl = 1; 398492f64ceSDavid S. Miller pip->daddr = fl4.daddr; 399e7aadb27SEric Dumazet 400e7aadb27SEric Dumazet rcu_read_lock(); 401a46182b0SKevin Cernekee pip->saddr = igmpv3_get_srcaddr(dev, &fl4); 402e7aadb27SEric Dumazet rcu_read_unlock(); 403e7aadb27SEric Dumazet 4041da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP; 4051da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */ 406b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 4071da177e4SLinus Torvalds ((u8 *)&pip[1])[0] = IPOPT_RA; 4081da177e4SLinus Torvalds ((u8 *)&pip[1])[1] = 4; 4091da177e4SLinus Torvalds ((u8 *)&pip[1])[2] = 0; 4101da177e4SLinus Torvalds ((u8 *)&pip[1])[3] = 0; 4111da177e4SLinus Torvalds 412b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; 413d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig)); 414d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb); 4151da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; 4161da177e4SLinus Torvalds pig->resv1 = 0; 4171da177e4SLinus Torvalds pig->csum = 0; 4181da177e4SLinus Torvalds pig->resv2 = 0; 4191da177e4SLinus Torvalds pig->ngrec = 0; 4201da177e4SLinus Torvalds return skb; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb) 4241da177e4SLinus Torvalds { 425d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb); 426f7c0c2aeSSimon Horman const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb); 4271da177e4SLinus Torvalds 428d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); 4291da177e4SLinus Torvalds 43033224b16SEric W. Biederman return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) 4341da177e4SLinus Torvalds { 4351da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, 439b5476022SEric Dumazet int type, struct igmpv3_grec **ppgr, unsigned int mtu) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4421da177e4SLinus Torvalds struct igmpv3_report *pih; 4431da177e4SLinus Torvalds struct igmpv3_grec *pgr; 4441da177e4SLinus Torvalds 445b5476022SEric Dumazet if (!skb) { 446b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 4471da177e4SLinus Torvalds if (!skb) 4481da177e4SLinus Torvalds return NULL; 449b5476022SEric Dumazet } 4504df864c1SJohannes Berg pgr = skb_put(skb, sizeof(struct igmpv3_grec)); 4511da177e4SLinus Torvalds pgr->grec_type = type; 4521da177e4SLinus Torvalds pgr->grec_auxwords = 0; 4531da177e4SLinus Torvalds pgr->grec_nsrcs = 0; 4541da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr; 455d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb); 4561da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1); 4571da177e4SLinus Torvalds *ppgr = pgr; 4581da177e4SLinus Torvalds return skb; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4614c672e4bSDaniel Borkmann #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0) 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, 4641da177e4SLinus Torvalds int type, int gdeleted, int sdeleted) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 46787a8a2aeSNikolay Borisov struct net *net = dev_net(dev); 4681da177e4SLinus Torvalds struct igmpv3_report *pih; 4691da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL; 4701da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; 471ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate; 472b5476022SEric Dumazet unsigned int mtu; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 4751da177e4SLinus Torvalds return skb; 47687a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 477df2cf4a7SPhilip Downey return skb; 4781da177e4SLinus Torvalds 479b5476022SEric Dumazet mtu = READ_ONCE(dev->mtu); 480b5476022SEric Dumazet if (mtu < IPV4_MIN_MTU) 481b5476022SEric Dumazet return skb; 482b5476022SEric Dumazet 4831da177e4SLinus Torvalds isquery = type == IGMPV3_MODE_IS_INCLUDE || 4841da177e4SLinus Torvalds type == IGMPV3_MODE_IS_EXCLUDE; 4851da177e4SLinus Torvalds truncate = type == IGMPV3_MODE_IS_EXCLUDE || 4861da177e4SLinus Torvalds type == IGMPV3_CHANGE_TO_EXCLUDE; 4871da177e4SLinus Torvalds 488ad12583fSDavid L Stevens stotal = scount = 0; 489ad12583fSDavid L Stevens 4901da177e4SLinus Torvalds psf_list = sdeleted ? &pmc->tomb : &pmc->sources; 4911da177e4SLinus Torvalds 492ad12583fSDavid L Stevens if (!*psf_list) 493ad12583fSDavid L Stevens goto empty_source; 494ad12583fSDavid L Stevens 495d9edf9e2SArnaldo Carvalho de Melo pih = skb ? igmpv3_report_hdr(skb) : NULL; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* EX and TO_EX get a fresh packet, if needed */ 4981da177e4SLinus Torvalds if (truncate) { 4991da177e4SLinus Torvalds if (pih && pih->ngrec && 5001da177e4SLinus Torvalds AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { 5011da177e4SLinus Torvalds if (skb) 5021da177e4SLinus Torvalds igmpv3_sendpack(skb); 503b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds first = 1; 5071da177e4SLinus Torvalds psf_prev = NULL; 5081da177e4SLinus Torvalds for (psf = *psf_list; psf; psf = psf_next) { 509ea4d9e72SAl Viro __be32 *psrc; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds psf_next = psf->sf_next; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { 5141da177e4SLinus Torvalds psf_prev = psf; 5151da177e4SLinus Torvalds continue; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 518a052517aSHangbin Liu /* Based on RFC3376 5.1. Should not send source-list change 519a052517aSHangbin Liu * records when there is a filter mode change. 520a052517aSHangbin Liu */ 521a052517aSHangbin Liu if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) || 522a052517aSHangbin Liu (!gdeleted && pmc->crcount)) && 523a052517aSHangbin Liu (type == IGMPV3_ALLOW_NEW_SOURCES || 524a052517aSHangbin Liu type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) 525a052517aSHangbin Liu goto decrease_sf_crcount; 526a052517aSHangbin Liu 5271da177e4SLinus Torvalds /* clear marks on query responses */ 5281da177e4SLinus Torvalds if (isquery) 5291da177e4SLinus Torvalds psf->sf_gsresp = 0; 5301da177e4SLinus Torvalds 53163007727SAl Viro if (AVAILABLE(skb) < sizeof(__be32) + 5321da177e4SLinus Torvalds first*sizeof(struct igmpv3_grec)) { 5331da177e4SLinus Torvalds if (truncate && !first) 5341da177e4SLinus Torvalds break; /* truncate these */ 5351da177e4SLinus Torvalds if (pgr) 5361da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5371da177e4SLinus Torvalds if (skb) 5381da177e4SLinus Torvalds igmpv3_sendpack(skb); 539b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 5401da177e4SLinus Torvalds first = 1; 5411da177e4SLinus Torvalds scount = 0; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds if (first) { 544b5476022SEric Dumazet skb = add_grhead(skb, pmc, type, &pgr, mtu); 5451da177e4SLinus Torvalds first = 0; 5461da177e4SLinus Torvalds } 547cc63f70bSAlexey Dobriyan if (!skb) 548cc63f70bSAlexey Dobriyan return NULL; 5494df864c1SJohannes Berg psrc = skb_put(skb, sizeof(__be32)); 5501da177e4SLinus Torvalds *psrc = psf->sf_inaddr; 551ad12583fSDavid L Stevens scount++; stotal++; 5521da177e4SLinus Torvalds if ((type == IGMPV3_ALLOW_NEW_SOURCES || 5531da177e4SLinus Torvalds type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { 554a052517aSHangbin Liu decrease_sf_crcount: 5551da177e4SLinus Torvalds psf->sf_crcount--; 5561da177e4SLinus Torvalds if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { 5571da177e4SLinus Torvalds if (psf_prev) 5581da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5591da177e4SLinus Torvalds else 5601da177e4SLinus Torvalds *psf_list = psf->sf_next; 5611da177e4SLinus Torvalds kfree(psf); 5621da177e4SLinus Torvalds continue; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds psf_prev = psf; 5661da177e4SLinus Torvalds } 567ad12583fSDavid L Stevens 568ad12583fSDavid L Stevens empty_source: 569ad12583fSDavid L Stevens if (!stotal) { 570ad12583fSDavid L Stevens if (type == IGMPV3_ALLOW_NEW_SOURCES || 571ad12583fSDavid L Stevens type == IGMPV3_BLOCK_OLD_SOURCES) 572ad12583fSDavid L Stevens return skb; 573ad12583fSDavid L Stevens if (pmc->crcount || isquery) { 574ad12583fSDavid L Stevens /* make sure we have room for group header */ 575ad12583fSDavid L Stevens if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) { 576ad12583fSDavid L Stevens igmpv3_sendpack(skb); 577ad12583fSDavid L Stevens skb = NULL; /* add_grhead will get a new one */ 578ad12583fSDavid L Stevens } 579b5476022SEric Dumazet skb = add_grhead(skb, pmc, type, &pgr, mtu); 580ad12583fSDavid L Stevens } 581ad12583fSDavid L Stevens } 5821da177e4SLinus Torvalds if (pgr) 5831da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds if (isquery) 5861da177e4SLinus Torvalds pmc->gsquery = 0; /* clear query state on report */ 5871da177e4SLinus Torvalds return skb; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds struct sk_buff *skb = NULL; 59387a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 5941da177e4SLinus Torvalds int type; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds if (!pmc) { 5971d7138deSEric Dumazet rcu_read_lock(); 5981d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 5991da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 6001da177e4SLinus Torvalds continue; 601df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(pmc->multiaddr) && 60287a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 603df2cf4a7SPhilip Downey continue; 6041da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6051da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 6061da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 6071da177e4SLinus Torvalds else 6081da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 6091da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6101da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6111da177e4SLinus Torvalds } 6121d7138deSEric Dumazet rcu_read_unlock(); 6131da177e4SLinus Torvalds } else { 6141da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6151da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 6161da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 6171da177e4SLinus Torvalds else 6181da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 6191da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6201da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds if (!skb) 6231da177e4SLinus Torvalds return 0; 6241da177e4SLinus Torvalds return igmpv3_sendpack(skb); 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds /* 6281da177e4SLinus Torvalds * remove zero-count source records from a source filter list 6291da177e4SLinus Torvalds */ 6301da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds psf_prev = NULL; 6351da177e4SLinus Torvalds for (psf = *ppsf; psf; psf = psf_next) { 6361da177e4SLinus Torvalds psf_next = psf->sf_next; 6371da177e4SLinus Torvalds if (psf->sf_crcount == 0) { 6381da177e4SLinus Torvalds if (psf_prev) 6391da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 6401da177e4SLinus Torvalds else 6411da177e4SLinus Torvalds *ppsf = psf->sf_next; 6421da177e4SLinus Torvalds kfree(psf); 6431da177e4SLinus Torvalds } else 6441da177e4SLinus Torvalds psf_prev = psf; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6483580d04aSEric Dumazet static void kfree_pmc(struct ip_mc_list *pmc) 6493580d04aSEric Dumazet { 6503580d04aSEric Dumazet ip_sf_list_clear_all(pmc->sources); 6513580d04aSEric Dumazet ip_sf_list_clear_all(pmc->tomb); 6523580d04aSEric Dumazet kfree(pmc); 6533580d04aSEric Dumazet } 6543580d04aSEric Dumazet 6551da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev) 6561da177e4SLinus Torvalds { 6571da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next; 6581da177e4SLinus Torvalds struct sk_buff *skb = NULL; 6591da177e4SLinus Torvalds int type, dtype; 6601da177e4SLinus Torvalds 6611d7138deSEric Dumazet rcu_read_lock(); 6621da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* deleted MCA's */ 6651da177e4SLinus Torvalds pmc_prev = NULL; 6661da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) { 6671da177e4SLinus Torvalds pmc_next = pmc->next; 6681da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 6691da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6701da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6711da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6721da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1); 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds if (pmc->crcount) { 6751da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) { 6761da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6771da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6781da177e4SLinus Torvalds } 679ad12583fSDavid L Stevens pmc->crcount--; 6801da177e4SLinus Torvalds if (pmc->crcount == 0) { 6811da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb); 6821da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources); 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { 6861da177e4SLinus Torvalds if (pmc_prev) 6871da177e4SLinus Torvalds pmc_prev->next = pmc_next; 6881da177e4SLinus Torvalds else 6891da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next; 6901da177e4SLinus Torvalds in_dev_put(pmc->interface); 6913580d04aSEric Dumazet kfree_pmc(pmc); 6921da177e4SLinus Torvalds } else 6931da177e4SLinus Torvalds pmc_prev = pmc; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* change recs */ 6981d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 6991da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 7001da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 7011da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 7021da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES; 7031da177e4SLinus Torvalds } else { 7041da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES; 7051da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 7081da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds /* filter mode changes */ 7111da177e4SLinus Torvalds if (pmc->crcount) { 7121da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) 7131da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE; 7141da177e4SLinus Torvalds else 7151da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 7161da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 717ad12583fSDavid L Stevens pmc->crcount--; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 7201da177e4SLinus Torvalds } 7211d7138deSEric Dumazet rcu_read_unlock(); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds if (!skb) 7241da177e4SLinus Torvalds return; 7251da177e4SLinus Torvalds (void) igmpv3_sendpack(skb); 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, 7291da177e4SLinus Torvalds int type) 7301da177e4SLinus Torvalds { 7311da177e4SLinus Torvalds struct sk_buff *skb; 7321da177e4SLinus Torvalds struct iphdr *iph; 7331da177e4SLinus Torvalds struct igmphdr *ih; 7341da177e4SLinus Torvalds struct rtable *rt; 7351da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 736877acedcSDaniel Lezcano struct net *net = dev_net(dev); 73763007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0; 73831e4543dSDavid S. Miller struct flowi4 fl4; 73963007727SAl Viro __be32 dst; 74066088243SHerbert Xu int hlen, tlen; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) 7431da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc); 744df2cf4a7SPhilip Downey 74587a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 746df2cf4a7SPhilip Downey return 0; 747df2cf4a7SPhilip Downey 748df2cf4a7SPhilip Downey if (type == IGMP_HOST_LEAVE_MESSAGE) 7491da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER; 7501da177e4SLinus Torvalds else 7511da177e4SLinus Torvalds dst = group; 7521da177e4SLinus Torvalds 75331e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, 75478fbfd8aSDavid S. Miller 0, 0, 75578fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 756b23dd4feSDavid S. Miller if (IS_ERR(rt)) 7571da177e4SLinus Torvalds return -1; 75878fbfd8aSDavid S. Miller 75966088243SHerbert Xu hlen = LL_RESERVED_SPACE(dev); 76066088243SHerbert Xu tlen = dev->needed_tailroom; 76166088243SHerbert Xu skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); 76251456b29SIan Morris if (!skb) { 7631da177e4SLinus Torvalds ip_rt_put(rt); 7641da177e4SLinus Torvalds return -1; 7651da177e4SLinus Torvalds } 7669d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 7671da177e4SLinus Torvalds 768d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 7691da177e4SLinus Torvalds 77066088243SHerbert Xu skb_reserve(skb, hlen); 7711da177e4SLinus Torvalds 7727e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 773eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 7747e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds iph->version = 4; 7771da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2; 7781da177e4SLinus Torvalds iph->tos = 0xc0; 7791da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 7801da177e4SLinus Torvalds iph->ttl = 1; 7811da177e4SLinus Torvalds iph->daddr = dst; 782492f64ceSDavid S. Miller iph->saddr = fl4.saddr; 7831da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP; 784b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 7851da177e4SLinus Torvalds ((u8 *)&iph[1])[0] = IPOPT_RA; 7861da177e4SLinus Torvalds ((u8 *)&iph[1])[1] = 4; 7871da177e4SLinus Torvalds ((u8 *)&iph[1])[2] = 0; 7881da177e4SLinus Torvalds ((u8 *)&iph[1])[3] = 0; 7891da177e4SLinus Torvalds 7904df864c1SJohannes Berg ih = skb_put(skb, sizeof(struct igmphdr)); 7911da177e4SLinus Torvalds ih->type = type; 7921da177e4SLinus Torvalds ih->code = 0; 7931da177e4SLinus Torvalds ih->csum = 0; 7941da177e4SLinus Torvalds ih->group = group; 7951da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 7961da177e4SLinus Torvalds 79733224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb); 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 800e99e88a9SKees Cook static void igmp_gq_timer_expire(struct timer_list *t) 8011da177e4SLinus Torvalds { 802e99e88a9SKees Cook struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer); 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 8051da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL); 806e2401654SSalam Noureddine in_dev_put(in_dev); 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 809e99e88a9SKees Cook static void igmp_ifc_timer_expire(struct timer_list *t) 8101da177e4SLinus Torvalds { 811e99e88a9SKees Cook struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 8141da177e4SLinus Torvalds if (in_dev->mr_ifc_count) { 8151da177e4SLinus Torvalds in_dev->mr_ifc_count--; 816cab70040SWilliam Manley igmp_ifc_start_timer(in_dev, 817cab70040SWilliam Manley unsolicited_report_interval(in_dev)); 8181da177e4SLinus Torvalds } 819e2401654SSalam Noureddine in_dev_put(in_dev); 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 8231da177e4SLinus Torvalds { 824165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 8251da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 8261da177e4SLinus Torvalds return; 827165094afSNikolay Borisov in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 8281da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds 832e99e88a9SKees Cook static void igmp_timer_expire(struct timer_list *t) 8331da177e4SLinus Torvalds { 834e99e88a9SKees Cook struct ip_mc_list *im = from_timer(im, t, timer); 8351da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds spin_lock(&im->lock); 8381da177e4SLinus Torvalds im->tm_running = 0; 8391da177e4SLinus Torvalds 8404fb7253eSHangbin Liu if (im->unsolicit_count && --im->unsolicit_count) 841cab70040SWilliam Manley igmp_start_timer(im, unsolicited_report_interval(in_dev)); 8424fb7253eSHangbin Liu 8431da177e4SLinus Torvalds im->reporter = 1; 8441da177e4SLinus Torvalds spin_unlock(&im->lock); 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 8471da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 8481da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 8491da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 8501da177e4SLinus Torvalds else 8511da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds ip_ma_put(im); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 856ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 857ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 8581da177e4SLinus Torvalds { 8591da177e4SLinus Torvalds struct ip_sf_list *psf; 8601da177e4SLinus Torvalds int i, scount; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds scount = 0; 8631da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 8641da177e4SLinus Torvalds if (scount == nsrcs) 8651da177e4SLinus Torvalds break; 866ad12583fSDavid L Stevens for (i = 0; i < nsrcs; i++) { 867ad12583fSDavid L Stevens /* skip inactive filters */ 868e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] || 869ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 870ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 871ce713ee5SRongQing.Li break; 872ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 873ad12583fSDavid L Stevens scount++; 874ad12583fSDavid L Stevens break; 875ad12583fSDavid L Stevens } 876ad12583fSDavid L Stevens } 877ad12583fSDavid L Stevens } 878ad12583fSDavid L Stevens pmc->gsquery = 0; 879ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 880ad12583fSDavid L Stevens return 0; 881ad12583fSDavid L Stevens return 1; 882ad12583fSDavid L Stevens } 883ad12583fSDavid L Stevens 884ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 885ad12583fSDavid L Stevens { 886ad12583fSDavid L Stevens struct ip_sf_list *psf; 887ad12583fSDavid L Stevens int i, scount; 888ad12583fSDavid L Stevens 889ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 890ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 891ad12583fSDavid L Stevens 892ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 893ad12583fSDavid L Stevens scount = 0; 894ad12583fSDavid L Stevens for (psf = pmc->sources; psf; psf = psf->sf_next) { 895ad12583fSDavid L Stevens if (scount == nsrcs) 896ad12583fSDavid L Stevens break; 8971da177e4SLinus Torvalds for (i = 0; i < nsrcs; i++) 8981da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 8991da177e4SLinus Torvalds psf->sf_gsresp = 1; 9001da177e4SLinus Torvalds scount++; 9011da177e4SLinus Torvalds break; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds } 904ad12583fSDavid L Stevens if (!scount) { 905ad12583fSDavid L Stevens pmc->gsquery = 0; 906ad12583fSDavid L Stevens return 0; 907ad12583fSDavid L Stevens } 908ad12583fSDavid L Stevens pmc->gsquery = 1; 909ad12583fSDavid L Stevens return 1; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 912d679c532SEric Dumazet /* return true if packet was dropped */ 913d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group) 9141da177e4SLinus Torvalds { 9151da177e4SLinus Torvalds struct ip_mc_list *im; 91687a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 921d679c532SEric Dumazet return false; 92287a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 923df2cf4a7SPhilip Downey return false; 9241da177e4SLinus Torvalds 9251d7138deSEric Dumazet rcu_read_lock(); 9261d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 9271da177e4SLinus Torvalds if (im->multiaddr == group) { 9281da177e4SLinus Torvalds igmp_stop_timer(im); 9291da177e4SLinus Torvalds break; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds } 9321d7138deSEric Dumazet rcu_read_unlock(); 933d679c532SEric Dumazet return false; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 936d679c532SEric Dumazet /* return true if packet was dropped */ 937d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 9381da177e4SLinus Torvalds int len) 9391da177e4SLinus Torvalds { 940d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 941d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 9421da177e4SLinus Torvalds struct ip_mc_list *im; 94363007727SAl Viro __be32 group = ih->group; 9441da177e4SLinus Torvalds int max_delay; 9451da177e4SLinus Torvalds int mark = 0; 94687a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds 9495b7c8406SDavid Stevens if (len == 8) { 9501da177e4SLinus Torvalds if (ih->code == 0) { 9511da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 9521da177e4SLinus Torvalds 953436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9541da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 955966c37f2SHangbin Liu (in_dev->mr_qrv * in_dev->mr_qi) + 956966c37f2SHangbin Liu in_dev->mr_qri; 9571da177e4SLinus Torvalds group = 0; 9581da177e4SLinus Torvalds } else { 9591da177e4SLinus Torvalds /* v2 router present */ 9601da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 9611da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 962966c37f2SHangbin Liu (in_dev->mr_qrv * in_dev->mr_qi) + 963966c37f2SHangbin Liu in_dev->mr_qri; 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds /* cancel the interface change timer */ 9661da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 9671da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 9681da177e4SLinus Torvalds __in_dev_put(in_dev); 9691da177e4SLinus Torvalds /* clear deleted report items */ 9701da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 9711da177e4SLinus Torvalds } else if (len < 12) { 972d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */ 9735b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) { 9745b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */ 975436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9765b7c8406SDavid Stevens group = 0; 9775b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) { 9785b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present; 9795b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here. 9805b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a 9815b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely 9825b7c8406SDavid Stevens * to be intended in a v3 query. 9835b7c8406SDavid Stevens */ 9845b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 985a8c1f65cSBen Hutchings if (!max_delay) 986a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */ 9871da177e4SLinus Torvalds } else { /* v3 */ 9881da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 989d679c532SEric Dumazet return true; 9901da177e4SLinus Torvalds 991d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9921da177e4SLinus Torvalds if (ih3->nsrcs) { 9931da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 99463007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 995d679c532SEric Dumazet return true; 996d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 10001da177e4SLinus Torvalds if (!max_delay) 10011da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 10021da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 1003966c37f2SHangbin Liu 1004966c37f2SHangbin Liu /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently 1005966c37f2SHangbin Liu * received value was zero, use the default or statically 1006966c37f2SHangbin Liu * configured value. 1007966c37f2SHangbin Liu */ 1008966c37f2SHangbin Liu in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv; 1009966c37f2SHangbin Liu in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL; 1010966c37f2SHangbin Liu 1011966c37f2SHangbin Liu /* RFC3376, 8.3. Query Response Interval: 1012966c37f2SHangbin Liu * The number of seconds represented by the [Query Response 1013966c37f2SHangbin Liu * Interval] must be less than the [Query Interval]. 1014966c37f2SHangbin Liu */ 1015966c37f2SHangbin Liu if (in_dev->mr_qri >= in_dev->mr_qi) 1016966c37f2SHangbin Liu in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ; 1017966c37f2SHangbin Liu 10181da177e4SLinus Torvalds if (!group) { /* general query */ 10191da177e4SLinus Torvalds if (ih3->nsrcs) 1020b47bd8d2SDaniel Borkmann return true; /* no sources allowed */ 10211da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 1022d679c532SEric Dumazet return false; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 10251da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds /* 10291da177e4SLinus Torvalds * - Start the timers in all of our membership records 10301da177e4SLinus Torvalds * that the query applies to for the interface on 10311da177e4SLinus Torvalds * which the query arrived excl. those that belong 10321da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 10331da177e4SLinus Torvalds * - For timers already running check if they need to 10341da177e4SLinus Torvalds * be reset. 10351da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 10361da177e4SLinus Torvalds * delay possible 10371da177e4SLinus Torvalds */ 10381d7138deSEric Dumazet rcu_read_lock(); 10391d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 1040ad12583fSDavid L Stevens int changed; 1041ad12583fSDavid L Stevens 10421da177e4SLinus Torvalds if (group && group != im->multiaddr) 10431da177e4SLinus Torvalds continue; 10441da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 10451da177e4SLinus Torvalds continue; 1046df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 104787a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1048df2cf4a7SPhilip Downey continue; 10491da177e4SLinus Torvalds spin_lock_bh(&im->lock); 10501da177e4SLinus Torvalds if (im->tm_running) 10511da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 10521da177e4SLinus Torvalds else 10531da177e4SLinus Torvalds im->gsquery = mark; 1054ad12583fSDavid L Stevens changed = !im->gsquery || 10551da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 10561da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 1057ad12583fSDavid L Stevens if (changed) 10581da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 10591da177e4SLinus Torvalds } 10601d7138deSEric Dumazet rcu_read_unlock(); 1061d679c532SEric Dumazet return false; 10621da177e4SLinus Torvalds } 10631da177e4SLinus Torvalds 10649a57a9d2SEric Dumazet /* called in rcu_read_lock() section */ 10651da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 10661da177e4SLinus Torvalds { 10671da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 10681da177e4SLinus Torvalds struct igmphdr *ih; 1069c7b725beSDavid Ahern struct net_device *dev = skb->dev; 1070c7b725beSDavid Ahern struct in_device *in_dev; 10711da177e4SLinus Torvalds int len = skb->len; 1072d679c532SEric Dumazet bool dropped = true; 10731da177e4SLinus Torvalds 1074c7b725beSDavid Ahern if (netif_is_l3_master(dev)) { 1075c7b725beSDavid Ahern dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif); 1076c7b725beSDavid Ahern if (!dev) 1077c7b725beSDavid Ahern goto drop; 1078c7b725beSDavid Ahern } 1079c7b725beSDavid Ahern 1080c7b725beSDavid Ahern in_dev = __in_dev_get_rcu(dev); 108151456b29SIan Morris if (!in_dev) 1082cd557bc1SDenis V. Lunev goto drop; 10831da177e4SLinus Torvalds 1084fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 10859a57a9d2SEric Dumazet goto drop; 1086fb286bb2SHerbert Xu 1087de08dc1aSTom Herbert if (skb_checksum_simple_validate(skb)) 10889a57a9d2SEric Dumazet goto drop; 10891da177e4SLinus Torvalds 1090d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 10911da177e4SLinus Torvalds switch (ih->type) { 10921da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 1093d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len); 10941da177e4SLinus Torvalds break; 10951da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 10961da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 10971da177e4SLinus Torvalds /* Is it our report looped back? */ 1098c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) 10991da177e4SLinus Torvalds break; 110024c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 110124c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 110224c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 1103d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group); 11041da177e4SLinus Torvalds break; 11051da177e4SLinus Torvalds case IGMP_PIM: 11061da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 11071da177e4SLinus Torvalds return pim_rcv_v1(skb); 11081da177e4SLinus Torvalds #endif 1109c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 11101da177e4SLinus Torvalds case IGMP_DVMRP: 11111da177e4SLinus Torvalds case IGMP_TRACE: 11121da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 11131da177e4SLinus Torvalds case IGMP_MTRACE: 11141da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 11151da177e4SLinus Torvalds break; 11161da177e4SLinus Torvalds default: 1117dd1c1853SLinus Torvalds break; 11181da177e4SLinus Torvalds } 1119fb286bb2SHerbert Xu 1120cd557bc1SDenis V. Lunev drop: 1121d679c532SEric Dumazet if (dropped) 11221da177e4SLinus Torvalds kfree_skb(skb); 1123d679c532SEric Dumazet else 1124d679c532SEric Dumazet consume_skb(skb); 11251da177e4SLinus Torvalds return 0; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds #endif 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds /* 11321da177e4SLinus Torvalds * Add a filter to a device 11331da177e4SLinus Torvalds */ 11341da177e4SLinus Torvalds 113563007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 11361da177e4SLinus Torvalds { 11371da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 11381da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 11411da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 1142b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode 11431da177e4SLinus Torvalds routine. Something sort of: 11441da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 11451da177e4SLinus Torvalds --ANK 11461da177e4SLinus Torvalds */ 11471da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 114822bedad3SJiri Pirko dev_mc_add(dev, buf); 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds /* 11521da177e4SLinus Torvalds * Remove a filter from a device 11531da177e4SLinus Torvalds */ 11541da177e4SLinus Torvalds 115563007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 11581da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 116122bedad3SJiri Pirko dev_mc_del(dev, buf); 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11651da177e4SLinus Torvalds /* 11661da177e4SLinus Torvalds * deleted ip_mc_list manipulation 11671da177e4SLinus Torvalds */ 11689fb20801SFlorian Fainelli static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im, 11699fb20801SFlorian Fainelli gfp_t gfp) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds struct ip_mc_list *pmc; 1172165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 11751da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 11761da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 11771da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 11781da177e4SLinus Torvalds * non-deleted or query-response MCA's. 11791da177e4SLinus Torvalds */ 11809fb20801SFlorian Fainelli pmc = kzalloc(sizeof(*pmc), gfp); 11811da177e4SLinus Torvalds if (!pmc) 11821da177e4SLinus Torvalds return; 1183b4846fc3SWANG Cong spin_lock_init(&pmc->lock); 11841da177e4SLinus Torvalds spin_lock_bh(&im->lock); 11851da177e4SLinus Torvalds pmc->interface = im->interface; 11861da177e4SLinus Torvalds in_dev_hold(in_dev); 11871da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 1188165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 11891da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 11901da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 11911da177e4SLinus Torvalds struct ip_sf_list *psf; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds pmc->tomb = im->tomb; 11941da177e4SLinus Torvalds pmc->sources = im->sources; 11951da177e4SLinus Torvalds im->tomb = im->sources = NULL; 11961da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 11971da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12021da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 12031da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 12041da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 120724803f38SHangbin Liu /* 120824803f38SHangbin Liu * restore ip_mc_list deleted records 120924803f38SHangbin Liu */ 121024803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im) 12111da177e4SLinus Torvalds { 12121da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 121324803f38SHangbin Liu struct ip_sf_list *psf; 121424803f38SHangbin Liu struct net *net = dev_net(in_dev->dev); 121524803f38SHangbin Liu __be32 multiaddr = im->multiaddr; 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12181da177e4SLinus Torvalds pmc_prev = NULL; 12191da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) { 12201da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 12211da177e4SLinus Torvalds break; 12221da177e4SLinus Torvalds pmc_prev = pmc; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds if (pmc) { 12251da177e4SLinus Torvalds if (pmc_prev) 12261da177e4SLinus Torvalds pmc_prev->next = pmc->next; 12271da177e4SLinus Torvalds else 12281da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 123124803f38SHangbin Liu 123224803f38SHangbin Liu spin_lock_bh(&im->lock); 12331da177e4SLinus Torvalds if (pmc) { 123424803f38SHangbin Liu im->interface = pmc->interface; 123508d3ffccSHangbin Liu if (im->sfmode == MCAST_INCLUDE) { 123624803f38SHangbin Liu im->tomb = pmc->tomb; 12373580d04aSEric Dumazet pmc->tomb = NULL; 12383580d04aSEric Dumazet 123924803f38SHangbin Liu im->sources = pmc->sources; 12403580d04aSEric Dumazet pmc->sources = NULL; 12413580d04aSEric Dumazet 124224803f38SHangbin Liu for (psf = im->sources; psf; psf = psf->sf_next) 12436e2059b5SHangbin Liu psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 12446e2059b5SHangbin Liu } else { 12456e2059b5SHangbin Liu im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds in_dev_put(pmc->interface); 12483580d04aSEric Dumazet kfree_pmc(pmc); 12491da177e4SLinus Torvalds } 125024803f38SHangbin Liu spin_unlock_bh(&im->lock); 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 125324803f38SHangbin Liu /* 125424803f38SHangbin Liu * flush ip_mc_list deleted records 125524803f38SHangbin Liu */ 12561da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 12571da177e4SLinus Torvalds { 12581da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12611da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 12621da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 12631da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 12661da177e4SLinus Torvalds nextpmc = pmc->next; 12671da177e4SLinus Torvalds ip_mc_clear_src(pmc); 12681da177e4SLinus Torvalds in_dev_put(pmc->interface); 12693580d04aSEric Dumazet kfree_pmc(pmc); 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds /* clear dead sources, too */ 12721d7138deSEric Dumazet rcu_read_lock(); 12731d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 12743580d04aSEric Dumazet struct ip_sf_list *psf; 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 12771da177e4SLinus Torvalds psf = pmc->tomb; 12781da177e4SLinus Torvalds pmc->tomb = NULL; 12791da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 12803580d04aSEric Dumazet ip_sf_list_clear_all(psf); 12811da177e4SLinus Torvalds } 12821d7138deSEric Dumazet rcu_read_unlock(); 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds #endif 12851da177e4SLinus Torvalds 12869fb20801SFlorian Fainelli static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp) 12871da177e4SLinus Torvalds { 12881da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 12891da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 129087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 12911da177e4SLinus Torvalds int reporter; 12921da177e4SLinus Torvalds #endif 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds if (im->loaded) { 12951da177e4SLinus Torvalds im->loaded = 0; 12961da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13001da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 13011da177e4SLinus Torvalds return; 130287a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1303df2cf4a7SPhilip Downey return; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds reporter = im->reporter; 13061da177e4SLinus Torvalds igmp_stop_timer(im); 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds if (!in_dev->dead) { 13091da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 131024cf3af3SVeaceslav Falico return; 13111da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 13121da177e4SLinus Torvalds if (reporter) 13131da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 131424cf3af3SVeaceslav Falico return; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds /* IGMPv3 */ 13179fb20801SFlorian Fainelli igmpv3_add_delrec(in_dev, im, gfp); 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds igmp_ifc_event(in_dev); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds #endif 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds 13249fb20801SFlorian Fainelli static void igmp_group_dropped(struct ip_mc_list *im) 13259fb20801SFlorian Fainelli { 13269fb20801SFlorian Fainelli __igmp_group_dropped(im, GFP_KERNEL); 13279fb20801SFlorian Fainelli } 13289fb20801SFlorian Fainelli 13290ae0d60aSHangbin Liu static void igmp_group_added(struct ip_mc_list *im) 13301da177e4SLinus Torvalds { 13311da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 1332dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 133387a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1334dcd87999SNikolay Borisov #endif 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds if (im->loaded == 0) { 13371da177e4SLinus Torvalds im->loaded = 1; 13381da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13421da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 13431da177e4SLinus Torvalds return; 134487a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1345df2cf4a7SPhilip Downey return; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds if (in_dev->dead) 13481da177e4SLinus Torvalds return; 1349ff06525fSHangbin Liu 1350ff06525fSHangbin Liu im->unsolicit_count = net->ipv4.sysctl_igmp_qrv; 13511da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 13521da177e4SLinus Torvalds spin_lock_bh(&im->lock); 1353436f7c20SFabian Frederick igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY); 13541da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 13551da177e4SLinus Torvalds return; 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds /* else, v3 */ 13581da177e4SLinus Torvalds 13596e2059b5SHangbin Liu /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should 13606e2059b5SHangbin Liu * not send filter-mode change record as the mode should be from 13616e2059b5SHangbin Liu * IN() to IN(A). 13626e2059b5SHangbin Liu */ 13630ae0d60aSHangbin Liu if (im->sfmode == MCAST_EXCLUDE) 1364165094afSNikolay Borisov im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 13656e2059b5SHangbin Liu 13661da177e4SLinus Torvalds igmp_ifc_event(in_dev); 13671da177e4SLinus Torvalds #endif 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds /* 13721da177e4SLinus Torvalds * Multicast list managers 13731da177e4SLinus Torvalds */ 13741da177e4SLinus Torvalds 1375e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im) 1376e9897071SEric Dumazet { 1377c70eba74SEric Dumazet return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); 1378e9897071SEric Dumazet } 1379e9897071SEric Dumazet 1380e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev, 1381e9897071SEric Dumazet struct ip_mc_list *im) 1382e9897071SEric Dumazet { 1383e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 1384e9897071SEric Dumazet u32 hash; 1385e9897071SEric Dumazet 1386e9897071SEric Dumazet mc_hash = rtnl_dereference(in_dev->mc_hash); 1387e9897071SEric Dumazet if (mc_hash) { 1388e9897071SEric Dumazet hash = ip_mc_hash(im); 1389c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1390e9897071SEric Dumazet rcu_assign_pointer(mc_hash[hash], im); 1391e9897071SEric Dumazet return; 1392e9897071SEric Dumazet } 1393e9897071SEric Dumazet 1394e9897071SEric Dumazet /* do not use a hash table for small number of items */ 1395e9897071SEric Dumazet if (in_dev->mc_count < 4) 1396e9897071SEric Dumazet return; 1397e9897071SEric Dumazet 1398e9897071SEric Dumazet mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG, 1399e9897071SEric Dumazet GFP_KERNEL); 1400e9897071SEric Dumazet if (!mc_hash) 1401e9897071SEric Dumazet return; 1402e9897071SEric Dumazet 1403e9897071SEric Dumazet for_each_pmc_rtnl(in_dev, im) { 1404e9897071SEric Dumazet hash = ip_mc_hash(im); 1405c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1406e9897071SEric Dumazet RCU_INIT_POINTER(mc_hash[hash], im); 1407e9897071SEric Dumazet } 1408e9897071SEric Dumazet 1409e9897071SEric Dumazet rcu_assign_pointer(in_dev->mc_hash, mc_hash); 1410e9897071SEric Dumazet } 1411e9897071SEric Dumazet 1412e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev, 1413e9897071SEric Dumazet struct ip_mc_list *im) 1414e9897071SEric Dumazet { 1415e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); 1416e9897071SEric Dumazet struct ip_mc_list *aux; 1417e9897071SEric Dumazet 1418e9897071SEric Dumazet if (!mc_hash) 1419e9897071SEric Dumazet return; 1420e9897071SEric Dumazet mc_hash += ip_mc_hash(im); 1421e9897071SEric Dumazet while ((aux = rtnl_dereference(*mc_hash)) != im) 1422e9897071SEric Dumazet mc_hash = &aux->next_hash; 1423e9897071SEric Dumazet *mc_hash = im->next_hash; 1424e9897071SEric Dumazet } 1425e9897071SEric Dumazet 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds /* 14281da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 14291da177e4SLinus Torvalds */ 14309fb20801SFlorian Fainelli static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, 14319fb20801SFlorian Fainelli unsigned int mode, gfp_t gfp) 14321da177e4SLinus Torvalds { 14331da177e4SLinus Torvalds struct ip_mc_list *im; 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds ASSERT_RTNL(); 14361da177e4SLinus Torvalds 14371d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) { 14381da177e4SLinus Torvalds if (im->multiaddr == addr) { 14391da177e4SLinus Torvalds im->users++; 14406e2059b5SHangbin Liu ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0); 14411da177e4SLinus Torvalds goto out; 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 14459fb20801SFlorian Fainelli im = kzalloc(sizeof(*im), gfp); 14461da177e4SLinus Torvalds if (!im) 14471da177e4SLinus Torvalds goto out; 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds im->users = 1; 14501da177e4SLinus Torvalds im->interface = in_dev; 14511da177e4SLinus Torvalds in_dev_hold(in_dev); 14521da177e4SLinus Torvalds im->multiaddr = addr; 14531da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 14546e2059b5SHangbin Liu im->sfmode = mode; 14556e2059b5SHangbin Liu im->sfcount[mode] = 1; 14568851ab52SReshetova, Elena refcount_set(&im->refcnt, 1); 14571da177e4SLinus Torvalds spin_lock_init(&im->lock); 14581da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1459e99e88a9SKees Cook timer_setup(&im->timer, igmp_timer_expire, 0); 14601da177e4SLinus Torvalds #endif 14611d7138deSEric Dumazet 14621d7138deSEric Dumazet im->next_rcu = in_dev->mc_list; 1463b8bae41eSRami Rosen in_dev->mc_count++; 1464cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im); 14651d7138deSEric Dumazet 1466e9897071SEric Dumazet ip_mc_hash_add(in_dev, im); 1467e9897071SEric Dumazet 14681da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 146924803f38SHangbin Liu igmpv3_del_delrec(in_dev, im); 14701da177e4SLinus Torvalds #endif 14710ae0d60aSHangbin Liu igmp_group_added(im); 14721da177e4SLinus Torvalds if (!in_dev->dead) 14731da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 14741da177e4SLinus Torvalds out: 14751da177e4SLinus Torvalds return; 14761da177e4SLinus Torvalds } 14776e2059b5SHangbin Liu 14789fb20801SFlorian Fainelli void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp) 14799fb20801SFlorian Fainelli { 14809fb20801SFlorian Fainelli ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp); 14819fb20801SFlorian Fainelli } 14829fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_inc_group); 14839fb20801SFlorian Fainelli 14846e2059b5SHangbin Liu void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 14856e2059b5SHangbin Liu { 14866e2059b5SHangbin Liu __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE); 14876e2059b5SHangbin Liu } 14884bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group); 14891da177e4SLinus Torvalds 14909afd85c9SLinus Lüssing static int ip_mc_check_iphdr(struct sk_buff *skb) 14919afd85c9SLinus Lüssing { 14929afd85c9SLinus Lüssing const struct iphdr *iph; 14939afd85c9SLinus Lüssing unsigned int len; 14949afd85c9SLinus Lüssing unsigned int offset = skb_network_offset(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 (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph)) 15029afd85c9SLinus Lüssing return -EINVAL; 15039afd85c9SLinus Lüssing 15049afd85c9SLinus Lüssing offset += ip_hdrlen(skb) - sizeof(*iph); 15059afd85c9SLinus Lüssing 15069afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset)) 15079afd85c9SLinus Lüssing return -EINVAL; 15089afd85c9SLinus Lüssing 15099afd85c9SLinus Lüssing iph = ip_hdr(skb); 15109afd85c9SLinus Lüssing 15119afd85c9SLinus Lüssing if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 15129afd85c9SLinus Lüssing return -EINVAL; 15139afd85c9SLinus Lüssing 15149afd85c9SLinus Lüssing len = skb_network_offset(skb) + ntohs(iph->tot_len); 15159afd85c9SLinus Lüssing if (skb->len < len || len < offset) 15169afd85c9SLinus Lüssing return -EINVAL; 15179afd85c9SLinus Lüssing 15189afd85c9SLinus Lüssing skb_set_transport_header(skb, offset); 15199afd85c9SLinus Lüssing 15209afd85c9SLinus Lüssing return 0; 15219afd85c9SLinus Lüssing } 15229afd85c9SLinus Lüssing 15239afd85c9SLinus Lüssing static int ip_mc_check_igmp_reportv3(struct sk_buff *skb) 15249afd85c9SLinus Lüssing { 15259afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb); 15269afd85c9SLinus Lüssing 15279afd85c9SLinus Lüssing len += sizeof(struct igmpv3_report); 15289afd85c9SLinus Lüssing 1529a2e2ca3bSLinus Lüssing return ip_mc_may_pull(skb, len) ? 0 : -EINVAL; 15309afd85c9SLinus Lüssing } 15319afd85c9SLinus Lüssing 15329afd85c9SLinus Lüssing static int ip_mc_check_igmp_query(struct sk_buff *skb) 15339afd85c9SLinus Lüssing { 1534a2e2ca3bSLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 1535a2e2ca3bSLinus Lüssing unsigned int len; 15369afd85c9SLinus Lüssing 15379afd85c9SLinus Lüssing /* IGMPv{1,2}? */ 1538a2e2ca3bSLinus Lüssing if (transport_len != sizeof(struct igmphdr)) { 15399afd85c9SLinus Lüssing /* or IGMPv3? */ 1540a2e2ca3bSLinus Lüssing if (transport_len < sizeof(struct igmpv3_query)) 1541a2e2ca3bSLinus Lüssing return -EINVAL; 1542a2e2ca3bSLinus Lüssing 1543a2e2ca3bSLinus Lüssing len = skb_transport_offset(skb) + sizeof(struct igmpv3_query); 1544a2e2ca3bSLinus Lüssing if (!ip_mc_may_pull(skb, len)) 15459afd85c9SLinus Lüssing return -EINVAL; 15469afd85c9SLinus Lüssing } 15479afd85c9SLinus Lüssing 15489afd85c9SLinus Lüssing /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer 15499afd85c9SLinus Lüssing * all-systems destination addresses (224.0.0.1) for general queries 15509afd85c9SLinus Lüssing */ 15519afd85c9SLinus Lüssing if (!igmp_hdr(skb)->group && 15529afd85c9SLinus Lüssing ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP)) 15539afd85c9SLinus Lüssing return -EINVAL; 15549afd85c9SLinus Lüssing 15559afd85c9SLinus Lüssing return 0; 15569afd85c9SLinus Lüssing } 15579afd85c9SLinus Lüssing 15589afd85c9SLinus Lüssing static int ip_mc_check_igmp_msg(struct sk_buff *skb) 15599afd85c9SLinus Lüssing { 15609afd85c9SLinus Lüssing switch (igmp_hdr(skb)->type) { 15619afd85c9SLinus Lüssing case IGMP_HOST_LEAVE_MESSAGE: 15629afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_REPORT: 15639afd85c9SLinus Lüssing case IGMPV2_HOST_MEMBERSHIP_REPORT: 15649afd85c9SLinus Lüssing return 0; 15659afd85c9SLinus Lüssing case IGMPV3_HOST_MEMBERSHIP_REPORT: 15669afd85c9SLinus Lüssing return ip_mc_check_igmp_reportv3(skb); 15679afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_QUERY: 15689afd85c9SLinus Lüssing return ip_mc_check_igmp_query(skb); 15699afd85c9SLinus Lüssing default: 15709afd85c9SLinus Lüssing return -ENOMSG; 15719afd85c9SLinus Lüssing } 15729afd85c9SLinus Lüssing } 15739afd85c9SLinus Lüssing 15749afd85c9SLinus Lüssing static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb) 15759afd85c9SLinus Lüssing { 15769afd85c9SLinus Lüssing return skb_checksum_simple_validate(skb); 15779afd85c9SLinus Lüssing } 15789afd85c9SLinus Lüssing 1579a2e2ca3bSLinus Lüssing static int ip_mc_check_igmp_csum(struct sk_buff *skb) 15809afd85c9SLinus Lüssing { 15819afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr); 1582a2e2ca3bSLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 1583a2e2ca3bSLinus Lüssing struct sk_buff *skb_chk; 15849afd85c9SLinus Lüssing 1585a2e2ca3bSLinus Lüssing if (!ip_mc_may_pull(skb, len)) 1586a2e2ca3bSLinus Lüssing return -EINVAL; 15879afd85c9SLinus Lüssing 15889afd85c9SLinus Lüssing skb_chk = skb_checksum_trimmed(skb, transport_len, 15899afd85c9SLinus Lüssing ip_mc_validate_checksum); 15909afd85c9SLinus Lüssing if (!skb_chk) 1591a2e2ca3bSLinus Lüssing return -EINVAL; 15929afd85c9SLinus Lüssing 1593a2e2ca3bSLinus Lüssing if (skb_chk != skb) 1594a516993fSLinus Lüssing kfree_skb(skb_chk); 1595a516993fSLinus Lüssing 1596a2e2ca3bSLinus Lüssing return 0; 15979afd85c9SLinus Lüssing } 15989afd85c9SLinus Lüssing 15999afd85c9SLinus Lüssing /** 16009afd85c9SLinus Lüssing * ip_mc_check_igmp - checks whether this is a sane IGMP packet 16019afd85c9SLinus Lüssing * @skb: the skb to validate 16029afd85c9SLinus Lüssing * 16039afd85c9SLinus Lüssing * Checks whether an IPv4 packet is a valid IGMP packet. If so sets 1604a516993fSLinus Lüssing * skb transport header accordingly and returns zero. 16059afd85c9SLinus Lüssing * 16069afd85c9SLinus Lüssing * -EINVAL: A broken packet was detected, i.e. it violates some internet 16079afd85c9SLinus Lüssing * standard 16089afd85c9SLinus Lüssing * -ENOMSG: IP header validation succeeded but it is not an IGMP packet. 16099afd85c9SLinus Lüssing * -ENOMEM: A memory allocation failure happened. 16109afd85c9SLinus Lüssing * 1611a516993fSLinus Lüssing * Caller needs to set the skb network header and free any returned skb if it 1612a516993fSLinus Lüssing * differs from the provided skb. 16139afd85c9SLinus Lüssing */ 1614ba5ea614SLinus Lüssing int ip_mc_check_igmp(struct sk_buff *skb) 16159afd85c9SLinus Lüssing { 16169afd85c9SLinus Lüssing int ret = ip_mc_check_iphdr(skb); 16179afd85c9SLinus Lüssing 16189afd85c9SLinus Lüssing if (ret < 0) 16199afd85c9SLinus Lüssing return ret; 16209afd85c9SLinus Lüssing 16219afd85c9SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP) 16229afd85c9SLinus Lüssing return -ENOMSG; 16239afd85c9SLinus Lüssing 1624a2e2ca3bSLinus Lüssing ret = ip_mc_check_igmp_csum(skb); 1625a2e2ca3bSLinus Lüssing if (ret < 0) 1626a2e2ca3bSLinus Lüssing return ret; 1627a2e2ca3bSLinus Lüssing 1628a2e2ca3bSLinus Lüssing return ip_mc_check_igmp_msg(skb); 16299afd85c9SLinus Lüssing } 16309afd85c9SLinus Lüssing EXPORT_SYMBOL(ip_mc_check_igmp); 16319afd85c9SLinus Lüssing 16321da177e4SLinus Torvalds /* 16334aa5dee4SJiri Pirko * Resend IGMP JOIN report; used by netdev notifier. 1634a816c7c7SJay Vosburgh */ 16354aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev) 1636a816c7c7SJay Vosburgh { 163708882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1638866f3b25SEric Dumazet struct ip_mc_list *im; 1639866f3b25SEric Dumazet int type; 164087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1641a816c7c7SJay Vosburgh 16424aa5dee4SJiri Pirko ASSERT_RTNL(); 16434aa5dee4SJiri Pirko 16444aa5dee4SJiri Pirko for_each_pmc_rtnl(in_dev, im) { 1645a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1646866f3b25SEric Dumazet continue; 1647df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 164887a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1649df2cf4a7SPhilip Downey continue; 1650a816c7c7SJay Vosburgh 1651e12b4539SFlavio Leitner /* a failover is happening and switches 1652866f3b25SEric Dumazet * must be notified immediately 1653866f3b25SEric Dumazet */ 1654e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev)) 1655866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT; 1656e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev)) 1657866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT; 1658e12b4539SFlavio Leitner else 1659866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT; 1660866f3b25SEric Dumazet igmp_send_report(in_dev, im, type); 1661866f3b25SEric Dumazet } 1662a816c7c7SJay Vosburgh #endif 1663a816c7c7SJay Vosburgh } 1664a816c7c7SJay Vosburgh 1665a816c7c7SJay Vosburgh /* 16661da177e4SLinus Torvalds * A socket has left a multicast group on device dev 16671da177e4SLinus Torvalds */ 16681da177e4SLinus Torvalds 16699fb20801SFlorian Fainelli void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp) 16701da177e4SLinus Torvalds { 16711d7138deSEric Dumazet struct ip_mc_list *i; 16721d7138deSEric Dumazet struct ip_mc_list __rcu **ip; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds ASSERT_RTNL(); 16751da177e4SLinus Torvalds 16761d7138deSEric Dumazet for (ip = &in_dev->mc_list; 16771d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL; 16781d7138deSEric Dumazet ip = &i->next_rcu) { 16791da177e4SLinus Torvalds if (i->multiaddr == addr) { 16801da177e4SLinus Torvalds if (--i->users == 0) { 1681e9897071SEric Dumazet ip_mc_hash_remove(in_dev, i); 16821d7138deSEric Dumazet *ip = i->next_rcu; 1683b8bae41eSRami Rosen in_dev->mc_count--; 16849fb20801SFlorian Fainelli __igmp_group_dropped(i, gfp); 168524cf3af3SVeaceslav Falico ip_mc_clear_src(i); 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds if (!in_dev->dead) 16881da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds ip_ma_put(i); 16911da177e4SLinus Torvalds return; 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds break; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds } 16979fb20801SFlorian Fainelli EXPORT_SYMBOL(__ip_mc_dec_group); 16981da177e4SLinus Torvalds 169975c78500SMoni Shoua /* Device changing type */ 170075c78500SMoni Shoua 170175c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 170275c78500SMoni Shoua { 17031d7138deSEric Dumazet struct ip_mc_list *pmc; 170475c78500SMoni Shoua 170575c78500SMoni Shoua ASSERT_RTNL(); 170675c78500SMoni Shoua 17071d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 17081d7138deSEric Dumazet igmp_group_dropped(pmc); 170975c78500SMoni Shoua } 171075c78500SMoni Shoua 171175c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 171275c78500SMoni Shoua { 17131d7138deSEric Dumazet struct ip_mc_list *pmc; 171475c78500SMoni Shoua 171575c78500SMoni Shoua ASSERT_RTNL(); 171675c78500SMoni Shoua 171724803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) { 171824803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 171924803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc); 172024803f38SHangbin Liu #endif 17210ae0d60aSHangbin Liu igmp_group_added(pmc); 172275c78500SMoni Shoua } 172324803f38SHangbin Liu } 172475c78500SMoni Shoua 17251da177e4SLinus Torvalds /* Device going down */ 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 17281da177e4SLinus Torvalds { 17291d7138deSEric Dumazet struct ip_mc_list *pmc; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds ASSERT_RTNL(); 17321da177e4SLinus Torvalds 17331d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 17341d7138deSEric Dumazet igmp_group_dropped(pmc); 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17371da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 17381da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 17391da177e4SLinus Torvalds __in_dev_put(in_dev); 17401da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 17411da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 17421da177e4SLinus Torvalds __in_dev_put(in_dev); 17431da177e4SLinus Torvalds #endif 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 1748966c37f2SHangbin Liu #ifdef CONFIG_IP_MULTICAST 1749966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev) 1750966c37f2SHangbin Liu { 1751966c37f2SHangbin Liu struct net *net = dev_net(in_dev->dev); 1752966c37f2SHangbin Liu 1753966c37f2SHangbin Liu in_dev->mr_qi = IGMP_QUERY_INTERVAL; 1754966c37f2SHangbin Liu in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL; 1755966c37f2SHangbin Liu in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv; 1756966c37f2SHangbin Liu } 1757966c37f2SHangbin Liu #else 1758966c37f2SHangbin Liu static void ip_mc_reset(struct in_device *in_dev) 1759966c37f2SHangbin Liu { 1760966c37f2SHangbin Liu } 1761966c37f2SHangbin Liu #endif 1762966c37f2SHangbin Liu 17631da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds ASSERT_RTNL(); 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1768e99e88a9SKees Cook timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0); 1769e99e88a9SKees Cook timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0); 17701da177e4SLinus Torvalds #endif 1771966c37f2SHangbin Liu ip_mc_reset(in_dev); 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds /* Device going up */ 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 17791da177e4SLinus Torvalds { 17801d7138deSEric Dumazet struct ip_mc_list *pmc; 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds ASSERT_RTNL(); 17831da177e4SLinus Torvalds 1784966c37f2SHangbin Liu ip_mc_reset(in_dev); 17851da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 17861da177e4SLinus Torvalds 178724803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) { 178824803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 178924803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc); 179024803f38SHangbin Liu #endif 17910ae0d60aSHangbin Liu igmp_group_added(pmc); 17921da177e4SLinus Torvalds } 179324803f38SHangbin Liu } 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds /* 17961da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 17971da177e4SLinus Torvalds */ 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 18001da177e4SLinus Torvalds { 18011da177e4SLinus Torvalds struct ip_mc_list *i; 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds ASSERT_RTNL(); 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds /* Deactivate timers */ 18061da177e4SLinus Torvalds ip_mc_down(in_dev); 180724803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 180824803f38SHangbin Liu igmpv3_clear_delrec(in_dev); 180924803f38SHangbin Liu #endif 18101da177e4SLinus Torvalds 18111d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { 18121d7138deSEric Dumazet in_dev->mc_list = i->next_rcu; 1813b8bae41eSRami Rosen in_dev->mc_count--; 18141da177e4SLinus Torvalds ip_ma_put(i); 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 18189e917dcaSEric Dumazet /* RTNL is locked */ 1819877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 18201da177e4SLinus Torvalds { 18211da177e4SLinus Torvalds struct net_device *dev = NULL; 18221da177e4SLinus Torvalds struct in_device *idev = NULL; 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds if (imr->imr_ifindex) { 1825877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 18261da177e4SLinus Torvalds return idev; 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 18299e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false); 18301da177e4SLinus Torvalds if (!dev) 18311da177e4SLinus Torvalds return NULL; 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds 1834b23dd4feSDavid S. Miller if (!dev) { 183578fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, 183678fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr, 183778fbfd8aSDavid S. Miller 0, 0, 0); 1838b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1839d8d1f30bSChangli Gao dev = rt->dst.dev; 18401da177e4SLinus Torvalds ip_rt_put(rt); 18411da177e4SLinus Torvalds } 1842b23dd4feSDavid S. Miller } 18431da177e4SLinus Torvalds if (dev) { 18441da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1845e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds return idev; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds /* 18511da177e4SLinus Torvalds * Join a socket to a group 18521da177e4SLinus Torvalds */ 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 18558f935bbdSAl Viro __be32 *psfsrc) 18561da177e4SLinus Torvalds { 18571da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 18581da177e4SLinus Torvalds int rv = 0; 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds psf_prev = NULL; 18611da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 18621da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 18631da177e4SLinus Torvalds break; 18641da177e4SLinus Torvalds psf_prev = psf; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 18671da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 18681da177e4SLinus Torvalds return -ESRCH; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds psf->sf_count[sfmode]--; 18711da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 18721da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 18751da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18761da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 1877165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 18781da177e4SLinus Torvalds #endif 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds /* no more filters for this source */ 18811da177e4SLinus Torvalds if (psf_prev) 18821da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 18831da177e4SLinus Torvalds else 18841da177e4SLinus Torvalds pmc->sources = psf->sf_next; 18851da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18861da177e4SLinus Torvalds if (psf->sf_oldin && 18871da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 1888165094afSNikolay Borisov psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 18891da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 18901da177e4SLinus Torvalds pmc->tomb = psf; 18911da177e4SLinus Torvalds rv = 1; 18921da177e4SLinus Torvalds } else 18931da177e4SLinus Torvalds #endif 18941da177e4SLinus Torvalds kfree(psf); 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds return rv; 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 19001da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 19011da177e4SLinus Torvalds #endif 19021da177e4SLinus Torvalds 19038f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 19048f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 19051da177e4SLinus Torvalds { 19061da177e4SLinus Torvalds struct ip_mc_list *pmc; 19071da177e4SLinus Torvalds int changerec = 0; 19081da177e4SLinus Torvalds int i, err; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds if (!in_dev) 19111da177e4SLinus Torvalds return -ENODEV; 19121d7138deSEric Dumazet rcu_read_lock(); 19131d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 19141da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 19151da177e4SLinus Torvalds break; 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds if (!pmc) { 19181da177e4SLinus Torvalds /* MCA not found?? bug */ 19191d7138deSEric Dumazet rcu_read_unlock(); 19201da177e4SLinus Torvalds return -ESRCH; 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 19231d7138deSEric Dumazet rcu_read_unlock(); 19241da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19251da177e4SLinus Torvalds sf_markstate(pmc); 19261da177e4SLinus Torvalds #endif 19271da177e4SLinus Torvalds if (!delta) { 19281da177e4SLinus Torvalds err = -EINVAL; 19291da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 19301da177e4SLinus Torvalds goto out_unlock; 19311da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds err = 0; 19341da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 19351da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds changerec |= rv > 0; 19381da177e4SLinus Torvalds if (!err && rv < 0) 19391da177e4SLinus Torvalds err = rv; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 19421da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 19431da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 19441da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19451da177e4SLinus Torvalds struct ip_sf_list *psf; 1946165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 19471da177e4SLinus Torvalds #endif 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds /* filter mode change */ 19501da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 19511da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1952165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 19531da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 19541da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 19551da177e4SLinus Torvalds psf->sf_crcount = 0; 19561da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 19571da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 19581da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 19591da177e4SLinus Torvalds #endif 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds out_unlock: 19621da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 19631da177e4SLinus Torvalds return err; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds /* 19671da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 19681da177e4SLinus Torvalds */ 19691da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 19705eb81e89SJun Zhao __be32 *psfsrc) 19711da177e4SLinus Torvalds { 19721da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds psf_prev = NULL; 19751da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 19761da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 19771da177e4SLinus Torvalds break; 19781da177e4SLinus Torvalds psf_prev = psf; 19791da177e4SLinus Torvalds } 19801da177e4SLinus Torvalds if (!psf) { 19810da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 19821da177e4SLinus Torvalds if (!psf) 19831da177e4SLinus Torvalds return -ENOBUFS; 19841da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 19851da177e4SLinus Torvalds if (psf_prev) { 19861da177e4SLinus Torvalds psf_prev->sf_next = psf; 19871da177e4SLinus Torvalds } else 19881da177e4SLinus Torvalds pmc->sources = psf; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds psf->sf_count[sfmode]++; 19911da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 19921da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds return 0; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19981da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 19991da177e4SLinus Torvalds { 20001da177e4SLinus Torvalds struct ip_sf_list *psf; 20011da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 20041da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 20051da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 20061da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 20071da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 20081da177e4SLinus Torvalds } else 20091da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 20101da177e4SLinus Torvalds } 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 20131da177e4SLinus Torvalds { 2014ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 20151da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 20161da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 20171da177e4SLinus Torvalds int new_in, rv; 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds rv = 0; 20201da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 20211da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 20221da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 20231da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 20241da177e4SLinus Torvalds } else 20251da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 2026ad12583fSDavid L Stevens if (new_in) { 2027ad12583fSDavid L Stevens if (!psf->sf_oldin) { 202876edc605SAl Viro struct ip_sf_list *prev = NULL; 2029ad12583fSDavid L Stevens 2030ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) { 2031ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 2032ad12583fSDavid L Stevens break; 2033ad12583fSDavid L Stevens prev = dpsf; 2034ad12583fSDavid L Stevens } 2035ad12583fSDavid L Stevens if (dpsf) { 2036ad12583fSDavid L Stevens if (prev) 2037ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 2038ad12583fSDavid L Stevens else 2039ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 2040ad12583fSDavid L Stevens kfree(dpsf); 2041ad12583fSDavid L Stevens } 20421da177e4SLinus Torvalds psf->sf_crcount = qrv; 20431da177e4SLinus Torvalds rv++; 20441da177e4SLinus Torvalds } 2045ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 2046ad12583fSDavid L Stevens 2047ad12583fSDavid L Stevens psf->sf_crcount = 0; 2048ad12583fSDavid L Stevens /* 2049ad12583fSDavid L Stevens * add or update "delete" records if an active filter 2050ad12583fSDavid L Stevens * is now inactive 2051ad12583fSDavid L Stevens */ 2052ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) 2053ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 2054ad12583fSDavid L Stevens break; 2055ad12583fSDavid L Stevens if (!dpsf) { 20563ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); 2057ad12583fSDavid L Stevens if (!dpsf) 2058ad12583fSDavid L Stevens continue; 2059ad12583fSDavid L Stevens *dpsf = *psf; 2060ad12583fSDavid L Stevens /* pmc->lock held by callers */ 2061ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 2062ad12583fSDavid L Stevens pmc->tomb = dpsf; 2063ad12583fSDavid L Stevens } 2064ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 2065ad12583fSDavid L Stevens rv++; 2066ad12583fSDavid L Stevens } 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds return rv; 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds #endif 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds /* 20731da177e4SLinus Torvalds * Add multicast source filter list to the interface list 20741da177e4SLinus Torvalds */ 20758f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 20768f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 20771da177e4SLinus Torvalds { 20781da177e4SLinus Torvalds struct ip_mc_list *pmc; 20791da177e4SLinus Torvalds int isexclude; 20801da177e4SLinus Torvalds int i, err; 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds if (!in_dev) 20831da177e4SLinus Torvalds return -ENODEV; 20841d7138deSEric Dumazet rcu_read_lock(); 20851d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 20861da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 20871da177e4SLinus Torvalds break; 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds if (!pmc) { 20901da177e4SLinus Torvalds /* MCA not found?? bug */ 20911d7138deSEric Dumazet rcu_read_unlock(); 20921da177e4SLinus Torvalds return -ESRCH; 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 20951d7138deSEric Dumazet rcu_read_unlock(); 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 20981da177e4SLinus Torvalds sf_markstate(pmc); 20991da177e4SLinus Torvalds #endif 21001da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 21011da177e4SLinus Torvalds if (!delta) 21021da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 21031da177e4SLinus Torvalds err = 0; 21041da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 21055eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); 21061da177e4SLinus Torvalds if (err) 21071da177e4SLinus Torvalds break; 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds if (err) { 21101da177e4SLinus Torvalds int j; 21111da177e4SLinus Torvalds 2112685f94e6SJun Zhao if (!delta) 21131da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 21141da177e4SLinus Torvalds for (j = 0; j < i; j++) 2115a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); 21161da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 21171da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 21181da177e4SLinus Torvalds struct ip_sf_list *psf; 2119165094afSNikolay Borisov struct net *net = dev_net(pmc->interface->dev); 2120cfcabdccSStephen Hemminger in_dev = pmc->interface; 21211da177e4SLinus Torvalds #endif 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds /* filter mode change */ 21241da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 21251da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 21261da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 21271da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 21281da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 21291da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 21301da177e4SLinus Torvalds 2131165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 21321da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 21331da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 21341da177e4SLinus Torvalds psf->sf_crcount = 0; 21351da177e4SLinus Torvalds igmp_ifc_event(in_dev); 21361da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 21371da177e4SLinus Torvalds igmp_ifc_event(in_dev); 21381da177e4SLinus Torvalds #endif 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 21411da177e4SLinus Torvalds return err; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 21451da177e4SLinus Torvalds { 21463580d04aSEric Dumazet struct ip_sf_list *tomb, *sources; 21471da177e4SLinus Torvalds 2148c38b7d32SWANG Cong spin_lock_bh(&pmc->lock); 2149c38b7d32SWANG Cong tomb = pmc->tomb; 21501da177e4SLinus Torvalds pmc->tomb = NULL; 2151c38b7d32SWANG Cong sources = pmc->sources; 21521da177e4SLinus Torvalds pmc->sources = NULL; 21531da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 2154de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 21551da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 2156c38b7d32SWANG Cong spin_unlock_bh(&pmc->lock); 2157c38b7d32SWANG Cong 21583580d04aSEric Dumazet ip_sf_list_clear_all(tomb); 21593580d04aSEric Dumazet ip_sf_list_clear_all(sources); 21601da177e4SLinus Torvalds } 21611da177e4SLinus Torvalds 216254ff9ef3SMarcelo Ricardo Leitner /* Join a multicast group 216354ff9ef3SMarcelo Ricardo Leitner */ 21646e2059b5SHangbin Liu static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr, 21656e2059b5SHangbin Liu unsigned int mode) 21661da177e4SLinus Torvalds { 21678f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 2168959d10f6SEric Dumazet struct ip_mc_socklist *iml, *i; 21691da177e4SLinus Torvalds struct in_device *in_dev; 21701da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2171877acedcSDaniel Lezcano struct net *net = sock_net(sk); 2172ca9b907dSDavid L Stevens int ifindex; 21731da177e4SLinus Torvalds int count = 0; 2174959d10f6SEric Dumazet int err; 2175959d10f6SEric Dumazet 2176959d10f6SEric Dumazet ASSERT_RTNL(); 21771da177e4SLinus Torvalds 2178f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21791da177e4SLinus Torvalds return -EINVAL; 21801da177e4SLinus Torvalds 2181877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 21821da177e4SLinus Torvalds 21831da177e4SLinus Torvalds if (!in_dev) { 21841da177e4SLinus Torvalds err = -ENODEV; 21851da177e4SLinus Torvalds goto done; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds err = -EADDRINUSE; 2189ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 21901d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 2191ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 2192ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 21931da177e4SLinus Torvalds goto done; 21941da177e4SLinus Torvalds count++; 21951da177e4SLinus Torvalds } 21961da177e4SLinus Torvalds err = -ENOBUFS; 2197815c5270SNikolay Borisov if (count >= net->ipv4.sysctl_igmp_max_memberships) 21981da177e4SLinus Torvalds goto done; 21998b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 220051456b29SIan Morris if (!iml) 2201ca9b907dSDavid L Stevens goto done; 2202ca9b907dSDavid L Stevens 22031da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 22041d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 22051da177e4SLinus Torvalds iml->sflist = NULL; 22066e2059b5SHangbin Liu iml->sfmode = mode; 2207cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 22086e2059b5SHangbin Liu __ip_mc_inc_group(in_dev, addr, mode); 22091da177e4SLinus Torvalds err = 0; 22101da177e4SLinus Torvalds done: 22111da177e4SLinus Torvalds return err; 22121da177e4SLinus Torvalds } 22136e2059b5SHangbin Liu 22146e2059b5SHangbin Liu /* Join ASM (Any-Source Multicast) group 22156e2059b5SHangbin Liu */ 22166e2059b5SHangbin Liu int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr) 22176e2059b5SHangbin Liu { 22186e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE); 22196e2059b5SHangbin Liu } 22204bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 22211da177e4SLinus Torvalds 22226e2059b5SHangbin Liu /* Join SSM (Source-Specific Multicast) group 22236e2059b5SHangbin Liu */ 22246e2059b5SHangbin Liu int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr, 22256e2059b5SHangbin Liu unsigned int mode) 22266e2059b5SHangbin Liu { 22276e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, mode); 22286e2059b5SHangbin Liu } 22296e2059b5SHangbin Liu 22301da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 22311da177e4SLinus Torvalds struct in_device *in_dev) 22321da177e4SLinus Torvalds { 22331d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 22341da177e4SLinus Torvalds int err; 22351da177e4SLinus Torvalds 223651456b29SIan Morris if (!psf) { 22371da177e4SLinus Torvalds /* any-source empty exclude case */ 22381da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 22391da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 2242c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 2243a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 2244c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2245c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); 22467519cce4SLai Jiangshan kfree_rcu(psf, rcu); 22471da177e4SLinus Torvalds return err; 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds 225054ff9ef3SMarcelo Ricardo Leitner int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 22511da177e4SLinus Torvalds { 22521da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22531d7138deSEric Dumazet struct ip_mc_socklist *iml; 22541d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 225584b42baeSDavid L Stevens struct in_device *in_dev; 2256877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22578f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 225884b42baeSDavid L Stevens u32 ifindex; 2259acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 22601da177e4SLinus Torvalds 2261959d10f6SEric Dumazet ASSERT_RTNL(); 2262959d10f6SEric Dumazet 2263877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 22644eba7bb1SAndrew Lunn if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) { 226552ad353aSdingtianhong ret = -ENODEV; 226652ad353aSdingtianhong goto out; 226752ad353aSdingtianhong } 226884b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 22691d7138deSEric Dumazet for (imlp = &inet->mc_list; 22701d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 22711d7138deSEric Dumazet imlp = &iml->next_rcu) { 2272acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 2273acd6e00bSDavid L Stevens continue; 2274acd6e00bSDavid L Stevens if (ifindex) { 2275acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 2276acd6e00bSDavid L Stevens continue; 2277acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 2278acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 2279acd6e00bSDavid L Stevens continue; 2280acd6e00bSDavid L Stevens 22811da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 22821da177e4SLinus Torvalds 22831d7138deSEric Dumazet *imlp = iml->next_rcu; 22841da177e4SLinus Torvalds 22854eba7bb1SAndrew Lunn if (in_dev) 228684b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 2287959d10f6SEric Dumazet 2288c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2289c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 229010d50e74SLai Jiangshan kfree_rcu(iml, rcu); 22911da177e4SLinus Torvalds return 0; 22921da177e4SLinus Torvalds } 229352ad353aSdingtianhong out: 2294959d10f6SEric Dumazet return ret; 2295959d10f6SEric Dumazet } 2296193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 22991da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 23001da177e4SLinus Torvalds { 23011da177e4SLinus Torvalds int err; 23021da177e4SLinus Torvalds struct ip_mreqn imr; 230363007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 23041da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 23051da177e4SLinus Torvalds struct in_device *in_dev = NULL; 23061da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23071da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2308877acedcSDaniel Lezcano struct net *net = sock_net(sk); 23098cdaaa15SDavid L Stevens int leavegroup = 0; 23101da177e4SLinus Torvalds int i, j, rv; 23111da177e4SLinus Torvalds 2312f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 23131da177e4SLinus Torvalds return -EINVAL; 23141da177e4SLinus Torvalds 231554ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 23181da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 23191da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2320877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds if (!in_dev) { 23231da177e4SLinus Torvalds err = -ENODEV; 23241da177e4SLinus Torvalds goto done; 23251da177e4SLinus Torvalds } 23261da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 23271da177e4SLinus Torvalds 23281d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 2329f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 2330f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 2331f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 23321da177e4SLinus Torvalds break; 23331da177e4SLinus Torvalds } 2334917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2335917f2f10SDavid L Stevens err = -EINVAL; 23361da177e4SLinus Torvalds goto done; 2337917f2f10SDavid L Stevens } 23381da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 23391da177e4SLinus Torvalds if (pmc->sflist) { 2340917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 2341917f2f10SDavid L Stevens err = -EINVAL; 23421da177e4SLinus Torvalds goto done; 2343917f2f10SDavid L Stevens } 23441da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 23451da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 23461da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 23471da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 23481da177e4SLinus Torvalds NULL, 0); 23491da177e4SLinus Torvalds pmc->sfmode = omode; 23501da177e4SLinus Torvalds } 23511da177e4SLinus Torvalds 23521d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 23531da177e4SLinus Torvalds if (!add) { 23541da177e4SLinus Torvalds if (!psl) 2355917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 23561da177e4SLinus Torvalds rv = !0; 23571da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 23581da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 235963007727SAl Viro sizeof(__be32)); 23601da177e4SLinus Torvalds if (rv == 0) 23611da177e4SLinus Torvalds break; 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds if (rv) /* source not found */ 2364917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 23651da177e4SLinus Torvalds 23668cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 23678cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 23688cdaaa15SDavid L Stevens leavegroup = 1; 23698cdaaa15SDavid L Stevens goto done; 23708cdaaa15SDavid L Stevens } 23718cdaaa15SDavid L Stevens 23721da177e4SLinus Torvalds /* update the interface filter */ 23731da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 23741da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds for (j = i+1; j < psl->sl_count; j++) 23771da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 23781da177e4SLinus Torvalds psl->sl_count--; 23791da177e4SLinus Torvalds err = 0; 23801da177e4SLinus Torvalds goto done; 23811da177e4SLinus Torvalds } 23821da177e4SLinus Torvalds /* else, add a new source to the filter */ 23831da177e4SLinus Torvalds 2384166b6b2dSNikolay Borisov if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) { 23851da177e4SLinus Torvalds err = -ENOBUFS; 23861da177e4SLinus Torvalds goto done; 23871da177e4SLinus Torvalds } 23881da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 23891da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 23901da177e4SLinus Torvalds int count = IP_SFBLOCK; 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds if (psl) 23931da177e4SLinus Torvalds count += psl->sl_max; 23948b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 23951da177e4SLinus Torvalds if (!newpsl) { 23961da177e4SLinus Torvalds err = -ENOBUFS; 23971da177e4SLinus Torvalds goto done; 23981da177e4SLinus Torvalds } 23991da177e4SLinus Torvalds newpsl->sl_max = count; 24001da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 24011da177e4SLinus Torvalds if (psl) { 24021da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) 24031da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2404c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2405c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 24067519cce4SLai Jiangshan kfree_rcu(psl, rcu); 24071da177e4SLinus Torvalds } 2408cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2409c85bb41eSFlavio Leitner psl = newpsl; 24101da177e4SLinus Torvalds } 24111da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 24121da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 24131da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 241463007727SAl Viro sizeof(__be32)); 24151da177e4SLinus Torvalds if (rv == 0) 24161da177e4SLinus Torvalds break; 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 24191da177e4SLinus Torvalds goto done; 24201da177e4SLinus Torvalds for (j = psl->sl_count-1; j >= i; j--) 24211da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 24221da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 24231da177e4SLinus Torvalds psl->sl_count++; 24241da177e4SLinus Torvalds err = 0; 24251da177e4SLinus Torvalds /* update the interface list */ 24261da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 24271da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 24281da177e4SLinus Torvalds done: 24298cdaaa15SDavid L Stevens if (leavegroup) 243054ff9ef3SMarcelo Ricardo Leitner err = ip_mc_leave_group(sk, &imr); 24311da177e4SLinus Torvalds return err; 24321da177e4SLinus Torvalds } 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 24351da177e4SLinus Torvalds { 24369951f036SDavid L Stevens int err = 0; 24371da177e4SLinus Torvalds struct ip_mreqn imr; 243863007727SAl Viro __be32 addr = msf->imsf_multiaddr; 24391da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 24401da177e4SLinus Torvalds struct in_device *in_dev; 24411da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 24421da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2443877acedcSDaniel Lezcano struct net *net = sock_net(sk); 24449951f036SDavid L Stevens int leavegroup = 0; 24451da177e4SLinus Torvalds 2446f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 24471da177e4SLinus Torvalds return -EINVAL; 24481da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 24491da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 24501da177e4SLinus Torvalds return -EINVAL; 24511da177e4SLinus Torvalds 245254ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 24531da177e4SLinus Torvalds 24541da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 24551da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 24561da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2457877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds if (!in_dev) { 24601da177e4SLinus Torvalds err = -ENODEV; 24611da177e4SLinus Torvalds goto done; 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds 24649951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 24659951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 24669951f036SDavid L Stevens leavegroup = 1; 24679951f036SDavid L Stevens goto done; 24689951f036SDavid L Stevens } 24699951f036SDavid L Stevens 24701d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 24711da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 24721da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 24731da177e4SLinus Torvalds break; 24741da177e4SLinus Torvalds } 2475917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2476917f2f10SDavid L Stevens err = -EINVAL; 24771da177e4SLinus Torvalds goto done; 2478917f2f10SDavid L Stevens } 24791da177e4SLinus Torvalds if (msf->imsf_numsrc) { 24808b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 24818b3a7005SKris Katterjohn GFP_KERNEL); 24821da177e4SLinus Torvalds if (!newpsl) { 24831da177e4SLinus Torvalds err = -ENOBUFS; 24841da177e4SLinus Torvalds goto done; 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 24871da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 24881da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 24891da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 24901da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 24911da177e4SLinus Torvalds if (err) { 24921da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 24931da177e4SLinus Torvalds goto done; 24941da177e4SLinus Torvalds } 24958713dbf0SYan Zheng } else { 24961da177e4SLinus Torvalds newpsl = NULL; 24978713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 24988713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 24998713dbf0SYan Zheng } 25001d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 25011da177e4SLinus Torvalds if (psl) { 25021da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 25031da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2504c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2505c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 25067519cce4SLai Jiangshan kfree_rcu(psl, rcu); 25071da177e4SLinus Torvalds } else 25081da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 25091da177e4SLinus Torvalds 0, NULL, 0); 2510cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 25111da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2512917f2f10SDavid L Stevens err = 0; 25131da177e4SLinus Torvalds done: 25149951f036SDavid L Stevens if (leavegroup) 25159951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 25161da177e4SLinus Torvalds return err; 25171da177e4SLinus Torvalds } 25181da177e4SLinus Torvalds 25191da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 25201da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 25211da177e4SLinus Torvalds { 25221da177e4SLinus Torvalds int err, len, count, copycount; 25231da177e4SLinus Torvalds struct ip_mreqn imr; 252463007727SAl Viro __be32 addr = msf->imsf_multiaddr; 25251da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25261da177e4SLinus Torvalds struct in_device *in_dev; 25271da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25281da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2529877acedcSDaniel Lezcano struct net *net = sock_net(sk); 25301da177e4SLinus Torvalds 253187e9f031SWANG Cong ASSERT_RTNL(); 253287e9f031SWANG Cong 2533f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 25341da177e4SLinus Torvalds return -EINVAL; 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 25371da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 25381da177e4SLinus Torvalds imr.imr_ifindex = 0; 2539877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 25401da177e4SLinus Torvalds 25411da177e4SLinus Torvalds if (!in_dev) { 25421da177e4SLinus Torvalds err = -ENODEV; 25431da177e4SLinus Torvalds goto done; 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 25461da177e4SLinus Torvalds 25471d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 25481da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 25491da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 25501da177e4SLinus Torvalds break; 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 25531da177e4SLinus Torvalds goto done; 25541da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 25551d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 25561da177e4SLinus Torvalds if (!psl) { 25571da177e4SLinus Torvalds len = 0; 25581da177e4SLinus Torvalds count = 0; 25591da177e4SLinus Torvalds } else { 25601da177e4SLinus Torvalds count = psl->sl_count; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 25631da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 25641da177e4SLinus Torvalds msf->imsf_numsrc = count; 25651da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 25661da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 25671da177e4SLinus Torvalds return -EFAULT; 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds if (len && 25701da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 25711da177e4SLinus Torvalds return -EFAULT; 25721da177e4SLinus Torvalds return 0; 25731da177e4SLinus Torvalds done: 25741da177e4SLinus Torvalds return err; 25751da177e4SLinus Torvalds } 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 25781da177e4SLinus Torvalds struct group_filter __user *optval, int __user *optlen) 25791da177e4SLinus Torvalds { 25801da177e4SLinus Torvalds int err, i, count, copycount; 25811da177e4SLinus Torvalds struct sockaddr_in *psin; 258263007727SAl Viro __be32 addr; 25831da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25841da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25851da177e4SLinus Torvalds struct ip_sf_socklist *psl; 25861da177e4SLinus Torvalds 258787e9f031SWANG Cong ASSERT_RTNL(); 258887e9f031SWANG Cong 25891da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 25901da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 25911da177e4SLinus Torvalds return -EINVAL; 25921da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2593f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 25941da177e4SLinus Torvalds return -EINVAL; 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 25971da177e4SLinus Torvalds 25981d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 25991da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 26001da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 26011da177e4SLinus Torvalds break; 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 26041da177e4SLinus Torvalds goto done; 26051da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 26061d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 26071da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 26081da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 26091da177e4SLinus Torvalds gsf->gf_numsrc = count; 26101da177e4SLinus Torvalds if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 26111da177e4SLinus Torvalds copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 26121da177e4SLinus Torvalds return -EFAULT; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds for (i = 0; i < copycount; i++) { 26151da177e4SLinus Torvalds struct sockaddr_storage ss; 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 26181da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 26191da177e4SLinus Torvalds psin->sin_family = AF_INET; 26201da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 26211da177e4SLinus Torvalds if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 26221da177e4SLinus Torvalds return -EFAULT; 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds return 0; 26251da177e4SLinus Torvalds done: 26261da177e4SLinus Torvalds return err; 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds 26291da177e4SLinus Torvalds /* 26301da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 26311da177e4SLinus Torvalds */ 263260d9b031SDavid Ahern int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, 263360d9b031SDavid Ahern int dif, int sdif) 26341da177e4SLinus Torvalds { 26351da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 26361da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 26371da177e4SLinus Torvalds struct ip_sf_socklist *psl; 26381da177e4SLinus Torvalds int i; 2639c85bb41eSFlavio Leitner int ret; 26401da177e4SLinus Torvalds 2641c85bb41eSFlavio Leitner ret = 1; 2642f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2643c85bb41eSFlavio Leitner goto out; 26441da177e4SLinus Torvalds 2645c85bb41eSFlavio Leitner rcu_read_lock(); 26461d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 26471da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 264860d9b031SDavid Ahern (pmc->multi.imr_ifindex == dif || 264960d9b031SDavid Ahern (sdif && pmc->multi.imr_ifindex == sdif))) 26501da177e4SLinus Torvalds break; 26511da177e4SLinus Torvalds } 2652c85bb41eSFlavio Leitner ret = inet->mc_all; 26531da177e4SLinus Torvalds if (!pmc) 2654c85bb41eSFlavio Leitner goto unlock; 26551d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2656c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 26571da177e4SLinus Torvalds if (!psl) 2658c85bb41eSFlavio Leitner goto unlock; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 26611da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 26621da177e4SLinus Torvalds break; 26631da177e4SLinus Torvalds } 2664c85bb41eSFlavio Leitner ret = 0; 26651da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2666c85bb41eSFlavio Leitner goto unlock; 26671da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2668c85bb41eSFlavio Leitner goto unlock; 2669c85bb41eSFlavio Leitner ret = 1; 2670c85bb41eSFlavio Leitner unlock: 2671c85bb41eSFlavio Leitner rcu_read_unlock(); 2672c85bb41eSFlavio Leitner out: 2673c85bb41eSFlavio Leitner return ret; 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds 26761da177e4SLinus Torvalds /* 26771da177e4SLinus Torvalds * A socket is closing. 26781da177e4SLinus Torvalds */ 26791da177e4SLinus Torvalds 26801da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 26811da177e4SLinus Torvalds { 26821da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 26831da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2684877acedcSDaniel Lezcano struct net *net = sock_net(sk); 26851da177e4SLinus Torvalds 268651456b29SIan Morris if (!inet->mc_list) 26871da177e4SLinus Torvalds return; 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds rtnl_lock(); 26901d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 26911da177e4SLinus Torvalds struct in_device *in_dev; 26921da177e4SLinus Torvalds 26931d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2694877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 26951da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 269600db4124SIan Morris if (in_dev) 26971da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2698c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2699c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 270010d50e74SLai Jiangshan kfree_rcu(iml, rcu); 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds rtnl_unlock(); 27031da177e4SLinus Torvalds } 27041da177e4SLinus Torvalds 2705dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 27062094acbbSAlexander Duyck int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto) 27071da177e4SLinus Torvalds { 27081da177e4SLinus Torvalds struct ip_mc_list *im; 2709e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 27101da177e4SLinus Torvalds struct ip_sf_list *psf; 27111da177e4SLinus Torvalds int rv = 0; 27121da177e4SLinus Torvalds 2713e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash); 2714e9897071SEric Dumazet if (mc_hash) { 2715c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); 2716e9897071SEric Dumazet 2717e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]); 2718e9897071SEric Dumazet im != NULL; 2719e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) { 2720e9897071SEric Dumazet if (im->multiaddr == mc_addr) 2721e9897071SEric Dumazet break; 2722e9897071SEric Dumazet } 2723e9897071SEric Dumazet } else { 27241d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 27251da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 27261da177e4SLinus Torvalds break; 27271da177e4SLinus Torvalds } 2728e9897071SEric Dumazet } 27291da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 27301da177e4SLinus Torvalds rv = 1; 27311da177e4SLinus Torvalds } else if (im) { 27321da177e4SLinus Torvalds if (src_addr) { 27331da177e4SLinus Torvalds for (psf = im->sources; psf; psf = psf->sf_next) { 27341da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 27351da177e4SLinus Torvalds break; 27361da177e4SLinus Torvalds } 27371da177e4SLinus Torvalds if (psf) 27381da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 27391da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 27401da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 27411da177e4SLinus Torvalds else 27421da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 27431da177e4SLinus Torvalds } else 27441da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds return rv; 27471da177e4SLinus Torvalds } 27481da177e4SLinus Torvalds 27491da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 27501da177e4SLinus Torvalds struct igmp_mc_iter_state { 27517091e728SAlexey Dobriyan struct seq_net_private p; 27521da177e4SLinus Torvalds struct net_device *dev; 27531da177e4SLinus Torvalds struct in_device *in_dev; 27541da177e4SLinus Torvalds }; 27551da177e4SLinus Torvalds 27561da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 27591da177e4SLinus Torvalds { 27607091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 27611da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 27621da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27631da177e4SLinus Torvalds 27647562f876SPavel Emelianov state->in_dev = NULL; 276561fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 27661da177e4SLinus Torvalds struct in_device *in_dev; 27676baff150SEric Dumazet 27686baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 27691da177e4SLinus Torvalds if (!in_dev) 27701da177e4SLinus Torvalds continue; 27711d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 27721da177e4SLinus Torvalds if (im) { 27731da177e4SLinus Torvalds state->in_dev = in_dev; 27741da177e4SLinus Torvalds break; 27751da177e4SLinus Torvalds } 27761da177e4SLinus Torvalds } 27771da177e4SLinus Torvalds return im; 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 27811da177e4SLinus Torvalds { 27821da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27836baff150SEric Dumazet 27841d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 27851d7138deSEric Dumazet while (!im) { 27866baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 27871da177e4SLinus Torvalds if (!state->dev) { 27881da177e4SLinus Torvalds state->in_dev = NULL; 27891da177e4SLinus Torvalds break; 27901da177e4SLinus Torvalds } 27916baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 27921da177e4SLinus Torvalds if (!state->in_dev) 27931da177e4SLinus Torvalds continue; 27941d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds return im; 27971da177e4SLinus Torvalds } 27981da177e4SLinus Torvalds 27991da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 28001da177e4SLinus Torvalds { 28011da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 28021da177e4SLinus Torvalds if (im) 28031da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 28041da177e4SLinus Torvalds --pos; 28051da177e4SLinus Torvalds return pos ? NULL : im; 28061da177e4SLinus Torvalds } 28071da177e4SLinus Torvalds 28081da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 280961fbab77Sstephen hemminger __acquires(rcu) 28101da177e4SLinus Torvalds { 281161fbab77Sstephen hemminger rcu_read_lock(); 28121da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 28131da177e4SLinus Torvalds } 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 28161da177e4SLinus Torvalds { 28171da177e4SLinus Torvalds struct ip_mc_list *im; 28181da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 28191da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 28201da177e4SLinus Torvalds else 28211da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 28221da177e4SLinus Torvalds ++*pos; 28231da177e4SLinus Torvalds return im; 28241da177e4SLinus Torvalds } 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 282761fbab77Sstephen hemminger __releases(rcu) 28281da177e4SLinus Torvalds { 28291da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 28301d7138deSEric Dumazet 28311da177e4SLinus Torvalds state->in_dev = NULL; 28321da177e4SLinus Torvalds state->dev = NULL; 283361fbab77Sstephen hemminger rcu_read_unlock(); 28341da177e4SLinus Torvalds } 28351da177e4SLinus Torvalds 28361da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 28371da177e4SLinus Torvalds { 28381da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 28391da177e4SLinus Torvalds seq_puts(seq, 28401da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 28411da177e4SLinus Torvalds else { 28421da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 28431da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 28441da177e4SLinus Torvalds char *querier; 2845a399a805SEric Dumazet long delta; 2846a399a805SEric Dumazet 28471da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 28481da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 28491da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 28501da177e4SLinus Torvalds "V3"; 28511da177e4SLinus Torvalds #else 28521da177e4SLinus Torvalds querier = "NONE"; 28531da177e4SLinus Torvalds #endif 28541da177e4SLinus Torvalds 2855e6b68883SAndreea-Cristina Bernat if (rcu_access_pointer(state->in_dev->mc_list) == im) { 28561da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2857b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 28581da177e4SLinus Torvalds } 28591da177e4SLinus Torvalds 2860a399a805SEric Dumazet delta = im->timer.expires - jiffies; 28611da177e4SLinus Torvalds seq_printf(seq, 2862338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 28631da177e4SLinus Torvalds im->multiaddr, im->users, 2864a399a805SEric Dumazet im->tm_running, 2865a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 28661da177e4SLinus Torvalds im->reporter); 28671da177e4SLinus Torvalds } 28681da177e4SLinus Torvalds return 0; 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 2871f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 28721da177e4SLinus Torvalds .start = igmp_mc_seq_start, 28731da177e4SLinus Torvalds .next = igmp_mc_seq_next, 28741da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 28751da177e4SLinus Torvalds .show = igmp_mc_seq_show, 28761da177e4SLinus Torvalds }; 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds struct igmp_mcf_iter_state { 28797091e728SAlexey Dobriyan struct seq_net_private p; 28801da177e4SLinus Torvalds struct net_device *dev; 28811da177e4SLinus Torvalds struct in_device *idev; 28821da177e4SLinus Torvalds struct ip_mc_list *im; 28831da177e4SLinus Torvalds }; 28841da177e4SLinus Torvalds 28851da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 28861da177e4SLinus Torvalds 28871da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 28881da177e4SLinus Torvalds { 28897091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 28901da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 28911da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 28921da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 28931da177e4SLinus Torvalds 28947562f876SPavel Emelianov state->idev = NULL; 28957562f876SPavel Emelianov state->im = NULL; 289661fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 28971da177e4SLinus Torvalds struct in_device *idev; 28986baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 289951456b29SIan Morris if (unlikely(!idev)) 29001da177e4SLinus Torvalds continue; 29011d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 290200db4124SIan Morris if (likely(im)) { 29031da177e4SLinus Torvalds spin_lock_bh(&im->lock); 29041da177e4SLinus Torvalds psf = im->sources; 290500db4124SIan Morris if (likely(psf)) { 29061da177e4SLinus Torvalds state->im = im; 29071da177e4SLinus Torvalds state->idev = idev; 29081da177e4SLinus Torvalds break; 29091da177e4SLinus Torvalds } 29101da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds } 29131da177e4SLinus Torvalds return psf; 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 29171da177e4SLinus Torvalds { 29181da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds psf = psf->sf_next; 29211da177e4SLinus Torvalds while (!psf) { 29221da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 29231da177e4SLinus Torvalds state->im = state->im->next; 29241da177e4SLinus Torvalds while (!state->im) { 29256baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 29261da177e4SLinus Torvalds if (!state->dev) { 29271da177e4SLinus Torvalds state->idev = NULL; 29281da177e4SLinus Torvalds goto out; 29291da177e4SLinus Torvalds } 29306baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 29311da177e4SLinus Torvalds if (!state->idev) 29321da177e4SLinus Torvalds continue; 29331d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds if (!state->im) 29361da177e4SLinus Torvalds break; 29371da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 29381da177e4SLinus Torvalds psf = state->im->sources; 29391da177e4SLinus Torvalds } 29401da177e4SLinus Torvalds out: 29411da177e4SLinus Torvalds return psf; 29421da177e4SLinus Torvalds } 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 29451da177e4SLinus Torvalds { 29461da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 29471da177e4SLinus Torvalds if (psf) 29481da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 29491da177e4SLinus Torvalds --pos; 29501da177e4SLinus Torvalds return pos ? NULL : psf; 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 295461fbab77Sstephen hemminger __acquires(rcu) 29551da177e4SLinus Torvalds { 295661fbab77Sstephen hemminger rcu_read_lock(); 29571da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 29581da177e4SLinus Torvalds } 29591da177e4SLinus Torvalds 29601da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 29611da177e4SLinus Torvalds { 29621da177e4SLinus Torvalds struct ip_sf_list *psf; 29631da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 29641da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 29651da177e4SLinus Torvalds else 29661da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 29671da177e4SLinus Torvalds ++*pos; 29681da177e4SLinus Torvalds return psf; 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 297261fbab77Sstephen hemminger __releases(rcu) 29731da177e4SLinus Torvalds { 29741da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 297500db4124SIan Morris if (likely(state->im)) { 29761da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 29771da177e4SLinus Torvalds state->im = NULL; 29781da177e4SLinus Torvalds } 29791da177e4SLinus Torvalds state->idev = NULL; 29801da177e4SLinus Torvalds state->dev = NULL; 298161fbab77Sstephen hemminger rcu_read_unlock(); 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 29841da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 29851da177e4SLinus Torvalds { 29861da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 29871da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 29881da177e4SLinus Torvalds 29891da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 29901744bea1SJoe Perches seq_puts(seq, "Idx Device MCA SRC INC EXC\n"); 29911da177e4SLinus Torvalds } else { 29921da177e4SLinus Torvalds seq_printf(seq, 29931da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 29941da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 29951da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 29961da177e4SLinus Torvalds ntohl(state->im->multiaddr), 29971da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 29981da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 29991da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 30001da177e4SLinus Torvalds } 30011da177e4SLinus Torvalds return 0; 30021da177e4SLinus Torvalds } 30031da177e4SLinus Torvalds 3004f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 30051da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 30061da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 30071da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 30081da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 30091da177e4SLinus Torvalds }; 30101da177e4SLinus Torvalds 30112c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 30127091e728SAlexey Dobriyan { 30137091e728SAlexey Dobriyan struct proc_dir_entry *pde; 301493a714d6SMadhu Challa int err; 30157091e728SAlexey Dobriyan 3016c3506372SChristoph Hellwig pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops, 3017c3506372SChristoph Hellwig sizeof(struct igmp_mc_iter_state)); 30187091e728SAlexey Dobriyan if (!pde) 30197091e728SAlexey Dobriyan goto out_igmp; 3020c3506372SChristoph Hellwig pde = proc_create_net("mcfilter", 0444, net->proc_net, 3021c3506372SChristoph Hellwig &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state)); 30227091e728SAlexey Dobriyan if (!pde) 30237091e728SAlexey Dobriyan goto out_mcfilter; 302493a714d6SMadhu Challa err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET, 302593a714d6SMadhu Challa SOCK_DGRAM, 0, net); 302693a714d6SMadhu Challa if (err < 0) { 302793a714d6SMadhu Challa pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n", 302893a714d6SMadhu Challa err); 302993a714d6SMadhu Challa goto out_sock; 303093a714d6SMadhu Challa } 303193a714d6SMadhu Challa 30327091e728SAlexey Dobriyan return 0; 30337091e728SAlexey Dobriyan 303493a714d6SMadhu Challa out_sock: 303593a714d6SMadhu Challa remove_proc_entry("mcfilter", net->proc_net); 30367091e728SAlexey Dobriyan out_mcfilter: 3037ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 30387091e728SAlexey Dobriyan out_igmp: 30397091e728SAlexey Dobriyan return -ENOMEM; 30407091e728SAlexey Dobriyan } 30417091e728SAlexey Dobriyan 30422c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 30437091e728SAlexey Dobriyan { 3044ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net); 3045ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 304693a714d6SMadhu Challa inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk); 30477091e728SAlexey Dobriyan } 30487091e728SAlexey Dobriyan 30497091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 30507091e728SAlexey Dobriyan .init = igmp_net_init, 30517091e728SAlexey Dobriyan .exit = igmp_net_exit, 30521da177e4SLinus Torvalds }; 305372c1d3bdSWANG Cong #endif 30541da177e4SLinus Torvalds 30554aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this, 30564aa5dee4SJiri Pirko unsigned long event, void *ptr) 30574aa5dee4SJiri Pirko { 30584aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 30594aa5dee4SJiri Pirko struct in_device *in_dev; 30604aa5dee4SJiri Pirko 30614aa5dee4SJiri Pirko switch (event) { 30624aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 30634aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev); 30644aa5dee4SJiri Pirko if (in_dev) 30654aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev); 30664aa5dee4SJiri Pirko break; 30674aa5dee4SJiri Pirko default: 30684aa5dee4SJiri Pirko break; 30694aa5dee4SJiri Pirko } 30704aa5dee4SJiri Pirko return NOTIFY_DONE; 30714aa5dee4SJiri Pirko } 30724aa5dee4SJiri Pirko 30734aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = { 30744aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event, 30754aa5dee4SJiri Pirko }; 30764aa5dee4SJiri Pirko 307772c1d3bdSWANG Cong int __init igmp_mc_init(void) 30781da177e4SLinus Torvalds { 307972c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS) 30804aa5dee4SJiri Pirko int err; 30814aa5dee4SJiri Pirko 30824aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops); 30834aa5dee4SJiri Pirko if (err) 30844aa5dee4SJiri Pirko return err; 30854aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier); 30864aa5dee4SJiri Pirko if (err) 30874aa5dee4SJiri Pirko goto reg_notif_fail; 30884aa5dee4SJiri Pirko return 0; 30894aa5dee4SJiri Pirko 30904aa5dee4SJiri Pirko reg_notif_fail: 30914aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops); 30924aa5dee4SJiri Pirko return err; 309372c1d3bdSWANG Cong #else 309472c1d3bdSWANG Cong return register_netdevice_notifier(&igmp_notifier); 30951da177e4SLinus Torvalds #endif 309672c1d3bdSWANG Cong } 3097