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_V1_ROUTER_PRESENT_TIMEOUT (400*HZ) 115436f7c20SFabian Frederick #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ) 116436f7c20SFabian Frederick #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ) 117436f7c20SFabian Frederick #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ) 118436f7c20SFabian Frederick #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ) 119436f7c20SFabian Frederick #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds 122436f7c20SFabian Frederick #define IGMP_INITIAL_REPORT_DELAY (1) 1231da177e4SLinus Torvalds 124436f7c20SFabian Frederick /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs! 1251da177e4SLinus Torvalds * IGMP specs require to report membership immediately after 1261da177e4SLinus Torvalds * joining a group, but we delay the first report by a 1271da177e4SLinus Torvalds * small interval. It seems more natural and still does not 1281da177e4SLinus Torvalds * contradict to specs provided this delay is small enough. 1291da177e4SLinus Torvalds */ 1301da177e4SLinus Torvalds 13142f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \ 132c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ 13342f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ 1341da177e4SLinus Torvalds ((in_dev)->mr_v1_seen && \ 1351da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v1_seen))) 13642f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \ 137c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ 13842f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ 1391da177e4SLinus Torvalds ((in_dev)->mr_v2_seen && \ 1401da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v2_seen))) 1411da177e4SLinus Torvalds 142cab70040SWilliam Manley static int unsolicited_report_interval(struct in_device *in_dev) 143cab70040SWilliam Manley { 1442690048cSWilliam Manley int interval_ms, interval_jiffies; 1452690048cSWilliam Manley 146cab70040SWilliam Manley if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 1472690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1482690048cSWilliam Manley in_dev, 1492690048cSWilliam Manley IGMPV2_UNSOLICITED_REPORT_INTERVAL); 150cab70040SWilliam Manley else /* v3 */ 1512690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1522690048cSWilliam Manley in_dev, 1532690048cSWilliam Manley IGMPV3_UNSOLICITED_REPORT_INTERVAL); 1542690048cSWilliam Manley 1552690048cSWilliam Manley interval_jiffies = msecs_to_jiffies(interval_ms); 1562690048cSWilliam Manley 1572690048cSWilliam Manley /* _timer functions can't handle a delay of 0 jiffies so ensure 1582690048cSWilliam Manley * we always return a positive value. 1592690048cSWilliam Manley */ 1602690048cSWilliam Manley if (interval_jiffies <= 0) 1612690048cSWilliam Manley interval_jiffies = 1; 1622690048cSWilliam Manley return interval_jiffies; 163cab70040SWilliam Manley } 164cab70040SWilliam Manley 1651da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im); 16624803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im); 1671da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev); 1681da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc); 1691da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc); 1701da177e4SLinus Torvalds #endif 1711da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc); 1728f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 1738f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta); 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im) 1761da177e4SLinus Torvalds { 1778851ab52SReshetova, Elena if (refcount_dec_and_test(&im->refcnt)) { 1781da177e4SLinus Torvalds in_dev_put(im->interface); 17942ea299dSLai Jiangshan kfree_rcu(im, rcu); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 183d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc) \ 184d9aa9380SDavid S. Miller for (pmc = rcu_dereference(in_dev->mc_list); \ 185d9aa9380SDavid S. Miller pmc != NULL; \ 186d9aa9380SDavid S. Miller pmc = rcu_dereference(pmc->next_rcu)) 187d9aa9380SDavid S. Miller 188d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc) \ 189d9aa9380SDavid S. Miller for (pmc = rtnl_dereference(in_dev->mc_list); \ 190d9aa9380SDavid S. Miller pmc != NULL; \ 191d9aa9380SDavid S. Miller pmc = rtnl_dereference(pmc->next_rcu)) 192d9aa9380SDavid S. Miller 1931da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* 1961da177e4SLinus Torvalds * Timer management 1971da177e4SLinus Torvalds */ 1981da177e4SLinus Torvalds 1991d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2021da177e4SLinus Torvalds if (del_timer(&im->timer)) 2038851ab52SReshetova, Elena refcount_dec(&im->refcnt); 2041da177e4SLinus Torvalds im->tm_running = 0; 2051da177e4SLinus Torvalds im->reporter = 0; 2061da177e4SLinus Torvalds im->unsolicit_count = 0; 2071da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* It must be called with locked im->lock */ 2111da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay) 2121da177e4SLinus Torvalds { 21363862b5bSAruna-Hewapathirane int tv = prandom_u32() % max_delay; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds im->tm_running = 1; 2161da177e4SLinus Torvalds if (!mod_timer(&im->timer, jiffies+tv+2)) 2178851ab52SReshetova, Elena refcount_inc(&im->refcnt); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev) 2211da177e4SLinus Torvalds { 22263862b5bSAruna-Hewapathirane int tv = prandom_u32() % in_dev->mr_maxdelay; 2237ababb78SMichal Tesar unsigned long exp = jiffies + tv + 2; 2247ababb78SMichal Tesar 2257ababb78SMichal Tesar if (in_dev->mr_gq_running && 2267ababb78SMichal Tesar time_after_eq(exp, (in_dev->mr_gq_timer).expires)) 2277ababb78SMichal Tesar return; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds in_dev->mr_gq_running = 1; 2307ababb78SMichal Tesar if (!mod_timer(&in_dev->mr_gq_timer, exp)) 2311da177e4SLinus Torvalds in_dev_hold(in_dev); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) 2351da177e4SLinus Torvalds { 23663862b5bSAruna-Hewapathirane int tv = prandom_u32() % delay; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) 2391da177e4SLinus Torvalds in_dev_hold(in_dev); 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) 2431da177e4SLinus Torvalds { 2441da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2451da177e4SLinus Torvalds im->unsolicit_count = 0; 2461da177e4SLinus Torvalds if (del_timer(&im->timer)) { 2471da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) { 2481da177e4SLinus Torvalds add_timer(&im->timer); 2491da177e4SLinus Torvalds im->tm_running = 1; 2501da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2511da177e4SLinus Torvalds return; 2521da177e4SLinus Torvalds } 2538851ab52SReshetova, Elena refcount_dec(&im->refcnt); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds igmp_start_timer(im, max_delay); 2561da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /* 2611da177e4SLinus Torvalds * Send an IGMP report. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, 2681da177e4SLinus Torvalds int gdeleted, int sdeleted) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds switch (type) { 2711da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE: 2721da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE: 2731da177e4SLinus Torvalds if (gdeleted || sdeleted) 2741da177e4SLinus Torvalds return 0; 275ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) { 276ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE) 277ad12583fSDavid L Stevens return 1; 278ad12583fSDavid L Stevens /* don't include if this source is excluded 279ad12583fSDavid L Stevens * in all filters 280ad12583fSDavid L Stevens */ 281ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE]) 282ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE; 283ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] == 284ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]; 285ad12583fSDavid L Stevens } 286ad12583fSDavid L Stevens return 0; 2871da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE: 2881da177e4SLinus Torvalds if (gdeleted || sdeleted) 2891da177e4SLinus Torvalds return 0; 2901da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0; 2911da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE: 2921da177e4SLinus Torvalds if (gdeleted || sdeleted) 2931da177e4SLinus Torvalds return 0; 2941da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 || 2951da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE]) 2961da177e4SLinus Torvalds return 0; 2971da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] == 2981da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]; 2991da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES: 3001da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount) 3011da177e4SLinus Torvalds return 0; 3021da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; 3031da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES: 3041da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) 3051da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted); 3061da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds return 0; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static int 3121da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds struct ip_sf_list *psf; 3151da177e4SLinus Torvalds int scount = 0; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 3181da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 3191da177e4SLinus Torvalds continue; 3201da177e4SLinus Torvalds scount++; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds return scount; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 325a46182b0SKevin Cernekee /* source address selection per RFC 3376 section 4.2.13 */ 326a46182b0SKevin Cernekee static __be32 igmpv3_get_srcaddr(struct net_device *dev, 327a46182b0SKevin Cernekee const struct flowi4 *fl4) 328a46182b0SKevin Cernekee { 329a46182b0SKevin Cernekee struct in_device *in_dev = __in_dev_get_rcu(dev); 330a46182b0SKevin Cernekee 331a46182b0SKevin Cernekee if (!in_dev) 332a46182b0SKevin Cernekee return htonl(INADDR_ANY); 333a46182b0SKevin Cernekee 334a46182b0SKevin Cernekee for_ifa(in_dev) { 335ad23b750SFelix Fietkau if (fl4->saddr == ifa->ifa_local) 336a46182b0SKevin Cernekee return fl4->saddr; 337a46182b0SKevin Cernekee } endfor_ifa(in_dev); 338a46182b0SKevin Cernekee 339a46182b0SKevin Cernekee return htonl(INADDR_ANY); 340a46182b0SKevin Cernekee } 341a46182b0SKevin Cernekee 3424c672e4bSDaniel Borkmann static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu) 3431da177e4SLinus Torvalds { 3441da177e4SLinus Torvalds struct sk_buff *skb; 3451da177e4SLinus Torvalds struct rtable *rt; 3461da177e4SLinus Torvalds struct iphdr *pip; 3471da177e4SLinus Torvalds struct igmpv3_report *pig; 348877acedcSDaniel Lezcano struct net *net = dev_net(dev); 34931e4543dSDavid S. Miller struct flowi4 fl4; 35066088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 35166088243SHerbert Xu int tlen = dev->needed_tailroom; 3524c672e4bSDaniel Borkmann unsigned int size = mtu; 3531da177e4SLinus Torvalds 35457e1ab6eSEric Dumazet while (1) { 35566088243SHerbert Xu skb = alloc_skb(size + hlen + tlen, 35657e1ab6eSEric Dumazet GFP_ATOMIC | __GFP_NOWARN); 35757e1ab6eSEric Dumazet if (skb) 35857e1ab6eSEric Dumazet break; 35957e1ab6eSEric Dumazet size >>= 1; 36057e1ab6eSEric Dumazet if (size < 256) 3611da177e4SLinus Torvalds return NULL; 36257e1ab6eSEric Dumazet } 3639d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 3641da177e4SLinus Torvalds 36531e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, 36678fbfd8aSDavid S. Miller 0, 0, 36778fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 368b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 3691da177e4SLinus Torvalds kfree_skb(skb); 3701da177e4SLinus Torvalds return NULL; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 373d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 3741da177e4SLinus Torvalds skb->dev = dev; 3751da177e4SLinus Torvalds 37666088243SHerbert Xu skb_reserve(skb, hlen); 3771837b2e2SBenjamin Poirier skb_tailroom_reserve(skb, mtu, tlen); 3781da177e4SLinus Torvalds 3797e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 380eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb); 3817e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds pip->version = 4; 3841da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2; 3851da177e4SLinus Torvalds pip->tos = 0xc0; 3861da177e4SLinus Torvalds pip->frag_off = htons(IP_DF); 3871da177e4SLinus Torvalds pip->ttl = 1; 388492f64ceSDavid S. Miller pip->daddr = fl4.daddr; 389e7aadb27SEric Dumazet 390e7aadb27SEric Dumazet rcu_read_lock(); 391a46182b0SKevin Cernekee pip->saddr = igmpv3_get_srcaddr(dev, &fl4); 392e7aadb27SEric Dumazet rcu_read_unlock(); 393e7aadb27SEric Dumazet 3941da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP; 3951da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */ 396b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 3971da177e4SLinus Torvalds ((u8 *)&pip[1])[0] = IPOPT_RA; 3981da177e4SLinus Torvalds ((u8 *)&pip[1])[1] = 4; 3991da177e4SLinus Torvalds ((u8 *)&pip[1])[2] = 0; 4001da177e4SLinus Torvalds ((u8 *)&pip[1])[3] = 0; 4011da177e4SLinus Torvalds 402b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; 403d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig)); 404d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb); 4051da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; 4061da177e4SLinus Torvalds pig->resv1 = 0; 4071da177e4SLinus Torvalds pig->csum = 0; 4081da177e4SLinus Torvalds pig->resv2 = 0; 4091da177e4SLinus Torvalds pig->ngrec = 0; 4101da177e4SLinus Torvalds return skb; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb) 4141da177e4SLinus Torvalds { 415d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb); 416f7c0c2aeSSimon Horman const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb); 4171da177e4SLinus Torvalds 418d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); 4191da177e4SLinus Torvalds 42033224b16SEric W. Biederman return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, 429b5476022SEric Dumazet int type, struct igmpv3_grec **ppgr, unsigned int mtu) 4301da177e4SLinus Torvalds { 4311da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4321da177e4SLinus Torvalds struct igmpv3_report *pih; 4331da177e4SLinus Torvalds struct igmpv3_grec *pgr; 4341da177e4SLinus Torvalds 435b5476022SEric Dumazet if (!skb) { 436b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 4371da177e4SLinus Torvalds if (!skb) 4381da177e4SLinus Torvalds return NULL; 439b5476022SEric Dumazet } 4404df864c1SJohannes Berg pgr = skb_put(skb, sizeof(struct igmpv3_grec)); 4411da177e4SLinus Torvalds pgr->grec_type = type; 4421da177e4SLinus Torvalds pgr->grec_auxwords = 0; 4431da177e4SLinus Torvalds pgr->grec_nsrcs = 0; 4441da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr; 445d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb); 4461da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1); 4471da177e4SLinus Torvalds *ppgr = pgr; 4481da177e4SLinus Torvalds return skb; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 4514c672e4bSDaniel Borkmann #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0) 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, 4541da177e4SLinus Torvalds int type, int gdeleted, int sdeleted) 4551da177e4SLinus Torvalds { 4561da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 45787a8a2aeSNikolay Borisov struct net *net = dev_net(dev); 4581da177e4SLinus Torvalds struct igmpv3_report *pih; 4591da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL; 4601da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; 461ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate; 462b5476022SEric Dumazet unsigned int mtu; 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 4651da177e4SLinus Torvalds return skb; 46687a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 467df2cf4a7SPhilip Downey return skb; 4681da177e4SLinus Torvalds 469b5476022SEric Dumazet mtu = READ_ONCE(dev->mtu); 470b5476022SEric Dumazet if (mtu < IPV4_MIN_MTU) 471b5476022SEric Dumazet return skb; 472b5476022SEric Dumazet 4731da177e4SLinus Torvalds isquery = type == IGMPV3_MODE_IS_INCLUDE || 4741da177e4SLinus Torvalds type == IGMPV3_MODE_IS_EXCLUDE; 4751da177e4SLinus Torvalds truncate = type == IGMPV3_MODE_IS_EXCLUDE || 4761da177e4SLinus Torvalds type == IGMPV3_CHANGE_TO_EXCLUDE; 4771da177e4SLinus Torvalds 478ad12583fSDavid L Stevens stotal = scount = 0; 479ad12583fSDavid L Stevens 4801da177e4SLinus Torvalds psf_list = sdeleted ? &pmc->tomb : &pmc->sources; 4811da177e4SLinus Torvalds 482ad12583fSDavid L Stevens if (!*psf_list) 483ad12583fSDavid L Stevens goto empty_source; 484ad12583fSDavid L Stevens 485d9edf9e2SArnaldo Carvalho de Melo pih = skb ? igmpv3_report_hdr(skb) : NULL; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* EX and TO_EX get a fresh packet, if needed */ 4881da177e4SLinus Torvalds if (truncate) { 4891da177e4SLinus Torvalds if (pih && pih->ngrec && 4901da177e4SLinus Torvalds AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { 4911da177e4SLinus Torvalds if (skb) 4921da177e4SLinus Torvalds igmpv3_sendpack(skb); 493b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds first = 1; 4971da177e4SLinus Torvalds psf_prev = NULL; 4981da177e4SLinus Torvalds for (psf = *psf_list; psf; psf = psf_next) { 499ea4d9e72SAl Viro __be32 *psrc; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds psf_next = psf->sf_next; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { 5041da177e4SLinus Torvalds psf_prev = psf; 5051da177e4SLinus Torvalds continue; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 508a052517aSHangbin Liu /* Based on RFC3376 5.1. Should not send source-list change 509a052517aSHangbin Liu * records when there is a filter mode change. 510a052517aSHangbin Liu */ 511a052517aSHangbin Liu if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) || 512a052517aSHangbin Liu (!gdeleted && pmc->crcount)) && 513a052517aSHangbin Liu (type == IGMPV3_ALLOW_NEW_SOURCES || 514a052517aSHangbin Liu type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) 515a052517aSHangbin Liu goto decrease_sf_crcount; 516a052517aSHangbin Liu 5171da177e4SLinus Torvalds /* clear marks on query responses */ 5181da177e4SLinus Torvalds if (isquery) 5191da177e4SLinus Torvalds psf->sf_gsresp = 0; 5201da177e4SLinus Torvalds 52163007727SAl Viro if (AVAILABLE(skb) < sizeof(__be32) + 5221da177e4SLinus Torvalds first*sizeof(struct igmpv3_grec)) { 5231da177e4SLinus Torvalds if (truncate && !first) 5241da177e4SLinus Torvalds break; /* truncate these */ 5251da177e4SLinus Torvalds if (pgr) 5261da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5271da177e4SLinus Torvalds if (skb) 5281da177e4SLinus Torvalds igmpv3_sendpack(skb); 529b5476022SEric Dumazet skb = igmpv3_newpack(dev, mtu); 5301da177e4SLinus Torvalds first = 1; 5311da177e4SLinus Torvalds scount = 0; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds if (first) { 534b5476022SEric Dumazet skb = add_grhead(skb, pmc, type, &pgr, mtu); 5351da177e4SLinus Torvalds first = 0; 5361da177e4SLinus Torvalds } 537cc63f70bSAlexey Dobriyan if (!skb) 538cc63f70bSAlexey Dobriyan return NULL; 5394df864c1SJohannes Berg psrc = skb_put(skb, sizeof(__be32)); 5401da177e4SLinus Torvalds *psrc = psf->sf_inaddr; 541ad12583fSDavid L Stevens scount++; stotal++; 5421da177e4SLinus Torvalds if ((type == IGMPV3_ALLOW_NEW_SOURCES || 5431da177e4SLinus Torvalds type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { 544a052517aSHangbin Liu decrease_sf_crcount: 5451da177e4SLinus Torvalds psf->sf_crcount--; 5461da177e4SLinus Torvalds if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { 5471da177e4SLinus Torvalds if (psf_prev) 5481da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5491da177e4SLinus Torvalds else 5501da177e4SLinus Torvalds *psf_list = psf->sf_next; 5511da177e4SLinus Torvalds kfree(psf); 5521da177e4SLinus Torvalds continue; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds psf_prev = psf; 5561da177e4SLinus Torvalds } 557ad12583fSDavid L Stevens 558ad12583fSDavid L Stevens empty_source: 559ad12583fSDavid L Stevens if (!stotal) { 560ad12583fSDavid L Stevens if (type == IGMPV3_ALLOW_NEW_SOURCES || 561ad12583fSDavid L Stevens type == IGMPV3_BLOCK_OLD_SOURCES) 562ad12583fSDavid L Stevens return skb; 563ad12583fSDavid L Stevens if (pmc->crcount || isquery) { 564ad12583fSDavid L Stevens /* make sure we have room for group header */ 565ad12583fSDavid L Stevens if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) { 566ad12583fSDavid L Stevens igmpv3_sendpack(skb); 567ad12583fSDavid L Stevens skb = NULL; /* add_grhead will get a new one */ 568ad12583fSDavid L Stevens } 569b5476022SEric Dumazet skb = add_grhead(skb, pmc, type, &pgr, mtu); 570ad12583fSDavid L Stevens } 571ad12583fSDavid L Stevens } 5721da177e4SLinus Torvalds if (pgr) 5731da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds if (isquery) 5761da177e4SLinus Torvalds pmc->gsquery = 0; /* clear query state on report */ 5771da177e4SLinus Torvalds return skb; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds struct sk_buff *skb = NULL; 58387a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 5841da177e4SLinus Torvalds int type; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds if (!pmc) { 5871d7138deSEric Dumazet rcu_read_lock(); 5881d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 5891da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 5901da177e4SLinus Torvalds continue; 591df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(pmc->multiaddr) && 59287a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 593df2cf4a7SPhilip Downey continue; 5941da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5951da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5961da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5971da177e4SLinus Torvalds else 5981da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5991da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6001da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6011da177e4SLinus Torvalds } 6021d7138deSEric Dumazet rcu_read_unlock(); 6031da177e4SLinus Torvalds } else { 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 } 6121da177e4SLinus Torvalds if (!skb) 6131da177e4SLinus Torvalds return 0; 6141da177e4SLinus Torvalds return igmpv3_sendpack(skb); 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /* 6181da177e4SLinus Torvalds * remove zero-count source records from a source filter list 6191da177e4SLinus Torvalds */ 6201da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds psf_prev = NULL; 6251da177e4SLinus Torvalds for (psf = *ppsf; psf; psf = psf_next) { 6261da177e4SLinus Torvalds psf_next = psf->sf_next; 6271da177e4SLinus Torvalds if (psf->sf_crcount == 0) { 6281da177e4SLinus Torvalds if (psf_prev) 6291da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 6301da177e4SLinus Torvalds else 6311da177e4SLinus Torvalds *ppsf = psf->sf_next; 6321da177e4SLinus Torvalds kfree(psf); 6331da177e4SLinus Torvalds } else 6341da177e4SLinus Torvalds psf_prev = psf; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev) 6391da177e4SLinus Torvalds { 6401da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next; 6411da177e4SLinus Torvalds struct sk_buff *skb = NULL; 6421da177e4SLinus Torvalds int type, dtype; 6431da177e4SLinus Torvalds 6441d7138deSEric Dumazet rcu_read_lock(); 6451da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* deleted MCA's */ 6481da177e4SLinus Torvalds pmc_prev = NULL; 6491da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) { 6501da177e4SLinus Torvalds pmc_next = pmc->next; 6511da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 6521da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6531da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6541da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6551da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1); 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds if (pmc->crcount) { 6581da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) { 6591da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6601da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6611da177e4SLinus Torvalds } 662ad12583fSDavid L Stevens pmc->crcount--; 6631da177e4SLinus Torvalds if (pmc->crcount == 0) { 6641da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb); 6651da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources); 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { 6691da177e4SLinus Torvalds if (pmc_prev) 6701da177e4SLinus Torvalds pmc_prev->next = pmc_next; 6711da177e4SLinus Torvalds else 6721da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next; 6731da177e4SLinus Torvalds in_dev_put(pmc->interface); 6741da177e4SLinus Torvalds kfree(pmc); 6751da177e4SLinus Torvalds } else 6761da177e4SLinus Torvalds pmc_prev = pmc; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds /* change recs */ 6811d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 6821da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6831da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 6841da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6851da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES; 6861da177e4SLinus Torvalds } else { 6871da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES; 6881da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6911da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds /* filter mode changes */ 6941da177e4SLinus Torvalds if (pmc->crcount) { 6951da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) 6961da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE; 6971da177e4SLinus Torvalds else 6981da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6991da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 700ad12583fSDavid L Stevens pmc->crcount--; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 7031da177e4SLinus Torvalds } 7041d7138deSEric Dumazet rcu_read_unlock(); 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds if (!skb) 7071da177e4SLinus Torvalds return; 7081da177e4SLinus Torvalds (void) igmpv3_sendpack(skb); 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, 7121da177e4SLinus Torvalds int type) 7131da177e4SLinus Torvalds { 7141da177e4SLinus Torvalds struct sk_buff *skb; 7151da177e4SLinus Torvalds struct iphdr *iph; 7161da177e4SLinus Torvalds struct igmphdr *ih; 7171da177e4SLinus Torvalds struct rtable *rt; 7181da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 719877acedcSDaniel Lezcano struct net *net = dev_net(dev); 72063007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0; 72131e4543dSDavid S. Miller struct flowi4 fl4; 72263007727SAl Viro __be32 dst; 72366088243SHerbert Xu int hlen, tlen; 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) 7261da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc); 727df2cf4a7SPhilip Downey 72887a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 729df2cf4a7SPhilip Downey return 0; 730df2cf4a7SPhilip Downey 731df2cf4a7SPhilip Downey if (type == IGMP_HOST_LEAVE_MESSAGE) 7321da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER; 7331da177e4SLinus Torvalds else 7341da177e4SLinus Torvalds dst = group; 7351da177e4SLinus Torvalds 73631e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, 73778fbfd8aSDavid S. Miller 0, 0, 73878fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 739b23dd4feSDavid S. Miller if (IS_ERR(rt)) 7401da177e4SLinus Torvalds return -1; 74178fbfd8aSDavid S. Miller 74266088243SHerbert Xu hlen = LL_RESERVED_SPACE(dev); 74366088243SHerbert Xu tlen = dev->needed_tailroom; 74466088243SHerbert Xu skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); 74551456b29SIan Morris if (!skb) { 7461da177e4SLinus Torvalds ip_rt_put(rt); 7471da177e4SLinus Torvalds return -1; 7481da177e4SLinus Torvalds } 7499d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 7501da177e4SLinus Torvalds 751d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 7521da177e4SLinus Torvalds 75366088243SHerbert Xu skb_reserve(skb, hlen); 7541da177e4SLinus Torvalds 7557e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 756eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 7577e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds iph->version = 4; 7601da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2; 7611da177e4SLinus Torvalds iph->tos = 0xc0; 7621da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 7631da177e4SLinus Torvalds iph->ttl = 1; 7641da177e4SLinus Torvalds iph->daddr = dst; 765492f64ceSDavid S. Miller iph->saddr = fl4.saddr; 7661da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP; 767b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 7681da177e4SLinus Torvalds ((u8 *)&iph[1])[0] = IPOPT_RA; 7691da177e4SLinus Torvalds ((u8 *)&iph[1])[1] = 4; 7701da177e4SLinus Torvalds ((u8 *)&iph[1])[2] = 0; 7711da177e4SLinus Torvalds ((u8 *)&iph[1])[3] = 0; 7721da177e4SLinus Torvalds 7734df864c1SJohannes Berg ih = skb_put(skb, sizeof(struct igmphdr)); 7741da177e4SLinus Torvalds ih->type = type; 7751da177e4SLinus Torvalds ih->code = 0; 7761da177e4SLinus Torvalds ih->csum = 0; 7771da177e4SLinus Torvalds ih->group = group; 7781da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 7791da177e4SLinus Torvalds 78033224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb); 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 783e99e88a9SKees Cook static void igmp_gq_timer_expire(struct timer_list *t) 7841da177e4SLinus Torvalds { 785e99e88a9SKees Cook struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer); 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 7881da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL); 789e2401654SSalam Noureddine in_dev_put(in_dev); 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 792e99e88a9SKees Cook static void igmp_ifc_timer_expire(struct timer_list *t) 7931da177e4SLinus Torvalds { 794e99e88a9SKees Cook struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer); 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 7971da177e4SLinus Torvalds if (in_dev->mr_ifc_count) { 7981da177e4SLinus Torvalds in_dev->mr_ifc_count--; 799cab70040SWilliam Manley igmp_ifc_start_timer(in_dev, 800cab70040SWilliam Manley unsolicited_report_interval(in_dev)); 8011da177e4SLinus Torvalds } 802e2401654SSalam Noureddine in_dev_put(in_dev); 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 8061da177e4SLinus Torvalds { 807165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 8081da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 8091da177e4SLinus Torvalds return; 810165094afSNikolay Borisov in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 8111da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds 815e99e88a9SKees Cook static void igmp_timer_expire(struct timer_list *t) 8161da177e4SLinus Torvalds { 817e99e88a9SKees Cook struct ip_mc_list *im = from_timer(im, t, timer); 8181da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds spin_lock(&im->lock); 8211da177e4SLinus Torvalds im->tm_running = 0; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds if (im->unsolicit_count) { 8241da177e4SLinus Torvalds im->unsolicit_count--; 825cab70040SWilliam Manley igmp_start_timer(im, unsolicited_report_interval(in_dev)); 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds im->reporter = 1; 8281da177e4SLinus Torvalds spin_unlock(&im->lock); 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 8311da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 8321da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 8331da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 8341da177e4SLinus Torvalds else 8351da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds ip_ma_put(im); 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 840ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 841ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 8421da177e4SLinus Torvalds { 8431da177e4SLinus Torvalds struct ip_sf_list *psf; 8441da177e4SLinus Torvalds int i, scount; 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds scount = 0; 8471da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 8481da177e4SLinus Torvalds if (scount == nsrcs) 8491da177e4SLinus Torvalds break; 850ad12583fSDavid L Stevens for (i = 0; i < nsrcs; i++) { 851ad12583fSDavid L Stevens /* skip inactive filters */ 852e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] || 853ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 854ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 855ce713ee5SRongQing.Li break; 856ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 857ad12583fSDavid L Stevens scount++; 858ad12583fSDavid L Stevens break; 859ad12583fSDavid L Stevens } 860ad12583fSDavid L Stevens } 861ad12583fSDavid L Stevens } 862ad12583fSDavid L Stevens pmc->gsquery = 0; 863ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 864ad12583fSDavid L Stevens return 0; 865ad12583fSDavid L Stevens return 1; 866ad12583fSDavid L Stevens } 867ad12583fSDavid L Stevens 868ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 869ad12583fSDavid L Stevens { 870ad12583fSDavid L Stevens struct ip_sf_list *psf; 871ad12583fSDavid L Stevens int i, scount; 872ad12583fSDavid L Stevens 873ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 874ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 875ad12583fSDavid L Stevens 876ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 877ad12583fSDavid L Stevens scount = 0; 878ad12583fSDavid L Stevens for (psf = pmc->sources; psf; psf = psf->sf_next) { 879ad12583fSDavid L Stevens if (scount == nsrcs) 880ad12583fSDavid L Stevens break; 8811da177e4SLinus Torvalds for (i = 0; i < nsrcs; i++) 8821da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 8831da177e4SLinus Torvalds psf->sf_gsresp = 1; 8841da177e4SLinus Torvalds scount++; 8851da177e4SLinus Torvalds break; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds } 888ad12583fSDavid L Stevens if (!scount) { 889ad12583fSDavid L Stevens pmc->gsquery = 0; 890ad12583fSDavid L Stevens return 0; 891ad12583fSDavid L Stevens } 892ad12583fSDavid L Stevens pmc->gsquery = 1; 893ad12583fSDavid L Stevens return 1; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 896d679c532SEric Dumazet /* return true if packet was dropped */ 897d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group) 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds struct ip_mc_list *im; 90087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 905d679c532SEric Dumazet return false; 90687a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 907df2cf4a7SPhilip Downey return false; 9081da177e4SLinus Torvalds 9091d7138deSEric Dumazet rcu_read_lock(); 9101d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 9111da177e4SLinus Torvalds if (im->multiaddr == group) { 9121da177e4SLinus Torvalds igmp_stop_timer(im); 9131da177e4SLinus Torvalds break; 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds } 9161d7138deSEric Dumazet rcu_read_unlock(); 917d679c532SEric Dumazet return false; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 920d679c532SEric Dumazet /* return true if packet was dropped */ 921d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 9221da177e4SLinus Torvalds int len) 9231da177e4SLinus Torvalds { 924d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 925d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 9261da177e4SLinus Torvalds struct ip_mc_list *im; 92763007727SAl Viro __be32 group = ih->group; 9281da177e4SLinus Torvalds int max_delay; 9291da177e4SLinus Torvalds int mark = 0; 93087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds 9335b7c8406SDavid Stevens if (len == 8) { 9341da177e4SLinus Torvalds if (ih->code == 0) { 9351da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 9361da177e4SLinus Torvalds 937436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9381da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 939436f7c20SFabian Frederick IGMP_V1_ROUTER_PRESENT_TIMEOUT; 9401da177e4SLinus Torvalds group = 0; 9411da177e4SLinus Torvalds } else { 9421da177e4SLinus Torvalds /* v2 router present */ 9431da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 9441da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 945436f7c20SFabian Frederick IGMP_V2_ROUTER_PRESENT_TIMEOUT; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds /* cancel the interface change timer */ 9481da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 9491da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 9501da177e4SLinus Torvalds __in_dev_put(in_dev); 9511da177e4SLinus Torvalds /* clear deleted report items */ 9521da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 9531da177e4SLinus Torvalds } else if (len < 12) { 954d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */ 9555b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) { 9565b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */ 957436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9585b7c8406SDavid Stevens group = 0; 9595b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) { 9605b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present; 9615b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here. 9625b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a 9635b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely 9645b7c8406SDavid Stevens * to be intended in a v3 query. 9655b7c8406SDavid Stevens */ 9665b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 967a8c1f65cSBen Hutchings if (!max_delay) 968a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */ 9691da177e4SLinus Torvalds } else { /* v3 */ 9701da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 971d679c532SEric Dumazet return true; 9721da177e4SLinus Torvalds 973d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9741da177e4SLinus Torvalds if (ih3->nsrcs) { 9751da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 97663007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 977d679c532SEric Dumazet return true; 978d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 9821da177e4SLinus Torvalds if (!max_delay) 9831da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 9841da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 9851da177e4SLinus Torvalds if (ih3->qrv) 9861da177e4SLinus Torvalds in_dev->mr_qrv = ih3->qrv; 9871da177e4SLinus Torvalds if (!group) { /* general query */ 9881da177e4SLinus Torvalds if (ih3->nsrcs) 989b47bd8d2SDaniel Borkmann return true; /* no sources allowed */ 9901da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 991d679c532SEric Dumazet return false; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 9941da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds /* 9981da177e4SLinus Torvalds * - Start the timers in all of our membership records 9991da177e4SLinus Torvalds * that the query applies to for the interface on 10001da177e4SLinus Torvalds * which the query arrived excl. those that belong 10011da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 10021da177e4SLinus Torvalds * - For timers already running check if they need to 10031da177e4SLinus Torvalds * be reset. 10041da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 10051da177e4SLinus Torvalds * delay possible 10061da177e4SLinus Torvalds */ 10071d7138deSEric Dumazet rcu_read_lock(); 10081d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 1009ad12583fSDavid L Stevens int changed; 1010ad12583fSDavid L Stevens 10111da177e4SLinus Torvalds if (group && group != im->multiaddr) 10121da177e4SLinus Torvalds continue; 10131da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 10141da177e4SLinus Torvalds continue; 1015df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 101687a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1017df2cf4a7SPhilip Downey continue; 10181da177e4SLinus Torvalds spin_lock_bh(&im->lock); 10191da177e4SLinus Torvalds if (im->tm_running) 10201da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 10211da177e4SLinus Torvalds else 10221da177e4SLinus Torvalds im->gsquery = mark; 1023ad12583fSDavid L Stevens changed = !im->gsquery || 10241da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 10251da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 1026ad12583fSDavid L Stevens if (changed) 10271da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 10281da177e4SLinus Torvalds } 10291d7138deSEric Dumazet rcu_read_unlock(); 1030d679c532SEric Dumazet return false; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10339a57a9d2SEric Dumazet /* called in rcu_read_lock() section */ 10341da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 10351da177e4SLinus Torvalds { 10361da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 10371da177e4SLinus Torvalds struct igmphdr *ih; 1038c7b725beSDavid Ahern struct net_device *dev = skb->dev; 1039c7b725beSDavid Ahern struct in_device *in_dev; 10401da177e4SLinus Torvalds int len = skb->len; 1041d679c532SEric Dumazet bool dropped = true; 10421da177e4SLinus Torvalds 1043c7b725beSDavid Ahern if (netif_is_l3_master(dev)) { 1044c7b725beSDavid Ahern dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif); 1045c7b725beSDavid Ahern if (!dev) 1046c7b725beSDavid Ahern goto drop; 1047c7b725beSDavid Ahern } 1048c7b725beSDavid Ahern 1049c7b725beSDavid Ahern in_dev = __in_dev_get_rcu(dev); 105051456b29SIan Morris if (!in_dev) 1051cd557bc1SDenis V. Lunev goto drop; 10521da177e4SLinus Torvalds 1053fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 10549a57a9d2SEric Dumazet goto drop; 1055fb286bb2SHerbert Xu 1056de08dc1aSTom Herbert if (skb_checksum_simple_validate(skb)) 10579a57a9d2SEric Dumazet goto drop; 10581da177e4SLinus Torvalds 1059d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 10601da177e4SLinus Torvalds switch (ih->type) { 10611da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 1062d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len); 10631da177e4SLinus Torvalds break; 10641da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 10651da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 10661da177e4SLinus Torvalds /* Is it our report looped back? */ 1067c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) 10681da177e4SLinus Torvalds break; 106924c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 107024c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 107124c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 1072d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group); 10731da177e4SLinus Torvalds break; 10741da177e4SLinus Torvalds case IGMP_PIM: 10751da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 10761da177e4SLinus Torvalds return pim_rcv_v1(skb); 10771da177e4SLinus Torvalds #endif 1078c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 10791da177e4SLinus Torvalds case IGMP_DVMRP: 10801da177e4SLinus Torvalds case IGMP_TRACE: 10811da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 10821da177e4SLinus Torvalds case IGMP_MTRACE: 10831da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 10841da177e4SLinus Torvalds break; 10851da177e4SLinus Torvalds default: 1086dd1c1853SLinus Torvalds break; 10871da177e4SLinus Torvalds } 1088fb286bb2SHerbert Xu 1089cd557bc1SDenis V. Lunev drop: 1090d679c532SEric Dumazet if (dropped) 10911da177e4SLinus Torvalds kfree_skb(skb); 1092d679c532SEric Dumazet else 1093d679c532SEric Dumazet consume_skb(skb); 10941da177e4SLinus Torvalds return 0; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds #endif 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* 11011da177e4SLinus Torvalds * Add a filter to a device 11021da177e4SLinus Torvalds */ 11031da177e4SLinus Torvalds 110463007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 11051da177e4SLinus Torvalds { 11061da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 11071da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 11101da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 1111b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode 11121da177e4SLinus Torvalds routine. Something sort of: 11131da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 11141da177e4SLinus Torvalds --ANK 11151da177e4SLinus Torvalds */ 11161da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 111722bedad3SJiri Pirko dev_mc_add(dev, buf); 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds /* 11211da177e4SLinus Torvalds * Remove a filter from a device 11221da177e4SLinus Torvalds */ 11231da177e4SLinus Torvalds 112463007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 11251da177e4SLinus Torvalds { 11261da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 11271da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 113022bedad3SJiri Pirko dev_mc_del(dev, buf); 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11341da177e4SLinus Torvalds /* 11351da177e4SLinus Torvalds * deleted ip_mc_list manipulation 11361da177e4SLinus Torvalds */ 11371da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im) 11381da177e4SLinus Torvalds { 11391da177e4SLinus Torvalds struct ip_mc_list *pmc; 1140165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 11431da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 11441da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 11451da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 11461da177e4SLinus Torvalds * non-deleted or query-response MCA's. 11471da177e4SLinus Torvalds */ 11480da974f4SPanagiotis Issaris pmc = kzalloc(sizeof(*pmc), GFP_KERNEL); 11491da177e4SLinus Torvalds if (!pmc) 11501da177e4SLinus Torvalds return; 1151b4846fc3SWANG Cong spin_lock_init(&pmc->lock); 11521da177e4SLinus Torvalds spin_lock_bh(&im->lock); 11531da177e4SLinus Torvalds pmc->interface = im->interface; 11541da177e4SLinus Torvalds in_dev_hold(in_dev); 11551da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 1156165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 11571da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 11581da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 11591da177e4SLinus Torvalds struct ip_sf_list *psf; 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds pmc->tomb = im->tomb; 11621da177e4SLinus Torvalds pmc->sources = im->sources; 11631da177e4SLinus Torvalds im->tomb = im->sources = NULL; 11641da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 11651da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11701da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 11711da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 11721da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds 117524803f38SHangbin Liu /* 117624803f38SHangbin Liu * restore ip_mc_list deleted records 117724803f38SHangbin Liu */ 117824803f38SHangbin Liu static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im) 11791da177e4SLinus Torvalds { 11801da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 118124803f38SHangbin Liu struct ip_sf_list *psf; 118224803f38SHangbin Liu struct net *net = dev_net(in_dev->dev); 118324803f38SHangbin Liu __be32 multiaddr = im->multiaddr; 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11861da177e4SLinus Torvalds pmc_prev = NULL; 11871da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) { 11881da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 11891da177e4SLinus Torvalds break; 11901da177e4SLinus Torvalds pmc_prev = pmc; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds if (pmc) { 11931da177e4SLinus Torvalds if (pmc_prev) 11941da177e4SLinus Torvalds pmc_prev->next = pmc->next; 11951da177e4SLinus Torvalds else 11961da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 119924803f38SHangbin Liu 120024803f38SHangbin Liu spin_lock_bh(&im->lock); 12011da177e4SLinus Torvalds if (pmc) { 120224803f38SHangbin Liu im->interface = pmc->interface; 120324803f38SHangbin Liu im->sfmode = pmc->sfmode; 120424803f38SHangbin Liu if (pmc->sfmode == MCAST_INCLUDE) { 120524803f38SHangbin Liu im->tomb = pmc->tomb; 120624803f38SHangbin Liu im->sources = pmc->sources; 120724803f38SHangbin Liu for (psf = im->sources; psf; psf = psf->sf_next) 1208*6e2059b5SHangbin Liu psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 1209*6e2059b5SHangbin Liu } else { 1210*6e2059b5SHangbin Liu im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds in_dev_put(pmc->interface); 12139c8bb163SHangbin Liu kfree(pmc); 12141da177e4SLinus Torvalds } 121524803f38SHangbin Liu spin_unlock_bh(&im->lock); 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 121824803f38SHangbin Liu /* 121924803f38SHangbin Liu * flush ip_mc_list deleted records 122024803f38SHangbin Liu */ 12211da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 12221da177e4SLinus Torvalds { 12231da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 12261da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 12271da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 12281da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 12311da177e4SLinus Torvalds nextpmc = pmc->next; 12321da177e4SLinus Torvalds ip_mc_clear_src(pmc); 12331da177e4SLinus Torvalds in_dev_put(pmc->interface); 12341da177e4SLinus Torvalds kfree(pmc); 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds /* clear dead sources, too */ 12371d7138deSEric Dumazet rcu_read_lock(); 12381d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 12391da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 12421da177e4SLinus Torvalds psf = pmc->tomb; 12431da177e4SLinus Torvalds pmc->tomb = NULL; 12441da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 12451da177e4SLinus Torvalds for (; psf; psf = psf_next) { 12461da177e4SLinus Torvalds psf_next = psf->sf_next; 12471da177e4SLinus Torvalds kfree(psf); 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds } 12501d7138deSEric Dumazet rcu_read_unlock(); 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds #endif 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im) 12551da177e4SLinus Torvalds { 12561da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 12571da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 125887a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 12591da177e4SLinus Torvalds int reporter; 12601da177e4SLinus Torvalds #endif 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds if (im->loaded) { 12631da177e4SLinus Torvalds im->loaded = 0; 12641da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12681da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 12691da177e4SLinus Torvalds return; 127087a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1271df2cf4a7SPhilip Downey return; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds reporter = im->reporter; 12741da177e4SLinus Torvalds igmp_stop_timer(im); 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds if (!in_dev->dead) { 12771da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 127824cf3af3SVeaceslav Falico return; 12791da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 12801da177e4SLinus Torvalds if (reporter) 12811da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 128224cf3af3SVeaceslav Falico return; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds /* IGMPv3 */ 12851da177e4SLinus Torvalds igmpv3_add_delrec(in_dev, im); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds igmp_ifc_event(in_dev); 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds #endif 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 1292*6e2059b5SHangbin Liu static void igmp_group_added(struct ip_mc_list *im, unsigned int mode) 12931da177e4SLinus Torvalds { 12941da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 1295dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 129687a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1297dcd87999SNikolay Borisov #endif 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds if (im->loaded == 0) { 13001da177e4SLinus Torvalds im->loaded = 1; 13011da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13051da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 13061da177e4SLinus Torvalds return; 130787a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1308df2cf4a7SPhilip Downey return; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds if (in_dev->dead) 13111da177e4SLinus Torvalds return; 13121da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 13131da177e4SLinus Torvalds spin_lock_bh(&im->lock); 1314436f7c20SFabian Frederick igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY); 13151da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 13161da177e4SLinus Torvalds return; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds /* else, v3 */ 13191da177e4SLinus Torvalds 1320*6e2059b5SHangbin Liu /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should 1321*6e2059b5SHangbin Liu * not send filter-mode change record as the mode should be from 1322*6e2059b5SHangbin Liu * IN() to IN(A). 1323*6e2059b5SHangbin Liu */ 1324*6e2059b5SHangbin Liu if (mode == MCAST_EXCLUDE) 1325165094afSNikolay Borisov im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 1326*6e2059b5SHangbin Liu 13271da177e4SLinus Torvalds igmp_ifc_event(in_dev); 13281da177e4SLinus Torvalds #endif 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds /* 13331da177e4SLinus Torvalds * Multicast list managers 13341da177e4SLinus Torvalds */ 13351da177e4SLinus Torvalds 1336e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im) 1337e9897071SEric Dumazet { 1338c70eba74SEric Dumazet return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); 1339e9897071SEric Dumazet } 1340e9897071SEric Dumazet 1341e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev, 1342e9897071SEric Dumazet struct ip_mc_list *im) 1343e9897071SEric Dumazet { 1344e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 1345e9897071SEric Dumazet u32 hash; 1346e9897071SEric Dumazet 1347e9897071SEric Dumazet mc_hash = rtnl_dereference(in_dev->mc_hash); 1348e9897071SEric Dumazet if (mc_hash) { 1349e9897071SEric Dumazet hash = ip_mc_hash(im); 1350c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1351e9897071SEric Dumazet rcu_assign_pointer(mc_hash[hash], im); 1352e9897071SEric Dumazet return; 1353e9897071SEric Dumazet } 1354e9897071SEric Dumazet 1355e9897071SEric Dumazet /* do not use a hash table for small number of items */ 1356e9897071SEric Dumazet if (in_dev->mc_count < 4) 1357e9897071SEric Dumazet return; 1358e9897071SEric Dumazet 1359e9897071SEric Dumazet mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG, 1360e9897071SEric Dumazet GFP_KERNEL); 1361e9897071SEric Dumazet if (!mc_hash) 1362e9897071SEric Dumazet return; 1363e9897071SEric Dumazet 1364e9897071SEric Dumazet for_each_pmc_rtnl(in_dev, im) { 1365e9897071SEric Dumazet hash = ip_mc_hash(im); 1366c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1367e9897071SEric Dumazet RCU_INIT_POINTER(mc_hash[hash], im); 1368e9897071SEric Dumazet } 1369e9897071SEric Dumazet 1370e9897071SEric Dumazet rcu_assign_pointer(in_dev->mc_hash, mc_hash); 1371e9897071SEric Dumazet } 1372e9897071SEric Dumazet 1373e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev, 1374e9897071SEric Dumazet struct ip_mc_list *im) 1375e9897071SEric Dumazet { 1376e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); 1377e9897071SEric Dumazet struct ip_mc_list *aux; 1378e9897071SEric Dumazet 1379e9897071SEric Dumazet if (!mc_hash) 1380e9897071SEric Dumazet return; 1381e9897071SEric Dumazet mc_hash += ip_mc_hash(im); 1382e9897071SEric Dumazet while ((aux = rtnl_dereference(*mc_hash)) != im) 1383e9897071SEric Dumazet mc_hash = &aux->next_hash; 1384e9897071SEric Dumazet *mc_hash = im->next_hash; 1385e9897071SEric Dumazet } 1386e9897071SEric Dumazet 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds /* 13891da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 13901da177e4SLinus Torvalds */ 1391*6e2059b5SHangbin Liu void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, unsigned int mode) 13921da177e4SLinus Torvalds { 13931da177e4SLinus Torvalds struct ip_mc_list *im; 1394dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 1395165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1396dcd87999SNikolay Borisov #endif 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds ASSERT_RTNL(); 13991da177e4SLinus Torvalds 14001d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) { 14011da177e4SLinus Torvalds if (im->multiaddr == addr) { 14021da177e4SLinus Torvalds im->users++; 1403*6e2059b5SHangbin Liu ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0); 14041da177e4SLinus Torvalds goto out; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 14081d7138deSEric Dumazet im = kzalloc(sizeof(*im), GFP_KERNEL); 14091da177e4SLinus Torvalds if (!im) 14101da177e4SLinus Torvalds goto out; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds im->users = 1; 14131da177e4SLinus Torvalds im->interface = in_dev; 14141da177e4SLinus Torvalds in_dev_hold(in_dev); 14151da177e4SLinus Torvalds im->multiaddr = addr; 14161da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 1417*6e2059b5SHangbin Liu im->sfmode = mode; 1418*6e2059b5SHangbin Liu im->sfcount[mode] = 1; 14198851ab52SReshetova, Elena refcount_set(&im->refcnt, 1); 14201da177e4SLinus Torvalds spin_lock_init(&im->lock); 14211da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1422e99e88a9SKees Cook timer_setup(&im->timer, igmp_timer_expire, 0); 1423165094afSNikolay Borisov im->unsolicit_count = net->ipv4.sysctl_igmp_qrv; 14241da177e4SLinus Torvalds #endif 14251d7138deSEric Dumazet 14261d7138deSEric Dumazet im->next_rcu = in_dev->mc_list; 1427b8bae41eSRami Rosen in_dev->mc_count++; 1428cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im); 14291d7138deSEric Dumazet 1430e9897071SEric Dumazet ip_mc_hash_add(in_dev, im); 1431e9897071SEric Dumazet 14321da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 143324803f38SHangbin Liu igmpv3_del_delrec(in_dev, im); 14341da177e4SLinus Torvalds #endif 1435*6e2059b5SHangbin Liu igmp_group_added(im, mode); 14361da177e4SLinus Torvalds if (!in_dev->dead) 14371da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 14381da177e4SLinus Torvalds out: 14391da177e4SLinus Torvalds return; 14401da177e4SLinus Torvalds } 1441*6e2059b5SHangbin Liu 1442*6e2059b5SHangbin Liu void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 1443*6e2059b5SHangbin Liu { 1444*6e2059b5SHangbin Liu __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE); 1445*6e2059b5SHangbin Liu } 14464bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group); 14471da177e4SLinus Torvalds 14489afd85c9SLinus Lüssing static int ip_mc_check_iphdr(struct sk_buff *skb) 14499afd85c9SLinus Lüssing { 14509afd85c9SLinus Lüssing const struct iphdr *iph; 14519afd85c9SLinus Lüssing unsigned int len; 14529afd85c9SLinus Lüssing unsigned int offset = skb_network_offset(skb) + sizeof(*iph); 14539afd85c9SLinus Lüssing 14549afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset)) 14559afd85c9SLinus Lüssing return -EINVAL; 14569afd85c9SLinus Lüssing 14579afd85c9SLinus Lüssing iph = ip_hdr(skb); 14589afd85c9SLinus Lüssing 14599afd85c9SLinus Lüssing if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph)) 14609afd85c9SLinus Lüssing return -EINVAL; 14619afd85c9SLinus Lüssing 14629afd85c9SLinus Lüssing offset += ip_hdrlen(skb) - sizeof(*iph); 14639afd85c9SLinus Lüssing 14649afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset)) 14659afd85c9SLinus Lüssing return -EINVAL; 14669afd85c9SLinus Lüssing 14679afd85c9SLinus Lüssing iph = ip_hdr(skb); 14689afd85c9SLinus Lüssing 14699afd85c9SLinus Lüssing if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 14709afd85c9SLinus Lüssing return -EINVAL; 14719afd85c9SLinus Lüssing 14729afd85c9SLinus Lüssing len = skb_network_offset(skb) + ntohs(iph->tot_len); 14739afd85c9SLinus Lüssing if (skb->len < len || len < offset) 14749afd85c9SLinus Lüssing return -EINVAL; 14759afd85c9SLinus Lüssing 14769afd85c9SLinus Lüssing skb_set_transport_header(skb, offset); 14779afd85c9SLinus Lüssing 14789afd85c9SLinus Lüssing return 0; 14799afd85c9SLinus Lüssing } 14809afd85c9SLinus Lüssing 14819afd85c9SLinus Lüssing static int ip_mc_check_igmp_reportv3(struct sk_buff *skb) 14829afd85c9SLinus Lüssing { 14839afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb); 14849afd85c9SLinus Lüssing 14859afd85c9SLinus Lüssing len += sizeof(struct igmpv3_report); 14869afd85c9SLinus Lüssing 14879afd85c9SLinus Lüssing return pskb_may_pull(skb, len) ? 0 : -EINVAL; 14889afd85c9SLinus Lüssing } 14899afd85c9SLinus Lüssing 14909afd85c9SLinus Lüssing static int ip_mc_check_igmp_query(struct sk_buff *skb) 14919afd85c9SLinus Lüssing { 14929afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb); 14939afd85c9SLinus Lüssing 14949afd85c9SLinus Lüssing len += sizeof(struct igmphdr); 14959afd85c9SLinus Lüssing if (skb->len < len) 14969afd85c9SLinus Lüssing return -EINVAL; 14979afd85c9SLinus Lüssing 14989afd85c9SLinus Lüssing /* IGMPv{1,2}? */ 14999afd85c9SLinus Lüssing if (skb->len != len) { 15009afd85c9SLinus Lüssing /* or IGMPv3? */ 15019afd85c9SLinus Lüssing len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr); 15029afd85c9SLinus Lüssing if (skb->len < len || !pskb_may_pull(skb, len)) 15039afd85c9SLinus Lüssing return -EINVAL; 15049afd85c9SLinus Lüssing } 15059afd85c9SLinus Lüssing 15069afd85c9SLinus Lüssing /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer 15079afd85c9SLinus Lüssing * all-systems destination addresses (224.0.0.1) for general queries 15089afd85c9SLinus Lüssing */ 15099afd85c9SLinus Lüssing if (!igmp_hdr(skb)->group && 15109afd85c9SLinus Lüssing ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP)) 15119afd85c9SLinus Lüssing return -EINVAL; 15129afd85c9SLinus Lüssing 15139afd85c9SLinus Lüssing return 0; 15149afd85c9SLinus Lüssing } 15159afd85c9SLinus Lüssing 15169afd85c9SLinus Lüssing static int ip_mc_check_igmp_msg(struct sk_buff *skb) 15179afd85c9SLinus Lüssing { 15189afd85c9SLinus Lüssing switch (igmp_hdr(skb)->type) { 15199afd85c9SLinus Lüssing case IGMP_HOST_LEAVE_MESSAGE: 15209afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_REPORT: 15219afd85c9SLinus Lüssing case IGMPV2_HOST_MEMBERSHIP_REPORT: 15229afd85c9SLinus Lüssing /* fall through */ 15239afd85c9SLinus Lüssing return 0; 15249afd85c9SLinus Lüssing case IGMPV3_HOST_MEMBERSHIP_REPORT: 15259afd85c9SLinus Lüssing return ip_mc_check_igmp_reportv3(skb); 15269afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_QUERY: 15279afd85c9SLinus Lüssing return ip_mc_check_igmp_query(skb); 15289afd85c9SLinus Lüssing default: 15299afd85c9SLinus Lüssing return -ENOMSG; 15309afd85c9SLinus Lüssing } 15319afd85c9SLinus Lüssing } 15329afd85c9SLinus Lüssing 15339afd85c9SLinus Lüssing static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb) 15349afd85c9SLinus Lüssing { 15359afd85c9SLinus Lüssing return skb_checksum_simple_validate(skb); 15369afd85c9SLinus Lüssing } 15379afd85c9SLinus Lüssing 15389afd85c9SLinus Lüssing static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed) 15399afd85c9SLinus Lüssing 15409afd85c9SLinus Lüssing { 15419afd85c9SLinus Lüssing struct sk_buff *skb_chk; 15429afd85c9SLinus Lüssing unsigned int transport_len; 15439afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr); 1544a516993fSLinus Lüssing int ret = -EINVAL; 15459afd85c9SLinus Lüssing 15469afd85c9SLinus Lüssing transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 15479afd85c9SLinus Lüssing 15489afd85c9SLinus Lüssing skb_chk = skb_checksum_trimmed(skb, transport_len, 15499afd85c9SLinus Lüssing ip_mc_validate_checksum); 15509afd85c9SLinus Lüssing if (!skb_chk) 1551a516993fSLinus Lüssing goto err; 15529afd85c9SLinus Lüssing 1553a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, len)) 1554a516993fSLinus Lüssing goto err; 15559afd85c9SLinus Lüssing 15569afd85c9SLinus Lüssing ret = ip_mc_check_igmp_msg(skb_chk); 1557a516993fSLinus Lüssing if (ret) 1558a516993fSLinus Lüssing goto err; 15599afd85c9SLinus Lüssing 15609afd85c9SLinus Lüssing if (skb_trimmed) 15619afd85c9SLinus Lüssing *skb_trimmed = skb_chk; 1562a516993fSLinus Lüssing /* free now unneeded clone */ 1563a516993fSLinus Lüssing else if (skb_chk != skb) 15649afd85c9SLinus Lüssing kfree_skb(skb_chk); 15659afd85c9SLinus Lüssing 1566a516993fSLinus Lüssing ret = 0; 1567a516993fSLinus Lüssing 1568a516993fSLinus Lüssing err: 1569a516993fSLinus Lüssing if (ret && skb_chk && skb_chk != skb) 1570a516993fSLinus Lüssing kfree_skb(skb_chk); 1571a516993fSLinus Lüssing 1572a516993fSLinus Lüssing return ret; 15739afd85c9SLinus Lüssing } 15749afd85c9SLinus Lüssing 15759afd85c9SLinus Lüssing /** 15769afd85c9SLinus Lüssing * ip_mc_check_igmp - checks whether this is a sane IGMP packet 15779afd85c9SLinus Lüssing * @skb: the skb to validate 15789afd85c9SLinus Lüssing * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional) 15799afd85c9SLinus Lüssing * 15809afd85c9SLinus Lüssing * Checks whether an IPv4 packet is a valid IGMP packet. If so sets 1581a516993fSLinus Lüssing * skb transport header accordingly and returns zero. 15829afd85c9SLinus Lüssing * 15839afd85c9SLinus Lüssing * -EINVAL: A broken packet was detected, i.e. it violates some internet 15849afd85c9SLinus Lüssing * standard 15859afd85c9SLinus Lüssing * -ENOMSG: IP header validation succeeded but it is not an IGMP packet. 15869afd85c9SLinus Lüssing * -ENOMEM: A memory allocation failure happened. 15879afd85c9SLinus Lüssing * 15889afd85c9SLinus Lüssing * Optionally, an skb pointer might be provided via skb_trimmed (or set it 15899afd85c9SLinus Lüssing * to NULL): After parsing an IGMP packet successfully it will point to 15909afd85c9SLinus Lüssing * an skb which has its tail aligned to the IP packet end. This might 15919afd85c9SLinus Lüssing * either be the originally provided skb or a trimmed, cloned version if 15929afd85c9SLinus Lüssing * the skb frame had data beyond the IP packet. A cloned skb allows us 15939afd85c9SLinus Lüssing * to leave the original skb and its full frame unchanged (which might be 15949afd85c9SLinus Lüssing * desirable for layer 2 frame jugglers). 15959afd85c9SLinus Lüssing * 1596a516993fSLinus Lüssing * Caller needs to set the skb network header and free any returned skb if it 1597a516993fSLinus Lüssing * differs from the provided skb. 15989afd85c9SLinus Lüssing */ 15999afd85c9SLinus Lüssing int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed) 16009afd85c9SLinus Lüssing { 16019afd85c9SLinus Lüssing int ret = ip_mc_check_iphdr(skb); 16029afd85c9SLinus Lüssing 16039afd85c9SLinus Lüssing if (ret < 0) 16049afd85c9SLinus Lüssing return ret; 16059afd85c9SLinus Lüssing 16069afd85c9SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP) 16079afd85c9SLinus Lüssing return -ENOMSG; 16089afd85c9SLinus Lüssing 16099afd85c9SLinus Lüssing return __ip_mc_check_igmp(skb, skb_trimmed); 16109afd85c9SLinus Lüssing } 16119afd85c9SLinus Lüssing EXPORT_SYMBOL(ip_mc_check_igmp); 16129afd85c9SLinus Lüssing 16131da177e4SLinus Torvalds /* 16144aa5dee4SJiri Pirko * Resend IGMP JOIN report; used by netdev notifier. 1615a816c7c7SJay Vosburgh */ 16164aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev) 1617a816c7c7SJay Vosburgh { 161808882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1619866f3b25SEric Dumazet struct ip_mc_list *im; 1620866f3b25SEric Dumazet int type; 162187a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1622a816c7c7SJay Vosburgh 16234aa5dee4SJiri Pirko ASSERT_RTNL(); 16244aa5dee4SJiri Pirko 16254aa5dee4SJiri Pirko for_each_pmc_rtnl(in_dev, im) { 1626a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1627866f3b25SEric Dumazet continue; 1628df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 162987a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1630df2cf4a7SPhilip Downey continue; 1631a816c7c7SJay Vosburgh 1632e12b4539SFlavio Leitner /* a failover is happening and switches 1633866f3b25SEric Dumazet * must be notified immediately 1634866f3b25SEric Dumazet */ 1635e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev)) 1636866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT; 1637e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev)) 1638866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT; 1639e12b4539SFlavio Leitner else 1640866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT; 1641866f3b25SEric Dumazet igmp_send_report(in_dev, im, type); 1642866f3b25SEric Dumazet } 1643a816c7c7SJay Vosburgh #endif 1644a816c7c7SJay Vosburgh } 1645a816c7c7SJay Vosburgh 1646a816c7c7SJay Vosburgh /* 16471da177e4SLinus Torvalds * A socket has left a multicast group on device dev 16481da177e4SLinus Torvalds */ 16491da177e4SLinus Torvalds 16508f935bbdSAl Viro void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) 16511da177e4SLinus Torvalds { 16521d7138deSEric Dumazet struct ip_mc_list *i; 16531d7138deSEric Dumazet struct ip_mc_list __rcu **ip; 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds ASSERT_RTNL(); 16561da177e4SLinus Torvalds 16571d7138deSEric Dumazet for (ip = &in_dev->mc_list; 16581d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL; 16591d7138deSEric Dumazet ip = &i->next_rcu) { 16601da177e4SLinus Torvalds if (i->multiaddr == addr) { 16611da177e4SLinus Torvalds if (--i->users == 0) { 1662e9897071SEric Dumazet ip_mc_hash_remove(in_dev, i); 16631d7138deSEric Dumazet *ip = i->next_rcu; 1664b8bae41eSRami Rosen in_dev->mc_count--; 16651da177e4SLinus Torvalds igmp_group_dropped(i); 166624cf3af3SVeaceslav Falico ip_mc_clear_src(i); 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds if (!in_dev->dead) 16691da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds ip_ma_put(i); 16721da177e4SLinus Torvalds return; 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds break; 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds } 16784bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_dec_group); 16791da177e4SLinus Torvalds 168075c78500SMoni Shoua /* Device changing type */ 168175c78500SMoni Shoua 168275c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 168375c78500SMoni Shoua { 16841d7138deSEric Dumazet struct ip_mc_list *pmc; 168575c78500SMoni Shoua 168675c78500SMoni Shoua ASSERT_RTNL(); 168775c78500SMoni Shoua 16881d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 16891d7138deSEric Dumazet igmp_group_dropped(pmc); 169075c78500SMoni Shoua } 169175c78500SMoni Shoua 169275c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 169375c78500SMoni Shoua { 16941d7138deSEric Dumazet struct ip_mc_list *pmc; 169575c78500SMoni Shoua 169675c78500SMoni Shoua ASSERT_RTNL(); 169775c78500SMoni Shoua 169824803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) { 169924803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 170024803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc); 170124803f38SHangbin Liu #endif 1702*6e2059b5SHangbin Liu igmp_group_added(pmc, pmc->sfmode); 170375c78500SMoni Shoua } 170424803f38SHangbin Liu } 170575c78500SMoni Shoua 17061da177e4SLinus Torvalds /* Device going down */ 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 17091da177e4SLinus Torvalds { 17101d7138deSEric Dumazet struct ip_mc_list *pmc; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds ASSERT_RTNL(); 17131da177e4SLinus Torvalds 17141d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 17151d7138deSEric Dumazet igmp_group_dropped(pmc); 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17181da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 17191da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 17201da177e4SLinus Torvalds __in_dev_put(in_dev); 17211da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 17221da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 17231da177e4SLinus Torvalds __in_dev_put(in_dev); 17241da177e4SLinus Torvalds #endif 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 17301da177e4SLinus Torvalds { 1731dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 1732165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1733dcd87999SNikolay Borisov #endif 17341da177e4SLinus Torvalds ASSERT_RTNL(); 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1737e99e88a9SKees Cook timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0); 1738e99e88a9SKees Cook timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0); 1739165094afSNikolay Borisov in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv; 17401da177e4SLinus Torvalds #endif 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds /* Device going up */ 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 17481da177e4SLinus Torvalds { 17491d7138deSEric Dumazet struct ip_mc_list *pmc; 1750dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 1751165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1752dcd87999SNikolay Borisov #endif 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds ASSERT_RTNL(); 17551da177e4SLinus Torvalds 1756a9fe8e29SHannes Frederic Sowa #ifdef CONFIG_IP_MULTICAST 1757165094afSNikolay Borisov in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv; 1758a9fe8e29SHannes Frederic Sowa #endif 17591da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 17601da177e4SLinus Torvalds 176124803f38SHangbin Liu for_each_pmc_rtnl(in_dev, pmc) { 176224803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 176324803f38SHangbin Liu igmpv3_del_delrec(in_dev, pmc); 176424803f38SHangbin Liu #endif 1765*6e2059b5SHangbin Liu igmp_group_added(pmc, pmc->sfmode); 17661da177e4SLinus Torvalds } 176724803f38SHangbin Liu } 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds /* 17701da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 17711da177e4SLinus Torvalds */ 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 17741da177e4SLinus Torvalds { 17751da177e4SLinus Torvalds struct ip_mc_list *i; 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds ASSERT_RTNL(); 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds /* Deactivate timers */ 17801da177e4SLinus Torvalds ip_mc_down(in_dev); 178124803f38SHangbin Liu #ifdef CONFIG_IP_MULTICAST 178224803f38SHangbin Liu igmpv3_clear_delrec(in_dev); 178324803f38SHangbin Liu #endif 17841da177e4SLinus Torvalds 17851d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { 17861d7138deSEric Dumazet in_dev->mc_list = i->next_rcu; 1787b8bae41eSRami Rosen in_dev->mc_count--; 17881da177e4SLinus Torvalds ip_ma_put(i); 17891da177e4SLinus Torvalds } 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 17929e917dcaSEric Dumazet /* RTNL is locked */ 1793877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 17941da177e4SLinus Torvalds { 17951da177e4SLinus Torvalds struct net_device *dev = NULL; 17961da177e4SLinus Torvalds struct in_device *idev = NULL; 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds if (imr->imr_ifindex) { 1799877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 18001da177e4SLinus Torvalds return idev; 18011da177e4SLinus Torvalds } 18021da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 18039e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false); 18041da177e4SLinus Torvalds if (!dev) 18051da177e4SLinus Torvalds return NULL; 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds 1808b23dd4feSDavid S. Miller if (!dev) { 180978fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, 181078fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr, 181178fbfd8aSDavid S. Miller 0, 0, 0); 1812b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1813d8d1f30bSChangli Gao dev = rt->dst.dev; 18141da177e4SLinus Torvalds ip_rt_put(rt); 18151da177e4SLinus Torvalds } 1816b23dd4feSDavid S. Miller } 18171da177e4SLinus Torvalds if (dev) { 18181da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1819e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds return idev; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds /* 18251da177e4SLinus Torvalds * Join a socket to a group 18261da177e4SLinus Torvalds */ 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 18298f935bbdSAl Viro __be32 *psfsrc) 18301da177e4SLinus Torvalds { 18311da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 18321da177e4SLinus Torvalds int rv = 0; 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds psf_prev = NULL; 18351da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 18361da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 18371da177e4SLinus Torvalds break; 18381da177e4SLinus Torvalds psf_prev = psf; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 18411da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 18421da177e4SLinus Torvalds return -ESRCH; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds psf->sf_count[sfmode]--; 18451da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 18461da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 18491da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18501da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 1851165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 18521da177e4SLinus Torvalds #endif 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds /* no more filters for this source */ 18551da177e4SLinus Torvalds if (psf_prev) 18561da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 18571da177e4SLinus Torvalds else 18581da177e4SLinus Torvalds pmc->sources = psf->sf_next; 18591da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18601da177e4SLinus Torvalds if (psf->sf_oldin && 18611da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 1862165094afSNikolay Borisov psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 18631da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 18641da177e4SLinus Torvalds pmc->tomb = psf; 18651da177e4SLinus Torvalds rv = 1; 18661da177e4SLinus Torvalds } else 18671da177e4SLinus Torvalds #endif 18681da177e4SLinus Torvalds kfree(psf); 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds return rv; 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 18741da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 18751da177e4SLinus Torvalds #endif 18761da177e4SLinus Torvalds 18778f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 18788f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 18791da177e4SLinus Torvalds { 18801da177e4SLinus Torvalds struct ip_mc_list *pmc; 18811da177e4SLinus Torvalds int changerec = 0; 18821da177e4SLinus Torvalds int i, err; 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds if (!in_dev) 18851da177e4SLinus Torvalds return -ENODEV; 18861d7138deSEric Dumazet rcu_read_lock(); 18871d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 18881da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 18891da177e4SLinus Torvalds break; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds if (!pmc) { 18921da177e4SLinus Torvalds /* MCA not found?? bug */ 18931d7138deSEric Dumazet rcu_read_unlock(); 18941da177e4SLinus Torvalds return -ESRCH; 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 18971d7138deSEric Dumazet rcu_read_unlock(); 18981da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18991da177e4SLinus Torvalds sf_markstate(pmc); 19001da177e4SLinus Torvalds #endif 19011da177e4SLinus Torvalds if (!delta) { 19021da177e4SLinus Torvalds err = -EINVAL; 19031da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 19041da177e4SLinus Torvalds goto out_unlock; 19051da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds err = 0; 19081da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 19091da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds changerec |= rv > 0; 19121da177e4SLinus Torvalds if (!err && rv < 0) 19131da177e4SLinus Torvalds err = rv; 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 19161da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 19171da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 19181da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19191da177e4SLinus Torvalds struct ip_sf_list *psf; 1920165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 19211da177e4SLinus Torvalds #endif 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds /* filter mode change */ 19241da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 19251da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1926165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 19271da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 19281da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 19291da177e4SLinus Torvalds psf->sf_crcount = 0; 19301da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 19311da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 19321da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 19331da177e4SLinus Torvalds #endif 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds out_unlock: 19361da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 19371da177e4SLinus Torvalds return err; 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds /* 19411da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 19421da177e4SLinus Torvalds */ 19431da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 19445eb81e89SJun Zhao __be32 *psfsrc) 19451da177e4SLinus Torvalds { 19461da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds psf_prev = NULL; 19491da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 19501da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 19511da177e4SLinus Torvalds break; 19521da177e4SLinus Torvalds psf_prev = psf; 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds if (!psf) { 19550da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 19561da177e4SLinus Torvalds if (!psf) 19571da177e4SLinus Torvalds return -ENOBUFS; 19581da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 19591da177e4SLinus Torvalds if (psf_prev) { 19601da177e4SLinus Torvalds psf_prev->sf_next = psf; 19611da177e4SLinus Torvalds } else 19621da177e4SLinus Torvalds pmc->sources = psf; 19631da177e4SLinus Torvalds } 19641da177e4SLinus Torvalds psf->sf_count[sfmode]++; 19651da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 19661da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds return 0; 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19721da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 19731da177e4SLinus Torvalds { 19741da177e4SLinus Torvalds struct ip_sf_list *psf; 19751da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 19781da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 19791da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 19801da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 19811da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 19821da177e4SLinus Torvalds } else 19831da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 19871da177e4SLinus Torvalds { 1988ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 19891da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 19901da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 19911da177e4SLinus Torvalds int new_in, rv; 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds rv = 0; 19941da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 19951da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 19961da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 19971da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 19981da177e4SLinus Torvalds } else 19991da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 2000ad12583fSDavid L Stevens if (new_in) { 2001ad12583fSDavid L Stevens if (!psf->sf_oldin) { 200276edc605SAl Viro struct ip_sf_list *prev = NULL; 2003ad12583fSDavid L Stevens 2004ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) { 2005ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 2006ad12583fSDavid L Stevens break; 2007ad12583fSDavid L Stevens prev = dpsf; 2008ad12583fSDavid L Stevens } 2009ad12583fSDavid L Stevens if (dpsf) { 2010ad12583fSDavid L Stevens if (prev) 2011ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 2012ad12583fSDavid L Stevens else 2013ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 2014ad12583fSDavid L Stevens kfree(dpsf); 2015ad12583fSDavid L Stevens } 20161da177e4SLinus Torvalds psf->sf_crcount = qrv; 20171da177e4SLinus Torvalds rv++; 20181da177e4SLinus Torvalds } 2019ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 2020ad12583fSDavid L Stevens 2021ad12583fSDavid L Stevens psf->sf_crcount = 0; 2022ad12583fSDavid L Stevens /* 2023ad12583fSDavid L Stevens * add or update "delete" records if an active filter 2024ad12583fSDavid L Stevens * is now inactive 2025ad12583fSDavid L Stevens */ 2026ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) 2027ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 2028ad12583fSDavid L Stevens break; 2029ad12583fSDavid L Stevens if (!dpsf) { 20303ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); 2031ad12583fSDavid L Stevens if (!dpsf) 2032ad12583fSDavid L Stevens continue; 2033ad12583fSDavid L Stevens *dpsf = *psf; 2034ad12583fSDavid L Stevens /* pmc->lock held by callers */ 2035ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 2036ad12583fSDavid L Stevens pmc->tomb = dpsf; 2037ad12583fSDavid L Stevens } 2038ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 2039ad12583fSDavid L Stevens rv++; 2040ad12583fSDavid L Stevens } 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds return rv; 20431da177e4SLinus Torvalds } 20441da177e4SLinus Torvalds #endif 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds /* 20471da177e4SLinus Torvalds * Add multicast source filter list to the interface list 20481da177e4SLinus Torvalds */ 20498f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 20508f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 20511da177e4SLinus Torvalds { 20521da177e4SLinus Torvalds struct ip_mc_list *pmc; 20531da177e4SLinus Torvalds int isexclude; 20541da177e4SLinus Torvalds int i, err; 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds if (!in_dev) 20571da177e4SLinus Torvalds return -ENODEV; 20581d7138deSEric Dumazet rcu_read_lock(); 20591d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 20601da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 20611da177e4SLinus Torvalds break; 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds if (!pmc) { 20641da177e4SLinus Torvalds /* MCA not found?? bug */ 20651d7138deSEric Dumazet rcu_read_unlock(); 20661da177e4SLinus Torvalds return -ESRCH; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 20691d7138deSEric Dumazet rcu_read_unlock(); 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 20721da177e4SLinus Torvalds sf_markstate(pmc); 20731da177e4SLinus Torvalds #endif 20741da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 20751da177e4SLinus Torvalds if (!delta) 20761da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 20771da177e4SLinus Torvalds err = 0; 20781da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 20795eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); 20801da177e4SLinus Torvalds if (err) 20811da177e4SLinus Torvalds break; 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds if (err) { 20841da177e4SLinus Torvalds int j; 20851da177e4SLinus Torvalds 2086685f94e6SJun Zhao if (!delta) 20871da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 20881da177e4SLinus Torvalds for (j = 0; j < i; j++) 2089a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); 20901da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 20911da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 20921da177e4SLinus Torvalds struct ip_sf_list *psf; 2093165094afSNikolay Borisov struct net *net = dev_net(pmc->interface->dev); 2094cfcabdccSStephen Hemminger in_dev = pmc->interface; 20951da177e4SLinus Torvalds #endif 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds /* filter mode change */ 20981da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 20991da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 21001da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 21011da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 21021da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 21031da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 21041da177e4SLinus Torvalds 2105165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 21061da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 21071da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 21081da177e4SLinus Torvalds psf->sf_crcount = 0; 21091da177e4SLinus Torvalds igmp_ifc_event(in_dev); 21101da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 21111da177e4SLinus Torvalds igmp_ifc_event(in_dev); 21121da177e4SLinus Torvalds #endif 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 21151da177e4SLinus Torvalds return err; 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 21191da177e4SLinus Torvalds { 2120c38b7d32SWANG Cong struct ip_sf_list *psf, *nextpsf, *tomb, *sources; 21211da177e4SLinus Torvalds 2122c38b7d32SWANG Cong spin_lock_bh(&pmc->lock); 2123c38b7d32SWANG Cong tomb = pmc->tomb; 21241da177e4SLinus Torvalds pmc->tomb = NULL; 2125c38b7d32SWANG Cong sources = pmc->sources; 21261da177e4SLinus Torvalds pmc->sources = NULL; 21271da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 2128de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 21291da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 2130c38b7d32SWANG Cong spin_unlock_bh(&pmc->lock); 2131c38b7d32SWANG Cong 2132c38b7d32SWANG Cong for (psf = tomb; psf; psf = nextpsf) { 2133c38b7d32SWANG Cong nextpsf = psf->sf_next; 2134c38b7d32SWANG Cong kfree(psf); 2135c38b7d32SWANG Cong } 2136c38b7d32SWANG Cong for (psf = sources; psf; psf = nextpsf) { 2137c38b7d32SWANG Cong nextpsf = psf->sf_next; 2138c38b7d32SWANG Cong kfree(psf); 2139c38b7d32SWANG Cong } 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 214254ff9ef3SMarcelo Ricardo Leitner /* Join a multicast group 214354ff9ef3SMarcelo Ricardo Leitner */ 2144*6e2059b5SHangbin Liu static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr, 2145*6e2059b5SHangbin Liu unsigned int mode) 21461da177e4SLinus Torvalds { 21478f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 2148959d10f6SEric Dumazet struct ip_mc_socklist *iml, *i; 21491da177e4SLinus Torvalds struct in_device *in_dev; 21501da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2151877acedcSDaniel Lezcano struct net *net = sock_net(sk); 2152ca9b907dSDavid L Stevens int ifindex; 21531da177e4SLinus Torvalds int count = 0; 2154959d10f6SEric Dumazet int err; 2155959d10f6SEric Dumazet 2156959d10f6SEric Dumazet ASSERT_RTNL(); 21571da177e4SLinus Torvalds 2158f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21591da177e4SLinus Torvalds return -EINVAL; 21601da177e4SLinus Torvalds 2161877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 21621da177e4SLinus Torvalds 21631da177e4SLinus Torvalds if (!in_dev) { 21641da177e4SLinus Torvalds err = -ENODEV; 21651da177e4SLinus Torvalds goto done; 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds err = -EADDRINUSE; 2169ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 21701d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 2171ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 2172ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 21731da177e4SLinus Torvalds goto done; 21741da177e4SLinus Torvalds count++; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds err = -ENOBUFS; 2177815c5270SNikolay Borisov if (count >= net->ipv4.sysctl_igmp_max_memberships) 21781da177e4SLinus Torvalds goto done; 21798b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 218051456b29SIan Morris if (!iml) 2181ca9b907dSDavid L Stevens goto done; 2182ca9b907dSDavid L Stevens 21831da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 21841d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 21851da177e4SLinus Torvalds iml->sflist = NULL; 2186*6e2059b5SHangbin Liu iml->sfmode = mode; 2187cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 2188*6e2059b5SHangbin Liu __ip_mc_inc_group(in_dev, addr, mode); 21891da177e4SLinus Torvalds err = 0; 21901da177e4SLinus Torvalds done: 21911da177e4SLinus Torvalds return err; 21921da177e4SLinus Torvalds } 2193*6e2059b5SHangbin Liu 2194*6e2059b5SHangbin Liu /* Join ASM (Any-Source Multicast) group 2195*6e2059b5SHangbin Liu */ 2196*6e2059b5SHangbin Liu int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr) 2197*6e2059b5SHangbin Liu { 2198*6e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE); 2199*6e2059b5SHangbin Liu } 22004bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 22011da177e4SLinus Torvalds 2202*6e2059b5SHangbin Liu /* Join SSM (Source-Specific Multicast) group 2203*6e2059b5SHangbin Liu */ 2204*6e2059b5SHangbin Liu int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr, 2205*6e2059b5SHangbin Liu unsigned int mode) 2206*6e2059b5SHangbin Liu { 2207*6e2059b5SHangbin Liu return __ip_mc_join_group(sk, imr, mode); 2208*6e2059b5SHangbin Liu } 2209*6e2059b5SHangbin Liu 22101da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 22111da177e4SLinus Torvalds struct in_device *in_dev) 22121da177e4SLinus Torvalds { 22131d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 22141da177e4SLinus Torvalds int err; 22151da177e4SLinus Torvalds 221651456b29SIan Morris if (!psf) { 22171da177e4SLinus Torvalds /* any-source empty exclude case */ 22181da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 22191da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 2222c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 2223a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 2224c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2225c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); 22267519cce4SLai Jiangshan kfree_rcu(psf, rcu); 22271da177e4SLinus Torvalds return err; 22281da177e4SLinus Torvalds } 22291da177e4SLinus Torvalds 223054ff9ef3SMarcelo Ricardo Leitner int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 22311da177e4SLinus Torvalds { 22321da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22331d7138deSEric Dumazet struct ip_mc_socklist *iml; 22341d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 223584b42baeSDavid L Stevens struct in_device *in_dev; 2236877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22378f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 223884b42baeSDavid L Stevens u32 ifindex; 2239acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 22401da177e4SLinus Torvalds 2241959d10f6SEric Dumazet ASSERT_RTNL(); 2242959d10f6SEric Dumazet 2243877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 22444eba7bb1SAndrew Lunn if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) { 224552ad353aSdingtianhong ret = -ENODEV; 224652ad353aSdingtianhong goto out; 224752ad353aSdingtianhong } 224884b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 22491d7138deSEric Dumazet for (imlp = &inet->mc_list; 22501d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 22511d7138deSEric Dumazet imlp = &iml->next_rcu) { 2252acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 2253acd6e00bSDavid L Stevens continue; 2254acd6e00bSDavid L Stevens if (ifindex) { 2255acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 2256acd6e00bSDavid L Stevens continue; 2257acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 2258acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 2259acd6e00bSDavid L Stevens continue; 2260acd6e00bSDavid L Stevens 22611da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 22621da177e4SLinus Torvalds 22631d7138deSEric Dumazet *imlp = iml->next_rcu; 22641da177e4SLinus Torvalds 22654eba7bb1SAndrew Lunn if (in_dev) 226684b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 2267959d10f6SEric Dumazet 2268c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2269c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 227010d50e74SLai Jiangshan kfree_rcu(iml, rcu); 22711da177e4SLinus Torvalds return 0; 22721da177e4SLinus Torvalds } 227352ad353aSdingtianhong out: 2274959d10f6SEric Dumazet return ret; 2275959d10f6SEric Dumazet } 2276193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 22791da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 22801da177e4SLinus Torvalds { 22811da177e4SLinus Torvalds int err; 22821da177e4SLinus Torvalds struct ip_mreqn imr; 228363007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 22841da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22851da177e4SLinus Torvalds struct in_device *in_dev = NULL; 22861da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22871da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2288877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22898cdaaa15SDavid L Stevens int leavegroup = 0; 22901da177e4SLinus Torvalds int i, j, rv; 22911da177e4SLinus Torvalds 2292f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 22931da177e4SLinus Torvalds return -EINVAL; 22941da177e4SLinus Torvalds 229554ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 22981da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 22991da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2300877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds if (!in_dev) { 23031da177e4SLinus Torvalds err = -ENODEV; 23041da177e4SLinus Torvalds goto done; 23051da177e4SLinus Torvalds } 23061da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 23071da177e4SLinus Torvalds 23081d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 2309f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 2310f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 2311f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 23121da177e4SLinus Torvalds break; 23131da177e4SLinus Torvalds } 2314917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2315917f2f10SDavid L Stevens err = -EINVAL; 23161da177e4SLinus Torvalds goto done; 2317917f2f10SDavid L Stevens } 23181da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 23191da177e4SLinus Torvalds if (pmc->sflist) { 2320917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 2321917f2f10SDavid L Stevens err = -EINVAL; 23221da177e4SLinus Torvalds goto done; 2323917f2f10SDavid L Stevens } 23241da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 23251da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 23261da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 23271da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 23281da177e4SLinus Torvalds NULL, 0); 23291da177e4SLinus Torvalds pmc->sfmode = omode; 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds 23321d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 23331da177e4SLinus Torvalds if (!add) { 23341da177e4SLinus Torvalds if (!psl) 2335917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 23361da177e4SLinus Torvalds rv = !0; 23371da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 23381da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 233963007727SAl Viro sizeof(__be32)); 23401da177e4SLinus Torvalds if (rv == 0) 23411da177e4SLinus Torvalds break; 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds if (rv) /* source not found */ 2344917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 23451da177e4SLinus Torvalds 23468cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 23478cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 23488cdaaa15SDavid L Stevens leavegroup = 1; 23498cdaaa15SDavid L Stevens goto done; 23508cdaaa15SDavid L Stevens } 23518cdaaa15SDavid L Stevens 23521da177e4SLinus Torvalds /* update the interface filter */ 23531da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 23541da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds for (j = i+1; j < psl->sl_count; j++) 23571da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 23581da177e4SLinus Torvalds psl->sl_count--; 23591da177e4SLinus Torvalds err = 0; 23601da177e4SLinus Torvalds goto done; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds /* else, add a new source to the filter */ 23631da177e4SLinus Torvalds 2364166b6b2dSNikolay Borisov if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) { 23651da177e4SLinus Torvalds err = -ENOBUFS; 23661da177e4SLinus Torvalds goto done; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 23691da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 23701da177e4SLinus Torvalds int count = IP_SFBLOCK; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds if (psl) 23731da177e4SLinus Torvalds count += psl->sl_max; 23748b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 23751da177e4SLinus Torvalds if (!newpsl) { 23761da177e4SLinus Torvalds err = -ENOBUFS; 23771da177e4SLinus Torvalds goto done; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds newpsl->sl_max = count; 23801da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 23811da177e4SLinus Torvalds if (psl) { 23821da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) 23831da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2384c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2385c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 23867519cce4SLai Jiangshan kfree_rcu(psl, rcu); 23871da177e4SLinus Torvalds } 2388cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2389c85bb41eSFlavio Leitner psl = newpsl; 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 23921da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 23931da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 239463007727SAl Viro sizeof(__be32)); 23951da177e4SLinus Torvalds if (rv == 0) 23961da177e4SLinus Torvalds break; 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 23991da177e4SLinus Torvalds goto done; 24001da177e4SLinus Torvalds for (j = psl->sl_count-1; j >= i; j--) 24011da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 24021da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 24031da177e4SLinus Torvalds psl->sl_count++; 24041da177e4SLinus Torvalds err = 0; 24051da177e4SLinus Torvalds /* update the interface list */ 24061da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 24071da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 24081da177e4SLinus Torvalds done: 24098cdaaa15SDavid L Stevens if (leavegroup) 241054ff9ef3SMarcelo Ricardo Leitner err = ip_mc_leave_group(sk, &imr); 24111da177e4SLinus Torvalds return err; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 24151da177e4SLinus Torvalds { 24169951f036SDavid L Stevens int err = 0; 24171da177e4SLinus Torvalds struct ip_mreqn imr; 241863007727SAl Viro __be32 addr = msf->imsf_multiaddr; 24191da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 24201da177e4SLinus Torvalds struct in_device *in_dev; 24211da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 24221da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2423877acedcSDaniel Lezcano struct net *net = sock_net(sk); 24249951f036SDavid L Stevens int leavegroup = 0; 24251da177e4SLinus Torvalds 2426f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 24271da177e4SLinus Torvalds return -EINVAL; 24281da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 24291da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 24301da177e4SLinus Torvalds return -EINVAL; 24311da177e4SLinus Torvalds 243254ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 24351da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 24361da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2437877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 24381da177e4SLinus Torvalds 24391da177e4SLinus Torvalds if (!in_dev) { 24401da177e4SLinus Torvalds err = -ENODEV; 24411da177e4SLinus Torvalds goto done; 24421da177e4SLinus Torvalds } 24431da177e4SLinus Torvalds 24449951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 24459951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 24469951f036SDavid L Stevens leavegroup = 1; 24479951f036SDavid L Stevens goto done; 24489951f036SDavid L Stevens } 24499951f036SDavid L Stevens 24501d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 24511da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 24521da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 24531da177e4SLinus Torvalds break; 24541da177e4SLinus Torvalds } 2455917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2456917f2f10SDavid L Stevens err = -EINVAL; 24571da177e4SLinus Torvalds goto done; 2458917f2f10SDavid L Stevens } 24591da177e4SLinus Torvalds if (msf->imsf_numsrc) { 24608b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 24618b3a7005SKris Katterjohn GFP_KERNEL); 24621da177e4SLinus Torvalds if (!newpsl) { 24631da177e4SLinus Torvalds err = -ENOBUFS; 24641da177e4SLinus Torvalds goto done; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 24671da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 24681da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 24691da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 24701da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 24711da177e4SLinus Torvalds if (err) { 24721da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 24731da177e4SLinus Torvalds goto done; 24741da177e4SLinus Torvalds } 24758713dbf0SYan Zheng } else { 24761da177e4SLinus Torvalds newpsl = NULL; 24778713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 24788713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 24798713dbf0SYan Zheng } 24801d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 24811da177e4SLinus Torvalds if (psl) { 24821da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 24831da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2484c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2485c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 24867519cce4SLai Jiangshan kfree_rcu(psl, rcu); 24871da177e4SLinus Torvalds } else 24881da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 24891da177e4SLinus Torvalds 0, NULL, 0); 2490cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 24911da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2492917f2f10SDavid L Stevens err = 0; 24931da177e4SLinus Torvalds done: 24949951f036SDavid L Stevens if (leavegroup) 24959951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 24961da177e4SLinus Torvalds return err; 24971da177e4SLinus Torvalds } 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 25001da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 25011da177e4SLinus Torvalds { 25021da177e4SLinus Torvalds int err, len, count, copycount; 25031da177e4SLinus Torvalds struct ip_mreqn imr; 250463007727SAl Viro __be32 addr = msf->imsf_multiaddr; 25051da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25061da177e4SLinus Torvalds struct in_device *in_dev; 25071da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25081da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2509877acedcSDaniel Lezcano struct net *net = sock_net(sk); 25101da177e4SLinus Torvalds 251187e9f031SWANG Cong ASSERT_RTNL(); 251287e9f031SWANG Cong 2513f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 25141da177e4SLinus Torvalds return -EINVAL; 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 25171da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 25181da177e4SLinus Torvalds imr.imr_ifindex = 0; 2519877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds if (!in_dev) { 25221da177e4SLinus Torvalds err = -ENODEV; 25231da177e4SLinus Torvalds goto done; 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 25261da177e4SLinus Torvalds 25271d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 25281da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 25291da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 25301da177e4SLinus Torvalds break; 25311da177e4SLinus Torvalds } 25321da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 25331da177e4SLinus Torvalds goto done; 25341da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 25351d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 25361da177e4SLinus Torvalds if (!psl) { 25371da177e4SLinus Torvalds len = 0; 25381da177e4SLinus Torvalds count = 0; 25391da177e4SLinus Torvalds } else { 25401da177e4SLinus Torvalds count = psl->sl_count; 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 25431da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 25441da177e4SLinus Torvalds msf->imsf_numsrc = count; 25451da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 25461da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 25471da177e4SLinus Torvalds return -EFAULT; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds if (len && 25501da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 25511da177e4SLinus Torvalds return -EFAULT; 25521da177e4SLinus Torvalds return 0; 25531da177e4SLinus Torvalds done: 25541da177e4SLinus Torvalds return err; 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 25581da177e4SLinus Torvalds struct group_filter __user *optval, int __user *optlen) 25591da177e4SLinus Torvalds { 25601da177e4SLinus Torvalds int err, i, count, copycount; 25611da177e4SLinus Torvalds struct sockaddr_in *psin; 256263007727SAl Viro __be32 addr; 25631da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25641da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25651da177e4SLinus Torvalds struct ip_sf_socklist *psl; 25661da177e4SLinus Torvalds 256787e9f031SWANG Cong ASSERT_RTNL(); 256887e9f031SWANG Cong 25691da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 25701da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 25711da177e4SLinus Torvalds return -EINVAL; 25721da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2573f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 25741da177e4SLinus Torvalds return -EINVAL; 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 25771da177e4SLinus Torvalds 25781d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 25791da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 25801da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 25811da177e4SLinus Torvalds break; 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 25841da177e4SLinus Torvalds goto done; 25851da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 25861d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 25871da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 25881da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 25891da177e4SLinus Torvalds gsf->gf_numsrc = count; 25901da177e4SLinus Torvalds if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 25911da177e4SLinus Torvalds copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 25921da177e4SLinus Torvalds return -EFAULT; 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds for (i = 0; i < copycount; i++) { 25951da177e4SLinus Torvalds struct sockaddr_storage ss; 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 25981da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 25991da177e4SLinus Torvalds psin->sin_family = AF_INET; 26001da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 26011da177e4SLinus Torvalds if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 26021da177e4SLinus Torvalds return -EFAULT; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds return 0; 26051da177e4SLinus Torvalds done: 26061da177e4SLinus Torvalds return err; 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds /* 26101da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 26111da177e4SLinus Torvalds */ 261260d9b031SDavid Ahern int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, 261360d9b031SDavid Ahern int dif, int sdif) 26141da177e4SLinus Torvalds { 26151da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 26161da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 26171da177e4SLinus Torvalds struct ip_sf_socklist *psl; 26181da177e4SLinus Torvalds int i; 2619c85bb41eSFlavio Leitner int ret; 26201da177e4SLinus Torvalds 2621c85bb41eSFlavio Leitner ret = 1; 2622f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2623c85bb41eSFlavio Leitner goto out; 26241da177e4SLinus Torvalds 2625c85bb41eSFlavio Leitner rcu_read_lock(); 26261d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 26271da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 262860d9b031SDavid Ahern (pmc->multi.imr_ifindex == dif || 262960d9b031SDavid Ahern (sdif && pmc->multi.imr_ifindex == sdif))) 26301da177e4SLinus Torvalds break; 26311da177e4SLinus Torvalds } 2632c85bb41eSFlavio Leitner ret = inet->mc_all; 26331da177e4SLinus Torvalds if (!pmc) 2634c85bb41eSFlavio Leitner goto unlock; 26351d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2636c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 26371da177e4SLinus Torvalds if (!psl) 2638c85bb41eSFlavio Leitner goto unlock; 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 26411da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 26421da177e4SLinus Torvalds break; 26431da177e4SLinus Torvalds } 2644c85bb41eSFlavio Leitner ret = 0; 26451da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2646c85bb41eSFlavio Leitner goto unlock; 26471da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2648c85bb41eSFlavio Leitner goto unlock; 2649c85bb41eSFlavio Leitner ret = 1; 2650c85bb41eSFlavio Leitner unlock: 2651c85bb41eSFlavio Leitner rcu_read_unlock(); 2652c85bb41eSFlavio Leitner out: 2653c85bb41eSFlavio Leitner return ret; 26541da177e4SLinus Torvalds } 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds /* 26571da177e4SLinus Torvalds * A socket is closing. 26581da177e4SLinus Torvalds */ 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 26611da177e4SLinus Torvalds { 26621da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 26631da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2664877acedcSDaniel Lezcano struct net *net = sock_net(sk); 26651da177e4SLinus Torvalds 266651456b29SIan Morris if (!inet->mc_list) 26671da177e4SLinus Torvalds return; 26681da177e4SLinus Torvalds 26691da177e4SLinus Torvalds rtnl_lock(); 26701d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 26711da177e4SLinus Torvalds struct in_device *in_dev; 26721da177e4SLinus Torvalds 26731d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2674877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 26751da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 267600db4124SIan Morris if (in_dev) 26771da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2678c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2679c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 268010d50e74SLai Jiangshan kfree_rcu(iml, rcu); 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds rtnl_unlock(); 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds 2685dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 26862094acbbSAlexander Duyck int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto) 26871da177e4SLinus Torvalds { 26881da177e4SLinus Torvalds struct ip_mc_list *im; 2689e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 26901da177e4SLinus Torvalds struct ip_sf_list *psf; 26911da177e4SLinus Torvalds int rv = 0; 26921da177e4SLinus Torvalds 2693e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash); 2694e9897071SEric Dumazet if (mc_hash) { 2695c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); 2696e9897071SEric Dumazet 2697e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]); 2698e9897071SEric Dumazet im != NULL; 2699e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) { 2700e9897071SEric Dumazet if (im->multiaddr == mc_addr) 2701e9897071SEric Dumazet break; 2702e9897071SEric Dumazet } 2703e9897071SEric Dumazet } else { 27041d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 27051da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 27061da177e4SLinus Torvalds break; 27071da177e4SLinus Torvalds } 2708e9897071SEric Dumazet } 27091da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 27101da177e4SLinus Torvalds rv = 1; 27111da177e4SLinus Torvalds } else if (im) { 27121da177e4SLinus Torvalds if (src_addr) { 27131da177e4SLinus Torvalds for (psf = im->sources; psf; psf = psf->sf_next) { 27141da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 27151da177e4SLinus Torvalds break; 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds if (psf) 27181da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 27191da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 27201da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 27211da177e4SLinus Torvalds else 27221da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 27231da177e4SLinus Torvalds } else 27241da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds return rv; 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds 27291da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 27301da177e4SLinus Torvalds struct igmp_mc_iter_state { 27317091e728SAlexey Dobriyan struct seq_net_private p; 27321da177e4SLinus Torvalds struct net_device *dev; 27331da177e4SLinus Torvalds struct in_device *in_dev; 27341da177e4SLinus Torvalds }; 27351da177e4SLinus Torvalds 27361da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 27371da177e4SLinus Torvalds 27381da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 27391da177e4SLinus Torvalds { 27407091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 27411da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 27421da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27431da177e4SLinus Torvalds 27447562f876SPavel Emelianov state->in_dev = NULL; 274561fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 27461da177e4SLinus Torvalds struct in_device *in_dev; 27476baff150SEric Dumazet 27486baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 27491da177e4SLinus Torvalds if (!in_dev) 27501da177e4SLinus Torvalds continue; 27511d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 27521da177e4SLinus Torvalds if (im) { 27531da177e4SLinus Torvalds state->in_dev = in_dev; 27541da177e4SLinus Torvalds break; 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds return im; 27581da177e4SLinus Torvalds } 27591da177e4SLinus Torvalds 27601da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 27611da177e4SLinus Torvalds { 27621da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27636baff150SEric Dumazet 27641d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 27651d7138deSEric Dumazet while (!im) { 27666baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 27671da177e4SLinus Torvalds if (!state->dev) { 27681da177e4SLinus Torvalds state->in_dev = NULL; 27691da177e4SLinus Torvalds break; 27701da177e4SLinus Torvalds } 27716baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 27721da177e4SLinus Torvalds if (!state->in_dev) 27731da177e4SLinus Torvalds continue; 27741d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 27751da177e4SLinus Torvalds } 27761da177e4SLinus Torvalds return im; 27771da177e4SLinus Torvalds } 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 27801da177e4SLinus Torvalds { 27811da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 27821da177e4SLinus Torvalds if (im) 27831da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 27841da177e4SLinus Torvalds --pos; 27851da177e4SLinus Torvalds return pos ? NULL : im; 27861da177e4SLinus Torvalds } 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 278961fbab77Sstephen hemminger __acquires(rcu) 27901da177e4SLinus Torvalds { 279161fbab77Sstephen hemminger rcu_read_lock(); 27921da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 27951da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 27961da177e4SLinus Torvalds { 27971da177e4SLinus Torvalds struct ip_mc_list *im; 27981da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 27991da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 28001da177e4SLinus Torvalds else 28011da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 28021da177e4SLinus Torvalds ++*pos; 28031da177e4SLinus Torvalds return im; 28041da177e4SLinus Torvalds } 28051da177e4SLinus Torvalds 28061da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 280761fbab77Sstephen hemminger __releases(rcu) 28081da177e4SLinus Torvalds { 28091da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 28101d7138deSEric Dumazet 28111da177e4SLinus Torvalds state->in_dev = NULL; 28121da177e4SLinus Torvalds state->dev = NULL; 281361fbab77Sstephen hemminger rcu_read_unlock(); 28141da177e4SLinus Torvalds } 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 28171da177e4SLinus Torvalds { 28181da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 28191da177e4SLinus Torvalds seq_puts(seq, 28201da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 28211da177e4SLinus Torvalds else { 28221da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 28231da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 28241da177e4SLinus Torvalds char *querier; 2825a399a805SEric Dumazet long delta; 2826a399a805SEric Dumazet 28271da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 28281da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 28291da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 28301da177e4SLinus Torvalds "V3"; 28311da177e4SLinus Torvalds #else 28321da177e4SLinus Torvalds querier = "NONE"; 28331da177e4SLinus Torvalds #endif 28341da177e4SLinus Torvalds 2835e6b68883SAndreea-Cristina Bernat if (rcu_access_pointer(state->in_dev->mc_list) == im) { 28361da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2837b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 28381da177e4SLinus Torvalds } 28391da177e4SLinus Torvalds 2840a399a805SEric Dumazet delta = im->timer.expires - jiffies; 28411da177e4SLinus Torvalds seq_printf(seq, 2842338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 28431da177e4SLinus Torvalds im->multiaddr, im->users, 2844a399a805SEric Dumazet im->tm_running, 2845a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 28461da177e4SLinus Torvalds im->reporter); 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds return 0; 28491da177e4SLinus Torvalds } 28501da177e4SLinus Torvalds 2851f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 28521da177e4SLinus Torvalds .start = igmp_mc_seq_start, 28531da177e4SLinus Torvalds .next = igmp_mc_seq_next, 28541da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 28551da177e4SLinus Torvalds .show = igmp_mc_seq_show, 28561da177e4SLinus Torvalds }; 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds struct igmp_mcf_iter_state { 28597091e728SAlexey Dobriyan struct seq_net_private p; 28601da177e4SLinus Torvalds struct net_device *dev; 28611da177e4SLinus Torvalds struct in_device *idev; 28621da177e4SLinus Torvalds struct ip_mc_list *im; 28631da177e4SLinus Torvalds }; 28641da177e4SLinus Torvalds 28651da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 28681da177e4SLinus Torvalds { 28697091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 28701da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 28711da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 28721da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 28731da177e4SLinus Torvalds 28747562f876SPavel Emelianov state->idev = NULL; 28757562f876SPavel Emelianov state->im = NULL; 287661fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 28771da177e4SLinus Torvalds struct in_device *idev; 28786baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 287951456b29SIan Morris if (unlikely(!idev)) 28801da177e4SLinus Torvalds continue; 28811d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 288200db4124SIan Morris if (likely(im)) { 28831da177e4SLinus Torvalds spin_lock_bh(&im->lock); 28841da177e4SLinus Torvalds psf = im->sources; 288500db4124SIan Morris if (likely(psf)) { 28861da177e4SLinus Torvalds state->im = im; 28871da177e4SLinus Torvalds state->idev = idev; 28881da177e4SLinus Torvalds break; 28891da177e4SLinus Torvalds } 28901da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 28911da177e4SLinus Torvalds } 28921da177e4SLinus Torvalds } 28931da177e4SLinus Torvalds return psf; 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 28971da177e4SLinus Torvalds { 28981da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 28991da177e4SLinus Torvalds 29001da177e4SLinus Torvalds psf = psf->sf_next; 29011da177e4SLinus Torvalds while (!psf) { 29021da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 29031da177e4SLinus Torvalds state->im = state->im->next; 29041da177e4SLinus Torvalds while (!state->im) { 29056baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 29061da177e4SLinus Torvalds if (!state->dev) { 29071da177e4SLinus Torvalds state->idev = NULL; 29081da177e4SLinus Torvalds goto out; 29091da177e4SLinus Torvalds } 29106baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 29111da177e4SLinus Torvalds if (!state->idev) 29121da177e4SLinus Torvalds continue; 29131d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds if (!state->im) 29161da177e4SLinus Torvalds break; 29171da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 29181da177e4SLinus Torvalds psf = state->im->sources; 29191da177e4SLinus Torvalds } 29201da177e4SLinus Torvalds out: 29211da177e4SLinus Torvalds return psf; 29221da177e4SLinus Torvalds } 29231da177e4SLinus Torvalds 29241da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 29251da177e4SLinus Torvalds { 29261da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 29271da177e4SLinus Torvalds if (psf) 29281da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 29291da177e4SLinus Torvalds --pos; 29301da177e4SLinus Torvalds return pos ? NULL : psf; 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 293461fbab77Sstephen hemminger __acquires(rcu) 29351da177e4SLinus Torvalds { 293661fbab77Sstephen hemminger rcu_read_lock(); 29371da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 29411da177e4SLinus Torvalds { 29421da177e4SLinus Torvalds struct ip_sf_list *psf; 29431da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 29441da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 29451da177e4SLinus Torvalds else 29461da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 29471da177e4SLinus Torvalds ++*pos; 29481da177e4SLinus Torvalds return psf; 29491da177e4SLinus Torvalds } 29501da177e4SLinus Torvalds 29511da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 295261fbab77Sstephen hemminger __releases(rcu) 29531da177e4SLinus Torvalds { 29541da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 295500db4124SIan Morris if (likely(state->im)) { 29561da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 29571da177e4SLinus Torvalds state->im = NULL; 29581da177e4SLinus Torvalds } 29591da177e4SLinus Torvalds state->idev = NULL; 29601da177e4SLinus Torvalds state->dev = NULL; 296161fbab77Sstephen hemminger rcu_read_unlock(); 29621da177e4SLinus Torvalds } 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 29651da177e4SLinus Torvalds { 29661da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 29671da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 29701744bea1SJoe Perches seq_puts(seq, "Idx Device MCA SRC INC EXC\n"); 29711da177e4SLinus Torvalds } else { 29721da177e4SLinus Torvalds seq_printf(seq, 29731da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 29741da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 29751da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 29761da177e4SLinus Torvalds ntohl(state->im->multiaddr), 29771da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 29781da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 29791da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 29801da177e4SLinus Torvalds } 29811da177e4SLinus Torvalds return 0; 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 2984f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 29851da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 29861da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 29871da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 29881da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 29891da177e4SLinus Torvalds }; 29901da177e4SLinus Torvalds 29912c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 29927091e728SAlexey Dobriyan { 29937091e728SAlexey Dobriyan struct proc_dir_entry *pde; 299493a714d6SMadhu Challa int err; 29957091e728SAlexey Dobriyan 2996c3506372SChristoph Hellwig pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops, 2997c3506372SChristoph Hellwig sizeof(struct igmp_mc_iter_state)); 29987091e728SAlexey Dobriyan if (!pde) 29997091e728SAlexey Dobriyan goto out_igmp; 3000c3506372SChristoph Hellwig pde = proc_create_net("mcfilter", 0444, net->proc_net, 3001c3506372SChristoph Hellwig &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state)); 30027091e728SAlexey Dobriyan if (!pde) 30037091e728SAlexey Dobriyan goto out_mcfilter; 300493a714d6SMadhu Challa err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET, 300593a714d6SMadhu Challa SOCK_DGRAM, 0, net); 300693a714d6SMadhu Challa if (err < 0) { 300793a714d6SMadhu Challa pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n", 300893a714d6SMadhu Challa err); 300993a714d6SMadhu Challa goto out_sock; 301093a714d6SMadhu Challa } 301193a714d6SMadhu Challa 30127091e728SAlexey Dobriyan return 0; 30137091e728SAlexey Dobriyan 301493a714d6SMadhu Challa out_sock: 301593a714d6SMadhu Challa remove_proc_entry("mcfilter", net->proc_net); 30167091e728SAlexey Dobriyan out_mcfilter: 3017ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 30187091e728SAlexey Dobriyan out_igmp: 30197091e728SAlexey Dobriyan return -ENOMEM; 30207091e728SAlexey Dobriyan } 30217091e728SAlexey Dobriyan 30222c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 30237091e728SAlexey Dobriyan { 3024ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net); 3025ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 302693a714d6SMadhu Challa inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk); 30277091e728SAlexey Dobriyan } 30287091e728SAlexey Dobriyan 30297091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 30307091e728SAlexey Dobriyan .init = igmp_net_init, 30317091e728SAlexey Dobriyan .exit = igmp_net_exit, 30321da177e4SLinus Torvalds }; 303372c1d3bdSWANG Cong #endif 30341da177e4SLinus Torvalds 30354aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this, 30364aa5dee4SJiri Pirko unsigned long event, void *ptr) 30374aa5dee4SJiri Pirko { 30384aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 30394aa5dee4SJiri Pirko struct in_device *in_dev; 30404aa5dee4SJiri Pirko 30414aa5dee4SJiri Pirko switch (event) { 30424aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 30434aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev); 30444aa5dee4SJiri Pirko if (in_dev) 30454aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev); 30464aa5dee4SJiri Pirko break; 30474aa5dee4SJiri Pirko default: 30484aa5dee4SJiri Pirko break; 30494aa5dee4SJiri Pirko } 30504aa5dee4SJiri Pirko return NOTIFY_DONE; 30514aa5dee4SJiri Pirko } 30524aa5dee4SJiri Pirko 30534aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = { 30544aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event, 30554aa5dee4SJiri Pirko }; 30564aa5dee4SJiri Pirko 305772c1d3bdSWANG Cong int __init igmp_mc_init(void) 30581da177e4SLinus Torvalds { 305972c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS) 30604aa5dee4SJiri Pirko int err; 30614aa5dee4SJiri Pirko 30624aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops); 30634aa5dee4SJiri Pirko if (err) 30644aa5dee4SJiri Pirko return err; 30654aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier); 30664aa5dee4SJiri Pirko if (err) 30674aa5dee4SJiri Pirko goto reg_notif_fail; 30684aa5dee4SJiri Pirko return 0; 30694aa5dee4SJiri Pirko 30704aa5dee4SJiri Pirko reg_notif_fail: 30714aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops); 30724aa5dee4SJiri Pirko return err; 307372c1d3bdSWANG Cong #else 307472c1d3bdSWANG Cong return register_netdevice_notifier(&igmp_notifier); 30751da177e4SLinus Torvalds #endif 307672c1d3bdSWANG Cong } 3077