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 3214c672e4bSDaniel 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; 3314c672e4bSDaniel 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 3554c672e4bSDaniel Borkmann skb->reserved_tailroom = skb_end_offset(skb) - 3564c672e4bSDaniel 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 4264c672e4bSDaniel 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 1852*959d10f6SEric Dumazet int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr) 18531da177e4SLinus Torvalds { 18548f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 1855*959d10f6SEric Dumazet struct ip_mc_socklist *iml, *i; 18561da177e4SLinus Torvalds struct in_device *in_dev; 18571da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1858877acedcSDaniel Lezcano struct net *net = sock_net(sk); 1859ca9b907dSDavid L Stevens int ifindex; 18601da177e4SLinus Torvalds int count = 0; 1861*959d10f6SEric Dumazet int err; 1862*959d10f6SEric Dumazet 1863*959d10f6SEric Dumazet ASSERT_RTNL(); 18641da177e4SLinus Torvalds 1865f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 18661da177e4SLinus Torvalds return -EINVAL; 18671da177e4SLinus Torvalds 1868877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds if (!in_dev) { 18711da177e4SLinus Torvalds err = -ENODEV; 18721da177e4SLinus Torvalds goto done; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds err = -EADDRINUSE; 1876ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 18771d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 1878ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 1879ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 18801da177e4SLinus Torvalds goto done; 18811da177e4SLinus Torvalds count++; 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds err = -ENOBUFS; 1884ca9b907dSDavid L Stevens if (count >= sysctl_igmp_max_memberships) 18851da177e4SLinus Torvalds goto done; 18868b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 1887ca9b907dSDavid L Stevens if (iml == NULL) 1888ca9b907dSDavid L Stevens goto done; 1889ca9b907dSDavid L Stevens 18901da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 18911d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 18921da177e4SLinus Torvalds iml->sflist = NULL; 18931da177e4SLinus Torvalds iml->sfmode = MCAST_EXCLUDE; 1894cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 18951da177e4SLinus Torvalds ip_mc_inc_group(in_dev, addr); 18961da177e4SLinus Torvalds err = 0; 18971da177e4SLinus Torvalds done: 18981da177e4SLinus Torvalds return err; 18991da177e4SLinus Torvalds } 1900*959d10f6SEric Dumazet EXPORT_SYMBOL(__ip_mc_join_group); 1901*959d10f6SEric Dumazet 1902*959d10f6SEric Dumazet /* Join a multicast group 1903*959d10f6SEric Dumazet */ 1904*959d10f6SEric Dumazet int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr) 1905*959d10f6SEric Dumazet { 1906*959d10f6SEric Dumazet int ret; 1907*959d10f6SEric Dumazet 1908*959d10f6SEric Dumazet rtnl_lock(); 1909*959d10f6SEric Dumazet ret = __ip_mc_join_group(sk, imr); 1910*959d10f6SEric Dumazet rtnl_unlock(); 1911*959d10f6SEric Dumazet 1912*959d10f6SEric Dumazet return ret; 1913*959d10f6SEric Dumazet } 19144bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 19171da177e4SLinus Torvalds struct in_device *in_dev) 19181da177e4SLinus Torvalds { 19191d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 19201da177e4SLinus Torvalds int err; 19211da177e4SLinus Torvalds 1922c85bb41eSFlavio Leitner if (psf == NULL) { 19231da177e4SLinus Torvalds /* any-source empty exclude case */ 19241da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 19251da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 1928c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 1929a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 1930c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 1931c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); 19327519cce4SLai Jiangshan kfree_rcu(psf, rcu); 19331da177e4SLinus Torvalds return err; 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds 1936*959d10f6SEric Dumazet int __ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 19371da177e4SLinus Torvalds { 19381da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19391d7138deSEric Dumazet struct ip_mc_socklist *iml; 19401d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 194184b42baeSDavid L Stevens struct in_device *in_dev; 1942877acedcSDaniel Lezcano struct net *net = sock_net(sk); 19438f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 194484b42baeSDavid L Stevens u32 ifindex; 1945acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 19461da177e4SLinus Torvalds 1947*959d10f6SEric Dumazet ASSERT_RTNL(); 1948*959d10f6SEric Dumazet 1949877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 195052ad353aSdingtianhong if (!in_dev) { 195152ad353aSdingtianhong ret = -ENODEV; 195252ad353aSdingtianhong goto out; 195352ad353aSdingtianhong } 195484b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 19551d7138deSEric Dumazet for (imlp = &inet->mc_list; 19561d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 19571d7138deSEric Dumazet imlp = &iml->next_rcu) { 1958acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 1959acd6e00bSDavid L Stevens continue; 1960acd6e00bSDavid L Stevens if (ifindex) { 1961acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 1962acd6e00bSDavid L Stevens continue; 1963acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 1964acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 1965acd6e00bSDavid L Stevens continue; 1966acd6e00bSDavid L Stevens 19671da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 19681da177e4SLinus Torvalds 19691d7138deSEric Dumazet *imlp = iml->next_rcu; 19701da177e4SLinus Torvalds 197184b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 1972*959d10f6SEric Dumazet 1973c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 1974c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 197510d50e74SLai Jiangshan kfree_rcu(iml, rcu); 19761da177e4SLinus Torvalds return 0; 19771da177e4SLinus Torvalds } 197852ad353aSdingtianhong out: 1979*959d10f6SEric Dumazet return ret; 1980*959d10f6SEric Dumazet } 1981*959d10f6SEric Dumazet EXPORT_SYMBOL(__ip_mc_leave_group); 1982*959d10f6SEric Dumazet 1983*959d10f6SEric Dumazet int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 1984*959d10f6SEric Dumazet { 1985*959d10f6SEric Dumazet int ret; 1986*959d10f6SEric Dumazet 1987*959d10f6SEric Dumazet rtnl_lock(); 1988*959d10f6SEric Dumazet ret = __ip_mc_leave_group(sk, imr); 19891da177e4SLinus Torvalds rtnl_unlock(); 1990*959d10f6SEric Dumazet 1991acd6e00bSDavid L Stevens return ret; 19921da177e4SLinus Torvalds } 1993193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 19961da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 19971da177e4SLinus Torvalds { 19981da177e4SLinus Torvalds int err; 19991da177e4SLinus Torvalds struct ip_mreqn imr; 200063007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 20011da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 20021da177e4SLinus Torvalds struct in_device *in_dev = NULL; 20031da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 20041da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2005877acedcSDaniel Lezcano struct net *net = sock_net(sk); 20068cdaaa15SDavid L Stevens int leavegroup = 0; 20071da177e4SLinus Torvalds int i, j, rv; 20081da177e4SLinus Torvalds 2009f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 20101da177e4SLinus Torvalds return -EINVAL; 20111da177e4SLinus Torvalds 20126756ae4bSStephen Hemminger rtnl_lock(); 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 20151da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 20161da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2017877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds if (!in_dev) { 20201da177e4SLinus Torvalds err = -ENODEV; 20211da177e4SLinus Torvalds goto done; 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 20241da177e4SLinus Torvalds 20251d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 2026f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 2027f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 2028f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 20291da177e4SLinus Torvalds break; 20301da177e4SLinus Torvalds } 2031917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2032917f2f10SDavid L Stevens err = -EINVAL; 20331da177e4SLinus Torvalds goto done; 2034917f2f10SDavid L Stevens } 20351da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 20361da177e4SLinus Torvalds if (pmc->sflist) { 2037917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 2038917f2f10SDavid L Stevens err = -EINVAL; 20391da177e4SLinus Torvalds goto done; 2040917f2f10SDavid L Stevens } 20411da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 20421da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 20431da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 20441da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 20451da177e4SLinus Torvalds NULL, 0); 20461da177e4SLinus Torvalds pmc->sfmode = omode; 20471da177e4SLinus Torvalds } 20481da177e4SLinus Torvalds 20491d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 20501da177e4SLinus Torvalds if (!add) { 20511da177e4SLinus Torvalds if (!psl) 2052917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 20531da177e4SLinus Torvalds rv = !0; 20541da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 20551da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 205663007727SAl Viro sizeof(__be32)); 20571da177e4SLinus Torvalds if (rv == 0) 20581da177e4SLinus Torvalds break; 20591da177e4SLinus Torvalds } 20601da177e4SLinus Torvalds if (rv) /* source not found */ 2061917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 20621da177e4SLinus Torvalds 20638cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 20648cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 20658cdaaa15SDavid L Stevens leavegroup = 1; 20668cdaaa15SDavid L Stevens goto done; 20678cdaaa15SDavid L Stevens } 20688cdaaa15SDavid L Stevens 20691da177e4SLinus Torvalds /* update the interface filter */ 20701da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 20711da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds for (j = i+1; j < psl->sl_count; j++) 20741da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 20751da177e4SLinus Torvalds psl->sl_count--; 20761da177e4SLinus Torvalds err = 0; 20771da177e4SLinus Torvalds goto done; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds /* else, add a new source to the filter */ 20801da177e4SLinus Torvalds 20811da177e4SLinus Torvalds if (psl && psl->sl_count >= sysctl_igmp_max_msf) { 20821da177e4SLinus Torvalds err = -ENOBUFS; 20831da177e4SLinus Torvalds goto done; 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 20861da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 20871da177e4SLinus Torvalds int count = IP_SFBLOCK; 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds if (psl) 20901da177e4SLinus Torvalds count += psl->sl_max; 20918b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 20921da177e4SLinus Torvalds if (!newpsl) { 20931da177e4SLinus Torvalds err = -ENOBUFS; 20941da177e4SLinus Torvalds goto done; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds newpsl->sl_max = count; 20971da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 20981da177e4SLinus Torvalds if (psl) { 20991da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) 21001da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2101c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2102c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 21037519cce4SLai Jiangshan kfree_rcu(psl, rcu); 21041da177e4SLinus Torvalds } 2105cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2106c85bb41eSFlavio Leitner psl = newpsl; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 21091da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 21101da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 211163007727SAl Viro sizeof(__be32)); 21121da177e4SLinus Torvalds if (rv == 0) 21131da177e4SLinus Torvalds break; 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 21161da177e4SLinus Torvalds goto done; 21171da177e4SLinus Torvalds for (j = psl->sl_count-1; j >= i; j--) 21181da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 21191da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 21201da177e4SLinus Torvalds psl->sl_count++; 21211da177e4SLinus Torvalds err = 0; 21221da177e4SLinus Torvalds /* update the interface list */ 21231da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 21241da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 21251da177e4SLinus Torvalds done: 21266756ae4bSStephen Hemminger rtnl_unlock(); 21278cdaaa15SDavid L Stevens if (leavegroup) 21288cdaaa15SDavid L Stevens return ip_mc_leave_group(sk, &imr); 21291da177e4SLinus Torvalds return err; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 21331da177e4SLinus Torvalds { 21349951f036SDavid L Stevens int err = 0; 21351da177e4SLinus Torvalds struct ip_mreqn imr; 213663007727SAl Viro __be32 addr = msf->imsf_multiaddr; 21371da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 21381da177e4SLinus Torvalds struct in_device *in_dev; 21391da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21401da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2141877acedcSDaniel Lezcano struct net *net = sock_net(sk); 21429951f036SDavid L Stevens int leavegroup = 0; 21431da177e4SLinus Torvalds 2144f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21451da177e4SLinus Torvalds return -EINVAL; 21461da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 21471da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 21481da177e4SLinus Torvalds return -EINVAL; 21491da177e4SLinus Torvalds 21506756ae4bSStephen Hemminger rtnl_lock(); 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 21531da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 21541da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2155877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds if (!in_dev) { 21581da177e4SLinus Torvalds err = -ENODEV; 21591da177e4SLinus Torvalds goto done; 21601da177e4SLinus Torvalds } 21611da177e4SLinus Torvalds 21629951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 21639951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 21649951f036SDavid L Stevens leavegroup = 1; 21659951f036SDavid L Stevens goto done; 21669951f036SDavid L Stevens } 21679951f036SDavid L Stevens 21681d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 21691da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 21701da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 21711da177e4SLinus Torvalds break; 21721da177e4SLinus Torvalds } 2173917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2174917f2f10SDavid L Stevens err = -EINVAL; 21751da177e4SLinus Torvalds goto done; 2176917f2f10SDavid L Stevens } 21771da177e4SLinus Torvalds if (msf->imsf_numsrc) { 21788b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 21798b3a7005SKris Katterjohn GFP_KERNEL); 21801da177e4SLinus Torvalds if (!newpsl) { 21811da177e4SLinus Torvalds err = -ENOBUFS; 21821da177e4SLinus Torvalds goto done; 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 21851da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 21861da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 21871da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 21881da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 21891da177e4SLinus Torvalds if (err) { 21901da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 21911da177e4SLinus Torvalds goto done; 21921da177e4SLinus Torvalds } 21938713dbf0SYan Zheng } else { 21941da177e4SLinus Torvalds newpsl = NULL; 21958713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 21968713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 21978713dbf0SYan Zheng } 21981d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 21991da177e4SLinus Torvalds if (psl) { 22001da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 22011da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2202c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2203c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 22047519cce4SLai Jiangshan kfree_rcu(psl, rcu); 22051da177e4SLinus Torvalds } else 22061da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 22071da177e4SLinus Torvalds 0, NULL, 0); 2208cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 22091da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2210917f2f10SDavid L Stevens err = 0; 22111da177e4SLinus Torvalds done: 22126756ae4bSStephen Hemminger rtnl_unlock(); 22139951f036SDavid L Stevens if (leavegroup) 22149951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 22151da177e4SLinus Torvalds return err; 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 22191da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 22201da177e4SLinus Torvalds { 22211da177e4SLinus Torvalds int err, len, count, copycount; 22221da177e4SLinus Torvalds struct ip_mreqn imr; 222363007727SAl Viro __be32 addr = msf->imsf_multiaddr; 22241da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22251da177e4SLinus Torvalds struct in_device *in_dev; 22261da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22271da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2228877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22291da177e4SLinus Torvalds 2230f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 22311da177e4SLinus Torvalds return -EINVAL; 22321da177e4SLinus Torvalds 22336756ae4bSStephen Hemminger rtnl_lock(); 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 22361da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 22371da177e4SLinus Torvalds imr.imr_ifindex = 0; 2238877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds if (!in_dev) { 22411da177e4SLinus Torvalds err = -ENODEV; 22421da177e4SLinus Torvalds goto done; 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 22451da177e4SLinus Torvalds 22461d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 22471da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 22481da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 22491da177e4SLinus Torvalds break; 22501da177e4SLinus Torvalds } 22511da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 22521da177e4SLinus Torvalds goto done; 22531da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 22541d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 22556756ae4bSStephen Hemminger rtnl_unlock(); 22561da177e4SLinus Torvalds if (!psl) { 22571da177e4SLinus Torvalds len = 0; 22581da177e4SLinus Torvalds count = 0; 22591da177e4SLinus Torvalds } else { 22601da177e4SLinus Torvalds count = psl->sl_count; 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 22631da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 22641da177e4SLinus Torvalds msf->imsf_numsrc = count; 22651da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 22661da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 22671da177e4SLinus Torvalds return -EFAULT; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds if (len && 22701da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 22711da177e4SLinus Torvalds return -EFAULT; 22721da177e4SLinus Torvalds return 0; 22731da177e4SLinus Torvalds done: 22746756ae4bSStephen Hemminger rtnl_unlock(); 22751da177e4SLinus Torvalds return err; 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 22791da177e4SLinus Torvalds struct group_filter __user *optval, int __user *optlen) 22801da177e4SLinus Torvalds { 22811da177e4SLinus Torvalds int err, i, count, copycount; 22821da177e4SLinus Torvalds struct sockaddr_in *psin; 228363007727SAl Viro __be32 addr; 22841da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22851da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22861da177e4SLinus Torvalds struct ip_sf_socklist *psl; 22871da177e4SLinus Torvalds 22881da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 22891da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 22901da177e4SLinus Torvalds return -EINVAL; 22911da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2292f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 22931da177e4SLinus Torvalds return -EINVAL; 22941da177e4SLinus Torvalds 22956756ae4bSStephen Hemminger rtnl_lock(); 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 22981da177e4SLinus Torvalds 22991d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 23001da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 23011da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 23021da177e4SLinus Torvalds break; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 23051da177e4SLinus Torvalds goto done; 23061da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 23071d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 23086756ae4bSStephen Hemminger rtnl_unlock(); 23091da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 23101da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 23111da177e4SLinus Torvalds gsf->gf_numsrc = count; 23121da177e4SLinus Torvalds if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 23131da177e4SLinus Torvalds copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 23141da177e4SLinus Torvalds return -EFAULT; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds for (i = 0; i < copycount; i++) { 23171da177e4SLinus Torvalds struct sockaddr_storage ss; 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 23201da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 23211da177e4SLinus Torvalds psin->sin_family = AF_INET; 23221da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 23231da177e4SLinus Torvalds if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 23241da177e4SLinus Torvalds return -EFAULT; 23251da177e4SLinus Torvalds } 23261da177e4SLinus Torvalds return 0; 23271da177e4SLinus Torvalds done: 23286756ae4bSStephen Hemminger rtnl_unlock(); 23291da177e4SLinus Torvalds return err; 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds /* 23331da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 23341da177e4SLinus Torvalds */ 2335c0cda068SAl Viro int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif) 23361da177e4SLinus Torvalds { 23371da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23381da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 23391da177e4SLinus Torvalds struct ip_sf_socklist *psl; 23401da177e4SLinus Torvalds int i; 2341c85bb41eSFlavio Leitner int ret; 23421da177e4SLinus Torvalds 2343c85bb41eSFlavio Leitner ret = 1; 2344f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2345c85bb41eSFlavio Leitner goto out; 23461da177e4SLinus Torvalds 2347c85bb41eSFlavio Leitner rcu_read_lock(); 23481d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 23491da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 23501da177e4SLinus Torvalds pmc->multi.imr_ifindex == dif) 23511da177e4SLinus Torvalds break; 23521da177e4SLinus Torvalds } 2353c85bb41eSFlavio Leitner ret = inet->mc_all; 23541da177e4SLinus Torvalds if (!pmc) 2355c85bb41eSFlavio Leitner goto unlock; 23561d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2357c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 23581da177e4SLinus Torvalds if (!psl) 2359c85bb41eSFlavio Leitner goto unlock; 23601da177e4SLinus Torvalds 23611da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 23621da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 23631da177e4SLinus Torvalds break; 23641da177e4SLinus Torvalds } 2365c85bb41eSFlavio Leitner ret = 0; 23661da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2367c85bb41eSFlavio Leitner goto unlock; 23681da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2369c85bb41eSFlavio Leitner goto unlock; 2370c85bb41eSFlavio Leitner ret = 1; 2371c85bb41eSFlavio Leitner unlock: 2372c85bb41eSFlavio Leitner rcu_read_unlock(); 2373c85bb41eSFlavio Leitner out: 2374c85bb41eSFlavio Leitner return ret; 23751da177e4SLinus Torvalds } 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds /* 23781da177e4SLinus Torvalds * A socket is closing. 23791da177e4SLinus Torvalds */ 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 23821da177e4SLinus Torvalds { 23831da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23841da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2385877acedcSDaniel Lezcano struct net *net = sock_net(sk); 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds if (inet->mc_list == NULL) 23881da177e4SLinus Torvalds return; 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds rtnl_lock(); 23911d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 23921da177e4SLinus Torvalds struct in_device *in_dev; 23931da177e4SLinus Torvalds 23941d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2395877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 23961da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 239718943d29SEric Dumazet if (in_dev != NULL) 23981da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2399c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2400c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 240110d50e74SLai Jiangshan kfree_rcu(iml, rcu); 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds rtnl_unlock(); 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 2406dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 2407dbdd9a52SDavid S. Miller int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto) 24081da177e4SLinus Torvalds { 24091da177e4SLinus Torvalds struct ip_mc_list *im; 2410e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 24111da177e4SLinus Torvalds struct ip_sf_list *psf; 24121da177e4SLinus Torvalds int rv = 0; 24131da177e4SLinus Torvalds 2414e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash); 2415e9897071SEric Dumazet if (mc_hash) { 2416c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); 2417e9897071SEric Dumazet 2418e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]); 2419e9897071SEric Dumazet im != NULL; 2420e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) { 2421e9897071SEric Dumazet if (im->multiaddr == mc_addr) 2422e9897071SEric Dumazet break; 2423e9897071SEric Dumazet } 2424e9897071SEric Dumazet } else { 24251d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 24261da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 24271da177e4SLinus Torvalds break; 24281da177e4SLinus Torvalds } 2429e9897071SEric Dumazet } 24301da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 24311da177e4SLinus Torvalds rv = 1; 24321da177e4SLinus Torvalds } else if (im) { 24331da177e4SLinus Torvalds if (src_addr) { 24341da177e4SLinus Torvalds for (psf = im->sources; psf; psf = psf->sf_next) { 24351da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 24361da177e4SLinus Torvalds break; 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds if (psf) 24391da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 24401da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 24411da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 24421da177e4SLinus Torvalds else 24431da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 24441da177e4SLinus Torvalds } else 24451da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds return rv; 24481da177e4SLinus Torvalds } 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 24511da177e4SLinus Torvalds struct igmp_mc_iter_state { 24527091e728SAlexey Dobriyan struct seq_net_private p; 24531da177e4SLinus Torvalds struct net_device *dev; 24541da177e4SLinus Torvalds struct in_device *in_dev; 24551da177e4SLinus Torvalds }; 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 24601da177e4SLinus Torvalds { 24617091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 24621da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 24631da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24641da177e4SLinus Torvalds 24657562f876SPavel Emelianov state->in_dev = NULL; 246661fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 24671da177e4SLinus Torvalds struct in_device *in_dev; 24686baff150SEric Dumazet 24696baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 24701da177e4SLinus Torvalds if (!in_dev) 24711da177e4SLinus Torvalds continue; 24721d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 24731da177e4SLinus Torvalds if (im) { 24741da177e4SLinus Torvalds state->in_dev = in_dev; 24751da177e4SLinus Torvalds break; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds } 24781da177e4SLinus Torvalds return im; 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds 24811da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 24821da177e4SLinus Torvalds { 24831da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24846baff150SEric Dumazet 24851d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 24861d7138deSEric Dumazet while (!im) { 24876baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 24881da177e4SLinus Torvalds if (!state->dev) { 24891da177e4SLinus Torvalds state->in_dev = NULL; 24901da177e4SLinus Torvalds break; 24911da177e4SLinus Torvalds } 24926baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 24931da177e4SLinus Torvalds if (!state->in_dev) 24941da177e4SLinus Torvalds continue; 24951d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds return im; 24981da177e4SLinus Torvalds } 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 25011da177e4SLinus Torvalds { 25021da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 25031da177e4SLinus Torvalds if (im) 25041da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 25051da177e4SLinus Torvalds --pos; 25061da177e4SLinus Torvalds return pos ? NULL : im; 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 251061fbab77Sstephen hemminger __acquires(rcu) 25111da177e4SLinus Torvalds { 251261fbab77Sstephen hemminger rcu_read_lock(); 25131da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 25141da177e4SLinus Torvalds } 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25171da177e4SLinus Torvalds { 25181da177e4SLinus Torvalds struct ip_mc_list *im; 25191da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 25201da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 25211da177e4SLinus Torvalds else 25221da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 25231da177e4SLinus Torvalds ++*pos; 25241da177e4SLinus Torvalds return im; 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 252861fbab77Sstephen hemminger __releases(rcu) 25291da177e4SLinus Torvalds { 25301da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 25311d7138deSEric Dumazet 25321da177e4SLinus Torvalds state->in_dev = NULL; 25331da177e4SLinus Torvalds state->dev = NULL; 253461fbab77Sstephen hemminger rcu_read_unlock(); 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 25381da177e4SLinus Torvalds { 25391da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 25401da177e4SLinus Torvalds seq_puts(seq, 25411da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 25421da177e4SLinus Torvalds else { 25431da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 25441da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 25451da177e4SLinus Torvalds char *querier; 2546a399a805SEric Dumazet long delta; 2547a399a805SEric Dumazet 25481da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 25491da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 25501da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 25511da177e4SLinus Torvalds "V3"; 25521da177e4SLinus Torvalds #else 25531da177e4SLinus Torvalds querier = "NONE"; 25541da177e4SLinus Torvalds #endif 25551da177e4SLinus Torvalds 2556e6b68883SAndreea-Cristina Bernat if (rcu_access_pointer(state->in_dev->mc_list) == im) { 25571da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2558b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds 2561a399a805SEric Dumazet delta = im->timer.expires - jiffies; 25621da177e4SLinus Torvalds seq_printf(seq, 2563338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 25641da177e4SLinus Torvalds im->multiaddr, im->users, 2565a399a805SEric Dumazet im->tm_running, 2566a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 25671da177e4SLinus Torvalds im->reporter); 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds return 0; 25701da177e4SLinus Torvalds } 25711da177e4SLinus Torvalds 2572f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 25731da177e4SLinus Torvalds .start = igmp_mc_seq_start, 25741da177e4SLinus Torvalds .next = igmp_mc_seq_next, 25751da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 25761da177e4SLinus Torvalds .show = igmp_mc_seq_show, 25771da177e4SLinus Torvalds }; 25781da177e4SLinus Torvalds 25791da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file) 25801da177e4SLinus Torvalds { 25817091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mc_seq_ops, 2582cf7732e4SPavel Emelyanov sizeof(struct igmp_mc_iter_state)); 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 25859a32144eSArjan van de Ven static const struct file_operations igmp_mc_seq_fops = { 25861da177e4SLinus Torvalds .owner = THIS_MODULE, 25871da177e4SLinus Torvalds .open = igmp_mc_seq_open, 25881da177e4SLinus Torvalds .read = seq_read, 25891da177e4SLinus Torvalds .llseek = seq_lseek, 25907091e728SAlexey Dobriyan .release = seq_release_net, 25911da177e4SLinus Torvalds }; 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds struct igmp_mcf_iter_state { 25947091e728SAlexey Dobriyan struct seq_net_private p; 25951da177e4SLinus Torvalds struct net_device *dev; 25961da177e4SLinus Torvalds struct in_device *idev; 25971da177e4SLinus Torvalds struct ip_mc_list *im; 25981da177e4SLinus Torvalds }; 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 26031da177e4SLinus Torvalds { 26047091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 26051da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 26061da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 26071da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26081da177e4SLinus Torvalds 26097562f876SPavel Emelianov state->idev = NULL; 26107562f876SPavel Emelianov state->im = NULL; 261161fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 26121da177e4SLinus Torvalds struct in_device *idev; 26136baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 26141da177e4SLinus Torvalds if (unlikely(idev == NULL)) 26151da177e4SLinus Torvalds continue; 26161d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 26171da177e4SLinus Torvalds if (likely(im != NULL)) { 26181da177e4SLinus Torvalds spin_lock_bh(&im->lock); 26191da177e4SLinus Torvalds psf = im->sources; 26201da177e4SLinus Torvalds if (likely(psf != NULL)) { 26211da177e4SLinus Torvalds state->im = im; 26221da177e4SLinus Torvalds state->idev = idev; 26231da177e4SLinus Torvalds break; 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds return psf; 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 26321da177e4SLinus Torvalds { 26331da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds psf = psf->sf_next; 26361da177e4SLinus Torvalds while (!psf) { 26371da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 26381da177e4SLinus Torvalds state->im = state->im->next; 26391da177e4SLinus Torvalds while (!state->im) { 26406baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 26411da177e4SLinus Torvalds if (!state->dev) { 26421da177e4SLinus Torvalds state->idev = NULL; 26431da177e4SLinus Torvalds goto out; 26441da177e4SLinus Torvalds } 26456baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 26461da177e4SLinus Torvalds if (!state->idev) 26471da177e4SLinus Torvalds continue; 26481d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds if (!state->im) 26511da177e4SLinus Torvalds break; 26521da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 26531da177e4SLinus Torvalds psf = state->im->sources; 26541da177e4SLinus Torvalds } 26551da177e4SLinus Torvalds out: 26561da177e4SLinus Torvalds return psf; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 26601da177e4SLinus Torvalds { 26611da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 26621da177e4SLinus Torvalds if (psf) 26631da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 26641da177e4SLinus Torvalds --pos; 26651da177e4SLinus Torvalds return pos ? NULL : psf; 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 266961fbab77Sstephen hemminger __acquires(rcu) 26701da177e4SLinus Torvalds { 267161fbab77Sstephen hemminger rcu_read_lock(); 26721da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26761da177e4SLinus Torvalds { 26771da177e4SLinus Torvalds struct ip_sf_list *psf; 26781da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 26791da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 26801da177e4SLinus Torvalds else 26811da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 26821da177e4SLinus Torvalds ++*pos; 26831da177e4SLinus Torvalds return psf; 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 268761fbab77Sstephen hemminger __releases(rcu) 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26901da177e4SLinus Torvalds if (likely(state->im != NULL)) { 26911da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 26921da177e4SLinus Torvalds state->im = NULL; 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds state->idev = NULL; 26951da177e4SLinus Torvalds state->dev = NULL; 269661fbab77Sstephen hemminger rcu_read_unlock(); 26971da177e4SLinus Torvalds } 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 27001da177e4SLinus Torvalds { 27011da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 27021da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 27051744bea1SJoe Perches seq_puts(seq, "Idx Device MCA SRC INC EXC\n"); 27061da177e4SLinus Torvalds } else { 27071da177e4SLinus Torvalds seq_printf(seq, 27081da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 27091da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 27101da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 27111da177e4SLinus Torvalds ntohl(state->im->multiaddr), 27121da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 27131da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 27141da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds return 0; 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 2719f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 27201da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 27211da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 27221da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 27231da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 27241da177e4SLinus Torvalds }; 27251da177e4SLinus Torvalds 27261da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file) 27271da177e4SLinus Torvalds { 27287091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mcf_seq_ops, 2729cf7732e4SPavel Emelyanov sizeof(struct igmp_mcf_iter_state)); 27301da177e4SLinus Torvalds } 27311da177e4SLinus Torvalds 27329a32144eSArjan van de Ven static const struct file_operations igmp_mcf_seq_fops = { 27331da177e4SLinus Torvalds .owner = THIS_MODULE, 27341da177e4SLinus Torvalds .open = igmp_mcf_seq_open, 27351da177e4SLinus Torvalds .read = seq_read, 27361da177e4SLinus Torvalds .llseek = seq_lseek, 27377091e728SAlexey Dobriyan .release = seq_release_net, 27387091e728SAlexey Dobriyan }; 27397091e728SAlexey Dobriyan 27402c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 27417091e728SAlexey Dobriyan { 27427091e728SAlexey Dobriyan struct proc_dir_entry *pde; 27437091e728SAlexey Dobriyan 2744d4beaa66SGao feng pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops); 27457091e728SAlexey Dobriyan if (!pde) 27467091e728SAlexey Dobriyan goto out_igmp; 2747d4beaa66SGao feng pde = proc_create("mcfilter", S_IRUGO, net->proc_net, 2748d4beaa66SGao feng &igmp_mcf_seq_fops); 27497091e728SAlexey Dobriyan if (!pde) 27507091e728SAlexey Dobriyan goto out_mcfilter; 27517091e728SAlexey Dobriyan return 0; 27527091e728SAlexey Dobriyan 27537091e728SAlexey Dobriyan out_mcfilter: 2754ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 27557091e728SAlexey Dobriyan out_igmp: 27567091e728SAlexey Dobriyan return -ENOMEM; 27577091e728SAlexey Dobriyan } 27587091e728SAlexey Dobriyan 27592c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 27607091e728SAlexey Dobriyan { 2761ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net); 2762ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 27637091e728SAlexey Dobriyan } 27647091e728SAlexey Dobriyan 27657091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 27667091e728SAlexey Dobriyan .init = igmp_net_init, 27677091e728SAlexey Dobriyan .exit = igmp_net_exit, 27681da177e4SLinus Torvalds }; 276972c1d3bdSWANG Cong #endif 27701da177e4SLinus Torvalds 27714aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this, 27724aa5dee4SJiri Pirko unsigned long event, void *ptr) 27734aa5dee4SJiri Pirko { 27744aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 27754aa5dee4SJiri Pirko struct in_device *in_dev; 27764aa5dee4SJiri Pirko 27774aa5dee4SJiri Pirko switch (event) { 27784aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 27794aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev); 27804aa5dee4SJiri Pirko if (in_dev) 27814aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev); 27824aa5dee4SJiri Pirko break; 27834aa5dee4SJiri Pirko default: 27844aa5dee4SJiri Pirko break; 27854aa5dee4SJiri Pirko } 27864aa5dee4SJiri Pirko return NOTIFY_DONE; 27874aa5dee4SJiri Pirko } 27884aa5dee4SJiri Pirko 27894aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = { 27904aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event, 27914aa5dee4SJiri Pirko }; 27924aa5dee4SJiri Pirko 279372c1d3bdSWANG Cong int __init igmp_mc_init(void) 27941da177e4SLinus Torvalds { 279572c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS) 27964aa5dee4SJiri Pirko int err; 27974aa5dee4SJiri Pirko 27984aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops); 27994aa5dee4SJiri Pirko if (err) 28004aa5dee4SJiri Pirko return err; 28014aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier); 28024aa5dee4SJiri Pirko if (err) 28034aa5dee4SJiri Pirko goto reg_notif_fail; 28044aa5dee4SJiri Pirko return 0; 28054aa5dee4SJiri Pirko 28064aa5dee4SJiri Pirko reg_notif_fail: 28074aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops); 28084aa5dee4SJiri Pirko return err; 280972c1d3bdSWANG Cong #else 281072c1d3bdSWANG Cong return register_netdevice_notifier(&igmp_notifier); 28111da177e4SLinus Torvalds #endif 281272c1d3bdSWANG Cong } 2813