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> 10093a714d6SMadhu Challa #include <net/inet_common.h> 1011da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 1021da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1031da177e4SLinus Torvalds #include <linux/mroute.h> 1041da177e4SLinus Torvalds #endif 1051da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1061da177e4SLinus Torvalds #include <linux/proc_fs.h> 1071da177e4SLinus Torvalds #include <linux/seq_file.h> 1081da177e4SLinus Torvalds #endif 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1111da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */ 1121da177e4SLinus Torvalds 113436f7c20SFabian Frederick #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ) 114436f7c20SFabian Frederick #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ) 115436f7c20SFabian Frederick #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ) 116436f7c20SFabian Frederick #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ) 117436f7c20SFabian Frederick #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ) 118436f7c20SFabian Frederick #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds 121436f7c20SFabian Frederick #define IGMP_INITIAL_REPORT_DELAY (1) 1221da177e4SLinus Torvalds 123436f7c20SFabian Frederick /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs! 1241da177e4SLinus Torvalds * IGMP specs require to report membership immediately after 1251da177e4SLinus Torvalds * joining a group, but we delay the first report by a 1261da177e4SLinus Torvalds * small interval. It seems more natural and still does not 1271da177e4SLinus Torvalds * contradict to specs provided this delay is small enough. 1281da177e4SLinus Torvalds */ 1291da177e4SLinus Torvalds 13042f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \ 131c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ 13242f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ 1331da177e4SLinus Torvalds ((in_dev)->mr_v1_seen && \ 1341da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v1_seen))) 13542f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \ 136c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ 13742f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ 1381da177e4SLinus Torvalds ((in_dev)->mr_v2_seen && \ 1391da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v2_seen))) 1401da177e4SLinus Torvalds 141cab70040SWilliam Manley static int unsolicited_report_interval(struct in_device *in_dev) 142cab70040SWilliam Manley { 1432690048cSWilliam Manley int interval_ms, interval_jiffies; 1442690048cSWilliam Manley 145cab70040SWilliam Manley if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 1462690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1472690048cSWilliam Manley in_dev, 1482690048cSWilliam Manley IGMPV2_UNSOLICITED_REPORT_INTERVAL); 149cab70040SWilliam Manley else /* v3 */ 1502690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1512690048cSWilliam Manley in_dev, 1522690048cSWilliam Manley IGMPV3_UNSOLICITED_REPORT_INTERVAL); 1532690048cSWilliam Manley 1542690048cSWilliam Manley interval_jiffies = msecs_to_jiffies(interval_ms); 1552690048cSWilliam Manley 1562690048cSWilliam Manley /* _timer functions can't handle a delay of 0 jiffies so ensure 1572690048cSWilliam Manley * we always return a positive value. 1582690048cSWilliam Manley */ 1592690048cSWilliam Manley if (interval_jiffies <= 0) 1602690048cSWilliam Manley interval_jiffies = 1; 1612690048cSWilliam Manley return interval_jiffies; 162cab70040SWilliam Manley } 163cab70040SWilliam Manley 1641da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im); 16563007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr); 1661da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev); 1671da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc); 1681da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc); 1691da177e4SLinus Torvalds #endif 1701da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc); 1718f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 1728f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta); 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds if (atomic_dec_and_test(&im->refcnt)) { 1771da177e4SLinus Torvalds in_dev_put(im->interface); 17842ea299dSLai Jiangshan kfree_rcu(im, rcu); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 182d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc) \ 183d9aa9380SDavid S. Miller for (pmc = rcu_dereference(in_dev->mc_list); \ 184d9aa9380SDavid S. Miller pmc != NULL; \ 185d9aa9380SDavid S. Miller pmc = rcu_dereference(pmc->next_rcu)) 186d9aa9380SDavid S. Miller 187d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc) \ 188d9aa9380SDavid S. Miller for (pmc = rtnl_dereference(in_dev->mc_list); \ 189d9aa9380SDavid S. Miller pmc != NULL; \ 190d9aa9380SDavid S. Miller pmc = rtnl_dereference(pmc->next_rcu)) 191d9aa9380SDavid S. Miller 1921da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* 1951da177e4SLinus Torvalds * Timer management 1961da177e4SLinus Torvalds */ 1971da177e4SLinus Torvalds 1981d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2011da177e4SLinus Torvalds if (del_timer(&im->timer)) 2021da177e4SLinus Torvalds atomic_dec(&im->refcnt); 2031da177e4SLinus Torvalds im->tm_running = 0; 2041da177e4SLinus Torvalds im->reporter = 0; 2051da177e4SLinus Torvalds im->unsolicit_count = 0; 2061da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds /* It must be called with locked im->lock */ 2101da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay) 2111da177e4SLinus Torvalds { 21263862b5bSAruna-Hewapathirane int tv = prandom_u32() % max_delay; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds im->tm_running = 1; 2151da177e4SLinus Torvalds if (!mod_timer(&im->timer, jiffies+tv+2)) 2161da177e4SLinus Torvalds atomic_inc(&im->refcnt); 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev) 2201da177e4SLinus Torvalds { 22163862b5bSAruna-Hewapathirane int tv = prandom_u32() % in_dev->mr_maxdelay; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds in_dev->mr_gq_running = 1; 2241da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2)) 2251da177e4SLinus Torvalds in_dev_hold(in_dev); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) 2291da177e4SLinus Torvalds { 23063862b5bSAruna-Hewapathirane int tv = prandom_u32() % delay; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) 2331da177e4SLinus Torvalds in_dev_hold(in_dev); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2391da177e4SLinus Torvalds im->unsolicit_count = 0; 2401da177e4SLinus Torvalds if (del_timer(&im->timer)) { 2411da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) { 2421da177e4SLinus Torvalds add_timer(&im->timer); 2431da177e4SLinus Torvalds im->tm_running = 1; 2441da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2451da177e4SLinus Torvalds return; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds atomic_dec(&im->refcnt); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds igmp_start_timer(im, max_delay); 2501da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds /* 2551da177e4SLinus Torvalds * Send an IGMP report. 2561da177e4SLinus Torvalds */ 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, 2621da177e4SLinus Torvalds int gdeleted, int sdeleted) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds switch (type) { 2651da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE: 2661da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE: 2671da177e4SLinus Torvalds if (gdeleted || sdeleted) 2681da177e4SLinus Torvalds return 0; 269ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) { 270ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE) 271ad12583fSDavid L Stevens return 1; 272ad12583fSDavid L Stevens /* don't include if this source is excluded 273ad12583fSDavid L Stevens * in all filters 274ad12583fSDavid L Stevens */ 275ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE]) 276ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE; 277ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] == 278ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]; 279ad12583fSDavid L Stevens } 280ad12583fSDavid L Stevens return 0; 2811da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE: 2821da177e4SLinus Torvalds if (gdeleted || sdeleted) 2831da177e4SLinus Torvalds return 0; 2841da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0; 2851da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE: 2861da177e4SLinus Torvalds if (gdeleted || sdeleted) 2871da177e4SLinus Torvalds return 0; 2881da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 || 2891da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE]) 2901da177e4SLinus Torvalds return 0; 2911da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] == 2921da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]; 2931da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES: 2941da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount) 2951da177e4SLinus Torvalds return 0; 2961da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; 2971da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES: 2981da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) 2991da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted); 3001da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds return 0; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds static int 3061da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) 3071da177e4SLinus Torvalds { 3081da177e4SLinus Torvalds struct ip_sf_list *psf; 3091da177e4SLinus Torvalds int scount = 0; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 3121da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 3131da177e4SLinus Torvalds continue; 3141da177e4SLinus Torvalds scount++; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds return scount; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3194c672e4bSDaniel Borkmann static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds struct sk_buff *skb; 3221da177e4SLinus Torvalds struct rtable *rt; 3231da177e4SLinus Torvalds struct iphdr *pip; 3241da177e4SLinus Torvalds struct igmpv3_report *pig; 325877acedcSDaniel Lezcano struct net *net = dev_net(dev); 32631e4543dSDavid S. Miller struct flowi4 fl4; 32766088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 32866088243SHerbert Xu int tlen = dev->needed_tailroom; 3294c672e4bSDaniel Borkmann unsigned int size = mtu; 3301da177e4SLinus Torvalds 33157e1ab6eSEric Dumazet while (1) { 33266088243SHerbert Xu skb = alloc_skb(size + hlen + tlen, 33357e1ab6eSEric Dumazet GFP_ATOMIC | __GFP_NOWARN); 33457e1ab6eSEric Dumazet if (skb) 33557e1ab6eSEric Dumazet break; 33657e1ab6eSEric Dumazet size >>= 1; 33757e1ab6eSEric Dumazet if (size < 256) 3381da177e4SLinus Torvalds return NULL; 33957e1ab6eSEric Dumazet } 3409d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 3411da177e4SLinus Torvalds 34231e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, 34378fbfd8aSDavid S. Miller 0, 0, 34478fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 345b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 3461da177e4SLinus Torvalds kfree_skb(skb); 3471da177e4SLinus Torvalds return NULL; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 350d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 3511da177e4SLinus Torvalds skb->dev = dev; 3521da177e4SLinus Torvalds 3534c672e4bSDaniel Borkmann skb->reserved_tailroom = skb_end_offset(skb) - 3544c672e4bSDaniel Borkmann min(mtu, skb_end_offset(skb)); 35566088243SHerbert Xu skb_reserve(skb, hlen); 3561da177e4SLinus Torvalds 3577e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 358eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb); 3597e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds pip->version = 4; 3621da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2; 3631da177e4SLinus Torvalds pip->tos = 0xc0; 3641da177e4SLinus Torvalds pip->frag_off = htons(IP_DF); 3651da177e4SLinus Torvalds pip->ttl = 1; 366492f64ceSDavid S. Miller pip->daddr = fl4.daddr; 367492f64ceSDavid S. Miller pip->saddr = fl4.saddr; 3681da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP; 3691da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */ 370b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 3711da177e4SLinus Torvalds ((u8 *)&pip[1])[0] = IPOPT_RA; 3721da177e4SLinus Torvalds ((u8 *)&pip[1])[1] = 4; 3731da177e4SLinus Torvalds ((u8 *)&pip[1])[2] = 0; 3741da177e4SLinus Torvalds ((u8 *)&pip[1])[3] = 0; 3751da177e4SLinus Torvalds 376b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; 377d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig)); 378d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb); 3791da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; 3801da177e4SLinus Torvalds pig->resv1 = 0; 3811da177e4SLinus Torvalds pig->csum = 0; 3821da177e4SLinus Torvalds pig->resv2 = 0; 3831da177e4SLinus Torvalds pig->ngrec = 0; 3841da177e4SLinus Torvalds return skb; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb) 3881da177e4SLinus Torvalds { 389d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb); 390f7c0c2aeSSimon Horman const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb); 3911da177e4SLinus Torvalds 392d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); 3931da177e4SLinus Torvalds 39433224b16SEric W. Biederman return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) 3981da177e4SLinus Torvalds { 3991da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, 4031da177e4SLinus Torvalds int type, struct igmpv3_grec **ppgr) 4041da177e4SLinus Torvalds { 4051da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4061da177e4SLinus Torvalds struct igmpv3_report *pih; 4071da177e4SLinus Torvalds struct igmpv3_grec *pgr; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (!skb) 4101da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4111da177e4SLinus Torvalds if (!skb) 4121da177e4SLinus Torvalds return NULL; 4131da177e4SLinus Torvalds pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec)); 4141da177e4SLinus Torvalds pgr->grec_type = type; 4151da177e4SLinus Torvalds pgr->grec_auxwords = 0; 4161da177e4SLinus Torvalds pgr->grec_nsrcs = 0; 4171da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr; 418d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb); 4191da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1); 4201da177e4SLinus Torvalds *ppgr = pgr; 4211da177e4SLinus Torvalds return skb; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4244c672e4bSDaniel Borkmann #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0) 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, 4271da177e4SLinus Torvalds int type, int gdeleted, int sdeleted) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 43087a8a2aeSNikolay Borisov struct net *net = dev_net(dev); 4311da177e4SLinus Torvalds struct igmpv3_report *pih; 4321da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL; 4331da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; 434ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate; 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 4371da177e4SLinus Torvalds return skb; 43887a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 439df2cf4a7SPhilip Downey return skb; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds isquery = type == IGMPV3_MODE_IS_INCLUDE || 4421da177e4SLinus Torvalds type == IGMPV3_MODE_IS_EXCLUDE; 4431da177e4SLinus Torvalds truncate = type == IGMPV3_MODE_IS_EXCLUDE || 4441da177e4SLinus Torvalds type == IGMPV3_CHANGE_TO_EXCLUDE; 4451da177e4SLinus Torvalds 446ad12583fSDavid L Stevens stotal = scount = 0; 447ad12583fSDavid L Stevens 4481da177e4SLinus Torvalds psf_list = sdeleted ? &pmc->tomb : &pmc->sources; 4491da177e4SLinus Torvalds 450ad12583fSDavid L Stevens if (!*psf_list) 451ad12583fSDavid L Stevens goto empty_source; 452ad12583fSDavid L Stevens 453d9edf9e2SArnaldo Carvalho de Melo pih = skb ? igmpv3_report_hdr(skb) : NULL; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* EX and TO_EX get a fresh packet, if needed */ 4561da177e4SLinus Torvalds if (truncate) { 4571da177e4SLinus Torvalds if (pih && pih->ngrec && 4581da177e4SLinus Torvalds AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { 4591da177e4SLinus Torvalds if (skb) 4601da177e4SLinus Torvalds igmpv3_sendpack(skb); 4611da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds first = 1; 4651da177e4SLinus Torvalds psf_prev = NULL; 4661da177e4SLinus Torvalds for (psf = *psf_list; psf; psf = psf_next) { 467ea4d9e72SAl Viro __be32 *psrc; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds psf_next = psf->sf_next; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { 4721da177e4SLinus Torvalds psf_prev = psf; 4731da177e4SLinus Torvalds continue; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds /* clear marks on query responses */ 4771da177e4SLinus Torvalds if (isquery) 4781da177e4SLinus Torvalds psf->sf_gsresp = 0; 4791da177e4SLinus Torvalds 48063007727SAl Viro if (AVAILABLE(skb) < sizeof(__be32) + 4811da177e4SLinus Torvalds first*sizeof(struct igmpv3_grec)) { 4821da177e4SLinus Torvalds if (truncate && !first) 4831da177e4SLinus Torvalds break; /* truncate these */ 4841da177e4SLinus Torvalds if (pgr) 4851da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 4861da177e4SLinus Torvalds if (skb) 4871da177e4SLinus Torvalds igmpv3_sendpack(skb); 4881da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4891da177e4SLinus Torvalds first = 1; 4901da177e4SLinus Torvalds scount = 0; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds if (first) { 4931da177e4SLinus Torvalds skb = add_grhead(skb, pmc, type, &pgr); 4941da177e4SLinus Torvalds first = 0; 4951da177e4SLinus Torvalds } 496cc63f70bSAlexey Dobriyan if (!skb) 497cc63f70bSAlexey Dobriyan return NULL; 49863007727SAl Viro psrc = (__be32 *)skb_put(skb, sizeof(__be32)); 4991da177e4SLinus Torvalds *psrc = psf->sf_inaddr; 500ad12583fSDavid L Stevens scount++; stotal++; 5011da177e4SLinus Torvalds if ((type == IGMPV3_ALLOW_NEW_SOURCES || 5021da177e4SLinus Torvalds type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { 5031da177e4SLinus Torvalds psf->sf_crcount--; 5041da177e4SLinus Torvalds if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { 5051da177e4SLinus Torvalds if (psf_prev) 5061da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5071da177e4SLinus Torvalds else 5081da177e4SLinus Torvalds *psf_list = psf->sf_next; 5091da177e4SLinus Torvalds kfree(psf); 5101da177e4SLinus Torvalds continue; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds psf_prev = psf; 5141da177e4SLinus Torvalds } 515ad12583fSDavid L Stevens 516ad12583fSDavid L Stevens empty_source: 517ad12583fSDavid L Stevens if (!stotal) { 518ad12583fSDavid L Stevens if (type == IGMPV3_ALLOW_NEW_SOURCES || 519ad12583fSDavid L Stevens type == IGMPV3_BLOCK_OLD_SOURCES) 520ad12583fSDavid L Stevens return skb; 521ad12583fSDavid L Stevens if (pmc->crcount || isquery) { 522ad12583fSDavid L Stevens /* make sure we have room for group header */ 523ad12583fSDavid L Stevens if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) { 524ad12583fSDavid L Stevens igmpv3_sendpack(skb); 525ad12583fSDavid L Stevens skb = NULL; /* add_grhead will get a new one */ 526ad12583fSDavid L Stevens } 527ad12583fSDavid L Stevens skb = add_grhead(skb, pmc, type, &pgr); 528ad12583fSDavid L Stevens } 529ad12583fSDavid L Stevens } 5301da177e4SLinus Torvalds if (pgr) 5311da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds if (isquery) 5341da177e4SLinus Torvalds pmc->gsquery = 0; /* clear query state on report */ 5351da177e4SLinus Torvalds return skb; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds struct sk_buff *skb = NULL; 54187a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 5421da177e4SLinus Torvalds int type; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds if (!pmc) { 5451d7138deSEric Dumazet rcu_read_lock(); 5461d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 5471da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 5481da177e4SLinus Torvalds continue; 549df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(pmc->multiaddr) && 55087a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 551df2cf4a7SPhilip Downey continue; 5521da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5531da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5541da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5551da177e4SLinus Torvalds else 5561da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5571da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5581da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5591da177e4SLinus Torvalds } 5601d7138deSEric Dumazet rcu_read_unlock(); 5611da177e4SLinus Torvalds } else { 5621da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5631da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5641da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5651da177e4SLinus Torvalds else 5661da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5671da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5681da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds if (!skb) 5711da177e4SLinus Torvalds return 0; 5721da177e4SLinus Torvalds return igmpv3_sendpack(skb); 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* 5761da177e4SLinus Torvalds * remove zero-count source records from a source filter list 5771da177e4SLinus Torvalds */ 5781da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) 5791da177e4SLinus Torvalds { 5801da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds psf_prev = NULL; 5831da177e4SLinus Torvalds for (psf = *ppsf; psf; psf = psf_next) { 5841da177e4SLinus Torvalds psf_next = psf->sf_next; 5851da177e4SLinus Torvalds if (psf->sf_crcount == 0) { 5861da177e4SLinus Torvalds if (psf_prev) 5871da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5881da177e4SLinus Torvalds else 5891da177e4SLinus Torvalds *ppsf = psf->sf_next; 5901da177e4SLinus Torvalds kfree(psf); 5911da177e4SLinus Torvalds } else 5921da177e4SLinus Torvalds psf_prev = psf; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next; 5991da177e4SLinus Torvalds struct sk_buff *skb = NULL; 6001da177e4SLinus Torvalds int type, dtype; 6011da177e4SLinus Torvalds 6021d7138deSEric Dumazet rcu_read_lock(); 6031da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /* deleted MCA's */ 6061da177e4SLinus Torvalds pmc_prev = NULL; 6071da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) { 6081da177e4SLinus Torvalds pmc_next = pmc->next; 6091da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 6101da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6111da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6121da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6131da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1); 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds if (pmc->crcount) { 6161da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) { 6171da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6181da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6191da177e4SLinus Torvalds } 620ad12583fSDavid L Stevens pmc->crcount--; 6211da177e4SLinus Torvalds if (pmc->crcount == 0) { 6221da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb); 6231da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources); 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { 6271da177e4SLinus Torvalds if (pmc_prev) 6281da177e4SLinus Torvalds pmc_prev->next = pmc_next; 6291da177e4SLinus Torvalds else 6301da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next; 6311da177e4SLinus Torvalds in_dev_put(pmc->interface); 6321da177e4SLinus Torvalds kfree(pmc); 6331da177e4SLinus Torvalds } else 6341da177e4SLinus Torvalds pmc_prev = pmc; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds /* change recs */ 6391d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 6401da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6411da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 6421da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6431da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES; 6441da177e4SLinus Torvalds } else { 6451da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES; 6461da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6491da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* filter mode changes */ 6521da177e4SLinus Torvalds if (pmc->crcount) { 6531da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) 6541da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE; 6551da177e4SLinus Torvalds else 6561da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6571da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 658ad12583fSDavid L Stevens pmc->crcount--; 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6611da177e4SLinus Torvalds } 6621d7138deSEric Dumazet rcu_read_unlock(); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds if (!skb) 6651da177e4SLinus Torvalds return; 6661da177e4SLinus Torvalds (void) igmpv3_sendpack(skb); 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, 6701da177e4SLinus Torvalds int type) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds struct sk_buff *skb; 6731da177e4SLinus Torvalds struct iphdr *iph; 6741da177e4SLinus Torvalds struct igmphdr *ih; 6751da177e4SLinus Torvalds struct rtable *rt; 6761da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 677877acedcSDaniel Lezcano struct net *net = dev_net(dev); 67863007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0; 67931e4543dSDavid S. Miller struct flowi4 fl4; 68063007727SAl Viro __be32 dst; 68166088243SHerbert Xu int hlen, tlen; 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) 6841da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc); 685df2cf4a7SPhilip Downey 68687a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 687df2cf4a7SPhilip Downey return 0; 688df2cf4a7SPhilip Downey 689df2cf4a7SPhilip Downey if (type == IGMP_HOST_LEAVE_MESSAGE) 6901da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER; 6911da177e4SLinus Torvalds else 6921da177e4SLinus Torvalds dst = group; 6931da177e4SLinus Torvalds 69431e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, 69578fbfd8aSDavid S. Miller 0, 0, 69678fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 697b23dd4feSDavid S. Miller if (IS_ERR(rt)) 6981da177e4SLinus Torvalds return -1; 69978fbfd8aSDavid S. Miller 70066088243SHerbert Xu hlen = LL_RESERVED_SPACE(dev); 70166088243SHerbert Xu tlen = dev->needed_tailroom; 70266088243SHerbert Xu skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); 70351456b29SIan Morris if (!skb) { 7041da177e4SLinus Torvalds ip_rt_put(rt); 7051da177e4SLinus Torvalds return -1; 7061da177e4SLinus Torvalds } 7079d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 7081da177e4SLinus Torvalds 709d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 7101da177e4SLinus Torvalds 71166088243SHerbert Xu skb_reserve(skb, hlen); 7121da177e4SLinus Torvalds 7137e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 714eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 7157e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds iph->version = 4; 7181da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2; 7191da177e4SLinus Torvalds iph->tos = 0xc0; 7201da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 7211da177e4SLinus Torvalds iph->ttl = 1; 7221da177e4SLinus Torvalds iph->daddr = dst; 723492f64ceSDavid S. Miller iph->saddr = fl4.saddr; 7241da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP; 725b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 7261da177e4SLinus Torvalds ((u8 *)&iph[1])[0] = IPOPT_RA; 7271da177e4SLinus Torvalds ((u8 *)&iph[1])[1] = 4; 7281da177e4SLinus Torvalds ((u8 *)&iph[1])[2] = 0; 7291da177e4SLinus Torvalds ((u8 *)&iph[1])[3] = 0; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 7321da177e4SLinus Torvalds ih->type = type; 7331da177e4SLinus Torvalds ih->code = 0; 7341da177e4SLinus Torvalds ih->csum = 0; 7351da177e4SLinus Torvalds ih->group = group; 7361da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 7371da177e4SLinus Torvalds 73833224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb); 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds static void igmp_gq_timer_expire(unsigned long data) 7421da177e4SLinus Torvalds { 7431da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 7461da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL); 747e2401654SSalam Noureddine in_dev_put(in_dev); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds static void igmp_ifc_timer_expire(unsigned long data) 7511da177e4SLinus Torvalds { 7521da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 7551da177e4SLinus Torvalds if (in_dev->mr_ifc_count) { 7561da177e4SLinus Torvalds in_dev->mr_ifc_count--; 757cab70040SWilliam Manley igmp_ifc_start_timer(in_dev, 758cab70040SWilliam Manley unsolicited_report_interval(in_dev)); 7591da177e4SLinus Torvalds } 760e2401654SSalam Noureddine in_dev_put(in_dev); 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 7641da177e4SLinus Torvalds { 765165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 7661da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 7671da177e4SLinus Torvalds return; 768165094afSNikolay Borisov in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 7691da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds static void igmp_timer_expire(unsigned long data) 7741da177e4SLinus Torvalds { 7751da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)data; 7761da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds spin_lock(&im->lock); 7791da177e4SLinus Torvalds im->tm_running = 0; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds if (im->unsolicit_count) { 7821da177e4SLinus Torvalds im->unsolicit_count--; 783cab70040SWilliam Manley igmp_start_timer(im, unsolicited_report_interval(in_dev)); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds im->reporter = 1; 7861da177e4SLinus Torvalds spin_unlock(&im->lock); 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 7891da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 7901da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 7911da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 7921da177e4SLinus Torvalds else 7931da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds ip_ma_put(im); 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 798ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 799ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds struct ip_sf_list *psf; 8021da177e4SLinus Torvalds int i, scount; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds scount = 0; 8051da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 8061da177e4SLinus Torvalds if (scount == nsrcs) 8071da177e4SLinus Torvalds break; 808ad12583fSDavid L Stevens for (i = 0; i < nsrcs; i++) { 809ad12583fSDavid L Stevens /* skip inactive filters */ 810e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] || 811ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 812ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 813ce713ee5SRongQing.Li break; 814ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 815ad12583fSDavid L Stevens scount++; 816ad12583fSDavid L Stevens break; 817ad12583fSDavid L Stevens } 818ad12583fSDavid L Stevens } 819ad12583fSDavid L Stevens } 820ad12583fSDavid L Stevens pmc->gsquery = 0; 821ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 822ad12583fSDavid L Stevens return 0; 823ad12583fSDavid L Stevens return 1; 824ad12583fSDavid L Stevens } 825ad12583fSDavid L Stevens 826ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 827ad12583fSDavid L Stevens { 828ad12583fSDavid L Stevens struct ip_sf_list *psf; 829ad12583fSDavid L Stevens int i, scount; 830ad12583fSDavid L Stevens 831ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 832ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 833ad12583fSDavid L Stevens 834ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 835ad12583fSDavid L Stevens scount = 0; 836ad12583fSDavid L Stevens for (psf = pmc->sources; psf; psf = psf->sf_next) { 837ad12583fSDavid L Stevens if (scount == nsrcs) 838ad12583fSDavid L Stevens break; 8391da177e4SLinus Torvalds for (i = 0; i < nsrcs; i++) 8401da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 8411da177e4SLinus Torvalds psf->sf_gsresp = 1; 8421da177e4SLinus Torvalds scount++; 8431da177e4SLinus Torvalds break; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds } 846ad12583fSDavid L Stevens if (!scount) { 847ad12583fSDavid L Stevens pmc->gsquery = 0; 848ad12583fSDavid L Stevens return 0; 849ad12583fSDavid L Stevens } 850ad12583fSDavid L Stevens pmc->gsquery = 1; 851ad12583fSDavid L Stevens return 1; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds 854d679c532SEric Dumazet /* return true if packet was dropped */ 855d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group) 8561da177e4SLinus Torvalds { 8571da177e4SLinus Torvalds struct ip_mc_list *im; 85887a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 863d679c532SEric Dumazet return false; 86487a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports) 865df2cf4a7SPhilip Downey return false; 8661da177e4SLinus Torvalds 8671d7138deSEric Dumazet rcu_read_lock(); 8681d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 8691da177e4SLinus Torvalds if (im->multiaddr == group) { 8701da177e4SLinus Torvalds igmp_stop_timer(im); 8711da177e4SLinus Torvalds break; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds } 8741d7138deSEric Dumazet rcu_read_unlock(); 875d679c532SEric Dumazet return false; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 878d679c532SEric Dumazet /* return true if packet was dropped */ 879d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 8801da177e4SLinus Torvalds int len) 8811da177e4SLinus Torvalds { 882d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 883d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 8841da177e4SLinus Torvalds struct ip_mc_list *im; 88563007727SAl Viro __be32 group = ih->group; 8861da177e4SLinus Torvalds int max_delay; 8871da177e4SLinus Torvalds int mark = 0; 88887a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds 8915b7c8406SDavid Stevens if (len == 8) { 8921da177e4SLinus Torvalds if (ih->code == 0) { 8931da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 8941da177e4SLinus Torvalds 895436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 8961da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 897436f7c20SFabian Frederick IGMP_V1_ROUTER_PRESENT_TIMEOUT; 8981da177e4SLinus Torvalds group = 0; 8991da177e4SLinus Torvalds } else { 9001da177e4SLinus Torvalds /* v2 router present */ 9011da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 9021da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 903436f7c20SFabian Frederick IGMP_V2_ROUTER_PRESENT_TIMEOUT; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds /* cancel the interface change timer */ 9061da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 9071da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 9081da177e4SLinus Torvalds __in_dev_put(in_dev); 9091da177e4SLinus Torvalds /* clear deleted report items */ 9101da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 9111da177e4SLinus Torvalds } else if (len < 12) { 912d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */ 9135b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) { 9145b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */ 915436f7c20SFabian Frederick max_delay = IGMP_QUERY_RESPONSE_INTERVAL; 9165b7c8406SDavid Stevens group = 0; 9175b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) { 9185b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present; 9195b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here. 9205b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a 9215b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely 9225b7c8406SDavid Stevens * to be intended in a v3 query. 9235b7c8406SDavid Stevens */ 9245b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 925a8c1f65cSBen Hutchings if (!max_delay) 926a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */ 9271da177e4SLinus Torvalds } else { /* v3 */ 9281da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 929d679c532SEric Dumazet return true; 9301da177e4SLinus Torvalds 931d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9321da177e4SLinus Torvalds if (ih3->nsrcs) { 9331da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 93463007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 935d679c532SEric Dumazet return true; 936d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 9401da177e4SLinus Torvalds if (!max_delay) 9411da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 9421da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 9431da177e4SLinus Torvalds if (ih3->qrv) 9441da177e4SLinus Torvalds in_dev->mr_qrv = ih3->qrv; 9451da177e4SLinus Torvalds if (!group) { /* general query */ 9461da177e4SLinus Torvalds if (ih3->nsrcs) 947b47bd8d2SDaniel Borkmann return true; /* no sources allowed */ 9481da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 949d679c532SEric Dumazet return false; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 9521da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* 9561da177e4SLinus Torvalds * - Start the timers in all of our membership records 9571da177e4SLinus Torvalds * that the query applies to for the interface on 9581da177e4SLinus Torvalds * which the query arrived excl. those that belong 9591da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 9601da177e4SLinus Torvalds * - For timers already running check if they need to 9611da177e4SLinus Torvalds * be reset. 9621da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 9631da177e4SLinus Torvalds * delay possible 9641da177e4SLinus Torvalds */ 9651d7138deSEric Dumazet rcu_read_lock(); 9661d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 967ad12583fSDavid L Stevens int changed; 968ad12583fSDavid L Stevens 9691da177e4SLinus Torvalds if (group && group != im->multiaddr) 9701da177e4SLinus Torvalds continue; 9711da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 9721da177e4SLinus Torvalds continue; 973df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 97487a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 975df2cf4a7SPhilip Downey continue; 9761da177e4SLinus Torvalds spin_lock_bh(&im->lock); 9771da177e4SLinus Torvalds if (im->tm_running) 9781da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 9791da177e4SLinus Torvalds else 9801da177e4SLinus Torvalds im->gsquery = mark; 981ad12583fSDavid L Stevens changed = !im->gsquery || 9821da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 9831da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 984ad12583fSDavid L Stevens if (changed) 9851da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 9861da177e4SLinus Torvalds } 9871d7138deSEric Dumazet rcu_read_unlock(); 988d679c532SEric Dumazet return false; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9919a57a9d2SEric Dumazet /* called in rcu_read_lock() section */ 9921da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 9931da177e4SLinus Torvalds { 9941da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 9951da177e4SLinus Torvalds struct igmphdr *ih; 9969a57a9d2SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(skb->dev); 9971da177e4SLinus Torvalds int len = skb->len; 998d679c532SEric Dumazet bool dropped = true; 9991da177e4SLinus Torvalds 100051456b29SIan Morris if (!in_dev) 1001cd557bc1SDenis V. Lunev goto drop; 10021da177e4SLinus Torvalds 1003fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 10049a57a9d2SEric Dumazet goto drop; 1005fb286bb2SHerbert Xu 1006de08dc1aSTom Herbert if (skb_checksum_simple_validate(skb)) 10079a57a9d2SEric Dumazet goto drop; 10081da177e4SLinus Torvalds 1009d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 10101da177e4SLinus Torvalds switch (ih->type) { 10111da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 1012d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len); 10131da177e4SLinus Torvalds break; 10141da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 10151da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 10161da177e4SLinus Torvalds /* Is it our report looped back? */ 1017c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) 10181da177e4SLinus Torvalds break; 101924c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 102024c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 102124c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 1022d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group); 10231da177e4SLinus Torvalds break; 10241da177e4SLinus Torvalds case IGMP_PIM: 10251da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 10261da177e4SLinus Torvalds return pim_rcv_v1(skb); 10271da177e4SLinus Torvalds #endif 1028c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 10291da177e4SLinus Torvalds case IGMP_DVMRP: 10301da177e4SLinus Torvalds case IGMP_TRACE: 10311da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 10321da177e4SLinus Torvalds case IGMP_MTRACE: 10331da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 10341da177e4SLinus Torvalds break; 10351da177e4SLinus Torvalds default: 1036dd1c1853SLinus Torvalds break; 10371da177e4SLinus Torvalds } 1038fb286bb2SHerbert Xu 1039cd557bc1SDenis V. Lunev drop: 1040d679c532SEric Dumazet if (dropped) 10411da177e4SLinus Torvalds kfree_skb(skb); 1042d679c532SEric Dumazet else 1043d679c532SEric Dumazet consume_skb(skb); 10441da177e4SLinus Torvalds return 0; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds #endif 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds /* 10511da177e4SLinus Torvalds * Add a filter to a device 10521da177e4SLinus Torvalds */ 10531da177e4SLinus Torvalds 105463007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 10551da177e4SLinus Torvalds { 10561da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10571da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 10601da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 1061b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode 10621da177e4SLinus Torvalds routine. Something sort of: 10631da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 10641da177e4SLinus Torvalds --ANK 10651da177e4SLinus Torvalds */ 10661da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 106722bedad3SJiri Pirko dev_mc_add(dev, buf); 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds /* 10711da177e4SLinus Torvalds * Remove a filter from a device 10721da177e4SLinus Torvalds */ 10731da177e4SLinus Torvalds 107463007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 10751da177e4SLinus Torvalds { 10761da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10771da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 108022bedad3SJiri Pirko dev_mc_del(dev, buf); 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 10841da177e4SLinus Torvalds /* 10851da177e4SLinus Torvalds * deleted ip_mc_list manipulation 10861da177e4SLinus Torvalds */ 10871da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im) 10881da177e4SLinus Torvalds { 10891da177e4SLinus Torvalds struct ip_mc_list *pmc; 1090165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 10931da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 10941da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 10951da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 10961da177e4SLinus Torvalds * non-deleted or query-response MCA's. 10971da177e4SLinus Torvalds */ 10980da974f4SPanagiotis Issaris pmc = kzalloc(sizeof(*pmc), GFP_KERNEL); 10991da177e4SLinus Torvalds if (!pmc) 11001da177e4SLinus Torvalds return; 11011da177e4SLinus Torvalds spin_lock_bh(&im->lock); 11021da177e4SLinus Torvalds pmc->interface = im->interface; 11031da177e4SLinus Torvalds in_dev_hold(in_dev); 11041da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 1105165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 11061da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 11071da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 11081da177e4SLinus Torvalds struct ip_sf_list *psf; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds pmc->tomb = im->tomb; 11111da177e4SLinus Torvalds pmc->sources = im->sources; 11121da177e4SLinus Torvalds im->tomb = im->sources = NULL; 11131da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 11141da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11191da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 11201da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 11211da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 112463007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr) 11251da177e4SLinus Torvalds { 11261da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 11271da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11301da177e4SLinus Torvalds pmc_prev = NULL; 11311da177e4SLinus Torvalds for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) { 11321da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 11331da177e4SLinus Torvalds break; 11341da177e4SLinus Torvalds pmc_prev = pmc; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds if (pmc) { 11371da177e4SLinus Torvalds if (pmc_prev) 11381da177e4SLinus Torvalds pmc_prev->next = pmc->next; 11391da177e4SLinus Torvalds else 11401da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11431da177e4SLinus Torvalds if (pmc) { 11441da177e4SLinus Torvalds for (psf = pmc->tomb; psf; psf = psf_next) { 11451da177e4SLinus Torvalds psf_next = psf->sf_next; 11461da177e4SLinus Torvalds kfree(psf); 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds in_dev_put(pmc->interface); 11491da177e4SLinus Torvalds kfree(pmc); 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11581da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 11591da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 11601da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 11631da177e4SLinus Torvalds nextpmc = pmc->next; 11641da177e4SLinus Torvalds ip_mc_clear_src(pmc); 11651da177e4SLinus Torvalds in_dev_put(pmc->interface); 11661da177e4SLinus Torvalds kfree(pmc); 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds /* clear dead sources, too */ 11691d7138deSEric Dumazet rcu_read_lock(); 11701d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 11711da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 11741da177e4SLinus Torvalds psf = pmc->tomb; 11751da177e4SLinus Torvalds pmc->tomb = NULL; 11761da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 11771da177e4SLinus Torvalds for (; psf; psf = psf_next) { 11781da177e4SLinus Torvalds psf_next = psf->sf_next; 11791da177e4SLinus Torvalds kfree(psf); 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds } 11821d7138deSEric Dumazet rcu_read_unlock(); 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds #endif 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im) 11871da177e4SLinus Torvalds { 11881da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 11891da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 119087a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 11911da177e4SLinus Torvalds int reporter; 11921da177e4SLinus Torvalds #endif 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds if (im->loaded) { 11951da177e4SLinus Torvalds im->loaded = 0; 11961da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12001da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 12011da177e4SLinus Torvalds return; 120287a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1203df2cf4a7SPhilip Downey return; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds reporter = im->reporter; 12061da177e4SLinus Torvalds igmp_stop_timer(im); 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds if (!in_dev->dead) { 12091da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 121024cf3af3SVeaceslav Falico return; 12111da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 12121da177e4SLinus Torvalds if (reporter) 12131da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 121424cf3af3SVeaceslav Falico return; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds /* IGMPv3 */ 12171da177e4SLinus Torvalds igmpv3_add_delrec(in_dev, im); 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds igmp_ifc_event(in_dev); 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds #endif 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds static void igmp_group_added(struct ip_mc_list *im) 12251da177e4SLinus Torvalds { 12261da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 1227*dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 122887a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1229*dcd87999SNikolay Borisov #endif 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds if (im->loaded == 0) { 12321da177e4SLinus Torvalds im->loaded = 1; 12331da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12371da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 12381da177e4SLinus Torvalds return; 123987a8a2aeSNikolay Borisov if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports) 1240df2cf4a7SPhilip Downey return; 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds if (in_dev->dead) 12431da177e4SLinus Torvalds return; 12441da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 12451da177e4SLinus Torvalds spin_lock_bh(&im->lock); 1246436f7c20SFabian Frederick igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY); 12471da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 12481da177e4SLinus Torvalds return; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds /* else, v3 */ 12511da177e4SLinus Torvalds 1252165094afSNikolay Borisov im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 12531da177e4SLinus Torvalds igmp_ifc_event(in_dev); 12541da177e4SLinus Torvalds #endif 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds /* 12591da177e4SLinus Torvalds * Multicast list managers 12601da177e4SLinus Torvalds */ 12611da177e4SLinus Torvalds 1262e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im) 1263e9897071SEric Dumazet { 1264c70eba74SEric Dumazet return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); 1265e9897071SEric Dumazet } 1266e9897071SEric Dumazet 1267e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev, 1268e9897071SEric Dumazet struct ip_mc_list *im) 1269e9897071SEric Dumazet { 1270e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 1271e9897071SEric Dumazet u32 hash; 1272e9897071SEric Dumazet 1273e9897071SEric Dumazet mc_hash = rtnl_dereference(in_dev->mc_hash); 1274e9897071SEric Dumazet if (mc_hash) { 1275e9897071SEric Dumazet hash = ip_mc_hash(im); 1276c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1277e9897071SEric Dumazet rcu_assign_pointer(mc_hash[hash], im); 1278e9897071SEric Dumazet return; 1279e9897071SEric Dumazet } 1280e9897071SEric Dumazet 1281e9897071SEric Dumazet /* do not use a hash table for small number of items */ 1282e9897071SEric Dumazet if (in_dev->mc_count < 4) 1283e9897071SEric Dumazet return; 1284e9897071SEric Dumazet 1285e9897071SEric Dumazet mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG, 1286e9897071SEric Dumazet GFP_KERNEL); 1287e9897071SEric Dumazet if (!mc_hash) 1288e9897071SEric Dumazet return; 1289e9897071SEric Dumazet 1290e9897071SEric Dumazet for_each_pmc_rtnl(in_dev, im) { 1291e9897071SEric Dumazet hash = ip_mc_hash(im); 1292c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1293e9897071SEric Dumazet RCU_INIT_POINTER(mc_hash[hash], im); 1294e9897071SEric Dumazet } 1295e9897071SEric Dumazet 1296e9897071SEric Dumazet rcu_assign_pointer(in_dev->mc_hash, mc_hash); 1297e9897071SEric Dumazet } 1298e9897071SEric Dumazet 1299e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev, 1300e9897071SEric Dumazet struct ip_mc_list *im) 1301e9897071SEric Dumazet { 1302e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); 1303e9897071SEric Dumazet struct ip_mc_list *aux; 1304e9897071SEric Dumazet 1305e9897071SEric Dumazet if (!mc_hash) 1306e9897071SEric Dumazet return; 1307e9897071SEric Dumazet mc_hash += ip_mc_hash(im); 1308e9897071SEric Dumazet while ((aux = rtnl_dereference(*mc_hash)) != im) 1309e9897071SEric Dumazet mc_hash = &aux->next_hash; 1310e9897071SEric Dumazet *mc_hash = im->next_hash; 1311e9897071SEric Dumazet } 1312e9897071SEric Dumazet 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds /* 13151da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 13161da177e4SLinus Torvalds */ 13171da177e4SLinus Torvalds 13188f935bbdSAl Viro void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 13191da177e4SLinus Torvalds { 13201da177e4SLinus Torvalds struct ip_mc_list *im; 1321*dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 1322165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1323*dcd87999SNikolay Borisov #endif 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds ASSERT_RTNL(); 13261da177e4SLinus Torvalds 13271d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) { 13281da177e4SLinus Torvalds if (im->multiaddr == addr) { 13291da177e4SLinus Torvalds im->users++; 13301da177e4SLinus Torvalds ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0); 13311da177e4SLinus Torvalds goto out; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 13351d7138deSEric Dumazet im = kzalloc(sizeof(*im), GFP_KERNEL); 13361da177e4SLinus Torvalds if (!im) 13371da177e4SLinus Torvalds goto out; 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds im->users = 1; 13401da177e4SLinus Torvalds im->interface = in_dev; 13411da177e4SLinus Torvalds in_dev_hold(in_dev); 13421da177e4SLinus Torvalds im->multiaddr = addr; 13431da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 13441da177e4SLinus Torvalds im->sfmode = MCAST_EXCLUDE; 13451da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE] = 1; 13461da177e4SLinus Torvalds atomic_set(&im->refcnt, 1); 13471da177e4SLinus Torvalds spin_lock_init(&im->lock); 13481da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1349179542a5SHimangi Saraogi setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im); 1350165094afSNikolay Borisov im->unsolicit_count = net->ipv4.sysctl_igmp_qrv; 13511da177e4SLinus Torvalds #endif 13521d7138deSEric Dumazet 13531d7138deSEric Dumazet im->next_rcu = in_dev->mc_list; 1354b8bae41eSRami Rosen in_dev->mc_count++; 1355cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im); 13561d7138deSEric Dumazet 1357e9897071SEric Dumazet ip_mc_hash_add(in_dev, im); 1358e9897071SEric Dumazet 13591da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13601da177e4SLinus Torvalds igmpv3_del_delrec(in_dev, im->multiaddr); 13611da177e4SLinus Torvalds #endif 13621da177e4SLinus Torvalds igmp_group_added(im); 13631da177e4SLinus Torvalds if (!in_dev->dead) 13641da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 13651da177e4SLinus Torvalds out: 13661da177e4SLinus Torvalds return; 13671da177e4SLinus Torvalds } 13684bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group); 13691da177e4SLinus Torvalds 13709afd85c9SLinus Lüssing static int ip_mc_check_iphdr(struct sk_buff *skb) 13719afd85c9SLinus Lüssing { 13729afd85c9SLinus Lüssing const struct iphdr *iph; 13739afd85c9SLinus Lüssing unsigned int len; 13749afd85c9SLinus Lüssing unsigned int offset = skb_network_offset(skb) + sizeof(*iph); 13759afd85c9SLinus Lüssing 13769afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset)) 13779afd85c9SLinus Lüssing return -EINVAL; 13789afd85c9SLinus Lüssing 13799afd85c9SLinus Lüssing iph = ip_hdr(skb); 13809afd85c9SLinus Lüssing 13819afd85c9SLinus Lüssing if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph)) 13829afd85c9SLinus Lüssing return -EINVAL; 13839afd85c9SLinus Lüssing 13849afd85c9SLinus Lüssing offset += ip_hdrlen(skb) - sizeof(*iph); 13859afd85c9SLinus Lüssing 13869afd85c9SLinus Lüssing if (!pskb_may_pull(skb, offset)) 13879afd85c9SLinus Lüssing return -EINVAL; 13889afd85c9SLinus Lüssing 13899afd85c9SLinus Lüssing iph = ip_hdr(skb); 13909afd85c9SLinus Lüssing 13919afd85c9SLinus Lüssing if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 13929afd85c9SLinus Lüssing return -EINVAL; 13939afd85c9SLinus Lüssing 13949afd85c9SLinus Lüssing len = skb_network_offset(skb) + ntohs(iph->tot_len); 13959afd85c9SLinus Lüssing if (skb->len < len || len < offset) 13969afd85c9SLinus Lüssing return -EINVAL; 13979afd85c9SLinus Lüssing 13989afd85c9SLinus Lüssing skb_set_transport_header(skb, offset); 13999afd85c9SLinus Lüssing 14009afd85c9SLinus Lüssing return 0; 14019afd85c9SLinus Lüssing } 14029afd85c9SLinus Lüssing 14039afd85c9SLinus Lüssing static int ip_mc_check_igmp_reportv3(struct sk_buff *skb) 14049afd85c9SLinus Lüssing { 14059afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb); 14069afd85c9SLinus Lüssing 14079afd85c9SLinus Lüssing len += sizeof(struct igmpv3_report); 14089afd85c9SLinus Lüssing 14099afd85c9SLinus Lüssing return pskb_may_pull(skb, len) ? 0 : -EINVAL; 14109afd85c9SLinus Lüssing } 14119afd85c9SLinus Lüssing 14129afd85c9SLinus Lüssing static int ip_mc_check_igmp_query(struct sk_buff *skb) 14139afd85c9SLinus Lüssing { 14149afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb); 14159afd85c9SLinus Lüssing 14169afd85c9SLinus Lüssing len += sizeof(struct igmphdr); 14179afd85c9SLinus Lüssing if (skb->len < len) 14189afd85c9SLinus Lüssing return -EINVAL; 14199afd85c9SLinus Lüssing 14209afd85c9SLinus Lüssing /* IGMPv{1,2}? */ 14219afd85c9SLinus Lüssing if (skb->len != len) { 14229afd85c9SLinus Lüssing /* or IGMPv3? */ 14239afd85c9SLinus Lüssing len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr); 14249afd85c9SLinus Lüssing if (skb->len < len || !pskb_may_pull(skb, len)) 14259afd85c9SLinus Lüssing return -EINVAL; 14269afd85c9SLinus Lüssing } 14279afd85c9SLinus Lüssing 14289afd85c9SLinus Lüssing /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer 14299afd85c9SLinus Lüssing * all-systems destination addresses (224.0.0.1) for general queries 14309afd85c9SLinus Lüssing */ 14319afd85c9SLinus Lüssing if (!igmp_hdr(skb)->group && 14329afd85c9SLinus Lüssing ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP)) 14339afd85c9SLinus Lüssing return -EINVAL; 14349afd85c9SLinus Lüssing 14359afd85c9SLinus Lüssing return 0; 14369afd85c9SLinus Lüssing } 14379afd85c9SLinus Lüssing 14389afd85c9SLinus Lüssing static int ip_mc_check_igmp_msg(struct sk_buff *skb) 14399afd85c9SLinus Lüssing { 14409afd85c9SLinus Lüssing switch (igmp_hdr(skb)->type) { 14419afd85c9SLinus Lüssing case IGMP_HOST_LEAVE_MESSAGE: 14429afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_REPORT: 14439afd85c9SLinus Lüssing case IGMPV2_HOST_MEMBERSHIP_REPORT: 14449afd85c9SLinus Lüssing /* fall through */ 14459afd85c9SLinus Lüssing return 0; 14469afd85c9SLinus Lüssing case IGMPV3_HOST_MEMBERSHIP_REPORT: 14479afd85c9SLinus Lüssing return ip_mc_check_igmp_reportv3(skb); 14489afd85c9SLinus Lüssing case IGMP_HOST_MEMBERSHIP_QUERY: 14499afd85c9SLinus Lüssing return ip_mc_check_igmp_query(skb); 14509afd85c9SLinus Lüssing default: 14519afd85c9SLinus Lüssing return -ENOMSG; 14529afd85c9SLinus Lüssing } 14539afd85c9SLinus Lüssing } 14549afd85c9SLinus Lüssing 14559afd85c9SLinus Lüssing static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb) 14569afd85c9SLinus Lüssing { 14579afd85c9SLinus Lüssing return skb_checksum_simple_validate(skb); 14589afd85c9SLinus Lüssing } 14599afd85c9SLinus Lüssing 14609afd85c9SLinus Lüssing static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed) 14619afd85c9SLinus Lüssing 14629afd85c9SLinus Lüssing { 14639afd85c9SLinus Lüssing struct sk_buff *skb_chk; 14649afd85c9SLinus Lüssing unsigned int transport_len; 14659afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr); 1466a516993fSLinus Lüssing int ret = -EINVAL; 14679afd85c9SLinus Lüssing 14689afd85c9SLinus Lüssing transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 14699afd85c9SLinus Lüssing 14709afd85c9SLinus Lüssing skb_chk = skb_checksum_trimmed(skb, transport_len, 14719afd85c9SLinus Lüssing ip_mc_validate_checksum); 14729afd85c9SLinus Lüssing if (!skb_chk) 1473a516993fSLinus Lüssing goto err; 14749afd85c9SLinus Lüssing 1475a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, len)) 1476a516993fSLinus Lüssing goto err; 14779afd85c9SLinus Lüssing 14789afd85c9SLinus Lüssing ret = ip_mc_check_igmp_msg(skb_chk); 1479a516993fSLinus Lüssing if (ret) 1480a516993fSLinus Lüssing goto err; 14819afd85c9SLinus Lüssing 14829afd85c9SLinus Lüssing if (skb_trimmed) 14839afd85c9SLinus Lüssing *skb_trimmed = skb_chk; 1484a516993fSLinus Lüssing /* free now unneeded clone */ 1485a516993fSLinus Lüssing else if (skb_chk != skb) 14869afd85c9SLinus Lüssing kfree_skb(skb_chk); 14879afd85c9SLinus Lüssing 1488a516993fSLinus Lüssing ret = 0; 1489a516993fSLinus Lüssing 1490a516993fSLinus Lüssing err: 1491a516993fSLinus Lüssing if (ret && skb_chk && skb_chk != skb) 1492a516993fSLinus Lüssing kfree_skb(skb_chk); 1493a516993fSLinus Lüssing 1494a516993fSLinus Lüssing return ret; 14959afd85c9SLinus Lüssing } 14969afd85c9SLinus Lüssing 14979afd85c9SLinus Lüssing /** 14989afd85c9SLinus Lüssing * ip_mc_check_igmp - checks whether this is a sane IGMP packet 14999afd85c9SLinus Lüssing * @skb: the skb to validate 15009afd85c9SLinus Lüssing * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional) 15019afd85c9SLinus Lüssing * 15029afd85c9SLinus Lüssing * Checks whether an IPv4 packet is a valid IGMP packet. If so sets 1503a516993fSLinus Lüssing * skb transport header accordingly and returns zero. 15049afd85c9SLinus Lüssing * 15059afd85c9SLinus Lüssing * -EINVAL: A broken packet was detected, i.e. it violates some internet 15069afd85c9SLinus Lüssing * standard 15079afd85c9SLinus Lüssing * -ENOMSG: IP header validation succeeded but it is not an IGMP packet. 15089afd85c9SLinus Lüssing * -ENOMEM: A memory allocation failure happened. 15099afd85c9SLinus Lüssing * 15109afd85c9SLinus Lüssing * Optionally, an skb pointer might be provided via skb_trimmed (or set it 15119afd85c9SLinus Lüssing * to NULL): After parsing an IGMP packet successfully it will point to 15129afd85c9SLinus Lüssing * an skb which has its tail aligned to the IP packet end. This might 15139afd85c9SLinus Lüssing * either be the originally provided skb or a trimmed, cloned version if 15149afd85c9SLinus Lüssing * the skb frame had data beyond the IP packet. A cloned skb allows us 15159afd85c9SLinus Lüssing * to leave the original skb and its full frame unchanged (which might be 15169afd85c9SLinus Lüssing * desirable for layer 2 frame jugglers). 15179afd85c9SLinus Lüssing * 1518a516993fSLinus Lüssing * Caller needs to set the skb network header and free any returned skb if it 1519a516993fSLinus Lüssing * differs from the provided skb. 15209afd85c9SLinus Lüssing */ 15219afd85c9SLinus Lüssing int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed) 15229afd85c9SLinus Lüssing { 15239afd85c9SLinus Lüssing int ret = ip_mc_check_iphdr(skb); 15249afd85c9SLinus Lüssing 15259afd85c9SLinus Lüssing if (ret < 0) 15269afd85c9SLinus Lüssing return ret; 15279afd85c9SLinus Lüssing 15289afd85c9SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP) 15299afd85c9SLinus Lüssing return -ENOMSG; 15309afd85c9SLinus Lüssing 15319afd85c9SLinus Lüssing return __ip_mc_check_igmp(skb, skb_trimmed); 15329afd85c9SLinus Lüssing } 15339afd85c9SLinus Lüssing EXPORT_SYMBOL(ip_mc_check_igmp); 15349afd85c9SLinus Lüssing 15351da177e4SLinus Torvalds /* 15364aa5dee4SJiri Pirko * Resend IGMP JOIN report; used by netdev notifier. 1537a816c7c7SJay Vosburgh */ 15384aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev) 1539a816c7c7SJay Vosburgh { 154008882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1541866f3b25SEric Dumazet struct ip_mc_list *im; 1542866f3b25SEric Dumazet int type; 154387a8a2aeSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1544a816c7c7SJay Vosburgh 15454aa5dee4SJiri Pirko ASSERT_RTNL(); 15464aa5dee4SJiri Pirko 15474aa5dee4SJiri Pirko for_each_pmc_rtnl(in_dev, im) { 1548a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1549866f3b25SEric Dumazet continue; 1550df2cf4a7SPhilip Downey if (ipv4_is_local_multicast(im->multiaddr) && 155187a8a2aeSNikolay Borisov !net->ipv4.sysctl_igmp_llm_reports) 1552df2cf4a7SPhilip Downey continue; 1553a816c7c7SJay Vosburgh 1554e12b4539SFlavio Leitner /* a failover is happening and switches 1555866f3b25SEric Dumazet * must be notified immediately 1556866f3b25SEric Dumazet */ 1557e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev)) 1558866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT; 1559e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev)) 1560866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT; 1561e12b4539SFlavio Leitner else 1562866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT; 1563866f3b25SEric Dumazet igmp_send_report(in_dev, im, type); 1564866f3b25SEric Dumazet } 1565a816c7c7SJay Vosburgh #endif 1566a816c7c7SJay Vosburgh } 1567a816c7c7SJay Vosburgh 1568a816c7c7SJay Vosburgh /* 15691da177e4SLinus Torvalds * A socket has left a multicast group on device dev 15701da177e4SLinus Torvalds */ 15711da177e4SLinus Torvalds 15728f935bbdSAl Viro void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) 15731da177e4SLinus Torvalds { 15741d7138deSEric Dumazet struct ip_mc_list *i; 15751d7138deSEric Dumazet struct ip_mc_list __rcu **ip; 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds ASSERT_RTNL(); 15781da177e4SLinus Torvalds 15791d7138deSEric Dumazet for (ip = &in_dev->mc_list; 15801d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL; 15811d7138deSEric Dumazet ip = &i->next_rcu) { 15821da177e4SLinus Torvalds if (i->multiaddr == addr) { 15831da177e4SLinus Torvalds if (--i->users == 0) { 1584e9897071SEric Dumazet ip_mc_hash_remove(in_dev, i); 15851d7138deSEric Dumazet *ip = i->next_rcu; 1586b8bae41eSRami Rosen in_dev->mc_count--; 15871da177e4SLinus Torvalds igmp_group_dropped(i); 158824cf3af3SVeaceslav Falico ip_mc_clear_src(i); 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds if (!in_dev->dead) 15911da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds ip_ma_put(i); 15941da177e4SLinus Torvalds return; 15951da177e4SLinus Torvalds } 15961da177e4SLinus Torvalds break; 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds } 15991da177e4SLinus Torvalds } 16004bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_dec_group); 16011da177e4SLinus Torvalds 160275c78500SMoni Shoua /* Device changing type */ 160375c78500SMoni Shoua 160475c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 160575c78500SMoni Shoua { 16061d7138deSEric Dumazet struct ip_mc_list *pmc; 160775c78500SMoni Shoua 160875c78500SMoni Shoua ASSERT_RTNL(); 160975c78500SMoni Shoua 16101d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 16111d7138deSEric Dumazet igmp_group_dropped(pmc); 161275c78500SMoni Shoua } 161375c78500SMoni Shoua 161475c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 161575c78500SMoni Shoua { 16161d7138deSEric Dumazet struct ip_mc_list *pmc; 161775c78500SMoni Shoua 161875c78500SMoni Shoua ASSERT_RTNL(); 161975c78500SMoni Shoua 16201d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 16211d7138deSEric Dumazet igmp_group_added(pmc); 162275c78500SMoni Shoua } 162375c78500SMoni Shoua 16241da177e4SLinus Torvalds /* Device going down */ 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 16271da177e4SLinus Torvalds { 16281d7138deSEric Dumazet struct ip_mc_list *pmc; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds ASSERT_RTNL(); 16311da177e4SLinus Torvalds 16321d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 16331d7138deSEric Dumazet igmp_group_dropped(pmc); 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16361da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 16371da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 16381da177e4SLinus Torvalds __in_dev_put(in_dev); 16391da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 16401da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 16411da177e4SLinus Torvalds __in_dev_put(in_dev); 16421da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 16431da177e4SLinus Torvalds #endif 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 16491da177e4SLinus Torvalds { 1650*dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 1651165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1652*dcd87999SNikolay Borisov #endif 16531da177e4SLinus Torvalds ASSERT_RTNL(); 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1656b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 1657b24b8a24SPavel Emelyanov (unsigned long)in_dev); 1658b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 1659b24b8a24SPavel Emelyanov (unsigned long)in_dev); 1660165094afSNikolay Borisov in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv; 16611da177e4SLinus Torvalds #endif 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds /* Device going up */ 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 16691da177e4SLinus Torvalds { 16701d7138deSEric Dumazet struct ip_mc_list *pmc; 1671*dcd87999SNikolay Borisov #ifdef CONFIG_IP_MULTICAST 1672165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 1673*dcd87999SNikolay Borisov #endif 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds ASSERT_RTNL(); 16761da177e4SLinus Torvalds 1677a9fe8e29SHannes Frederic Sowa #ifdef CONFIG_IP_MULTICAST 1678165094afSNikolay Borisov in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv; 1679a9fe8e29SHannes Frederic Sowa #endif 16801da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 16811da177e4SLinus Torvalds 16821d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 16831d7138deSEric Dumazet igmp_group_added(pmc); 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds /* 16871da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 16881da177e4SLinus Torvalds */ 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 16911da177e4SLinus Torvalds { 16921da177e4SLinus Torvalds struct ip_mc_list *i; 16931da177e4SLinus Torvalds 16941da177e4SLinus Torvalds ASSERT_RTNL(); 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds /* Deactivate timers */ 16971da177e4SLinus Torvalds ip_mc_down(in_dev); 16981da177e4SLinus Torvalds 16991d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { 17001d7138deSEric Dumazet in_dev->mc_list = i->next_rcu; 1701b8bae41eSRami Rosen in_dev->mc_count--; 17021d7138deSEric Dumazet 170324cf3af3SVeaceslav Falico /* We've dropped the groups in ip_mc_down already */ 170424cf3af3SVeaceslav Falico ip_mc_clear_src(i); 17051da177e4SLinus Torvalds ip_ma_put(i); 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds 17099e917dcaSEric Dumazet /* RTNL is locked */ 1710877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 17111da177e4SLinus Torvalds { 17121da177e4SLinus Torvalds struct net_device *dev = NULL; 17131da177e4SLinus Torvalds struct in_device *idev = NULL; 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds if (imr->imr_ifindex) { 1716877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 17171da177e4SLinus Torvalds return idev; 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 17209e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false); 17211da177e4SLinus Torvalds if (!dev) 17221da177e4SLinus Torvalds return NULL; 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 1725b23dd4feSDavid S. Miller if (!dev) { 172678fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, 172778fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr, 172878fbfd8aSDavid S. Miller 0, 0, 0); 1729b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1730d8d1f30bSChangli Gao dev = rt->dst.dev; 17311da177e4SLinus Torvalds ip_rt_put(rt); 17321da177e4SLinus Torvalds } 1733b23dd4feSDavid S. Miller } 17341da177e4SLinus Torvalds if (dev) { 17351da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1736e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds return idev; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds /* 17421da177e4SLinus Torvalds * Join a socket to a group 17431da177e4SLinus Torvalds */ 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 17468f935bbdSAl Viro __be32 *psfsrc) 17471da177e4SLinus Torvalds { 17481da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 17491da177e4SLinus Torvalds int rv = 0; 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds psf_prev = NULL; 17521da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 17531da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 17541da177e4SLinus Torvalds break; 17551da177e4SLinus Torvalds psf_prev = psf; 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 17581da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 17591da177e4SLinus Torvalds return -ESRCH; 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds psf->sf_count[sfmode]--; 17621da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 17631da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 17661da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17671da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 1768165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 17691da177e4SLinus Torvalds #endif 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /* no more filters for this source */ 17721da177e4SLinus Torvalds if (psf_prev) 17731da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 17741da177e4SLinus Torvalds else 17751da177e4SLinus Torvalds pmc->sources = psf->sf_next; 17761da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17771da177e4SLinus Torvalds if (psf->sf_oldin && 17781da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 1779165094afSNikolay Borisov psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 17801da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 17811da177e4SLinus Torvalds pmc->tomb = psf; 17821da177e4SLinus Torvalds rv = 1; 17831da177e4SLinus Torvalds } else 17841da177e4SLinus Torvalds #endif 17851da177e4SLinus Torvalds kfree(psf); 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds return rv; 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 17911da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 17921da177e4SLinus Torvalds #endif 17931da177e4SLinus Torvalds 17948f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 17958f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 17961da177e4SLinus Torvalds { 17971da177e4SLinus Torvalds struct ip_mc_list *pmc; 17981da177e4SLinus Torvalds int changerec = 0; 17991da177e4SLinus Torvalds int i, err; 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds if (!in_dev) 18021da177e4SLinus Torvalds return -ENODEV; 18031d7138deSEric Dumazet rcu_read_lock(); 18041d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 18051da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 18061da177e4SLinus Torvalds break; 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds if (!pmc) { 18091da177e4SLinus Torvalds /* MCA not found?? bug */ 18101d7138deSEric Dumazet rcu_read_unlock(); 18111da177e4SLinus Torvalds return -ESRCH; 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 18141d7138deSEric Dumazet rcu_read_unlock(); 18151da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18161da177e4SLinus Torvalds sf_markstate(pmc); 18171da177e4SLinus Torvalds #endif 18181da177e4SLinus Torvalds if (!delta) { 18191da177e4SLinus Torvalds err = -EINVAL; 18201da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 18211da177e4SLinus Torvalds goto out_unlock; 18221da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds err = 0; 18251da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 18261da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds changerec |= rv > 0; 18291da177e4SLinus Torvalds if (!err && rv < 0) 18301da177e4SLinus Torvalds err = rv; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 18331da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 18341da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 18351da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18361da177e4SLinus Torvalds struct ip_sf_list *psf; 1837165094afSNikolay Borisov struct net *net = dev_net(in_dev->dev); 18381da177e4SLinus Torvalds #endif 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds /* filter mode change */ 18411da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 18421da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1843165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 18441da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 18451da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 18461da177e4SLinus Torvalds psf->sf_crcount = 0; 18471da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 18481da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 18491da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 18501da177e4SLinus Torvalds #endif 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds out_unlock: 18531da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 18541da177e4SLinus Torvalds return err; 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds /* 18581da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 18591da177e4SLinus Torvalds */ 18601da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 18615eb81e89SJun Zhao __be32 *psfsrc) 18621da177e4SLinus Torvalds { 18631da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds psf_prev = NULL; 18661da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 18671da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 18681da177e4SLinus Torvalds break; 18691da177e4SLinus Torvalds psf_prev = psf; 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds if (!psf) { 18720da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 18731da177e4SLinus Torvalds if (!psf) 18741da177e4SLinus Torvalds return -ENOBUFS; 18751da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 18761da177e4SLinus Torvalds if (psf_prev) { 18771da177e4SLinus Torvalds psf_prev->sf_next = psf; 18781da177e4SLinus Torvalds } else 18791da177e4SLinus Torvalds pmc->sources = psf; 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds psf->sf_count[sfmode]++; 18821da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 18831da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds return 0; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18891da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 18901da177e4SLinus Torvalds { 18911da177e4SLinus Torvalds struct ip_sf_list *psf; 18921da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 18931da177e4SLinus Torvalds 18941da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 18951da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 18961da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 18971da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 18981da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 18991da177e4SLinus Torvalds } else 19001da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 19011da177e4SLinus Torvalds } 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 19041da177e4SLinus Torvalds { 1905ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 19061da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 19071da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 19081da177e4SLinus Torvalds int new_in, rv; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds rv = 0; 19111da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) { 19121da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 19131da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 19141da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 19151da177e4SLinus Torvalds } else 19161da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 1917ad12583fSDavid L Stevens if (new_in) { 1918ad12583fSDavid L Stevens if (!psf->sf_oldin) { 191976edc605SAl Viro struct ip_sf_list *prev = NULL; 1920ad12583fSDavid L Stevens 1921ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) { 1922ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1923ad12583fSDavid L Stevens break; 1924ad12583fSDavid L Stevens prev = dpsf; 1925ad12583fSDavid L Stevens } 1926ad12583fSDavid L Stevens if (dpsf) { 1927ad12583fSDavid L Stevens if (prev) 1928ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 1929ad12583fSDavid L Stevens else 1930ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 1931ad12583fSDavid L Stevens kfree(dpsf); 1932ad12583fSDavid L Stevens } 19331da177e4SLinus Torvalds psf->sf_crcount = qrv; 19341da177e4SLinus Torvalds rv++; 19351da177e4SLinus Torvalds } 1936ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 1937ad12583fSDavid L Stevens 1938ad12583fSDavid L Stevens psf->sf_crcount = 0; 1939ad12583fSDavid L Stevens /* 1940ad12583fSDavid L Stevens * add or update "delete" records if an active filter 1941ad12583fSDavid L Stevens * is now inactive 1942ad12583fSDavid L Stevens */ 1943ad12583fSDavid L Stevens for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) 1944ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1945ad12583fSDavid L Stevens break; 1946ad12583fSDavid L Stevens if (!dpsf) { 19473ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); 1948ad12583fSDavid L Stevens if (!dpsf) 1949ad12583fSDavid L Stevens continue; 1950ad12583fSDavid L Stevens *dpsf = *psf; 1951ad12583fSDavid L Stevens /* pmc->lock held by callers */ 1952ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 1953ad12583fSDavid L Stevens pmc->tomb = dpsf; 1954ad12583fSDavid L Stevens } 1955ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 1956ad12583fSDavid L Stevens rv++; 1957ad12583fSDavid L Stevens } 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds return rv; 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds #endif 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds /* 19641da177e4SLinus Torvalds * Add multicast source filter list to the interface list 19651da177e4SLinus Torvalds */ 19668f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 19678f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 19681da177e4SLinus Torvalds { 19691da177e4SLinus Torvalds struct ip_mc_list *pmc; 19701da177e4SLinus Torvalds int isexclude; 19711da177e4SLinus Torvalds int i, err; 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds if (!in_dev) 19741da177e4SLinus Torvalds return -ENODEV; 19751d7138deSEric Dumazet rcu_read_lock(); 19761d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 19771da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 19781da177e4SLinus Torvalds break; 19791da177e4SLinus Torvalds } 19801da177e4SLinus Torvalds if (!pmc) { 19811da177e4SLinus Torvalds /* MCA not found?? bug */ 19821d7138deSEric Dumazet rcu_read_unlock(); 19831da177e4SLinus Torvalds return -ESRCH; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 19861d7138deSEric Dumazet rcu_read_unlock(); 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 19891da177e4SLinus Torvalds sf_markstate(pmc); 19901da177e4SLinus Torvalds #endif 19911da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 19921da177e4SLinus Torvalds if (!delta) 19931da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 19941da177e4SLinus Torvalds err = 0; 19951da177e4SLinus Torvalds for (i = 0; i < sfcount; i++) { 19965eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); 19971da177e4SLinus Torvalds if (err) 19981da177e4SLinus Torvalds break; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds if (err) { 20011da177e4SLinus Torvalds int j; 20021da177e4SLinus Torvalds 2003685f94e6SJun Zhao if (!delta) 20041da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 20051da177e4SLinus Torvalds for (j = 0; j < i; j++) 2006a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); 20071da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 20081da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 20091da177e4SLinus Torvalds struct ip_sf_list *psf; 2010165094afSNikolay Borisov struct net *net = dev_net(pmc->interface->dev); 2011cfcabdccSStephen Hemminger in_dev = pmc->interface; 20121da177e4SLinus Torvalds #endif 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds /* filter mode change */ 20151da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 20161da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 20171da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 20181da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 20191da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 20201da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 20211da177e4SLinus Torvalds 2022165094afSNikolay Borisov pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv; 20231da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 20241da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = psf->sf_next) 20251da177e4SLinus Torvalds psf->sf_crcount = 0; 20261da177e4SLinus Torvalds igmp_ifc_event(in_dev); 20271da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 20281da177e4SLinus Torvalds igmp_ifc_event(in_dev); 20291da177e4SLinus Torvalds #endif 20301da177e4SLinus Torvalds } 20311da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 20321da177e4SLinus Torvalds return err; 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds 20351da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 20361da177e4SLinus Torvalds { 20371da177e4SLinus Torvalds struct ip_sf_list *psf, *nextpsf; 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds for (psf = pmc->tomb; psf; psf = nextpsf) { 20401da177e4SLinus Torvalds nextpsf = psf->sf_next; 20411da177e4SLinus Torvalds kfree(psf); 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds pmc->tomb = NULL; 20441da177e4SLinus Torvalds for (psf = pmc->sources; psf; psf = nextpsf) { 20451da177e4SLinus Torvalds nextpsf = psf->sf_next; 20461da177e4SLinus Torvalds kfree(psf); 20471da177e4SLinus Torvalds } 20481da177e4SLinus Torvalds pmc->sources = NULL; 20491da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 2050de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 20511da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 20521da177e4SLinus Torvalds } 20531da177e4SLinus Torvalds 205454ff9ef3SMarcelo Ricardo Leitner /* Join a multicast group 205554ff9ef3SMarcelo Ricardo Leitner */ 205654ff9ef3SMarcelo Ricardo Leitner 205754ff9ef3SMarcelo Ricardo Leitner int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr) 20581da177e4SLinus Torvalds { 20598f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 2060959d10f6SEric Dumazet struct ip_mc_socklist *iml, *i; 20611da177e4SLinus Torvalds struct in_device *in_dev; 20621da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2063877acedcSDaniel Lezcano struct net *net = sock_net(sk); 2064ca9b907dSDavid L Stevens int ifindex; 20651da177e4SLinus Torvalds int count = 0; 2066959d10f6SEric Dumazet int err; 2067959d10f6SEric Dumazet 2068959d10f6SEric Dumazet ASSERT_RTNL(); 20691da177e4SLinus Torvalds 2070f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 20711da177e4SLinus Torvalds return -EINVAL; 20721da177e4SLinus Torvalds 2073877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds if (!in_dev) { 20761da177e4SLinus Torvalds err = -ENODEV; 20771da177e4SLinus Torvalds goto done; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds err = -EADDRINUSE; 2081ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 20821d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 2083ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 2084ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 20851da177e4SLinus Torvalds goto done; 20861da177e4SLinus Torvalds count++; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds err = -ENOBUFS; 2089815c5270SNikolay Borisov if (count >= net->ipv4.sysctl_igmp_max_memberships) 20901da177e4SLinus Torvalds goto done; 20918b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 209251456b29SIan Morris if (!iml) 2093ca9b907dSDavid L Stevens goto done; 2094ca9b907dSDavid L Stevens 20951da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 20961d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 20971da177e4SLinus Torvalds iml->sflist = NULL; 20981da177e4SLinus Torvalds iml->sfmode = MCAST_EXCLUDE; 2099cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 21001da177e4SLinus Torvalds ip_mc_inc_group(in_dev, addr); 21011da177e4SLinus Torvalds err = 0; 21021da177e4SLinus Torvalds done: 21031da177e4SLinus Torvalds return err; 21041da177e4SLinus Torvalds } 21054bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 21081da177e4SLinus Torvalds struct in_device *in_dev) 21091da177e4SLinus Torvalds { 21101d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 21111da177e4SLinus Torvalds int err; 21121da177e4SLinus Torvalds 211351456b29SIan Morris if (!psf) { 21141da177e4SLinus Torvalds /* any-source empty exclude case */ 21151da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 21161da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 2119c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 2120a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 2121c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2122c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); 21237519cce4SLai Jiangshan kfree_rcu(psf, rcu); 21241da177e4SLinus Torvalds return err; 21251da177e4SLinus Torvalds } 21261da177e4SLinus Torvalds 212754ff9ef3SMarcelo Ricardo Leitner int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 21281da177e4SLinus Torvalds { 21291da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21301d7138deSEric Dumazet struct ip_mc_socklist *iml; 21311d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 213284b42baeSDavid L Stevens struct in_device *in_dev; 2133877acedcSDaniel Lezcano struct net *net = sock_net(sk); 21348f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 213584b42baeSDavid L Stevens u32 ifindex; 2136acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 21371da177e4SLinus Torvalds 2138959d10f6SEric Dumazet ASSERT_RTNL(); 2139959d10f6SEric Dumazet 2140877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 21414eba7bb1SAndrew Lunn if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) { 214252ad353aSdingtianhong ret = -ENODEV; 214352ad353aSdingtianhong goto out; 214452ad353aSdingtianhong } 214584b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 21461d7138deSEric Dumazet for (imlp = &inet->mc_list; 21471d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 21481d7138deSEric Dumazet imlp = &iml->next_rcu) { 2149acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 2150acd6e00bSDavid L Stevens continue; 2151acd6e00bSDavid L Stevens if (ifindex) { 2152acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 2153acd6e00bSDavid L Stevens continue; 2154acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 2155acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 2156acd6e00bSDavid L Stevens continue; 2157acd6e00bSDavid L Stevens 21581da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 21591da177e4SLinus Torvalds 21601d7138deSEric Dumazet *imlp = iml->next_rcu; 21611da177e4SLinus Torvalds 21624eba7bb1SAndrew Lunn if (in_dev) 216384b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 2164959d10f6SEric Dumazet 2165c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2166c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 216710d50e74SLai Jiangshan kfree_rcu(iml, rcu); 21681da177e4SLinus Torvalds return 0; 21691da177e4SLinus Torvalds } 217052ad353aSdingtianhong out: 2171959d10f6SEric Dumazet return ret; 2172959d10f6SEric Dumazet } 2173193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 21741da177e4SLinus Torvalds 21751da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 21761da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 21771da177e4SLinus Torvalds { 21781da177e4SLinus Torvalds int err; 21791da177e4SLinus Torvalds struct ip_mreqn imr; 218063007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 21811da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 21821da177e4SLinus Torvalds struct in_device *in_dev = NULL; 21831da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21841da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2185877acedcSDaniel Lezcano struct net *net = sock_net(sk); 21868cdaaa15SDavid L Stevens int leavegroup = 0; 21871da177e4SLinus Torvalds int i, j, rv; 21881da177e4SLinus Torvalds 2189f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21901da177e4SLinus Torvalds return -EINVAL; 21911da177e4SLinus Torvalds 219254ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 21951da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 21961da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2197877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds if (!in_dev) { 22001da177e4SLinus Torvalds err = -ENODEV; 22011da177e4SLinus Torvalds goto done; 22021da177e4SLinus Torvalds } 22031da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 22041da177e4SLinus Torvalds 22051d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 2206f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 2207f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 2208f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 22091da177e4SLinus Torvalds break; 22101da177e4SLinus Torvalds } 2211917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2212917f2f10SDavid L Stevens err = -EINVAL; 22131da177e4SLinus Torvalds goto done; 2214917f2f10SDavid L Stevens } 22151da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 22161da177e4SLinus Torvalds if (pmc->sflist) { 2217917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 2218917f2f10SDavid L Stevens err = -EINVAL; 22191da177e4SLinus Torvalds goto done; 2220917f2f10SDavid L Stevens } 22211da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 22221da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 22231da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 22241da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 22251da177e4SLinus Torvalds NULL, 0); 22261da177e4SLinus Torvalds pmc->sfmode = omode; 22271da177e4SLinus Torvalds } 22281da177e4SLinus Torvalds 22291d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 22301da177e4SLinus Torvalds if (!add) { 22311da177e4SLinus Torvalds if (!psl) 2232917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 22331da177e4SLinus Torvalds rv = !0; 22341da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 22351da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 223663007727SAl Viro sizeof(__be32)); 22371da177e4SLinus Torvalds if (rv == 0) 22381da177e4SLinus Torvalds break; 22391da177e4SLinus Torvalds } 22401da177e4SLinus Torvalds if (rv) /* source not found */ 2241917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 22421da177e4SLinus Torvalds 22438cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 22448cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 22458cdaaa15SDavid L Stevens leavegroup = 1; 22468cdaaa15SDavid L Stevens goto done; 22478cdaaa15SDavid L Stevens } 22488cdaaa15SDavid L Stevens 22491da177e4SLinus Torvalds /* update the interface filter */ 22501da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 22511da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds for (j = i+1; j < psl->sl_count; j++) 22541da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 22551da177e4SLinus Torvalds psl->sl_count--; 22561da177e4SLinus Torvalds err = 0; 22571da177e4SLinus Torvalds goto done; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds /* else, add a new source to the filter */ 22601da177e4SLinus Torvalds 2261166b6b2dSNikolay Borisov if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) { 22621da177e4SLinus Torvalds err = -ENOBUFS; 22631da177e4SLinus Torvalds goto done; 22641da177e4SLinus Torvalds } 22651da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 22661da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 22671da177e4SLinus Torvalds int count = IP_SFBLOCK; 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds if (psl) 22701da177e4SLinus Torvalds count += psl->sl_max; 22718b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 22721da177e4SLinus Torvalds if (!newpsl) { 22731da177e4SLinus Torvalds err = -ENOBUFS; 22741da177e4SLinus Torvalds goto done; 22751da177e4SLinus Torvalds } 22761da177e4SLinus Torvalds newpsl->sl_max = count; 22771da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 22781da177e4SLinus Torvalds if (psl) { 22791da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) 22801da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2281c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2282c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 22837519cce4SLai Jiangshan kfree_rcu(psl, rcu); 22841da177e4SLinus Torvalds } 2285cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2286c85bb41eSFlavio Leitner psl = newpsl; 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 22891da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 22901da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 229163007727SAl Viro sizeof(__be32)); 22921da177e4SLinus Torvalds if (rv == 0) 22931da177e4SLinus Torvalds break; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 22961da177e4SLinus Torvalds goto done; 22971da177e4SLinus Torvalds for (j = psl->sl_count-1; j >= i; j--) 22981da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 22991da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 23001da177e4SLinus Torvalds psl->sl_count++; 23011da177e4SLinus Torvalds err = 0; 23021da177e4SLinus Torvalds /* update the interface list */ 23031da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 23041da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 23051da177e4SLinus Torvalds done: 23068cdaaa15SDavid L Stevens if (leavegroup) 230754ff9ef3SMarcelo Ricardo Leitner err = ip_mc_leave_group(sk, &imr); 23081da177e4SLinus Torvalds return err; 23091da177e4SLinus Torvalds } 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 23121da177e4SLinus Torvalds { 23139951f036SDavid L Stevens int err = 0; 23141da177e4SLinus Torvalds struct ip_mreqn imr; 231563007727SAl Viro __be32 addr = msf->imsf_multiaddr; 23161da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 23171da177e4SLinus Torvalds struct in_device *in_dev; 23181da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23191da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2320877acedcSDaniel Lezcano struct net *net = sock_net(sk); 23219951f036SDavid L Stevens int leavegroup = 0; 23221da177e4SLinus Torvalds 2323f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 23241da177e4SLinus Torvalds return -EINVAL; 23251da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 23261da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 23271da177e4SLinus Torvalds return -EINVAL; 23281da177e4SLinus Torvalds 232954ff9ef3SMarcelo Ricardo Leitner ASSERT_RTNL(); 23301da177e4SLinus Torvalds 23311da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 23321da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 23331da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2334877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds if (!in_dev) { 23371da177e4SLinus Torvalds err = -ENODEV; 23381da177e4SLinus Torvalds goto done; 23391da177e4SLinus Torvalds } 23401da177e4SLinus Torvalds 23419951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 23429951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 23439951f036SDavid L Stevens leavegroup = 1; 23449951f036SDavid L Stevens goto done; 23459951f036SDavid L Stevens } 23469951f036SDavid L Stevens 23471d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 23481da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 23491da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 23501da177e4SLinus Torvalds break; 23511da177e4SLinus Torvalds } 2352917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2353917f2f10SDavid L Stevens err = -EINVAL; 23541da177e4SLinus Torvalds goto done; 2355917f2f10SDavid L Stevens } 23561da177e4SLinus Torvalds if (msf->imsf_numsrc) { 23578b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 23588b3a7005SKris Katterjohn GFP_KERNEL); 23591da177e4SLinus Torvalds if (!newpsl) { 23601da177e4SLinus Torvalds err = -ENOBUFS; 23611da177e4SLinus Torvalds goto done; 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 23641da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 23651da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 23661da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 23671da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 23681da177e4SLinus Torvalds if (err) { 23691da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 23701da177e4SLinus Torvalds goto done; 23711da177e4SLinus Torvalds } 23728713dbf0SYan Zheng } else { 23731da177e4SLinus Torvalds newpsl = NULL; 23748713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 23758713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 23768713dbf0SYan Zheng } 23771d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 23781da177e4SLinus Torvalds if (psl) { 23791da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 23801da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2381c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2382c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 23837519cce4SLai Jiangshan kfree_rcu(psl, rcu); 23841da177e4SLinus Torvalds } else 23851da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 23861da177e4SLinus Torvalds 0, NULL, 0); 2387cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 23881da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2389917f2f10SDavid L Stevens err = 0; 23901da177e4SLinus Torvalds done: 23919951f036SDavid L Stevens if (leavegroup) 23929951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 23931da177e4SLinus Torvalds return err; 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 23971da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 23981da177e4SLinus Torvalds { 23991da177e4SLinus Torvalds int err, len, count, copycount; 24001da177e4SLinus Torvalds struct ip_mreqn imr; 240163007727SAl Viro __be32 addr = msf->imsf_multiaddr; 24021da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 24031da177e4SLinus Torvalds struct in_device *in_dev; 24041da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 24051da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2406877acedcSDaniel Lezcano struct net *net = sock_net(sk); 24071da177e4SLinus Torvalds 240887e9f031SWANG Cong ASSERT_RTNL(); 240987e9f031SWANG Cong 2410f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 24111da177e4SLinus Torvalds return -EINVAL; 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 24141da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 24151da177e4SLinus Torvalds imr.imr_ifindex = 0; 2416877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds if (!in_dev) { 24191da177e4SLinus Torvalds err = -ENODEV; 24201da177e4SLinus Torvalds goto done; 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 24231da177e4SLinus Torvalds 24241d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 24251da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 24261da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 24271da177e4SLinus Torvalds break; 24281da177e4SLinus Torvalds } 24291da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 24301da177e4SLinus Torvalds goto done; 24311da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 24321d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 24331da177e4SLinus Torvalds if (!psl) { 24341da177e4SLinus Torvalds len = 0; 24351da177e4SLinus Torvalds count = 0; 24361da177e4SLinus Torvalds } else { 24371da177e4SLinus Torvalds count = psl->sl_count; 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 24401da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 24411da177e4SLinus Torvalds msf->imsf_numsrc = count; 24421da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 24431da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 24441da177e4SLinus Torvalds return -EFAULT; 24451da177e4SLinus Torvalds } 24461da177e4SLinus Torvalds if (len && 24471da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 24481da177e4SLinus Torvalds return -EFAULT; 24491da177e4SLinus Torvalds return 0; 24501da177e4SLinus Torvalds done: 24511da177e4SLinus Torvalds return err; 24521da177e4SLinus Torvalds } 24531da177e4SLinus Torvalds 24541da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 24551da177e4SLinus Torvalds struct group_filter __user *optval, int __user *optlen) 24561da177e4SLinus Torvalds { 24571da177e4SLinus Torvalds int err, i, count, copycount; 24581da177e4SLinus Torvalds struct sockaddr_in *psin; 245963007727SAl Viro __be32 addr; 24601da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 24611da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 24621da177e4SLinus Torvalds struct ip_sf_socklist *psl; 24631da177e4SLinus Torvalds 246487e9f031SWANG Cong ASSERT_RTNL(); 246587e9f031SWANG Cong 24661da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 24671da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 24681da177e4SLinus Torvalds return -EINVAL; 24691da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2470f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 24711da177e4SLinus Torvalds return -EINVAL; 24721da177e4SLinus Torvalds 24731da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 24741da177e4SLinus Torvalds 24751d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 24761da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 24771da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 24781da177e4SLinus Torvalds break; 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 24811da177e4SLinus Torvalds goto done; 24821da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 24831d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 24841da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 24851da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 24861da177e4SLinus Torvalds gsf->gf_numsrc = count; 24871da177e4SLinus Torvalds if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 24881da177e4SLinus Torvalds copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 24891da177e4SLinus Torvalds return -EFAULT; 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds for (i = 0; i < copycount; i++) { 24921da177e4SLinus Torvalds struct sockaddr_storage ss; 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 24951da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 24961da177e4SLinus Torvalds psin->sin_family = AF_INET; 24971da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 24981da177e4SLinus Torvalds if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 24991da177e4SLinus Torvalds return -EFAULT; 25001da177e4SLinus Torvalds } 25011da177e4SLinus Torvalds return 0; 25021da177e4SLinus Torvalds done: 25031da177e4SLinus Torvalds return err; 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds /* 25071da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 25081da177e4SLinus Torvalds */ 2509c0cda068SAl Viro int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif) 25101da177e4SLinus Torvalds { 25111da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25121da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 25131da177e4SLinus Torvalds struct ip_sf_socklist *psl; 25141da177e4SLinus Torvalds int i; 2515c85bb41eSFlavio Leitner int ret; 25161da177e4SLinus Torvalds 2517c85bb41eSFlavio Leitner ret = 1; 2518f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2519c85bb41eSFlavio Leitner goto out; 25201da177e4SLinus Torvalds 2521c85bb41eSFlavio Leitner rcu_read_lock(); 25221d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 25231da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 25241da177e4SLinus Torvalds pmc->multi.imr_ifindex == dif) 25251da177e4SLinus Torvalds break; 25261da177e4SLinus Torvalds } 2527c85bb41eSFlavio Leitner ret = inet->mc_all; 25281da177e4SLinus Torvalds if (!pmc) 2529c85bb41eSFlavio Leitner goto unlock; 25301d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2531c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 25321da177e4SLinus Torvalds if (!psl) 2533c85bb41eSFlavio Leitner goto unlock; 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds for (i = 0; i < psl->sl_count; i++) { 25361da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 25371da177e4SLinus Torvalds break; 25381da177e4SLinus Torvalds } 2539c85bb41eSFlavio Leitner ret = 0; 25401da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2541c85bb41eSFlavio Leitner goto unlock; 25421da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2543c85bb41eSFlavio Leitner goto unlock; 2544c85bb41eSFlavio Leitner ret = 1; 2545c85bb41eSFlavio Leitner unlock: 2546c85bb41eSFlavio Leitner rcu_read_unlock(); 2547c85bb41eSFlavio Leitner out: 2548c85bb41eSFlavio Leitner return ret; 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds /* 25521da177e4SLinus Torvalds * A socket is closing. 25531da177e4SLinus Torvalds */ 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 25561da177e4SLinus Torvalds { 25571da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 25581da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2559877acedcSDaniel Lezcano struct net *net = sock_net(sk); 25601da177e4SLinus Torvalds 256151456b29SIan Morris if (!inet->mc_list) 25621da177e4SLinus Torvalds return; 25631da177e4SLinus Torvalds 25641da177e4SLinus Torvalds rtnl_lock(); 25651d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 25661da177e4SLinus Torvalds struct in_device *in_dev; 25671da177e4SLinus Torvalds 25681d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2569877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 25701da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 257100db4124SIan Morris if (in_dev) 25721da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2573c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2574c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 257510d50e74SLai Jiangshan kfree_rcu(iml, rcu); 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds rtnl_unlock(); 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 2580dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 25812094acbbSAlexander Duyck int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto) 25821da177e4SLinus Torvalds { 25831da177e4SLinus Torvalds struct ip_mc_list *im; 2584e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 25851da177e4SLinus Torvalds struct ip_sf_list *psf; 25861da177e4SLinus Torvalds int rv = 0; 25871da177e4SLinus Torvalds 2588e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash); 2589e9897071SEric Dumazet if (mc_hash) { 2590c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); 2591e9897071SEric Dumazet 2592e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]); 2593e9897071SEric Dumazet im != NULL; 2594e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) { 2595e9897071SEric Dumazet if (im->multiaddr == mc_addr) 2596e9897071SEric Dumazet break; 2597e9897071SEric Dumazet } 2598e9897071SEric Dumazet } else { 25991d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 26001da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 26011da177e4SLinus Torvalds break; 26021da177e4SLinus Torvalds } 2603e9897071SEric Dumazet } 26041da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 26051da177e4SLinus Torvalds rv = 1; 26061da177e4SLinus Torvalds } else if (im) { 26071da177e4SLinus Torvalds if (src_addr) { 26081da177e4SLinus Torvalds for (psf = im->sources; psf; psf = psf->sf_next) { 26091da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 26101da177e4SLinus Torvalds break; 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds if (psf) 26131da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 26141da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 26151da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 26161da177e4SLinus Torvalds else 26171da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 26181da177e4SLinus Torvalds } else 26191da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds return rv; 26221da177e4SLinus Torvalds } 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 26251da177e4SLinus Torvalds struct igmp_mc_iter_state { 26267091e728SAlexey Dobriyan struct seq_net_private p; 26271da177e4SLinus Torvalds struct net_device *dev; 26281da177e4SLinus Torvalds struct in_device *in_dev; 26291da177e4SLinus Torvalds }; 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 26341da177e4SLinus Torvalds { 26357091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 26361da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 26371da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 26381da177e4SLinus Torvalds 26397562f876SPavel Emelianov state->in_dev = NULL; 264061fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 26411da177e4SLinus Torvalds struct in_device *in_dev; 26426baff150SEric Dumazet 26436baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 26441da177e4SLinus Torvalds if (!in_dev) 26451da177e4SLinus Torvalds continue; 26461d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 26471da177e4SLinus Torvalds if (im) { 26481da177e4SLinus Torvalds state->in_dev = in_dev; 26491da177e4SLinus Torvalds break; 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds return im; 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 26561da177e4SLinus Torvalds { 26571da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 26586baff150SEric Dumazet 26591d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 26601d7138deSEric Dumazet while (!im) { 26616baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 26621da177e4SLinus Torvalds if (!state->dev) { 26631da177e4SLinus Torvalds state->in_dev = NULL; 26641da177e4SLinus Torvalds break; 26651da177e4SLinus Torvalds } 26666baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 26671da177e4SLinus Torvalds if (!state->in_dev) 26681da177e4SLinus Torvalds continue; 26691d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds return im; 26721da177e4SLinus Torvalds } 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 26751da177e4SLinus Torvalds { 26761da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 26771da177e4SLinus Torvalds if (im) 26781da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 26791da177e4SLinus Torvalds --pos; 26801da177e4SLinus Torvalds return pos ? NULL : im; 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 268461fbab77Sstephen hemminger __acquires(rcu) 26851da177e4SLinus Torvalds { 268661fbab77Sstephen hemminger rcu_read_lock(); 26871da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 26881da177e4SLinus Torvalds } 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26911da177e4SLinus Torvalds { 26921da177e4SLinus Torvalds struct ip_mc_list *im; 26931da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 26941da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 26951da177e4SLinus Torvalds else 26961da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 26971da177e4SLinus Torvalds ++*pos; 26981da177e4SLinus Torvalds return im; 26991da177e4SLinus Torvalds } 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 270261fbab77Sstephen hemminger __releases(rcu) 27031da177e4SLinus Torvalds { 27041da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27051d7138deSEric Dumazet 27061da177e4SLinus Torvalds state->in_dev = NULL; 27071da177e4SLinus Torvalds state->dev = NULL; 270861fbab77Sstephen hemminger rcu_read_unlock(); 27091da177e4SLinus Torvalds } 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 27121da177e4SLinus Torvalds { 27131da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 27141da177e4SLinus Torvalds seq_puts(seq, 27151da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 27161da177e4SLinus Torvalds else { 27171da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 27181da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 27191da177e4SLinus Torvalds char *querier; 2720a399a805SEric Dumazet long delta; 2721a399a805SEric Dumazet 27221da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 27231da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 27241da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 27251da177e4SLinus Torvalds "V3"; 27261da177e4SLinus Torvalds #else 27271da177e4SLinus Torvalds querier = "NONE"; 27281da177e4SLinus Torvalds #endif 27291da177e4SLinus Torvalds 2730e6b68883SAndreea-Cristina Bernat if (rcu_access_pointer(state->in_dev->mc_list) == im) { 27311da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2732b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 2735a399a805SEric Dumazet delta = im->timer.expires - jiffies; 27361da177e4SLinus Torvalds seq_printf(seq, 2737338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 27381da177e4SLinus Torvalds im->multiaddr, im->users, 2739a399a805SEric Dumazet im->tm_running, 2740a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 27411da177e4SLinus Torvalds im->reporter); 27421da177e4SLinus Torvalds } 27431da177e4SLinus Torvalds return 0; 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 2746f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 27471da177e4SLinus Torvalds .start = igmp_mc_seq_start, 27481da177e4SLinus Torvalds .next = igmp_mc_seq_next, 27491da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 27501da177e4SLinus Torvalds .show = igmp_mc_seq_show, 27511da177e4SLinus Torvalds }; 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file) 27541da177e4SLinus Torvalds { 27557091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mc_seq_ops, 2756cf7732e4SPavel Emelyanov sizeof(struct igmp_mc_iter_state)); 27571da177e4SLinus Torvalds } 27581da177e4SLinus Torvalds 27599a32144eSArjan van de Ven static const struct file_operations igmp_mc_seq_fops = { 27601da177e4SLinus Torvalds .owner = THIS_MODULE, 27611da177e4SLinus Torvalds .open = igmp_mc_seq_open, 27621da177e4SLinus Torvalds .read = seq_read, 27631da177e4SLinus Torvalds .llseek = seq_lseek, 27647091e728SAlexey Dobriyan .release = seq_release_net, 27651da177e4SLinus Torvalds }; 27661da177e4SLinus Torvalds 27671da177e4SLinus Torvalds struct igmp_mcf_iter_state { 27687091e728SAlexey Dobriyan struct seq_net_private p; 27691da177e4SLinus Torvalds struct net_device *dev; 27701da177e4SLinus Torvalds struct in_device *idev; 27711da177e4SLinus Torvalds struct ip_mc_list *im; 27721da177e4SLinus Torvalds }; 27731da177e4SLinus Torvalds 27741da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 27751da177e4SLinus Torvalds 27761da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 27771da177e4SLinus Torvalds { 27787091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 27791da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 27801da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 27811da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 27821da177e4SLinus Torvalds 27837562f876SPavel Emelianov state->idev = NULL; 27847562f876SPavel Emelianov state->im = NULL; 278561fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 27861da177e4SLinus Torvalds struct in_device *idev; 27876baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 278851456b29SIan Morris if (unlikely(!idev)) 27891da177e4SLinus Torvalds continue; 27901d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 279100db4124SIan Morris if (likely(im)) { 27921da177e4SLinus Torvalds spin_lock_bh(&im->lock); 27931da177e4SLinus Torvalds psf = im->sources; 279400db4124SIan Morris if (likely(psf)) { 27951da177e4SLinus Torvalds state->im = im; 27961da177e4SLinus Torvalds state->idev = idev; 27971da177e4SLinus Torvalds break; 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds } 28021da177e4SLinus Torvalds return psf; 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 28061da177e4SLinus Torvalds { 28071da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 28081da177e4SLinus Torvalds 28091da177e4SLinus Torvalds psf = psf->sf_next; 28101da177e4SLinus Torvalds while (!psf) { 28111da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 28121da177e4SLinus Torvalds state->im = state->im->next; 28131da177e4SLinus Torvalds while (!state->im) { 28146baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 28151da177e4SLinus Torvalds if (!state->dev) { 28161da177e4SLinus Torvalds state->idev = NULL; 28171da177e4SLinus Torvalds goto out; 28181da177e4SLinus Torvalds } 28196baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 28201da177e4SLinus Torvalds if (!state->idev) 28211da177e4SLinus Torvalds continue; 28221d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 28231da177e4SLinus Torvalds } 28241da177e4SLinus Torvalds if (!state->im) 28251da177e4SLinus Torvalds break; 28261da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 28271da177e4SLinus Torvalds psf = state->im->sources; 28281da177e4SLinus Torvalds } 28291da177e4SLinus Torvalds out: 28301da177e4SLinus Torvalds return psf; 28311da177e4SLinus Torvalds } 28321da177e4SLinus Torvalds 28331da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 28341da177e4SLinus Torvalds { 28351da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 28361da177e4SLinus Torvalds if (psf) 28371da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 28381da177e4SLinus Torvalds --pos; 28391da177e4SLinus Torvalds return pos ? NULL : psf; 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 284361fbab77Sstephen hemminger __acquires(rcu) 28441da177e4SLinus Torvalds { 284561fbab77Sstephen hemminger rcu_read_lock(); 28461da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 28501da177e4SLinus Torvalds { 28511da177e4SLinus Torvalds struct ip_sf_list *psf; 28521da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 28531da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 28541da177e4SLinus Torvalds else 28551da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 28561da177e4SLinus Torvalds ++*pos; 28571da177e4SLinus Torvalds return psf; 28581da177e4SLinus Torvalds } 28591da177e4SLinus Torvalds 28601da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 286161fbab77Sstephen hemminger __releases(rcu) 28621da177e4SLinus Torvalds { 28631da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 286400db4124SIan Morris if (likely(state->im)) { 28651da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 28661da177e4SLinus Torvalds state->im = NULL; 28671da177e4SLinus Torvalds } 28681da177e4SLinus Torvalds state->idev = NULL; 28691da177e4SLinus Torvalds state->dev = NULL; 287061fbab77Sstephen hemminger rcu_read_unlock(); 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 28741da177e4SLinus Torvalds { 28751da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 28761da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 28791744bea1SJoe Perches seq_puts(seq, "Idx Device MCA SRC INC EXC\n"); 28801da177e4SLinus Torvalds } else { 28811da177e4SLinus Torvalds seq_printf(seq, 28821da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 28831da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 28841da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 28851da177e4SLinus Torvalds ntohl(state->im->multiaddr), 28861da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 28871da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 28881da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 28891da177e4SLinus Torvalds } 28901da177e4SLinus Torvalds return 0; 28911da177e4SLinus Torvalds } 28921da177e4SLinus Torvalds 2893f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 28941da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 28951da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 28961da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 28971da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 28981da177e4SLinus Torvalds }; 28991da177e4SLinus Torvalds 29001da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file) 29011da177e4SLinus Torvalds { 29027091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mcf_seq_ops, 2903cf7732e4SPavel Emelyanov sizeof(struct igmp_mcf_iter_state)); 29041da177e4SLinus Torvalds } 29051da177e4SLinus Torvalds 29069a32144eSArjan van de Ven static const struct file_operations igmp_mcf_seq_fops = { 29071da177e4SLinus Torvalds .owner = THIS_MODULE, 29081da177e4SLinus Torvalds .open = igmp_mcf_seq_open, 29091da177e4SLinus Torvalds .read = seq_read, 29101da177e4SLinus Torvalds .llseek = seq_lseek, 29117091e728SAlexey Dobriyan .release = seq_release_net, 29127091e728SAlexey Dobriyan }; 29137091e728SAlexey Dobriyan 29142c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 29157091e728SAlexey Dobriyan { 29167091e728SAlexey Dobriyan struct proc_dir_entry *pde; 291793a714d6SMadhu Challa int err; 29187091e728SAlexey Dobriyan 2919d4beaa66SGao feng pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops); 29207091e728SAlexey Dobriyan if (!pde) 29217091e728SAlexey Dobriyan goto out_igmp; 2922d4beaa66SGao feng pde = proc_create("mcfilter", S_IRUGO, net->proc_net, 2923d4beaa66SGao feng &igmp_mcf_seq_fops); 29247091e728SAlexey Dobriyan if (!pde) 29257091e728SAlexey Dobriyan goto out_mcfilter; 292693a714d6SMadhu Challa err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET, 292793a714d6SMadhu Challa SOCK_DGRAM, 0, net); 292893a714d6SMadhu Challa if (err < 0) { 292993a714d6SMadhu Challa pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n", 293093a714d6SMadhu Challa err); 293193a714d6SMadhu Challa goto out_sock; 293293a714d6SMadhu Challa } 293393a714d6SMadhu Challa 2934*dcd87999SNikolay Borisov /* Sysctl initialization */ 2935*dcd87999SNikolay Borisov net->ipv4.sysctl_igmp_max_memberships = 20; 2936*dcd87999SNikolay Borisov net->ipv4.sysctl_igmp_max_msf = 10; 2937*dcd87999SNikolay Borisov /* IGMP reports for link-local multicast groups are enabled by default */ 2938*dcd87999SNikolay Borisov net->ipv4.sysctl_igmp_llm_reports = 1; 2939*dcd87999SNikolay Borisov net->ipv4.sysctl_igmp_qrv = 2; 29407091e728SAlexey Dobriyan return 0; 29417091e728SAlexey Dobriyan 294293a714d6SMadhu Challa out_sock: 294393a714d6SMadhu Challa remove_proc_entry("mcfilter", net->proc_net); 29447091e728SAlexey Dobriyan out_mcfilter: 2945ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 29467091e728SAlexey Dobriyan out_igmp: 29477091e728SAlexey Dobriyan return -ENOMEM; 29487091e728SAlexey Dobriyan } 29497091e728SAlexey Dobriyan 29502c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 29517091e728SAlexey Dobriyan { 2952ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net); 2953ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 295493a714d6SMadhu Challa inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk); 29557091e728SAlexey Dobriyan } 29567091e728SAlexey Dobriyan 29577091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 29587091e728SAlexey Dobriyan .init = igmp_net_init, 29597091e728SAlexey Dobriyan .exit = igmp_net_exit, 29601da177e4SLinus Torvalds }; 296172c1d3bdSWANG Cong #endif 29621da177e4SLinus Torvalds 29634aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this, 29644aa5dee4SJiri Pirko unsigned long event, void *ptr) 29654aa5dee4SJiri Pirko { 29664aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 29674aa5dee4SJiri Pirko struct in_device *in_dev; 29684aa5dee4SJiri Pirko 29694aa5dee4SJiri Pirko switch (event) { 29704aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 29714aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev); 29724aa5dee4SJiri Pirko if (in_dev) 29734aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev); 29744aa5dee4SJiri Pirko break; 29754aa5dee4SJiri Pirko default: 29764aa5dee4SJiri Pirko break; 29774aa5dee4SJiri Pirko } 29784aa5dee4SJiri Pirko return NOTIFY_DONE; 29794aa5dee4SJiri Pirko } 29804aa5dee4SJiri Pirko 29814aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = { 29824aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event, 29834aa5dee4SJiri Pirko }; 29844aa5dee4SJiri Pirko 298572c1d3bdSWANG Cong int __init igmp_mc_init(void) 29861da177e4SLinus Torvalds { 298772c1d3bdSWANG Cong #if defined(CONFIG_PROC_FS) 29884aa5dee4SJiri Pirko int err; 29894aa5dee4SJiri Pirko 29904aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops); 29914aa5dee4SJiri Pirko if (err) 29924aa5dee4SJiri Pirko return err; 29934aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier); 29944aa5dee4SJiri Pirko if (err) 29954aa5dee4SJiri Pirko goto reg_notif_fail; 29964aa5dee4SJiri Pirko return 0; 29974aa5dee4SJiri Pirko 29984aa5dee4SJiri Pirko reg_notif_fail: 29994aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops); 30004aa5dee4SJiri Pirko return err; 300172c1d3bdSWANG Cong #else 300272c1d3bdSWANG Cong return register_netdevice_notifier(&igmp_notifier); 30031da177e4SLinus Torvalds #endif 300472c1d3bdSWANG Cong } 3005