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> 751da177e4SLinus Torvalds #include <asm/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> 9214c85021SArnaldo Carvalho de Melo 93457c4cbcSEric W. Biederman #include <net/net_namespace.h> 9414c85021SArnaldo Carvalho de Melo #include <net/arp.h> 951da177e4SLinus Torvalds #include <net/ip.h> 961da177e4SLinus Torvalds #include <net/protocol.h> 971da177e4SLinus Torvalds #include <net/route.h> 981da177e4SLinus Torvalds #include <net/sock.h> 991da177e4SLinus Torvalds #include <net/checksum.h> 1001da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 1011da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1021da177e4SLinus Torvalds #include <linux/mroute.h> 1031da177e4SLinus Torvalds #endif 1041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1051da177e4SLinus Torvalds #include <linux/proc_fs.h> 1061da177e4SLinus Torvalds #include <linux/seq_file.h> 1071da177e4SLinus Torvalds #endif 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds #define IP_MAX_MEMBERSHIPS 20 1101da177e4SLinus Torvalds #define IP_MAX_MSF 10 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1131da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */ 1141da177e4SLinus Torvalds 115436f7c20SFabian Frederick #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ) 116436f7c20SFabian Frederick #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ) 117436f7c20SFabian Frederick #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ) 118436f7c20SFabian Frederick #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ) 119436f7c20SFabian Frederick #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ) 120436f7c20SFabian Frederick #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds 123436f7c20SFabian Frederick #define IGMP_INITIAL_REPORT_DELAY (1) 1241da177e4SLinus Torvalds 125436f7c20SFabian Frederick /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs! 1261da177e4SLinus Torvalds * IGMP specs require to report membership immediately after 1271da177e4SLinus Torvalds * joining a group, but we delay the first report by a 1281da177e4SLinus Torvalds * small interval. It seems more natural and still does not 1291da177e4SLinus Torvalds * contradict to specs provided this delay is small enough. 1301da177e4SLinus Torvalds */ 1311da177e4SLinus Torvalds 13242f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \ 133c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ 13442f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ 1351da177e4SLinus Torvalds ((in_dev)->mr_v1_seen && \ 1361da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v1_seen))) 13742f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \ 138c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ 13942f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ 1401da177e4SLinus Torvalds ((in_dev)->mr_v2_seen && \ 1411da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v2_seen))) 1421da177e4SLinus Torvalds 143cab70040SWilliam Manley static int unsolicited_report_interval(struct in_device *in_dev) 144cab70040SWilliam Manley { 1452690048cSWilliam Manley int interval_ms, interval_jiffies; 1462690048cSWilliam Manley 147cab70040SWilliam Manley if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 1482690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1492690048cSWilliam Manley in_dev, 1502690048cSWilliam Manley IGMPV2_UNSOLICITED_REPORT_INTERVAL); 151cab70040SWilliam Manley else /* v3 */ 1522690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1532690048cSWilliam Manley in_dev, 1542690048cSWilliam Manley IGMPV3_UNSOLICITED_REPORT_INTERVAL); 1552690048cSWilliam Manley 1562690048cSWilliam Manley interval_jiffies = msecs_to_jiffies(interval_ms); 1572690048cSWilliam Manley 1582690048cSWilliam Manley /* _timer functions can't handle a delay of 0 jiffies so ensure 1592690048cSWilliam Manley * we always return a positive value. 1602690048cSWilliam Manley */ 1612690048cSWilliam Manley if (interval_jiffies <= 0) 1622690048cSWilliam Manley interval_jiffies = 1; 1632690048cSWilliam Manley return interval_jiffies; 164cab70040SWilliam Manley } 165cab70040SWilliam Manley 1661da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im); 16763007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr); 1681da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev); 1691da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc); 1701da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc); 1711da177e4SLinus Torvalds #endif 1721da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc); 1738f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 1748f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta); 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds if (atomic_dec_and_test(&im->refcnt)) { 1791da177e4SLinus Torvalds in_dev_put(im->interface); 18042ea299dSLai Jiangshan kfree_rcu(im, rcu); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 184d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc) \ 185d9aa9380SDavid S. Miller for (pmc = rcu_dereference(in_dev->mc_list); \ 186d9aa9380SDavid S. Miller pmc != NULL; \ 187d9aa9380SDavid S. Miller pmc = rcu_dereference(pmc->next_rcu)) 188d9aa9380SDavid S. Miller 189d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc) \ 190d9aa9380SDavid S. Miller for (pmc = rtnl_dereference(in_dev->mc_list); \ 191d9aa9380SDavid S. Miller pmc != NULL; \ 192d9aa9380SDavid S. Miller pmc = rtnl_dereference(pmc->next_rcu)) 193d9aa9380SDavid S. Miller 1941da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* 1971da177e4SLinus Torvalds * Timer management 1981da177e4SLinus Torvalds */ 1991da177e4SLinus Torvalds 2001d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im) 2011da177e4SLinus Torvalds { 2021da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2031da177e4SLinus Torvalds if (del_timer(&im->timer)) 2041da177e4SLinus Torvalds atomic_dec(&im->refcnt); 2051da177e4SLinus Torvalds im->tm_running = 0; 2061da177e4SLinus Torvalds im->reporter = 0; 2071da177e4SLinus Torvalds im->unsolicit_count = 0; 2081da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* It must be called with locked im->lock */ 2121da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay) 2131da177e4SLinus Torvalds { 21463862b5bSAruna-Hewapathirane int tv = prandom_u32() % max_delay; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds im->tm_running = 1; 2171da177e4SLinus Torvalds if (!mod_timer(&im->timer, jiffies+tv+2)) 2181da177e4SLinus Torvalds atomic_inc(&im->refcnt); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev) 2221da177e4SLinus Torvalds { 22363862b5bSAruna-Hewapathirane int tv = prandom_u32() % in_dev->mr_maxdelay; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds in_dev->mr_gq_running = 1; 2261da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2)) 2271da177e4SLinus Torvalds in_dev_hold(in_dev); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) 2311da177e4SLinus Torvalds { 23263862b5bSAruna-Hewapathirane int tv = prandom_u32() % delay; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) 2351da177e4SLinus Torvalds in_dev_hold(in_dev); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2411da177e4SLinus Torvalds im->unsolicit_count = 0; 2421da177e4SLinus Torvalds if (del_timer(&im->timer)) { 2431da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) { 2441da177e4SLinus Torvalds add_timer(&im->timer); 2451da177e4SLinus Torvalds im->tm_running = 1; 2461da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2471da177e4SLinus Torvalds return; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds atomic_dec(&im->refcnt); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds igmp_start_timer(im, max_delay); 2521da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* 2571da177e4SLinus Torvalds * Send an IGMP report. 2581da177e4SLinus Torvalds */ 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, 2641da177e4SLinus Torvalds int gdeleted, int sdeleted) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds switch (type) { 2671da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE: 2681da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE: 2691da177e4SLinus Torvalds if (gdeleted || sdeleted) 2701da177e4SLinus Torvalds return 0; 271ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) { 272ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE) 273ad12583fSDavid L Stevens return 1; 274ad12583fSDavid L Stevens /* don't include if this source is excluded 275ad12583fSDavid L Stevens * in all filters 276ad12583fSDavid L Stevens */ 277ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE]) 278ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE; 279ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] == 280ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]; 281ad12583fSDavid L Stevens } 282ad12583fSDavid L Stevens return 0; 2831da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE: 2841da177e4SLinus Torvalds if (gdeleted || sdeleted) 2851da177e4SLinus Torvalds return 0; 2861da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0; 2871da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE: 2881da177e4SLinus Torvalds if (gdeleted || sdeleted) 2891da177e4SLinus Torvalds return 0; 2901da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 || 2911da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE]) 2921da177e4SLinus Torvalds return 0; 2931da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] == 2941da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]; 2951da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES: 2961da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount) 2971da177e4SLinus Torvalds return 0; 2981da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; 2991da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES: 3001da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) 3011da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted); 3021da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds return 0; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds static int 3081da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds struct ip_sf_list *psf; 3111da177e4SLinus Torvalds int scount = 0; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 3141da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 3151da177e4SLinus Torvalds continue; 3161da177e4SLinus Torvalds scount++; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds return scount; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 321*4c672e4bSDaniel Borkmann static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds struct sk_buff *skb; 3241da177e4SLinus Torvalds struct rtable *rt; 3251da177e4SLinus Torvalds struct iphdr *pip; 3261da177e4SLinus Torvalds struct igmpv3_report *pig; 327877acedcSDaniel Lezcano struct net *net = dev_net(dev); 32831e4543dSDavid S. Miller struct flowi4 fl4; 32966088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 33066088243SHerbert Xu int tlen = dev->needed_tailroom; 331*4c672e4bSDaniel Borkmann unsigned int size = mtu; 3321da177e4SLinus Torvalds 33357e1ab6eSEric Dumazet while (1) { 33466088243SHerbert Xu skb = alloc_skb(size + hlen + tlen, 33557e1ab6eSEric Dumazet GFP_ATOMIC | __GFP_NOWARN); 33657e1ab6eSEric Dumazet if (skb) 33757e1ab6eSEric Dumazet break; 33857e1ab6eSEric Dumazet size >>= 1; 33957e1ab6eSEric Dumazet if (size < 256) 3401da177e4SLinus Torvalds return NULL; 34157e1ab6eSEric Dumazet } 3429d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 3431da177e4SLinus Torvalds 34431e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, 34578fbfd8aSDavid S. Miller 0, 0, 34678fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 347b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 3481da177e4SLinus Torvalds kfree_skb(skb); 3491da177e4SLinus Torvalds return NULL; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 352d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 3531da177e4SLinus Torvalds skb->dev = dev; 3541da177e4SLinus Torvalds 355*4c672e4bSDaniel Borkmann skb->reserved_tailroom = skb_end_offset(skb) - 356*4c672e4bSDaniel Borkmann min(mtu, skb_end_offset(skb)); 35766088243SHerbert Xu skb_reserve(skb, hlen); 3581da177e4SLinus Torvalds 3597e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 360eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb); 3617e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds pip->version = 4; 3641da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2; 3651da177e4SLinus Torvalds pip->tos = 0xc0; 3661da177e4SLinus Torvalds pip->frag_off = htons(IP_DF); 3671da177e4SLinus Torvalds pip->ttl = 1; 368492f64ceSDavid S. Miller pip->daddr = fl4.daddr; 369492f64ceSDavid S. Miller pip->saddr = fl4.saddr; 3701da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP; 3711da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */ 37273f156a6SEric Dumazet ip_select_ident(skb, NULL); 3731da177e4SLinus Torvalds ((u8 *)&pip[1])[0] = IPOPT_RA; 3741da177e4SLinus Torvalds ((u8 *)&pip[1])[1] = 4; 3751da177e4SLinus Torvalds ((u8 *)&pip[1])[2] = 0; 3761da177e4SLinus Torvalds ((u8 *)&pip[1])[3] = 0; 3771da177e4SLinus Torvalds 378b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; 379d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig)); 380d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb); 3811da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; 3821da177e4SLinus Torvalds pig->resv1 = 0; 3831da177e4SLinus Torvalds pig->csum = 0; 3841da177e4SLinus Torvalds pig->resv2 = 0; 3851da177e4SLinus Torvalds pig->ngrec = 0; 3861da177e4SLinus Torvalds return skb; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb) 3901da177e4SLinus Torvalds { 391d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb); 392f7c0c2aeSSimon Horman const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb); 3931da177e4SLinus Torvalds 394d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); 3951da177e4SLinus Torvalds 396c439cb2eSHerbert Xu return ip_local_out(skb); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, 4051da177e4SLinus Torvalds int type, struct igmpv3_grec **ppgr) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4081da177e4SLinus Torvalds struct igmpv3_report *pih; 4091da177e4SLinus Torvalds struct igmpv3_grec *pgr; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds if (!skb) 4121da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4131da177e4SLinus Torvalds if (!skb) 4141da177e4SLinus Torvalds return NULL; 4151da177e4SLinus Torvalds pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec)); 4161da177e4SLinus Torvalds pgr->grec_type = type; 4171da177e4SLinus Torvalds pgr->grec_auxwords = 0; 4181da177e4SLinus Torvalds pgr->grec_nsrcs = 0; 4191da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr; 420d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb); 4211da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1); 4221da177e4SLinus Torvalds *ppgr = pgr; 4231da177e4SLinus Torvalds return skb; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 426*4c672e4bSDaniel Borkmann #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0) 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, 4291da177e4SLinus Torvalds int type, int gdeleted, int sdeleted) 4301da177e4SLinus Torvalds { 4311da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4321da177e4SLinus Torvalds struct igmpv3_report *pih; 4331da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL; 4341da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; 435ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 4381da177e4SLinus Torvalds return skb; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds isquery = type == IGMPV3_MODE_IS_INCLUDE || 4411da177e4SLinus Torvalds type == IGMPV3_MODE_IS_EXCLUDE; 4421da177e4SLinus Torvalds truncate = type == IGMPV3_MODE_IS_EXCLUDE || 4431da177e4SLinus Torvalds type == IGMPV3_CHANGE_TO_EXCLUDE; 4441da177e4SLinus Torvalds 445ad12583fSDavid L Stevens stotal = scount = 0; 446ad12583fSDavid L Stevens 4471da177e4SLinus Torvalds psf_list = sdeleted ? &pmc->tomb : &pmc->sources; 4481da177e4SLinus Torvalds 449ad12583fSDavid L Stevens if (!*psf_list) 450ad12583fSDavid L Stevens goto empty_source; 451ad12583fSDavid L Stevens 452d9edf9e2SArnaldo Carvalho de Melo pih = skb ? igmpv3_report_hdr(skb) : NULL; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* EX and TO_EX get a fresh packet, if needed */ 4551da177e4SLinus Torvalds if (truncate) { 4561da177e4SLinus Torvalds if (pih && pih->ngrec && 4571da177e4SLinus Torvalds AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { 4581da177e4SLinus Torvalds if (skb) 4591da177e4SLinus Torvalds igmpv3_sendpack(skb); 4601da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds first = 1; 4641da177e4SLinus Torvalds psf_prev = NULL; 4651da177e4SLinus Torvalds for (psf = *psf_list; psf; psf = psf_next) { 466ea4d9e72SAl Viro __be32 *psrc; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds psf_next = psf->sf_next; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { 4711da177e4SLinus Torvalds psf_prev = psf; 4721da177e4SLinus Torvalds continue; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds /* clear marks on query responses */ 4761da177e4SLinus Torvalds if (isquery) 4771da177e4SLinus Torvalds psf->sf_gsresp = 0; 4781da177e4SLinus Torvalds 47963007727SAl Viro if (AVAILABLE(skb) < sizeof(__be32) + 4801da177e4SLinus Torvalds first*sizeof(struct igmpv3_grec)) { 4811da177e4SLinus Torvalds if (truncate && !first) 4821da177e4SLinus Torvalds break; /* truncate these */ 4831da177e4SLinus Torvalds if (pgr) 4841da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 4851da177e4SLinus Torvalds if (skb) 4861da177e4SLinus Torvalds igmpv3_sendpack(skb); 4871da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4881da177e4SLinus Torvalds first = 1; 4891da177e4SLinus Torvalds scount = 0; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds if (first) { 4921da177e4SLinus Torvalds skb = add_grhead(skb, pmc, type, &pgr); 4931da177e4SLinus Torvalds first = 0; 4941da177e4SLinus Torvalds } 495cc63f70bSAlexey Dobriyan if (!skb) 496cc63f70bSAlexey Dobriyan return NULL; 49763007727SAl Viro psrc = (__be32 *)skb_put(skb, sizeof(__be32)); 4981da177e4SLinus Torvalds *psrc = psf->sf_inaddr; 499ad12583fSDavid L Stevens scount++; stotal++; 5001da177e4SLinus Torvalds if ((type == IGMPV3_ALLOW_NEW_SOURCES || 5011da177e4SLinus Torvalds type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { 5021da177e4SLinus Torvalds psf->sf_crcount--; 5031da177e4SLinus Torvalds if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { 5041da177e4SLinus Torvalds if (psf_prev) 5051da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5061da177e4SLinus Torvalds else 5071da177e4SLinus Torvalds *psf_list = psf->sf_next; 5081da177e4SLinus Torvalds kfree(psf); 5091da177e4SLinus Torvalds continue; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds psf_prev = psf; 5131da177e4SLinus Torvalds } 514ad12583fSDavid L Stevens 515ad12583fSDavid L Stevens empty_source: 516ad12583fSDavid L Stevens if (!stotal) { 517ad12583fSDavid L Stevens if (type == IGMPV3_ALLOW_NEW_SOURCES || 518ad12583fSDavid L Stevens type == IGMPV3_BLOCK_OLD_SOURCES) 519ad12583fSDavid L Stevens return skb; 520ad12583fSDavid L Stevens if (pmc->crcount || isquery) { 521ad12583fSDavid L Stevens /* make sure we have room for group header */ 522ad12583fSDavid L Stevens if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) { 523ad12583fSDavid L Stevens igmpv3_sendpack(skb); 524ad12583fSDavid L Stevens skb = NULL; /* add_grhead will get a new one */ 525ad12583fSDavid L Stevens } 526ad12583fSDavid L Stevens skb = add_grhead(skb, pmc, type, &pgr); 527ad12583fSDavid L Stevens } 528ad12583fSDavid L Stevens } 5291da177e4SLinus Torvalds if (pgr) 5301da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds if (isquery) 5331da177e4SLinus Torvalds pmc->gsquery = 0; /* clear query state on report */ 5341da177e4SLinus Torvalds return skb; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) 5381da177e4SLinus Torvalds { 5391da177e4SLinus Torvalds struct sk_buff *skb = NULL; 5401da177e4SLinus Torvalds int type; 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds if (!pmc) { 5431d7138deSEric Dumazet rcu_read_lock(); 5441d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 5451da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 5461da177e4SLinus Torvalds continue; 5471da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5481da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5491da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5501da177e4SLinus Torvalds else 5511da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5521da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5531da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5541da177e4SLinus Torvalds } 5551d7138deSEric Dumazet rcu_read_unlock(); 5561da177e4SLinus Torvalds } else { 5571da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5581da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5591da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5601da177e4SLinus Torvalds else 5611da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5621da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5631da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds if (!skb) 5661da177e4SLinus Torvalds return 0; 5671da177e4SLinus Torvalds return igmpv3_sendpack(skb); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds /* 5711da177e4SLinus Torvalds * remove zero-count source records from a source filter list 5721da177e4SLinus Torvalds */ 5731da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) 5741da177e4SLinus Torvalds { 5751da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf; 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds psf_prev = NULL; 5781da177e4SLinus Torvalds for (psf = *ppsf; psf; psf = psf_next) { 5791da177e4SLinus Torvalds psf_next = psf->sf_next; 5801da177e4SLinus Torvalds if (psf->sf_crcount == 0) { 5811da177e4SLinus Torvalds if (psf_prev) 5821da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5831da177e4SLinus Torvalds else 5841da177e4SLinus Torvalds *ppsf = psf->sf_next; 5851da177e4SLinus Torvalds kfree(psf); 5861da177e4SLinus Torvalds } else 5871da177e4SLinus Torvalds psf_prev = psf; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev) 5921da177e4SLinus Torvalds { 5931da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next; 5941da177e4SLinus Torvalds struct sk_buff *skb = NULL; 5951da177e4SLinus Torvalds int type, dtype; 5961da177e4SLinus Torvalds 5971d7138deSEric Dumazet rcu_read_lock(); 5981da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* deleted MCA's */ 6011da177e4SLinus Torvalds pmc_prev = NULL; 6021da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) { 6031da177e4SLinus Torvalds pmc_next = pmc->next; 6041da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 6051da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6061da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6071da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6081da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds if (pmc->crcount) { 6111da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) { 6121da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6131da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6141da177e4SLinus Torvalds } 615ad12583fSDavid L Stevens pmc->crcount--; 6161da177e4SLinus Torvalds if (pmc->crcount == 0) { 6171da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb); 6181da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources); 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { 6221da177e4SLinus Torvalds if (pmc_prev) 6231da177e4SLinus Torvalds pmc_prev->next = pmc_next; 6241da177e4SLinus Torvalds else 6251da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next; 6261da177e4SLinus Torvalds in_dev_put(pmc->interface); 6271da177e4SLinus Torvalds kfree(pmc); 6281da177e4SLinus Torvalds } else 6291da177e4SLinus Torvalds pmc_prev = pmc; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds /* change recs */ 6341d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 6351da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6361da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 6371da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6381da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES; 6391da177e4SLinus Torvalds } else { 6401da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES; 6411da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6441da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* filter mode changes */ 6471da177e4SLinus Torvalds if (pmc->crcount) { 6481da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) 6491da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE; 6501da177e4SLinus Torvalds else 6511da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6521da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 653ad12583fSDavid L Stevens pmc->crcount--; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6561da177e4SLinus Torvalds } 6571d7138deSEric Dumazet rcu_read_unlock(); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds if (!skb) 6601da177e4SLinus Torvalds return; 6611da177e4SLinus Torvalds (void) igmpv3_sendpack(skb); 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, 6651da177e4SLinus Torvalds int type) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds struct sk_buff *skb; 6681da177e4SLinus Torvalds struct iphdr *iph; 6691da177e4SLinus Torvalds struct igmphdr *ih; 6701da177e4SLinus Torvalds struct rtable *rt; 6711da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 672877acedcSDaniel Lezcano struct net *net = dev_net(dev); 67363007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0; 67431e4543dSDavid S. Miller struct flowi4 fl4; 67563007727SAl Viro __be32 dst; 67666088243SHerbert Xu int hlen, tlen; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) 6791da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc); 6801da177e4SLinus Torvalds else if (type == IGMP_HOST_LEAVE_MESSAGE) 6811da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER; 6821da177e4SLinus Torvalds else 6831da177e4SLinus Torvalds dst = group; 6841da177e4SLinus Torvalds 68531e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, 68678fbfd8aSDavid S. Miller 0, 0, 68778fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 688b23dd4feSDavid S. Miller if (IS_ERR(rt)) 6891da177e4SLinus Torvalds return -1; 69078fbfd8aSDavid S. Miller 69166088243SHerbert Xu hlen = LL_RESERVED_SPACE(dev); 69266088243SHerbert Xu tlen = dev->needed_tailroom; 69366088243SHerbert Xu skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); 6941da177e4SLinus Torvalds if (skb == NULL) { 6951da177e4SLinus Torvalds ip_rt_put(rt); 6961da177e4SLinus Torvalds return -1; 6971da177e4SLinus Torvalds } 6989d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 6991da177e4SLinus Torvalds 700d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 7011da177e4SLinus Torvalds 70266088243SHerbert Xu skb_reserve(skb, hlen); 7031da177e4SLinus Torvalds 7047e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 705eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 7067e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds iph->version = 4; 7091da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2; 7101da177e4SLinus Torvalds iph->tos = 0xc0; 7111da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 7121da177e4SLinus Torvalds iph->ttl = 1; 7131da177e4SLinus Torvalds iph->daddr = dst; 714492f64ceSDavid S. Miller iph->saddr = fl4.saddr; 7151da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP; 71673f156a6SEric Dumazet ip_select_ident(skb, NULL); 7171da177e4SLinus Torvalds ((u8 *)&iph[1])[0] = IPOPT_RA; 7181da177e4SLinus Torvalds ((u8 *)&iph[1])[1] = 4; 7191da177e4SLinus Torvalds ((u8 *)&iph[1])[2] = 0; 7201da177e4SLinus Torvalds ((u8 *)&iph[1])[3] = 0; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 7231da177e4SLinus Torvalds ih->type = type; 7241da177e4SLinus Torvalds ih->code = 0; 7251da177e4SLinus Torvalds ih->csum = 0; 7261da177e4SLinus Torvalds ih->group = group; 7271da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 7281da177e4SLinus Torvalds 729c439cb2eSHerbert Xu return ip_local_out(skb); 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds static void igmp_gq_timer_expire(unsigned long data) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 7371da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL); 738e2401654SSalam Noureddine in_dev_put(in_dev); 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds static void igmp_ifc_timer_expire(unsigned long data) 7421da177e4SLinus Torvalds { 7431da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 7461da177e4SLinus Torvalds if (in_dev->mr_ifc_count) { 7471da177e4SLinus Torvalds in_dev->mr_ifc_count--; 748cab70040SWilliam Manley igmp_ifc_start_timer(in_dev, 749cab70040SWilliam Manley unsolicited_report_interval(in_dev)); 7501da177e4SLinus Torvalds } 751e2401654SSalam Noureddine in_dev_put(in_dev); 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 7551da177e4SLinus Torvalds { 7561da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 7571da177e4SLinus Torvalds return; 758a9fe8e29SHannes Frederic Sowa in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv; 7591da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds static void igmp_timer_expire(unsigned long data) 7641da177e4SLinus Torvalds { 7651da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)data; 7661da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds spin_lock(&im->lock); 7691da177e4SLinus Torvalds im->tm_running = 0; 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds if (im->unsolicit_count) { 7721da177e4SLinus Torvalds im->unsolicit_count--; 773cab70040SWilliam Manley igmp_start_timer(im, unsolicited_report_interval(in_dev)); 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds im->reporter = 1; 7761da177e4SLinus Torvalds spin_unlock(&im->lock); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 7791da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 7801da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 7811da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 7821da177e4SLinus Torvalds else 7831da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds ip_ma_put(im); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 788ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 789ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds struct ip_sf_list *psf; 7921da177e4SLinus Torvalds int i, scount; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds scount = 0; 7951da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 7961da177e4SLinus Torvalds if (scount == nsrcs) 7971da177e4SLinus Torvalds break; 798ad12583fSDavid L Stevens for (i = 0; i < nsrcs; i++) { 799ad12583fSDavid L Stevens /* skip inactive filters */ 800e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] || 801ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 802ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 803ce713ee5SRongQing.Li break; 804ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 805ad12583fSDavid L Stevens scount++; 806ad12583fSDavid L Stevens break; 807ad12583fSDavid L Stevens } 808ad12583fSDavid L Stevens } 809ad12583fSDavid L Stevens } 810ad12583fSDavid L Stevens pmc->gsquery = 0; 811ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 812ad12583fSDavid L Stevens return 0; 813ad12583fSDavid L Stevens return 1; 814ad12583fSDavid L Stevens } 815ad12583fSDavid L Stevens 816ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 817ad12583fSDavid L Stevens { 818ad12583fSDavid L Stevens struct ip_sf_list *psf; 819ad12583fSDavid L Stevens int i, scount; 820ad12583fSDavid L Stevens 821ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 822ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 823ad12583fSDavid L Stevens 824ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 825ad12583fSDavid L Stevens scount = 0; 826ad12583fSDavid L Stevens for (psf = pmc->sources; psf; psf = psf->sf_next) { 827ad12583fSDavid L Stevens if (scount == nsrcs) 828ad12583fSDavid L Stevens break; 8291da177e4SLinus Torvalds for (i = 0; i < nsrcs; i++) 8301da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 8311da177e4SLinus Torvalds psf->sf_gsresp = 1; 8321da177e4SLinus Torvalds scount++; 8331da177e4SLinus Torvalds break; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds } 836ad12583fSDavid L Stevens if (!scount) { 837ad12583fSDavid L Stevens pmc->gsquery = 0; 838ad12583fSDavid L Stevens return 0; 839ad12583fSDavid L Stevens } 840ad12583fSDavid L Stevens pmc->gsquery = 1; 841ad12583fSDavid L Stevens return 1; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 844d679c532SEric Dumazet /* return true if packet was dropped */ 845d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group) 8461da177e4SLinus Torvalds { 8471da177e4SLinus Torvalds struct ip_mc_list *im; 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 852d679c532SEric Dumazet return false; 8531da177e4SLinus Torvalds 8541d7138deSEric Dumazet rcu_read_lock(); 8551d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 8561da177e4SLinus Torvalds if (im->multiaddr == group) { 8571da177e4SLinus Torvalds igmp_stop_timer(im); 8581da177e4SLinus Torvalds break; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds } 8611d7138deSEric Dumazet rcu_read_unlock(); 862d679c532SEric Dumazet return false; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 865d679c532SEric Dumazet /* return true if packet was dropped */ 866d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 8671da177e4SLinus Torvalds int len) 8681da177e4SLinus Torvalds { 869d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 870d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 8711da177e4SLinus Torvalds struct ip_mc_list *im; 87263007727SAl Viro __be32 group = ih->group; 8731da177e4SLinus Torvalds int max_delay; 8741da177e4SLinus Torvalds int mark = 0; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds 8775b7c8406SDavid Stevens if (len == 8) { 8781da177e4SLinus Torvalds if (ih->code == 0) { 8791da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 8801da177e4SLinus Torvalds 881436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 8821da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 883436f7c20SFabian Frederick IGMP_V1_ROUTER_PRESENT_TIMEOUT; 8841da177e4SLinus Torvalds group = 0; 8851da177e4SLinus Torvalds } else { 8861da177e4SLinus Torvalds /* v2 router present */ 8871da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 8881da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 889436f7c20SFabian Frederick IGMP_V2_ROUTER_PRESENT_TIMEOUT; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds /* cancel the interface change timer */ 8921da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 8931da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 8941da177e4SLinus Torvalds __in_dev_put(in_dev); 8951da177e4SLinus Torvalds /* clear deleted report items */ 8961da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 8971da177e4SLinus Torvalds } else if (len < 12) { 898d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */ 8995b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) { 9005b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */ 901436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9025b7c8406SDavid Stevens group = 0; 9035b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) { 9045b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present; 9055b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here. 9065b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a 9075b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely 9085b7c8406SDavid Stevens * to be intended in a v3 query. 9095b7c8406SDavid Stevens */ 9105b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 911a8c1f65cSBen Hutchings if (!max_delay) 912a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */ 9131da177e4SLinus Torvalds } else { /* v3 */ 9141da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 915d679c532SEric Dumazet return true; 9161da177e4SLinus Torvalds 917d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9181da177e4SLinus Torvalds if (ih3->nsrcs) { 9191da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 92063007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 921d679c532SEric Dumazet return true; 922d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 9261da177e4SLinus Torvalds if (!max_delay) 9271da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 9281da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 9291da177e4SLinus Torvalds if (ih3->qrv) 9301da177e4SLinus Torvalds in_dev->mr_qrv = ih3->qrv; 9311da177e4SLinus Torvalds if (!group) { /* general query */ 9321da177e4SLinus Torvalds if (ih3->nsrcs) 933b47bd8d2SDaniel Borkmann return true; /* no sources allowed */ 9341da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 935d679c532SEric Dumazet return false; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 9381da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /* 9421da177e4SLinus Torvalds * - Start the timers in all of our membership records 9431da177e4SLinus Torvalds * that the query applies to for the interface on 9441da177e4SLinus Torvalds * which the query arrived excl. those that belong 9451da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 9461da177e4SLinus Torvalds * - For timers already running check if they need to 9471da177e4SLinus Torvalds * be reset. 9481da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 9491da177e4SLinus Torvalds * delay possible 9501da177e4SLinus Torvalds */ 9511d7138deSEric Dumazet rcu_read_lock(); 9521d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 953ad12583fSDavid L Stevens int changed; 954ad12583fSDavid L Stevens 9551da177e4SLinus Torvalds if (group && group != im->multiaddr) 9561da177e4SLinus Torvalds continue; 9571da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 9581da177e4SLinus Torvalds continue; 9591da177e4SLinus Torvalds spin_lock_bh(&im->lock); 9601da177e4SLinus Torvalds if (im->tm_running) 9611da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 9621da177e4SLinus Torvalds else 9631da177e4SLinus Torvalds im->gsquery = mark; 964ad12583fSDavid L Stevens changed = !im->gsquery || 9651da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 9661da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 967ad12583fSDavid L Stevens if (changed) 9681da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 9691da177e4SLinus Torvalds } 9701d7138deSEric Dumazet rcu_read_unlock(); 971d679c532SEric Dumazet return false; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 9749a57a9d2SEric Dumazet /* called in rcu_read_lock() section */ 9751da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 9761da177e4SLinus Torvalds { 9771da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 9781da177e4SLinus Torvalds struct igmphdr *ih; 9799a57a9d2SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(skb->dev); 9801da177e4SLinus Torvalds int len = skb->len; 981d679c532SEric Dumazet bool dropped = true; 9821da177e4SLinus Torvalds 983cd557bc1SDenis V. Lunev if (in_dev == NULL) 984cd557bc1SDenis V. Lunev goto drop; 9851da177e4SLinus Torvalds 986fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 9879a57a9d2SEric Dumazet goto drop; 988fb286bb2SHerbert Xu 989de08dc1aSTom Herbert if (skb_checksum_simple_validate(skb)) 9909a57a9d2SEric Dumazet goto drop; 9911da177e4SLinus Torvalds 992d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 9931da177e4SLinus Torvalds switch (ih->type) { 9941da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 995d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len); 9961da177e4SLinus Torvalds break; 9971da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 9981da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 9991da177e4SLinus Torvalds /* Is it our report looped back? */ 1000c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) 10011da177e4SLinus Torvalds break; 100224c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 100324c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 100424c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 1005d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group); 10061da177e4SLinus Torvalds break; 10071da177e4SLinus Torvalds case IGMP_PIM: 10081da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 10091da177e4SLinus Torvalds return pim_rcv_v1(skb); 10101da177e4SLinus Torvalds #endif 1011c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 10121da177e4SLinus Torvalds case IGMP_DVMRP: 10131da177e4SLinus Torvalds case IGMP_TRACE: 10141da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 10151da177e4SLinus Torvalds case IGMP_MTRACE: 10161da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 10171da177e4SLinus Torvalds break; 10181da177e4SLinus Torvalds default: 1019dd1c1853SLinus Torvalds break; 10201da177e4SLinus Torvalds } 1021fb286bb2SHerbert Xu 1022cd557bc1SDenis V. Lunev drop: 1023d679c532SEric Dumazet if (dropped) 10241da177e4SLinus Torvalds kfree_skb(skb); 1025d679c532SEric Dumazet else 1026d679c532SEric Dumazet consume_skb(skb); 10271da177e4SLinus Torvalds return 0; 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds #endif 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds /* 10341da177e4SLinus Torvalds * Add a filter to a device 10351da177e4SLinus Torvalds */ 10361da177e4SLinus Torvalds 103763007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10401da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 10431da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 1044b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode 10451da177e4SLinus Torvalds routine. Something sort of: 10461da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 10471da177e4SLinus Torvalds --ANK 10481da177e4SLinus Torvalds */ 10491da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 105022bedad3SJiri Pirko dev_mc_add(dev, buf); 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds /* 10541da177e4SLinus Torvalds * Remove a filter from a device 10551da177e4SLinus Torvalds */ 10561da177e4SLinus Torvalds 105763007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 10581da177e4SLinus Torvalds { 10591da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10601da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 106322bedad3SJiri Pirko dev_mc_del(dev, buf); 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 10671da177e4SLinus Torvalds /* 10681da177e4SLinus Torvalds * deleted ip_mc_list manipulation 10691da177e4SLinus Torvalds */ 10701da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im) 10711da177e4SLinus Torvalds { 10721da177e4SLinus Torvalds struct ip_mc_list *pmc; 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 10751da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 10761da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 10771da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 10781da177e4SLinus Torvalds * non-deleted or query-response MCA's. 10791da177e4SLinus Torvalds */ 10800da974f4SPanagiotis Issaris pmc = kzalloc(sizeof(*pmc), GFP_KERNEL); 10811da177e4SLinus Torvalds if (!pmc) 10821da177e4SLinus Torvalds return; 10831da177e4SLinus Torvalds spin_lock_bh(&im->lock); 10841da177e4SLinus Torvalds pmc->interface = im->interface; 10851da177e4SLinus Torvalds in_dev_hold(in_dev); 10861da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 1087a9fe8e29SHannes Frederic Sowa pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; 10881da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 10891da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 10901da177e4SLinus Torvalds struct ip_sf_list *psf; 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds pmc->tomb = im->tomb; 10931da177e4SLinus Torvalds pmc->sources = im->sources; 10941da177e4SLinus Torvalds im->tomb = im->sources = NULL; 10951da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 10961da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11011da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 11021da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 11031da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 110663007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr) 11071da177e4SLinus Torvalds { 11081da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 11091da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11121da177e4SLinus Torvalds pmc_prev = NULL; 11131da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) { 11141da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 11151da177e4SLinus Torvalds break; 11161da177e4SLinus Torvalds pmc_prev = pmc; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds if (pmc) { 11191da177e4SLinus Torvalds if (pmc_prev) 11201da177e4SLinus Torvalds pmc_prev->next = pmc->next; 11211da177e4SLinus Torvalds else 11221da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11251da177e4SLinus Torvalds if (pmc) { 11261da177e4SLinus Torvalds for (psf = pmc->tomb; psf; psf = psf_next) { 11271da177e4SLinus Torvalds psf_next = psf->sf_next; 11281da177e4SLinus Torvalds kfree(psf); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds in_dev_put(pmc->interface); 11311da177e4SLinus Torvalds kfree(pmc); 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 11361da177e4SLinus Torvalds { 11371da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11401da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 11411da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 11421da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 11451da177e4SLinus Torvalds nextpmc = pmc->next; 11461da177e4SLinus Torvalds ip_mc_clear_src(pmc); 11471da177e4SLinus Torvalds in_dev_put(pmc->interface); 11481da177e4SLinus Torvalds kfree(pmc); 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds /* clear dead sources, too */ 11511d7138deSEric Dumazet rcu_read_lock(); 11521d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 11531da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 11561da177e4SLinus Torvalds psf = pmc->tomb; 11571da177e4SLinus Torvalds pmc->tomb = NULL; 11581da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 11591da177e4SLinus Torvalds for (; psf; psf = psf_next) { 11601da177e4SLinus Torvalds psf_next = psf->sf_next; 11611da177e4SLinus Torvalds kfree(psf); 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds } 11641d7138deSEric Dumazet rcu_read_unlock(); 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds #endif 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im) 11691da177e4SLinus Torvalds { 11701da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 11711da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11721da177e4SLinus Torvalds int reporter; 11731da177e4SLinus Torvalds #endif 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds if (im->loaded) { 11761da177e4SLinus Torvalds im->loaded = 0; 11771da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11811da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 11821da177e4SLinus Torvalds return; 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds reporter = im->reporter; 11851da177e4SLinus Torvalds igmp_stop_timer(im); 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds if (!in_dev->dead) { 11881da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 118924cf3af3SVeaceslav Falico return; 11901da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 11911da177e4SLinus Torvalds if (reporter) 11921da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 119324cf3af3SVeaceslav Falico return; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds /* IGMPv3 */ 11961da177e4SLinus Torvalds igmpv3_add_delrec(in_dev, im); 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds igmp_ifc_event(in_dev); 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds #endif 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds static void igmp_group_added(struct ip_mc_list *im) 12041da177e4SLinus Torvalds { 12051da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds if (im->loaded == 0) { 12081da177e4SLinus Torvalds im->loaded = 1; 12091da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12131da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 12141da177e4SLinus Torvalds return; 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds if (in_dev->dead) 12171da177e4SLinus Torvalds return; 12181da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 12191da177e4SLinus Torvalds spin_lock_bh(&im->lock); 1220436f7c20SFabian Frederick igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY); 12211da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 12221da177e4SLinus Torvalds return; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds /* else, v3 */ 12251da177e4SLinus Torvalds 1226a9fe8e29SHannes Frederic Sowa im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; 12271da177e4SLinus Torvalds igmp_ifc_event(in_dev); 12281da177e4SLinus Torvalds #endif 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds /* 12331da177e4SLinus Torvalds * Multicast list managers 12341da177e4SLinus Torvalds */ 12351da177e4SLinus Torvalds 1236e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im) 1237e9897071SEric Dumazet { 1238c70eba74SEric Dumazet return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); 1239e9897071SEric Dumazet } 1240e9897071SEric Dumazet 1241e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev, 1242e9897071SEric Dumazet struct ip_mc_list *im) 1243e9897071SEric Dumazet { 1244e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 1245e9897071SEric Dumazet u32 hash; 1246e9897071SEric Dumazet 1247e9897071SEric Dumazet mc_hash = rtnl_dereference(in_dev->mc_hash); 1248e9897071SEric Dumazet if (mc_hash) { 1249e9897071SEric Dumazet hash = ip_mc_hash(im); 1250c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1251e9897071SEric Dumazet rcu_assign_pointer(mc_hash[hash], im); 1252e9897071SEric Dumazet return; 1253e9897071SEric Dumazet } 1254e9897071SEric Dumazet 1255e9897071SEric Dumazet /* do not use a hash table for small number of items */ 1256e9897071SEric Dumazet if (in_dev->mc_count < 4) 1257e9897071SEric Dumazet return; 1258e9897071SEric Dumazet 1259e9897071SEric Dumazet mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG, 1260e9897071SEric Dumazet GFP_KERNEL); 1261e9897071SEric Dumazet if (!mc_hash) 1262e9897071SEric Dumazet return; 1263e9897071SEric Dumazet 1264e9897071SEric Dumazet for_each_pmc_rtnl(in_dev, im) { 1265e9897071SEric Dumazet hash = ip_mc_hash(im); 1266c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1267e9897071SEric Dumazet RCU_INIT_POINTER(mc_hash[hash], im); 1268e9897071SEric Dumazet } 1269e9897071SEric Dumazet 1270e9897071SEric Dumazet rcu_assign_pointer(in_dev->mc_hash, mc_hash); 1271e9897071SEric Dumazet } 1272e9897071SEric Dumazet 1273e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev, 1274e9897071SEric Dumazet struct ip_mc_list *im) 1275e9897071SEric Dumazet { 1276e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); 1277e9897071SEric Dumazet struct ip_mc_list *aux; 1278e9897071SEric Dumazet 1279e9897071SEric Dumazet if (!mc_hash) 1280e9897071SEric Dumazet return; 1281e9897071SEric Dumazet mc_hash += ip_mc_hash(im); 1282e9897071SEric Dumazet while ((aux = rtnl_dereference(*mc_hash)) != im) 1283e9897071SEric Dumazet mc_hash = &aux->next_hash; 1284e9897071SEric Dumazet *mc_hash = im->next_hash; 1285e9897071SEric Dumazet } 1286e9897071SEric Dumazet 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds /* 12891da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 12901da177e4SLinus Torvalds */ 12911da177e4SLinus Torvalds 12928f935bbdSAl Viro void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 12931da177e4SLinus Torvalds { 12941da177e4SLinus Torvalds struct ip_mc_list *im; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds ASSERT_RTNL(); 12971da177e4SLinus Torvalds 12981d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) { 12991da177e4SLinus Torvalds if (im->multiaddr == addr) { 13001da177e4SLinus Torvalds im->users++; 13011da177e4SLinus Torvalds ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0); 13021da177e4SLinus Torvalds goto out; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13061d7138deSEric Dumazet im = kzalloc(sizeof(*im), GFP_KERNEL); 13071da177e4SLinus Torvalds if (!im) 13081da177e4SLinus Torvalds goto out; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds im->users = 1; 13111da177e4SLinus Torvalds im->interface = in_dev; 13121da177e4SLinus Torvalds in_dev_hold(in_dev); 13131da177e4SLinus Torvalds im->multiaddr = addr; 13141da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 13151da177e4SLinus Torvalds im->sfmode = MCAST_EXCLUDE; 13161da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE] = 1; 13171da177e4SLinus Torvalds atomic_set(&im->refcnt, 1); 13181da177e4SLinus Torvalds spin_lock_init(&im->lock); 13191da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1320179542a5SHimangi Saraogi setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im); 1321a9fe8e29SHannes Frederic Sowa im->unsolicit_count = sysctl_igmp_qrv; 13221da177e4SLinus Torvalds #endif 13231d7138deSEric Dumazet 13241d7138deSEric Dumazet im->next_rcu = in_dev->mc_list; 1325b8bae41eSRami Rosen in_dev->mc_count++; 1326cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im); 13271d7138deSEric Dumazet 1328e9897071SEric Dumazet ip_mc_hash_add(in_dev, im); 1329e9897071SEric Dumazet 13301da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13311da177e4SLinus Torvalds igmpv3_del_delrec(in_dev, im->multiaddr); 13321da177e4SLinus Torvalds #endif 13331da177e4SLinus Torvalds igmp_group_added(im); 13341da177e4SLinus Torvalds if (!in_dev->dead) 13351da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 13361da177e4SLinus Torvalds out: 13371da177e4SLinus Torvalds return; 13381da177e4SLinus Torvalds } 13394bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group); 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds /* 13424aa5dee4SJiri Pirko * Resend IGMP JOIN report; used by netdev notifier. 1343a816c7c7SJay Vosburgh */ 13444aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev) 1345a816c7c7SJay Vosburgh { 134608882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1347866f3b25SEric Dumazet struct ip_mc_list *im; 1348866f3b25SEric Dumazet int type; 1349a816c7c7SJay Vosburgh 13504aa5dee4SJiri Pirko ASSERT_RTNL(); 13514aa5dee4SJiri Pirko 13524aa5dee4SJiri Pirko for_each_pmc_rtnl(in_dev, im) { 1353a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1354866f3b25SEric Dumazet continue; 1355a816c7c7SJay Vosburgh 1356e12b4539SFlavio Leitner /* a failover is happening and switches 1357866f3b25SEric Dumazet * must be notified immediately 1358866f3b25SEric Dumazet */ 1359e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev)) 1360866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT; 1361e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev)) 1362866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT; 1363e12b4539SFlavio Leitner else 1364866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT; 1365866f3b25SEric Dumazet igmp_send_report(in_dev, im, type); 1366866f3b25SEric Dumazet } 1367a816c7c7SJay Vosburgh #endif 1368a816c7c7SJay Vosburgh } 1369a816c7c7SJay Vosburgh 1370a816c7c7SJay Vosburgh /* 13711da177e4SLinus Torvalds * A socket has left a multicast group on device dev 13721da177e4SLinus Torvalds */ 13731da177e4SLinus Torvalds 13748f935bbdSAl Viro void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) 13751da177e4SLinus Torvalds { 13761d7138deSEric Dumazet struct ip_mc_list *i; 13771d7138deSEric Dumazet struct ip_mc_list __rcu **ip; 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds ASSERT_RTNL(); 13801da177e4SLinus Torvalds 13811d7138deSEric Dumazet for (ip = &in_dev->mc_list; 13821d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL; 13831d7138deSEric Dumazet ip = &i->next_rcu) { 13841da177e4SLinus Torvalds if (i->multiaddr == addr) { 13851da177e4SLinus Torvalds if (--i->users == 0) { 1386e9897071SEric Dumazet ip_mc_hash_remove(in_dev, i); 13871d7138deSEric Dumazet *ip = i->next_rcu; 1388b8bae41eSRami Rosen in_dev->mc_count--; 13891da177e4SLinus Torvalds igmp_group_dropped(i); 139024cf3af3SVeaceslav Falico ip_mc_clear_src(i); 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds if (!in_dev->dead) 13931da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds ip_ma_put(i); 13961da177e4SLinus Torvalds return; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds break; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds } 14024bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_dec_group); 14031da177e4SLinus Torvalds 140475c78500SMoni Shoua /* Device changing type */ 140575c78500SMoni Shoua 140675c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 140775c78500SMoni Shoua { 14081d7138deSEric Dumazet struct ip_mc_list *pmc; 140975c78500SMoni Shoua 141075c78500SMoni Shoua ASSERT_RTNL(); 141175c78500SMoni Shoua 14121d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14131d7138deSEric Dumazet igmp_group_dropped(pmc); 141475c78500SMoni Shoua } 141575c78500SMoni Shoua 141675c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 141775c78500SMoni Shoua { 14181d7138deSEric Dumazet struct ip_mc_list *pmc; 141975c78500SMoni Shoua 142075c78500SMoni Shoua ASSERT_RTNL(); 142175c78500SMoni Shoua 14221d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14231d7138deSEric Dumazet igmp_group_added(pmc); 142475c78500SMoni Shoua } 142575c78500SMoni Shoua 14261da177e4SLinus Torvalds /* Device going down */ 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 14291da177e4SLinus Torvalds { 14301d7138deSEric Dumazet struct ip_mc_list *pmc; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds ASSERT_RTNL(); 14331da177e4SLinus Torvalds 14341d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14351d7138deSEric Dumazet igmp_group_dropped(pmc); 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 14381da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 14391da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 14401da177e4SLinus Torvalds __in_dev_put(in_dev); 14411da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 14421da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 14431da177e4SLinus Torvalds __in_dev_put(in_dev); 14441da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 14451da177e4SLinus Torvalds #endif 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 14511da177e4SLinus Torvalds { 14521da177e4SLinus Torvalds ASSERT_RTNL(); 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1455b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 1456b24b8a24SPavel Emelyanov (unsigned long)in_dev); 1457b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 1458b24b8a24SPavel Emelyanov (unsigned long)in_dev); 1459a9fe8e29SHannes Frederic Sowa in_dev->mr_qrv = sysctl_igmp_qrv; 14601da177e4SLinus Torvalds #endif 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds /* Device going up */ 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 14681da177e4SLinus Torvalds { 14691d7138deSEric Dumazet struct ip_mc_list *pmc; 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds ASSERT_RTNL(); 14721da177e4SLinus Torvalds 1473a9fe8e29SHannes Frederic Sowa #ifdef CONFIG_IP_MULTICAST 1474a9fe8e29SHannes Frederic Sowa in_dev->mr_qrv = sysctl_igmp_qrv; 1475a9fe8e29SHannes Frederic Sowa #endif 14761da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 14771da177e4SLinus Torvalds 14781d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14791d7138deSEric Dumazet igmp_group_added(pmc); 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds /* 14831da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 14841da177e4SLinus Torvalds */ 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 14871da177e4SLinus Torvalds { 14881da177e4SLinus Torvalds struct ip_mc_list *i; 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds ASSERT_RTNL(); 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds /* Deactivate timers */ 14931da177e4SLinus Torvalds ip_mc_down(in_dev); 14941da177e4SLinus Torvalds 14951d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { 14961d7138deSEric Dumazet in_dev->mc_list = i->next_rcu; 1497b8bae41eSRami Rosen in_dev->mc_count--; 14981d7138deSEric Dumazet 149924cf3af3SVeaceslav Falico /* We've dropped the groups in ip_mc_down already */ 150024cf3af3SVeaceslav Falico ip_mc_clear_src(i); 15011da177e4SLinus Torvalds ip_ma_put(i); 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15059e917dcaSEric Dumazet /* RTNL is locked */ 1506877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 15071da177e4SLinus Torvalds { 15081da177e4SLinus Torvalds struct net_device *dev = NULL; 15091da177e4SLinus Torvalds struct in_device *idev = NULL; 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds if (imr->imr_ifindex) { 1512877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 15131da177e4SLinus Torvalds return idev; 15141da177e4SLinus Torvalds } 15151da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 15169e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false); 15171da177e4SLinus Torvalds if (!dev) 15181da177e4SLinus Torvalds return NULL; 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 1521b23dd4feSDavid S. Miller if (!dev) { 152278fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, 152378fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr, 152478fbfd8aSDavid S. Miller 0, 0, 0); 1525b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1526d8d1f30bSChangli Gao dev = rt->dst.dev; 15271da177e4SLinus Torvalds ip_rt_put(rt); 15281da177e4SLinus Torvalds } 1529b23dd4feSDavid S. Miller } 15301da177e4SLinus Torvalds if (dev) { 15311da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1532e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds return idev; 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds /* 15381da177e4SLinus Torvalds * Join a socket to a group 15391da177e4SLinus Torvalds */ 1540ab32ea5dSBrian Haley int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS; 1541ab32ea5dSBrian Haley int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF; 1542a9fe8e29SHannes Frederic Sowa #ifdef CONFIG_IP_MULTICAST 1543436f7c20SFabian Frederick int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE; 1544a9fe8e29SHannes Frederic Sowa #endif 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 15478f935bbdSAl Viro __be32 *psfsrc) 15481da177e4SLinus Torvalds { 15491da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 15501da177e4SLinus Torvalds int rv = 0; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds psf_prev = NULL; 15531da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 15541da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 15551da177e4SLinus Torvalds break; 15561da177e4SLinus Torvalds psf_prev = psf; 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 15591da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 15601da177e4SLinus Torvalds return -ESRCH; 15611da177e4SLinus Torvalds } 15621da177e4SLinus Torvalds psf->sf_count[sfmode]--; 15631da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 15641da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 15671da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15681da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 15691da177e4SLinus Torvalds #endif 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds /* no more filters for this source */ 15721da177e4SLinus Torvalds if (psf_prev) 15731da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 15741da177e4SLinus Torvalds else 15751da177e4SLinus Torvalds pmc->sources = psf->sf_next; 15761da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15771da177e4SLinus Torvalds if (psf->sf_oldin && 15781da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 1579a9fe8e29SHannes Frederic Sowa psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; 15801da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 15811da177e4SLinus Torvalds pmc->tomb = psf; 15821da177e4SLinus Torvalds rv = 1; 15831da177e4SLinus Torvalds } else 15841da177e4SLinus Torvalds #endif 15851da177e4SLinus Torvalds kfree(psf); 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds return rv; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 15911da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 15921da177e4SLinus Torvalds #endif 15931da177e4SLinus Torvalds 15948f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 15958f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 15961da177e4SLinus Torvalds { 15971da177e4SLinus Torvalds struct ip_mc_list *pmc; 15981da177e4SLinus Torvalds int changerec = 0; 15991da177e4SLinus Torvalds int i, err; 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds if (!in_dev) 16021da177e4SLinus Torvalds return -ENODEV; 16031d7138deSEric Dumazet rcu_read_lock(); 16041d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 16051da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 16061da177e4SLinus Torvalds break; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds if (!pmc) { 16091da177e4SLinus Torvalds /* MCA not found?? bug */ 16101d7138deSEric Dumazet rcu_read_unlock(); 16111da177e4SLinus Torvalds return -ESRCH; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 16141d7138deSEric Dumazet rcu_read_unlock(); 16151da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16161da177e4SLinus Torvalds sf_markstate(pmc); 16171da177e4SLinus Torvalds #endif 16181da177e4SLinus Torvalds if (!delta) { 16191da177e4SLinus Torvalds err = -EINVAL; 16201da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 16211da177e4SLinus Torvalds goto out_unlock; 16221da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds err = 0; 16251da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 16261da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds changerec |= rv > 0; 16291da177e4SLinus Torvalds if (!err && rv < 0) 16301da177e4SLinus Torvalds err = rv; 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 16331da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 16341da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 16351da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16361da177e4SLinus Torvalds struct ip_sf_list *psf; 16371da177e4SLinus Torvalds #endif 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds /* filter mode change */ 16401da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 16411da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1642a9fe8e29SHannes Frederic Sowa pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; 16431da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 16441da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 16451da177e4SLinus Torvalds psf->sf_crcount = 0; 16461da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 16471da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 16481da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 16491da177e4SLinus Torvalds #endif 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds out_unlock: 16521da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 16531da177e4SLinus Torvalds return err; 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds /* 16571da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 16581da177e4SLinus Torvalds */ 16591da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 16605eb81e89SJun Zhao __be32 *psfsrc) 16611da177e4SLinus Torvalds { 16621da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds psf_prev = NULL; 16651da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 16661da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 16671da177e4SLinus Torvalds break; 16681da177e4SLinus Torvalds psf_prev = psf; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds if (!psf) { 16710da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 16721da177e4SLinus Torvalds if (!psf) 16731da177e4SLinus Torvalds return -ENOBUFS; 16741da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 16751da177e4SLinus Torvalds if (psf_prev) { 16761da177e4SLinus Torvalds psf_prev->sf_next = psf; 16771da177e4SLinus Torvalds } else 16781da177e4SLinus Torvalds pmc->sources = psf; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds psf->sf_count[sfmode]++; 16811da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 16821da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds return 0; 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16881da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 16891da177e4SLinus Torvalds { 16901da177e4SLinus Torvalds struct ip_sf_list *psf; 16911da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 16941da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 16951da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 16961da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 16971da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 16981da177e4SLinus Torvalds } else 16991da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 17031da177e4SLinus Torvalds { 1704ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 17051da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 17061da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 17071da177e4SLinus Torvalds int new_in, rv; 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds rv = 0; 17101da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 17111da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 17121da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 17131da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 17141da177e4SLinus Torvalds } else 17151da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 1716ad12583fSDavid L Stevens if (new_in) { 1717ad12583fSDavid L Stevens if (!psf->sf_oldin) { 171876edc605SAl Viro struct ip_sf_list *prev = NULL; 1719ad12583fSDavid L Stevens 1720ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) { 1721ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1722ad12583fSDavid L Stevens break; 1723ad12583fSDavid L Stevens prev = dpsf; 1724ad12583fSDavid L Stevens } 1725ad12583fSDavid L Stevens if (dpsf) { 1726ad12583fSDavid L Stevens if (prev) 1727ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 1728ad12583fSDavid L Stevens else 1729ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 1730ad12583fSDavid L Stevens kfree(dpsf); 1731ad12583fSDavid L Stevens } 17321da177e4SLinus Torvalds psf->sf_crcount = qrv; 17331da177e4SLinus Torvalds rv++; 17341da177e4SLinus Torvalds } 1735ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 1736ad12583fSDavid L Stevens 1737ad12583fSDavid L Stevens psf->sf_crcount = 0; 1738ad12583fSDavid L Stevens /* 1739ad12583fSDavid L Stevens * add or update "delete" records if an active filter 1740ad12583fSDavid L Stevens * is now inactive 1741ad12583fSDavid L Stevens */ 1742ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) 1743ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1744ad12583fSDavid L Stevens break; 1745ad12583fSDavid L Stevens if (!dpsf) { 17463ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); 1747ad12583fSDavid L Stevens if (!dpsf) 1748ad12583fSDavid L Stevens continue; 1749ad12583fSDavid L Stevens *dpsf = *psf; 1750ad12583fSDavid L Stevens /* pmc->lock held by callers */ 1751ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 1752ad12583fSDavid L Stevens pmc->tomb = dpsf; 1753ad12583fSDavid L Stevens } 1754ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 1755ad12583fSDavid L Stevens rv++; 1756ad12583fSDavid L Stevens } 17571da177e4SLinus Torvalds } 17581da177e4SLinus Torvalds return rv; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds #endif 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds /* 17631da177e4SLinus Torvalds * Add multicast source filter list to the interface list 17641da177e4SLinus Torvalds */ 17658f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 17668f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 17671da177e4SLinus Torvalds { 17681da177e4SLinus Torvalds struct ip_mc_list *pmc; 17691da177e4SLinus Torvalds int isexclude; 17701da177e4SLinus Torvalds int i, err; 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds if (!in_dev) 17731da177e4SLinus Torvalds return -ENODEV; 17741d7138deSEric Dumazet rcu_read_lock(); 17751d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 17761da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 17771da177e4SLinus Torvalds break; 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds if (!pmc) { 17801da177e4SLinus Torvalds /* MCA not found?? bug */ 17811d7138deSEric Dumazet rcu_read_unlock(); 17821da177e4SLinus Torvalds return -ESRCH; 17831da177e4SLinus Torvalds } 17841da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 17851d7138deSEric Dumazet rcu_read_unlock(); 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17881da177e4SLinus Torvalds sf_markstate(pmc); 17891da177e4SLinus Torvalds #endif 17901da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 17911da177e4SLinus Torvalds if (!delta) 17921da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 17931da177e4SLinus Torvalds err = 0; 17941da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 17955eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); 17961da177e4SLinus Torvalds if (err) 17971da177e4SLinus Torvalds break; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds if (err) { 18001da177e4SLinus Torvalds int j; 18011da177e4SLinus Torvalds 1802685f94e6SJun Zhao if (!delta) 18031da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 18041da177e4SLinus Torvalds for (j = 0; j < i; j++) 1805a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); 18061da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 18071da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18081da177e4SLinus Torvalds struct ip_sf_list *psf; 1809cfcabdccSStephen Hemminger in_dev = pmc->interface; 18101da177e4SLinus Torvalds #endif 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds /* filter mode change */ 18131da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 18141da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 18151da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 18161da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 18171da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18181da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 18191da177e4SLinus Torvalds 1820a9fe8e29SHannes Frederic Sowa pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; 18211da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 18221da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 18231da177e4SLinus Torvalds psf->sf_crcount = 0; 18241da177e4SLinus Torvalds igmp_ifc_event(in_dev); 18251da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 18261da177e4SLinus Torvalds igmp_ifc_event(in_dev); 18271da177e4SLinus Torvalds #endif 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 18301da177e4SLinus Torvalds return err; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 18341da177e4SLinus Torvalds { 18351da177e4SLinus Torvalds struct ip_sf_list *psf, *nextpsf; 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds for (psf = pmc->tomb; psf; psf = nextpsf) { 18381da177e4SLinus Torvalds nextpsf = psf->sf_next; 18391da177e4SLinus Torvalds kfree(psf); 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds pmc->tomb = NULL; 18421da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = nextpsf) { 18431da177e4SLinus Torvalds nextpsf = psf->sf_next; 18441da177e4SLinus Torvalds kfree(psf); 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds pmc->sources = NULL; 18471da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 1848de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 18491da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds /* 18541da177e4SLinus Torvalds * Join a multicast group 18551da177e4SLinus Torvalds */ 18561da177e4SLinus Torvalds int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr) 18571da177e4SLinus Torvalds { 18581da177e4SLinus Torvalds int err; 18598f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 1860ca9b907dSDavid L Stevens struct ip_mc_socklist *iml = NULL, *i; 18611da177e4SLinus Torvalds struct in_device *in_dev; 18621da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1863877acedcSDaniel Lezcano struct net *net = sock_net(sk); 1864ca9b907dSDavid L Stevens int ifindex; 18651da177e4SLinus Torvalds int count = 0; 18661da177e4SLinus Torvalds 1867f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 18681da177e4SLinus Torvalds return -EINVAL; 18691da177e4SLinus Torvalds 18706756ae4bSStephen Hemminger rtnl_lock(); 18711da177e4SLinus Torvalds 1872877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds if (!in_dev) { 18751da177e4SLinus Torvalds iml = NULL; 18761da177e4SLinus Torvalds err = -ENODEV; 18771da177e4SLinus Torvalds goto done; 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds err = -EADDRINUSE; 1881ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 18821d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 1883ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 1884ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 18851da177e4SLinus Torvalds goto done; 18861da177e4SLinus Torvalds count++; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds err = -ENOBUFS; 1889ca9b907dSDavid L Stevens if (count >= sysctl_igmp_max_memberships) 18901da177e4SLinus Torvalds goto done; 18918b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 1892ca9b907dSDavid L Stevens if (iml == NULL) 1893ca9b907dSDavid L Stevens goto done; 1894ca9b907dSDavid L Stevens 18951da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 18961d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 18971da177e4SLinus Torvalds iml->sflist = NULL; 18981da177e4SLinus Torvalds iml->sfmode = MCAST_EXCLUDE; 1899cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 19001da177e4SLinus Torvalds ip_mc_inc_group(in_dev, addr); 19011da177e4SLinus Torvalds err = 0; 19021da177e4SLinus Torvalds done: 19036756ae4bSStephen Hemminger rtnl_unlock(); 19041da177e4SLinus Torvalds return err; 19051da177e4SLinus Torvalds } 19064bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 19091da177e4SLinus Torvalds struct in_device *in_dev) 19101da177e4SLinus Torvalds { 19111d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 19121da177e4SLinus Torvalds int err; 19131da177e4SLinus Torvalds 1914c85bb41eSFlavio Leitner if (psf == NULL) { 19151da177e4SLinus Torvalds /* any-source empty exclude case */ 19161da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 19171da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 1920c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 1921a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 1922c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 1923c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); 19247519cce4SLai Jiangshan kfree_rcu(psf, rcu); 19251da177e4SLinus Torvalds return err; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds /* 19291da177e4SLinus Torvalds * Ask a socket to leave a group. 19301da177e4SLinus Torvalds */ 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 19331da177e4SLinus Torvalds { 19341da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19351d7138deSEric Dumazet struct ip_mc_socklist *iml; 19361d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 193784b42baeSDavid L Stevens struct in_device *in_dev; 1938877acedcSDaniel Lezcano struct net *net = sock_net(sk); 19398f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 194084b42baeSDavid L Stevens u32 ifindex; 1941acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds rtnl_lock(); 1944877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 194552ad353aSdingtianhong if (!in_dev) { 194652ad353aSdingtianhong ret = -ENODEV; 194752ad353aSdingtianhong goto out; 194852ad353aSdingtianhong } 194984b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 19501d7138deSEric Dumazet for (imlp = &inet->mc_list; 19511d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 19521d7138deSEric Dumazet imlp = &iml->next_rcu) { 1953acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 1954acd6e00bSDavid L Stevens continue; 1955acd6e00bSDavid L Stevens if (ifindex) { 1956acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 1957acd6e00bSDavid L Stevens continue; 1958acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 1959acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 1960acd6e00bSDavid L Stevens continue; 1961acd6e00bSDavid L Stevens 19621da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 19631da177e4SLinus Torvalds 19641d7138deSEric Dumazet *imlp = iml->next_rcu; 19651da177e4SLinus Torvalds 196684b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 19671da177e4SLinus Torvalds rtnl_unlock(); 1968c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 1969c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 197010d50e74SLai Jiangshan kfree_rcu(iml, rcu); 19711da177e4SLinus Torvalds return 0; 19721da177e4SLinus Torvalds } 197352ad353aSdingtianhong out: 19741da177e4SLinus Torvalds rtnl_unlock(); 1975acd6e00bSDavid L Stevens return ret; 19761da177e4SLinus Torvalds } 1977193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 19801da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 19811da177e4SLinus Torvalds { 19821da177e4SLinus Torvalds int err; 19831da177e4SLinus Torvalds struct ip_mreqn imr; 198463007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 19851da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 19861da177e4SLinus Torvalds struct in_device *in_dev = NULL; 19871da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19881da177e4SLinus Torvalds struct ip_sf_socklist *psl; 1989877acedcSDaniel Lezcano struct net *net = sock_net(sk); 19908cdaaa15SDavid L Stevens int leavegroup = 0; 19911da177e4SLinus Torvalds int i, j, rv; 19921da177e4SLinus Torvalds 1993f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 19941da177e4SLinus Torvalds return -EINVAL; 19951da177e4SLinus Torvalds 19966756ae4bSStephen Hemminger rtnl_lock(); 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 19991da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 20001da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2001877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (!in_dev) { 20041da177e4SLinus Torvalds err = -ENODEV; 20051da177e4SLinus Torvalds goto done; 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 20081da177e4SLinus Torvalds 20091d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 2010f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 2011f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 2012f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 20131da177e4SLinus Torvalds break; 20141da177e4SLinus Torvalds } 2015917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2016917f2f10SDavid L Stevens err = -EINVAL; 20171da177e4SLinus Torvalds goto done; 2018917f2f10SDavid L Stevens } 20191da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 20201da177e4SLinus Torvalds if (pmc->sflist) { 2021917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 2022917f2f10SDavid L Stevens err = -EINVAL; 20231da177e4SLinus Torvalds goto done; 2024917f2f10SDavid L Stevens } 20251da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 20261da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 20271da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 20281da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 20291da177e4SLinus Torvalds NULL, 0); 20301da177e4SLinus Torvalds pmc->sfmode = omode; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 20331d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 20341da177e4SLinus Torvalds if (!add) { 20351da177e4SLinus Torvalds if (!psl) 2036917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 20371da177e4SLinus Torvalds rv = !0; 20381da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 20391da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 204063007727SAl Viro sizeof(__be32)); 20411da177e4SLinus Torvalds if (rv == 0) 20421da177e4SLinus Torvalds break; 20431da177e4SLinus Torvalds } 20441da177e4SLinus Torvalds if (rv) /* source not found */ 2045917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 20461da177e4SLinus Torvalds 20478cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 20488cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 20498cdaaa15SDavid L Stevens leavegroup = 1; 20508cdaaa15SDavid L Stevens goto done; 20518cdaaa15SDavid L Stevens } 20528cdaaa15SDavid L Stevens 20531da177e4SLinus Torvalds /* update the interface filter */ 20541da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 20551da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds for (j = i+1; j < psl->sl_count; j++) 20581da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 20591da177e4SLinus Torvalds psl->sl_count--; 20601da177e4SLinus Torvalds err = 0; 20611da177e4SLinus Torvalds goto done; 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds /* else, add a new source to the filter */ 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds if (psl && psl->sl_count >= sysctl_igmp_max_msf) { 20661da177e4SLinus Torvalds err = -ENOBUFS; 20671da177e4SLinus Torvalds goto done; 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 20701da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 20711da177e4SLinus Torvalds int count = IP_SFBLOCK; 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds if (psl) 20741da177e4SLinus Torvalds count += psl->sl_max; 20758b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 20761da177e4SLinus Torvalds if (!newpsl) { 20771da177e4SLinus Torvalds err = -ENOBUFS; 20781da177e4SLinus Torvalds goto done; 20791da177e4SLinus Torvalds } 20801da177e4SLinus Torvalds newpsl->sl_max = count; 20811da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 20821da177e4SLinus Torvalds if (psl) { 20831da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) 20841da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2085c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2086c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 20877519cce4SLai Jiangshan kfree_rcu(psl, rcu); 20881da177e4SLinus Torvalds } 2089cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2090c85bb41eSFlavio Leitner psl = newpsl; 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 20931da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 20941da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 209563007727SAl Viro sizeof(__be32)); 20961da177e4SLinus Torvalds if (rv == 0) 20971da177e4SLinus Torvalds break; 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 21001da177e4SLinus Torvalds goto done; 21011da177e4SLinus Torvalds for (j = psl->sl_count-1; j >= i; j--) 21021da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 21031da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 21041da177e4SLinus Torvalds psl->sl_count++; 21051da177e4SLinus Torvalds err = 0; 21061da177e4SLinus Torvalds /* update the interface list */ 21071da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 21081da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 21091da177e4SLinus Torvalds done: 21106756ae4bSStephen Hemminger rtnl_unlock(); 21118cdaaa15SDavid L Stevens if (leavegroup) 21128cdaaa15SDavid L Stevens return ip_mc_leave_group(sk, &imr); 21131da177e4SLinus Torvalds return err; 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 21171da177e4SLinus Torvalds { 21189951f036SDavid L Stevens int err = 0; 21191da177e4SLinus Torvalds struct ip_mreqn imr; 212063007727SAl Viro __be32 addr = msf->imsf_multiaddr; 21211da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 21221da177e4SLinus Torvalds struct in_device *in_dev; 21231da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21241da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2125877acedcSDaniel Lezcano struct net *net = sock_net(sk); 21269951f036SDavid L Stevens int leavegroup = 0; 21271da177e4SLinus Torvalds 2128f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21291da177e4SLinus Torvalds return -EINVAL; 21301da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 21311da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 21321da177e4SLinus Torvalds return -EINVAL; 21331da177e4SLinus Torvalds 21346756ae4bSStephen Hemminger rtnl_lock(); 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 21371da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 21381da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2139877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds if (!in_dev) { 21421da177e4SLinus Torvalds err = -ENODEV; 21431da177e4SLinus Torvalds goto done; 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 21469951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 21479951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 21489951f036SDavid L Stevens leavegroup = 1; 21499951f036SDavid L Stevens goto done; 21509951f036SDavid L Stevens } 21519951f036SDavid L Stevens 21521d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 21531da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 21541da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 21551da177e4SLinus Torvalds break; 21561da177e4SLinus Torvalds } 2157917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2158917f2f10SDavid L Stevens err = -EINVAL; 21591da177e4SLinus Torvalds goto done; 2160917f2f10SDavid L Stevens } 21611da177e4SLinus Torvalds if (msf->imsf_numsrc) { 21628b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 21638b3a7005SKris Katterjohn GFP_KERNEL); 21641da177e4SLinus Torvalds if (!newpsl) { 21651da177e4SLinus Torvalds err = -ENOBUFS; 21661da177e4SLinus Torvalds goto done; 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 21691da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 21701da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 21711da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 21721da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 21731da177e4SLinus Torvalds if (err) { 21741da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 21751da177e4SLinus Torvalds goto done; 21761da177e4SLinus Torvalds } 21778713dbf0SYan Zheng } else { 21781da177e4SLinus Torvalds newpsl = NULL; 21798713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 21808713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 21818713dbf0SYan Zheng } 21821d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 21831da177e4SLinus Torvalds if (psl) { 21841da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 21851da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2186c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2187c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 21887519cce4SLai Jiangshan kfree_rcu(psl, rcu); 21891da177e4SLinus Torvalds } else 21901da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 21911da177e4SLinus Torvalds 0, NULL, 0); 2192cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 21931da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2194917f2f10SDavid L Stevens err = 0; 21951da177e4SLinus Torvalds done: 21966756ae4bSStephen Hemminger rtnl_unlock(); 21979951f036SDavid L Stevens if (leavegroup) 21989951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 21991da177e4SLinus Torvalds return err; 22001da177e4SLinus Torvalds } 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 22031da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 22041da177e4SLinus Torvalds { 22051da177e4SLinus Torvalds int err, len, count, copycount; 22061da177e4SLinus Torvalds struct ip_mreqn imr; 220763007727SAl Viro __be32 addr = msf->imsf_multiaddr; 22081da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22091da177e4SLinus Torvalds struct in_device *in_dev; 22101da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22111da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2212877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22131da177e4SLinus Torvalds 2214f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 22151da177e4SLinus Torvalds return -EINVAL; 22161da177e4SLinus Torvalds 22176756ae4bSStephen Hemminger rtnl_lock(); 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 22201da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 22211da177e4SLinus Torvalds imr.imr_ifindex = 0; 2222877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds if (!in_dev) { 22251da177e4SLinus Torvalds err = -ENODEV; 22261da177e4SLinus Torvalds goto done; 22271da177e4SLinus Torvalds } 22281da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 22291da177e4SLinus Torvalds 22301d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 22311da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 22321da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 22331da177e4SLinus Torvalds break; 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 22361da177e4SLinus Torvalds goto done; 22371da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 22381d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 22396756ae4bSStephen Hemminger rtnl_unlock(); 22401da177e4SLinus Torvalds if (!psl) { 22411da177e4SLinus Torvalds len = 0; 22421da177e4SLinus Torvalds count = 0; 22431da177e4SLinus Torvalds } else { 22441da177e4SLinus Torvalds count = psl->sl_count; 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 22471da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 22481da177e4SLinus Torvalds msf->imsf_numsrc = count; 22491da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 22501da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 22511da177e4SLinus Torvalds return -EFAULT; 22521da177e4SLinus Torvalds } 22531da177e4SLinus Torvalds if (len && 22541da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 22551da177e4SLinus Torvalds return -EFAULT; 22561da177e4SLinus Torvalds return 0; 22571da177e4SLinus Torvalds done: 22586756ae4bSStephen Hemminger rtnl_unlock(); 22591da177e4SLinus Torvalds return err; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 22631da177e4SLinus Torvalds struct group_filter __user *optval, int __user *optlen) 22641da177e4SLinus Torvalds { 22651da177e4SLinus Torvalds int err, i, count, copycount; 22661da177e4SLinus Torvalds struct sockaddr_in *psin; 226763007727SAl Viro __be32 addr; 22681da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22691da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22701da177e4SLinus Torvalds struct ip_sf_socklist *psl; 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 22731da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 22741da177e4SLinus Torvalds return -EINVAL; 22751da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2276f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 22771da177e4SLinus Torvalds return -EINVAL; 22781da177e4SLinus Torvalds 22796756ae4bSStephen Hemminger rtnl_lock(); 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 22821da177e4SLinus Torvalds 22831d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 22841da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 22851da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 22861da177e4SLinus Torvalds break; 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 22891da177e4SLinus Torvalds goto done; 22901da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 22911d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 22926756ae4bSStephen Hemminger rtnl_unlock(); 22931da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 22941da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 22951da177e4SLinus Torvalds gsf->gf_numsrc = count; 22961da177e4SLinus Torvalds if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 22971da177e4SLinus Torvalds copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 22981da177e4SLinus Torvalds return -EFAULT; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds for (i = 0; i < copycount; i++) { 23011da177e4SLinus Torvalds struct sockaddr_storage ss; 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 23041da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 23051da177e4SLinus Torvalds psin->sin_family = AF_INET; 23061da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 23071da177e4SLinus Torvalds if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 23081da177e4SLinus Torvalds return -EFAULT; 23091da177e4SLinus Torvalds } 23101da177e4SLinus Torvalds return 0; 23111da177e4SLinus Torvalds done: 23126756ae4bSStephen Hemminger rtnl_unlock(); 23131da177e4SLinus Torvalds return err; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds /* 23171da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 23181da177e4SLinus Torvalds */ 2319c0cda068SAl Viro int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif) 23201da177e4SLinus Torvalds { 23211da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23221da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 23231da177e4SLinus Torvalds struct ip_sf_socklist *psl; 23241da177e4SLinus Torvalds int i; 2325c85bb41eSFlavio Leitner int ret; 23261da177e4SLinus Torvalds 2327c85bb41eSFlavio Leitner ret = 1; 2328f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2329c85bb41eSFlavio Leitner goto out; 23301da177e4SLinus Torvalds 2331c85bb41eSFlavio Leitner rcu_read_lock(); 23321d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 23331da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 23341da177e4SLinus Torvalds pmc->multi.imr_ifindex == dif) 23351da177e4SLinus Torvalds break; 23361da177e4SLinus Torvalds } 2337c85bb41eSFlavio Leitner ret = inet->mc_all; 23381da177e4SLinus Torvalds if (!pmc) 2339c85bb41eSFlavio Leitner goto unlock; 23401d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2341c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 23421da177e4SLinus Torvalds if (!psl) 2343c85bb41eSFlavio Leitner goto unlock; 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 23461da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 23471da177e4SLinus Torvalds break; 23481da177e4SLinus Torvalds } 2349c85bb41eSFlavio Leitner ret = 0; 23501da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2351c85bb41eSFlavio Leitner goto unlock; 23521da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2353c85bb41eSFlavio Leitner goto unlock; 2354c85bb41eSFlavio Leitner ret = 1; 2355c85bb41eSFlavio Leitner unlock: 2356c85bb41eSFlavio Leitner rcu_read_unlock(); 2357c85bb41eSFlavio Leitner out: 2358c85bb41eSFlavio Leitner return ret; 23591da177e4SLinus Torvalds } 23601da177e4SLinus Torvalds 23611da177e4SLinus Torvalds /* 23621da177e4SLinus Torvalds * A socket is closing. 23631da177e4SLinus Torvalds */ 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 23661da177e4SLinus Torvalds { 23671da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23681da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2369877acedcSDaniel Lezcano struct net *net = sock_net(sk); 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds if (inet->mc_list == NULL) 23721da177e4SLinus Torvalds return; 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds rtnl_lock(); 23751d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 23761da177e4SLinus Torvalds struct in_device *in_dev; 23771da177e4SLinus Torvalds 23781d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2379877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 23801da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 238118943d29SEric Dumazet if (in_dev != NULL) 23821da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2383c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2384c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 238510d50e74SLai Jiangshan kfree_rcu(iml, rcu); 23861da177e4SLinus Torvalds } 23871da177e4SLinus Torvalds rtnl_unlock(); 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds 2390dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 2391dbdd9a52SDavid S. Miller int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto) 23921da177e4SLinus Torvalds { 23931da177e4SLinus Torvalds struct ip_mc_list *im; 2394e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 23951da177e4SLinus Torvalds struct ip_sf_list *psf; 23961da177e4SLinus Torvalds int rv = 0; 23971da177e4SLinus Torvalds 2398e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash); 2399e9897071SEric Dumazet if (mc_hash) { 2400c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); 2401e9897071SEric Dumazet 2402e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]); 2403e9897071SEric Dumazet im != NULL; 2404e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) { 2405e9897071SEric Dumazet if (im->multiaddr == mc_addr) 2406e9897071SEric Dumazet break; 2407e9897071SEric Dumazet } 2408e9897071SEric Dumazet } else { 24091d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 24101da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 24111da177e4SLinus Torvalds break; 24121da177e4SLinus Torvalds } 2413e9897071SEric Dumazet } 24141da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 24151da177e4SLinus Torvalds rv = 1; 24161da177e4SLinus Torvalds } else if (im) { 24171da177e4SLinus Torvalds if (src_addr) { 24181da177e4SLinus Torvalds for (psf = im->sources; psf; psf = psf->sf_next) { 24191da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 24201da177e4SLinus Torvalds break; 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds if (psf) 24231da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 24241da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 24251da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 24261da177e4SLinus Torvalds else 24271da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 24281da177e4SLinus Torvalds } else 24291da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds return rv; 24321da177e4SLinus Torvalds } 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 24351da177e4SLinus Torvalds struct igmp_mc_iter_state { 24367091e728SAlexey Dobriyan struct seq_net_private p; 24371da177e4SLinus Torvalds struct net_device *dev; 24381da177e4SLinus Torvalds struct in_device *in_dev; 24391da177e4SLinus Torvalds }; 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 24441da177e4SLinus Torvalds { 24457091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 24461da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 24471da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24481da177e4SLinus Torvalds 24497562f876SPavel Emelianov state->in_dev = NULL; 245061fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 24511da177e4SLinus Torvalds struct in_device *in_dev; 24526baff150SEric Dumazet 24536baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 24541da177e4SLinus Torvalds if (!in_dev) 24551da177e4SLinus Torvalds continue; 24561d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 24571da177e4SLinus Torvalds if (im) { 24581da177e4SLinus Torvalds state->in_dev = in_dev; 24591da177e4SLinus Torvalds break; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds } 24621da177e4SLinus Torvalds return im; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 24661da177e4SLinus Torvalds { 24671da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24686baff150SEric Dumazet 24691d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 24701d7138deSEric Dumazet while (!im) { 24716baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 24721da177e4SLinus Torvalds if (!state->dev) { 24731da177e4SLinus Torvalds state->in_dev = NULL; 24741da177e4SLinus Torvalds break; 24751da177e4SLinus Torvalds } 24766baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 24771da177e4SLinus Torvalds if (!state->in_dev) 24781da177e4SLinus Torvalds continue; 24791d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds return im; 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds 24841da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 24851da177e4SLinus Torvalds { 24861da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 24871da177e4SLinus Torvalds if (im) 24881da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 24891da177e4SLinus Torvalds --pos; 24901da177e4SLinus Torvalds return pos ? NULL : im; 24911da177e4SLinus Torvalds } 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 249461fbab77Sstephen hemminger __acquires(rcu) 24951da177e4SLinus Torvalds { 249661fbab77Sstephen hemminger rcu_read_lock(); 24971da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 24981da177e4SLinus Torvalds } 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25011da177e4SLinus Torvalds { 25021da177e4SLinus Torvalds struct ip_mc_list *im; 25031da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 25041da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 25051da177e4SLinus Torvalds else 25061da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 25071da177e4SLinus Torvalds ++*pos; 25081da177e4SLinus Torvalds return im; 25091da177e4SLinus Torvalds } 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 251261fbab77Sstephen hemminger __releases(rcu) 25131da177e4SLinus Torvalds { 25141da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 25151d7138deSEric Dumazet 25161da177e4SLinus Torvalds state->in_dev = NULL; 25171da177e4SLinus Torvalds state->dev = NULL; 251861fbab77Sstephen hemminger rcu_read_unlock(); 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 25221da177e4SLinus Torvalds { 25231da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 25241da177e4SLinus Torvalds seq_puts(seq, 25251da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 25261da177e4SLinus Torvalds else { 25271da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 25281da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 25291da177e4SLinus Torvalds char *querier; 2530a399a805SEric Dumazet long delta; 2531a399a805SEric Dumazet 25321da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 25331da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 25341da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 25351da177e4SLinus Torvalds "V3"; 25361da177e4SLinus Torvalds #else 25371da177e4SLinus Torvalds querier = "NONE"; 25381da177e4SLinus Torvalds #endif 25391da177e4SLinus Torvalds 2540e6b68883SAndreea-Cristina Bernat if (rcu_access_pointer(state->in_dev->mc_list) == im) { 25411da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2542b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds 2545a399a805SEric Dumazet delta = im->timer.expires - jiffies; 25461da177e4SLinus Torvalds seq_printf(seq, 2547338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 25481da177e4SLinus Torvalds im->multiaddr, im->users, 2549a399a805SEric Dumazet im->tm_running, 2550a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 25511da177e4SLinus Torvalds im->reporter); 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds return 0; 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds 2556f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 25571da177e4SLinus Torvalds .start = igmp_mc_seq_start, 25581da177e4SLinus Torvalds .next = igmp_mc_seq_next, 25591da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 25601da177e4SLinus Torvalds .show = igmp_mc_seq_show, 25611da177e4SLinus Torvalds }; 25621da177e4SLinus Torvalds 25631da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file) 25641da177e4SLinus Torvalds { 25657091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mc_seq_ops, 2566cf7732e4SPavel Emelyanov sizeof(struct igmp_mc_iter_state)); 25671da177e4SLinus Torvalds } 25681da177e4SLinus Torvalds 25699a32144eSArjan van de Ven static const struct file_operations igmp_mc_seq_fops = { 25701da177e4SLinus Torvalds .owner = THIS_MODULE, 25711da177e4SLinus Torvalds .open = igmp_mc_seq_open, 25721da177e4SLinus Torvalds .read = seq_read, 25731da177e4SLinus Torvalds .llseek = seq_lseek, 25747091e728SAlexey Dobriyan .release = seq_release_net, 25751da177e4SLinus Torvalds }; 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds struct igmp_mcf_iter_state { 25787091e728SAlexey Dobriyan struct seq_net_private p; 25791da177e4SLinus Torvalds struct net_device *dev; 25801da177e4SLinus Torvalds struct in_device *idev; 25811da177e4SLinus Torvalds struct ip_mc_list *im; 25821da177e4SLinus Torvalds }; 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 25871da177e4SLinus Torvalds { 25887091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 25891da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 25901da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 25911da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 25921da177e4SLinus Torvalds 25937562f876SPavel Emelianov state->idev = NULL; 25947562f876SPavel Emelianov state->im = NULL; 259561fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 25961da177e4SLinus Torvalds struct in_device *idev; 25976baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 25981da177e4SLinus Torvalds if (unlikely(idev == NULL)) 25991da177e4SLinus Torvalds continue; 26001d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 26011da177e4SLinus Torvalds if (likely(im != NULL)) { 26021da177e4SLinus Torvalds spin_lock_bh(&im->lock); 26031da177e4SLinus Torvalds psf = im->sources; 26041da177e4SLinus Torvalds if (likely(psf != NULL)) { 26051da177e4SLinus Torvalds state->im = im; 26061da177e4SLinus Torvalds state->idev = idev; 26071da177e4SLinus Torvalds break; 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds return psf; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 26161da177e4SLinus Torvalds { 26171da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26181da177e4SLinus Torvalds 26191da177e4SLinus Torvalds psf = psf->sf_next; 26201da177e4SLinus Torvalds while (!psf) { 26211da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 26221da177e4SLinus Torvalds state->im = state->im->next; 26231da177e4SLinus Torvalds while (!state->im) { 26246baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 26251da177e4SLinus Torvalds if (!state->dev) { 26261da177e4SLinus Torvalds state->idev = NULL; 26271da177e4SLinus Torvalds goto out; 26281da177e4SLinus Torvalds } 26296baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 26301da177e4SLinus Torvalds if (!state->idev) 26311da177e4SLinus Torvalds continue; 26321d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 26331da177e4SLinus Torvalds } 26341da177e4SLinus Torvalds if (!state->im) 26351da177e4SLinus Torvalds break; 26361da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 26371da177e4SLinus Torvalds psf = state->im->sources; 26381da177e4SLinus Torvalds } 26391da177e4SLinus Torvalds out: 26401da177e4SLinus Torvalds return psf; 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 26441da177e4SLinus Torvalds { 26451da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 26461da177e4SLinus Torvalds if (psf) 26471da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 26481da177e4SLinus Torvalds --pos; 26491da177e4SLinus Torvalds return pos ? NULL : psf; 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 265361fbab77Sstephen hemminger __acquires(rcu) 26541da177e4SLinus Torvalds { 265561fbab77Sstephen hemminger rcu_read_lock(); 26561da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26601da177e4SLinus Torvalds { 26611da177e4SLinus Torvalds struct ip_sf_list *psf; 26621da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 26631da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 26641da177e4SLinus Torvalds else 26651da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 26661da177e4SLinus Torvalds ++*pos; 26671da177e4SLinus Torvalds return psf; 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 267161fbab77Sstephen hemminger __releases(rcu) 26721da177e4SLinus Torvalds { 26731da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26741da177e4SLinus Torvalds if (likely(state->im != NULL)) { 26751da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 26761da177e4SLinus Torvalds state->im = NULL; 26771da177e4SLinus Torvalds } 26781da177e4SLinus Torvalds state->idev = NULL; 26791da177e4SLinus Torvalds state->dev = NULL; 268061fbab77Sstephen hemminger rcu_read_unlock(); 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 26841da177e4SLinus Torvalds { 26851da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 26861da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26871da177e4SLinus Torvalds 26881da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 26891744bea1SJoe Perches seq_puts(seq, "Idx Device MCA SRC INC EXC\n"); 26901da177e4SLinus Torvalds } else { 26911da177e4SLinus Torvalds seq_printf(seq, 26921da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 26931da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 26941da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 26951da177e4SLinus Torvalds ntohl(state->im->multiaddr), 26961da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 26971da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 26981da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 26991da177e4SLinus Torvalds } 27001da177e4SLinus Torvalds return 0; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds 2703f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 27041da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 27051da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 27061da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 27071da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 27081da177e4SLinus Torvalds }; 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file) 27111da177e4SLinus Torvalds { 27127091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mcf_seq_ops, 2713cf7732e4SPavel Emelyanov sizeof(struct igmp_mcf_iter_state)); 27141da177e4SLinus Torvalds } 27151da177e4SLinus Torvalds 27169a32144eSArjan van de Ven static const struct file_operations igmp_mcf_seq_fops = { 27171da177e4SLinus Torvalds .owner = THIS_MODULE, 27181da177e4SLinus Torvalds .open = igmp_mcf_seq_open, 27191da177e4SLinus Torvalds .read = seq_read, 27201da177e4SLinus Torvalds .llseek = seq_lseek, 27217091e728SAlexey Dobriyan .release = seq_release_net, 27227091e728SAlexey Dobriyan }; 27237091e728SAlexey Dobriyan 27242c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 27257091e728SAlexey Dobriyan { 27267091e728SAlexey Dobriyan struct proc_dir_entry *pde; 27277091e728SAlexey Dobriyan 2728d4beaa66SGao feng pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops); 27297091e728SAlexey Dobriyan if (!pde) 27307091e728SAlexey Dobriyan goto out_igmp; 2731d4beaa66SGao feng pde = proc_create("mcfilter", S_IRUGO, net->proc_net, 2732d4beaa66SGao feng &igmp_mcf_seq_fops); 27337091e728SAlexey Dobriyan if (!pde) 27347091e728SAlexey Dobriyan goto out_mcfilter; 27357091e728SAlexey Dobriyan return 0; 27367091e728SAlexey Dobriyan 27377091e728SAlexey Dobriyan out_mcfilter: 2738ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 27397091e728SAlexey Dobriyan out_igmp: 27407091e728SAlexey Dobriyan return -ENOMEM; 27417091e728SAlexey Dobriyan } 27427091e728SAlexey Dobriyan 27432c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 27447091e728SAlexey Dobriyan { 2745ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net); 2746ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 27477091e728SAlexey Dobriyan } 27487091e728SAlexey Dobriyan 27497091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 27507091e728SAlexey Dobriyan .init = igmp_net_init, 27517091e728SAlexey Dobriyan .exit = igmp_net_exit, 27521da177e4SLinus Torvalds }; 275372c1d3bdSWANG Cong #endif 27541da177e4SLinus Torvalds 27554aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this, 27564aa5dee4SJiri Pirko unsigned long event, void *ptr) 27574aa5dee4SJiri Pirko { 27584aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 27594aa5dee4SJiri Pirko struct in_device *in_dev; 27604aa5dee4SJiri Pirko 27614aa5dee4SJiri Pirko switch (event) { 27624aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 27634aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev); 27644aa5dee4SJiri Pirko if (in_dev) 27654aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev); 27664aa5dee4SJiri Pirko break; 27674aa5dee4SJiri Pirko default: 27684aa5dee4SJiri Pirko break; 27694aa5dee4SJiri Pirko } 27704aa5dee4SJiri Pirko return NOTIFY_DONE; 27714aa5dee4SJiri Pirko } 27724aa5dee4SJiri Pirko 27734aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = { 27744aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event, 27754aa5dee4SJiri Pirko }; 27764aa5dee4SJiri Pirko 277772c1d3bdSWANG Cong int __init igmp_mc_init(void) 27781da177e4SLinus Torvalds { 277972c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS) 27804aa5dee4SJiri Pirko int err; 27814aa5dee4SJiri Pirko 27824aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops); 27834aa5dee4SJiri Pirko if (err) 27844aa5dee4SJiri Pirko return err; 27854aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier); 27864aa5dee4SJiri Pirko if (err) 27874aa5dee4SJiri Pirko goto reg_notif_fail; 27884aa5dee4SJiri Pirko return 0; 27894aa5dee4SJiri Pirko 27904aa5dee4SJiri Pirko reg_notif_fail: 27914aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops); 27924aa5dee4SJiri Pirko return err; 279372c1d3bdSWANG Cong #else 279472c1d3bdSWANG Cong return register_netdevice_notifier(&igmp_notifier); 27951da177e4SLinus Torvalds #endif 279672c1d3bdSWANG Cong } 2797