11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux NET3: Internet Group Management Protocol [IGMP] 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This code implements the IGMP protocol as defined in RFC1112. There has 51da177e4SLinus Torvalds * been a further revision of this protocol since which is now supported. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * If you have trouble with this module be careful what gcc you have used, 81da177e4SLinus Torvalds * the older version didn't come out right using gcc 2.5.8, the newer one 91da177e4SLinus Torvalds * seems to fall out with gcc 2.6.2. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Authors: 12113aa838SAlan Cox * Alan Cox <alan@lxorguk.ukuu.org.uk> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 151da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 161da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 171da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Fixes: 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Alan Cox : Added lots of __inline__ to optimise 221da177e4SLinus Torvalds * the memory usage of all the tiny little 231da177e4SLinus Torvalds * functions. 241da177e4SLinus Torvalds * Alan Cox : Dumped the header building experiment. 251da177e4SLinus Torvalds * Alan Cox : Minor tweaks ready for multicast routing 261da177e4SLinus Torvalds * and extended IGMP protocol. 271da177e4SLinus Torvalds * Alan Cox : Removed a load of inline directives. Gcc 2.5.8 281da177e4SLinus Torvalds * writes utterly bogus code otherwise (sigh) 291da177e4SLinus Torvalds * fixed IGMP loopback to behave in the manner 301da177e4SLinus Torvalds * desired by mrouted, fixed the fact it has been 311da177e4SLinus Torvalds * broken since 1.3.6 and cleaned up a few minor 321da177e4SLinus Torvalds * points. 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Chih-Jen Chang : Tried to revise IGMP to Version 2 351da177e4SLinus Torvalds * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu 361da177e4SLinus Torvalds * The enhancements are mainly based on Steve Deering's 371da177e4SLinus Torvalds * ipmulti-3.5 source code. 381da177e4SLinus Torvalds * Chih-Jen Chang : Added the igmp_get_mrouter_info and 391da177e4SLinus Torvalds * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of 401da177e4SLinus Torvalds * the mrouted version on that device. 411da177e4SLinus Torvalds * Chih-Jen Chang : Added the max_resp_time parameter to 421da177e4SLinus Torvalds * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter 431da177e4SLinus Torvalds * to identify the multicast router version 441da177e4SLinus Torvalds * and do what the IGMP version 2 specified. 451da177e4SLinus Torvalds * Chih-Jen Chang : Added a timer to revert to IGMP V2 router 461da177e4SLinus Torvalds * Tsu-Sheng Tsao if the specified time expired. 471da177e4SLinus Torvalds * Alan Cox : Stop IGMP from 0.0.0.0 being accepted. 481da177e4SLinus Torvalds * Alan Cox : Use GFP_ATOMIC in the right places. 491da177e4SLinus Torvalds * Christian Daudt : igmp timer wasn't set for local group 501da177e4SLinus Torvalds * memberships but was being deleted, 511da177e4SLinus Torvalds * which caused a "del_timer() called 521da177e4SLinus Torvalds * from %p with timer not initialized\n" 531da177e4SLinus Torvalds * message (960131). 541da177e4SLinus Torvalds * Christian Daudt : removed del_timer from 551da177e4SLinus Torvalds * igmp_timer_expire function (960205). 561da177e4SLinus Torvalds * Christian Daudt : igmp_heard_report now only calls 571da177e4SLinus Torvalds * igmp_timer_expire if tm->running is 581da177e4SLinus Torvalds * true (960216). 591da177e4SLinus Torvalds * Malcolm Beattie : ttl comparison wrong in igmp_rcv made 601da177e4SLinus Torvalds * igmp_heard_query never trigger. Expiry 611da177e4SLinus Torvalds * miscalculation fixed in igmp_heard_query 621da177e4SLinus Torvalds * and random() made to return unsigned to 631da177e4SLinus Torvalds * prevent negative expiry times. 641da177e4SLinus Torvalds * Alexey Kuznetsov: Wrong group leaving behaviour, backport 651da177e4SLinus Torvalds * fix from pending 2.1.x patches. 661da177e4SLinus Torvalds * Alan Cox: Forget to enable FDDI support earlier. 671da177e4SLinus Torvalds * Alexey Kuznetsov: Fixed leaving groups on device down. 681da177e4SLinus Torvalds * Alexey Kuznetsov: Accordance to igmp-v2-06 draft. 691da177e4SLinus Torvalds * David L Stevens: IGMPv3 support, with help from 701da177e4SLinus Torvalds * Vinay Kulkarni 711da177e4SLinus Torvalds */ 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds #include <linux/module.h> 745a0e3ad6STejun Heo #include <linux/slab.h> 751da177e4SLinus Torvalds #include <asm/uaccess.h> 761da177e4SLinus Torvalds #include <linux/types.h> 771da177e4SLinus Torvalds #include <linux/kernel.h> 781da177e4SLinus Torvalds #include <linux/jiffies.h> 791da177e4SLinus Torvalds #include <linux/string.h> 801da177e4SLinus Torvalds #include <linux/socket.h> 811da177e4SLinus Torvalds #include <linux/sockios.h> 821da177e4SLinus Torvalds #include <linux/in.h> 831da177e4SLinus Torvalds #include <linux/inet.h> 841da177e4SLinus Torvalds #include <linux/netdevice.h> 851da177e4SLinus Torvalds #include <linux/skbuff.h> 861da177e4SLinus Torvalds #include <linux/inetdevice.h> 871da177e4SLinus Torvalds #include <linux/igmp.h> 881da177e4SLinus Torvalds #include <linux/if_arp.h> 891da177e4SLinus Torvalds #include <linux/rtnetlink.h> 901da177e4SLinus Torvalds #include <linux/times.h> 919d4a0314SHannes Frederic Sowa #include <linux/pkt_sched.h> 9214c85021SArnaldo Carvalho de Melo 93457c4cbcSEric W. Biederman #include <net/net_namespace.h> 9414c85021SArnaldo Carvalho de Melo #include <net/arp.h> 951da177e4SLinus Torvalds #include <net/ip.h> 961da177e4SLinus Torvalds #include <net/protocol.h> 971da177e4SLinus Torvalds #include <net/route.h> 981da177e4SLinus Torvalds #include <net/sock.h> 991da177e4SLinus Torvalds #include <net/checksum.h> 1001da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 1011da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1021da177e4SLinus Torvalds #include <linux/mroute.h> 1031da177e4SLinus Torvalds #endif 1041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1051da177e4SLinus Torvalds #include <linux/proc_fs.h> 1061da177e4SLinus Torvalds #include <linux/seq_file.h> 1071da177e4SLinus Torvalds #endif 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds #define IP_MAX_MEMBERSHIPS 20 1101da177e4SLinus Torvalds #define IP_MAX_MSF 10 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1131da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */ 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #define IGMP_V1_Router_Present_Timeout (400*HZ) 1161da177e4SLinus Torvalds #define IGMP_V2_Router_Present_Timeout (400*HZ) 117cab70040SWilliam Manley #define IGMP_V2_Unsolicited_Report_Interval (10*HZ) 118cab70040SWilliam Manley #define IGMP_V3_Unsolicited_Report_Interval (1*HZ) 1191da177e4SLinus Torvalds #define IGMP_Query_Response_Interval (10*HZ) 1201da177e4SLinus Torvalds #define IGMP_Unsolicited_Report_Count 2 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds #define IGMP_Initial_Report_Delay (1) 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* IGMP_Initial_Report_Delay is not from IGMP specs! 1261da177e4SLinus Torvalds * IGMP specs require to report membership immediately after 1271da177e4SLinus Torvalds * joining a group, but we delay the first report by a 1281da177e4SLinus Torvalds * small interval. It seems more natural and still does not 1291da177e4SLinus Torvalds * contradict to specs provided this delay is small enough. 1301da177e4SLinus Torvalds */ 1311da177e4SLinus Torvalds 13242f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \ 133c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ 13442f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ 1351da177e4SLinus Torvalds ((in_dev)->mr_v1_seen && \ 1361da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v1_seen))) 13742f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \ 138c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ 13942f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ 1401da177e4SLinus Torvalds ((in_dev)->mr_v2_seen && \ 1411da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v2_seen))) 1421da177e4SLinus Torvalds 143cab70040SWilliam Manley static int unsolicited_report_interval(struct in_device *in_dev) 144cab70040SWilliam Manley { 1452690048cSWilliam Manley int interval_ms, interval_jiffies; 1462690048cSWilliam Manley 147cab70040SWilliam Manley if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 1482690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1492690048cSWilliam Manley in_dev, 1502690048cSWilliam Manley IGMPV2_UNSOLICITED_REPORT_INTERVAL); 151cab70040SWilliam Manley else /* v3 */ 1522690048cSWilliam Manley interval_ms = IN_DEV_CONF_GET( 1532690048cSWilliam Manley in_dev, 1542690048cSWilliam Manley IGMPV3_UNSOLICITED_REPORT_INTERVAL); 1552690048cSWilliam Manley 1562690048cSWilliam Manley interval_jiffies = msecs_to_jiffies(interval_ms); 1572690048cSWilliam Manley 1582690048cSWilliam Manley /* _timer functions can't handle a delay of 0 jiffies so ensure 1592690048cSWilliam Manley * we always return a positive value. 1602690048cSWilliam Manley */ 1612690048cSWilliam Manley if (interval_jiffies <= 0) 1622690048cSWilliam Manley interval_jiffies = 1; 1632690048cSWilliam Manley return interval_jiffies; 164cab70040SWilliam Manley } 165cab70040SWilliam Manley 1661da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im); 16763007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr); 1681da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev); 1691da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc); 1701da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc); 1711da177e4SLinus Torvalds #endif 1721da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc); 1738f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 1748f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta); 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds if (atomic_dec_and_test(&im->refcnt)) { 1791da177e4SLinus Torvalds in_dev_put(im->interface); 18042ea299dSLai Jiangshan kfree_rcu(im, rcu); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 184d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc) \ 185d9aa9380SDavid S. Miller for (pmc = rcu_dereference(in_dev->mc_list); \ 186d9aa9380SDavid S. Miller pmc != NULL; \ 187d9aa9380SDavid S. Miller pmc = rcu_dereference(pmc->next_rcu)) 188d9aa9380SDavid S. Miller 189d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc) \ 190d9aa9380SDavid S. Miller for (pmc = rtnl_dereference(in_dev->mc_list); \ 191d9aa9380SDavid S. Miller pmc != NULL; \ 192d9aa9380SDavid S. Miller pmc = rtnl_dereference(pmc->next_rcu)) 193d9aa9380SDavid S. Miller 1941da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* 1971da177e4SLinus Torvalds * Timer management 1981da177e4SLinus Torvalds */ 1991da177e4SLinus Torvalds 2001d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im) 2011da177e4SLinus Torvalds { 2021da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2031da177e4SLinus Torvalds if (del_timer(&im->timer)) 2041da177e4SLinus Torvalds atomic_dec(&im->refcnt); 2051da177e4SLinus Torvalds im->tm_running = 0; 2061da177e4SLinus Torvalds im->reporter = 0; 2071da177e4SLinus Torvalds im->unsolicit_count = 0; 2081da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* It must be called with locked im->lock */ 2121da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds int tv = net_random() % max_delay; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds im->tm_running = 1; 2171da177e4SLinus Torvalds if (!mod_timer(&im->timer, jiffies+tv+2)) 2181da177e4SLinus Torvalds atomic_inc(&im->refcnt); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds int tv = net_random() % in_dev->mr_maxdelay; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds in_dev->mr_gq_running = 1; 2261da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2)) 2271da177e4SLinus Torvalds in_dev_hold(in_dev); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds int tv = net_random() % delay; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) 2351da177e4SLinus Torvalds in_dev_hold(in_dev); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2411da177e4SLinus Torvalds im->unsolicit_count = 0; 2421da177e4SLinus Torvalds if (del_timer(&im->timer)) { 2431da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) { 2441da177e4SLinus Torvalds add_timer(&im->timer); 2451da177e4SLinus Torvalds im->tm_running = 1; 2461da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2471da177e4SLinus Torvalds return; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds atomic_dec(&im->refcnt); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds igmp_start_timer(im, max_delay); 2521da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* 2571da177e4SLinus Torvalds * Send an IGMP report. 2581da177e4SLinus Torvalds */ 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, 2641da177e4SLinus Torvalds int gdeleted, int sdeleted) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds switch (type) { 2671da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE: 2681da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE: 2691da177e4SLinus Torvalds if (gdeleted || sdeleted) 2701da177e4SLinus Torvalds return 0; 271ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) { 272ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE) 273ad12583fSDavid L Stevens return 1; 274ad12583fSDavid L Stevens /* don't include if this source is excluded 275ad12583fSDavid L Stevens * in all filters 276ad12583fSDavid L Stevens */ 277ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE]) 278ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE; 279ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] == 280ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]; 281ad12583fSDavid L Stevens } 282ad12583fSDavid L Stevens return 0; 2831da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE: 2841da177e4SLinus Torvalds if (gdeleted || sdeleted) 2851da177e4SLinus Torvalds return 0; 2861da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0; 2871da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE: 2881da177e4SLinus Torvalds if (gdeleted || sdeleted) 2891da177e4SLinus Torvalds return 0; 2901da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 || 2911da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE]) 2921da177e4SLinus Torvalds return 0; 2931da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] == 2941da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]; 2951da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES: 2961da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount) 2971da177e4SLinus Torvalds return 0; 2981da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; 2991da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES: 3001da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) 3011da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted); 3021da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds return 0; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds static int 3081da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds struct ip_sf_list *psf; 3111da177e4SLinus Torvalds int scount = 0; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 3141da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 3151da177e4SLinus Torvalds continue; 3161da177e4SLinus Torvalds scount++; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds return scount; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 32157e1ab6eSEric Dumazet #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb)) 32257e1ab6eSEric Dumazet 3231da177e4SLinus Torvalds static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size) 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds struct sk_buff *skb; 3261da177e4SLinus Torvalds struct rtable *rt; 3271da177e4SLinus Torvalds struct iphdr *pip; 3281da177e4SLinus Torvalds struct igmpv3_report *pig; 329877acedcSDaniel Lezcano struct net *net = dev_net(dev); 33031e4543dSDavid S. Miller struct flowi4 fl4; 33166088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 33266088243SHerbert Xu int tlen = dev->needed_tailroom; 3331da177e4SLinus Torvalds 33457e1ab6eSEric Dumazet while (1) { 33566088243SHerbert Xu skb = alloc_skb(size + hlen + tlen, 33657e1ab6eSEric Dumazet GFP_ATOMIC | __GFP_NOWARN); 33757e1ab6eSEric Dumazet if (skb) 33857e1ab6eSEric Dumazet break; 33957e1ab6eSEric Dumazet size >>= 1; 34057e1ab6eSEric Dumazet if (size < 256) 3411da177e4SLinus Torvalds return NULL; 34257e1ab6eSEric Dumazet } 3439d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 34457e1ab6eSEric Dumazet igmp_skb_size(skb) = size; 3451da177e4SLinus Torvalds 34631e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, 34778fbfd8aSDavid S. Miller 0, 0, 34878fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 349b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 3501da177e4SLinus Torvalds kfree_skb(skb); 3511da177e4SLinus Torvalds return NULL; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 354d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 3551da177e4SLinus Torvalds skb->dev = dev; 3561da177e4SLinus Torvalds 35766088243SHerbert Xu skb_reserve(skb, hlen); 3581da177e4SLinus Torvalds 3597e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 360eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb); 3617e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds pip->version = 4; 3641da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2; 3651da177e4SLinus Torvalds pip->tos = 0xc0; 3661da177e4SLinus Torvalds pip->frag_off = htons(IP_DF); 3671da177e4SLinus Torvalds pip->ttl = 1; 368492f64ceSDavid S. Miller pip->daddr = fl4.daddr; 369492f64ceSDavid S. Miller pip->saddr = fl4.saddr; 3701da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP; 3711da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */ 372703133deSAnsis Atteka ip_select_ident(skb, &rt->dst, NULL); 3731da177e4SLinus Torvalds ((u8 *)&pip[1])[0] = IPOPT_RA; 3741da177e4SLinus Torvalds ((u8 *)&pip[1])[1] = 4; 3751da177e4SLinus Torvalds ((u8 *)&pip[1])[2] = 0; 3761da177e4SLinus Torvalds ((u8 *)&pip[1])[3] = 0; 3771da177e4SLinus Torvalds 378b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; 379d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig)); 380d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb); 3811da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; 3821da177e4SLinus Torvalds pig->resv1 = 0; 3831da177e4SLinus Torvalds pig->csum = 0; 3841da177e4SLinus Torvalds pig->resv2 = 0; 3851da177e4SLinus Torvalds pig->ngrec = 0; 3861da177e4SLinus Torvalds return skb; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb) 3901da177e4SLinus Torvalds { 391d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb); 392f7c0c2aeSSimon Horman const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb); 3931da177e4SLinus Torvalds 394d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); 3951da177e4SLinus Torvalds 396c439cb2eSHerbert Xu return ip_local_out(skb); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, 4051da177e4SLinus Torvalds int type, struct igmpv3_grec **ppgr) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4081da177e4SLinus Torvalds struct igmpv3_report *pih; 4091da177e4SLinus Torvalds struct igmpv3_grec *pgr; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds if (!skb) 4121da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4131da177e4SLinus Torvalds if (!skb) 4141da177e4SLinus Torvalds return NULL; 4151da177e4SLinus Torvalds pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec)); 4161da177e4SLinus Torvalds pgr->grec_type = type; 4171da177e4SLinus Torvalds pgr->grec_auxwords = 0; 4181da177e4SLinus Torvalds pgr->grec_nsrcs = 0; 4191da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr; 420d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb); 4211da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1); 4221da177e4SLinus Torvalds *ppgr = pgr; 4231da177e4SLinus Torvalds return skb; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 42657e1ab6eSEric Dumazet #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \ 4271da177e4SLinus Torvalds skb_tailroom(skb)) : 0) 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, 4301da177e4SLinus Torvalds int type, int gdeleted, int sdeleted) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4331da177e4SLinus Torvalds struct igmpv3_report *pih; 4341da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL; 4351da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; 436ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 4391da177e4SLinus Torvalds 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; 5411da177e4SLinus Torvalds int type; 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds if (!pmc) { 5441d7138deSEric Dumazet rcu_read_lock(); 5451d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 5461da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 5471da177e4SLinus Torvalds continue; 5481da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5491da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5501da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5511da177e4SLinus Torvalds else 5521da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5531da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5541da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5551da177e4SLinus Torvalds } 5561d7138deSEric Dumazet rcu_read_unlock(); 5571da177e4SLinus Torvalds } else { 5581da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5591da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5601da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5611da177e4SLinus Torvalds else 5621da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5631da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5641da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds if (!skb) 5671da177e4SLinus Torvalds return 0; 5681da177e4SLinus Torvalds return igmpv3_sendpack(skb); 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* 5721da177e4SLinus Torvalds * remove zero-count source records from a source filter list 5731da177e4SLinus Torvalds */ 5741da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds psf_prev = NULL; 5791da177e4SLinus Torvalds for (psf=*ppsf; psf; psf = psf_next) { 5801da177e4SLinus Torvalds psf_next = psf->sf_next; 5811da177e4SLinus Torvalds if (psf->sf_crcount == 0) { 5821da177e4SLinus Torvalds if (psf_prev) 5831da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5841da177e4SLinus Torvalds else 5851da177e4SLinus Torvalds *ppsf = psf->sf_next; 5861da177e4SLinus Torvalds kfree(psf); 5871da177e4SLinus Torvalds } else 5881da177e4SLinus Torvalds psf_prev = psf; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next; 5951da177e4SLinus Torvalds struct sk_buff *skb = NULL; 5961da177e4SLinus Torvalds int type, dtype; 5971da177e4SLinus Torvalds 5981d7138deSEric Dumazet rcu_read_lock(); 5991da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds /* deleted MCA's */ 6021da177e4SLinus Torvalds pmc_prev = NULL; 6031da177e4SLinus Torvalds for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) { 6041da177e4SLinus Torvalds pmc_next = pmc->next; 6051da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 6061da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6071da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6081da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6091da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds if (pmc->crcount) { 6121da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) { 6131da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6141da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 6151da177e4SLinus Torvalds } 616ad12583fSDavid L Stevens pmc->crcount--; 6171da177e4SLinus Torvalds if (pmc->crcount == 0) { 6181da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb); 6191da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { 6231da177e4SLinus Torvalds if (pmc_prev) 6241da177e4SLinus Torvalds pmc_prev->next = pmc_next; 6251da177e4SLinus Torvalds else 6261da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next; 6271da177e4SLinus Torvalds in_dev_put(pmc->interface); 6281da177e4SLinus Torvalds kfree(pmc); 6291da177e4SLinus Torvalds } else 6301da177e4SLinus Torvalds pmc_prev = pmc; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /* change recs */ 6351d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 6361da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6371da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 6381da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6391da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES; 6401da177e4SLinus Torvalds } else { 6411da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES; 6421da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6451da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* filter mode changes */ 6481da177e4SLinus Torvalds if (pmc->crcount) { 6491da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) 6501da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE; 6511da177e4SLinus Torvalds else 6521da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6531da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 654ad12583fSDavid L Stevens pmc->crcount--; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6571da177e4SLinus Torvalds } 6581d7138deSEric Dumazet rcu_read_unlock(); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds if (!skb) 6611da177e4SLinus Torvalds return; 6621da177e4SLinus Torvalds (void) igmpv3_sendpack(skb); 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, 6661da177e4SLinus Torvalds int type) 6671da177e4SLinus Torvalds { 6681da177e4SLinus Torvalds struct sk_buff *skb; 6691da177e4SLinus Torvalds struct iphdr *iph; 6701da177e4SLinus Torvalds struct igmphdr *ih; 6711da177e4SLinus Torvalds struct rtable *rt; 6721da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 673877acedcSDaniel Lezcano struct net *net = dev_net(dev); 67463007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0; 67531e4543dSDavid S. Miller struct flowi4 fl4; 67663007727SAl Viro __be32 dst; 67766088243SHerbert Xu int hlen, tlen; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) 6801da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc); 6811da177e4SLinus Torvalds else if (type == IGMP_HOST_LEAVE_MESSAGE) 6821da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER; 6831da177e4SLinus Torvalds else 6841da177e4SLinus Torvalds dst = group; 6851da177e4SLinus Torvalds 68631e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, 68778fbfd8aSDavid S. Miller 0, 0, 68878fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 689b23dd4feSDavid S. Miller if (IS_ERR(rt)) 6901da177e4SLinus Torvalds return -1; 69178fbfd8aSDavid S. Miller 69266088243SHerbert Xu hlen = LL_RESERVED_SPACE(dev); 69366088243SHerbert Xu tlen = dev->needed_tailroom; 69466088243SHerbert Xu skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); 6951da177e4SLinus Torvalds if (skb == NULL) { 6961da177e4SLinus Torvalds ip_rt_put(rt); 6971da177e4SLinus Torvalds return -1; 6981da177e4SLinus Torvalds } 6999d4a0314SHannes Frederic Sowa skb->priority = TC_PRIO_CONTROL; 7001da177e4SLinus Torvalds 701d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 7021da177e4SLinus Torvalds 70366088243SHerbert Xu skb_reserve(skb, hlen); 7041da177e4SLinus Torvalds 7057e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 706eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 7077e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds iph->version = 4; 7101da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2; 7111da177e4SLinus Torvalds iph->tos = 0xc0; 7121da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 7131da177e4SLinus Torvalds iph->ttl = 1; 7141da177e4SLinus Torvalds iph->daddr = dst; 715492f64ceSDavid S. Miller iph->saddr = fl4.saddr; 7161da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP; 717703133deSAnsis Atteka ip_select_ident(skb, &rt->dst, NULL); 7181da177e4SLinus Torvalds ((u8 *)&iph[1])[0] = IPOPT_RA; 7191da177e4SLinus Torvalds ((u8 *)&iph[1])[1] = 4; 7201da177e4SLinus Torvalds ((u8 *)&iph[1])[2] = 0; 7211da177e4SLinus Torvalds ((u8 *)&iph[1])[3] = 0; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 7241da177e4SLinus Torvalds ih->type = type; 7251da177e4SLinus Torvalds ih->code = 0; 7261da177e4SLinus Torvalds ih->csum = 0; 7271da177e4SLinus Torvalds ih->group = group; 7281da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 7291da177e4SLinus Torvalds 730c439cb2eSHerbert Xu return ip_local_out(skb); 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds static void igmp_gq_timer_expire(unsigned long data) 7341da177e4SLinus Torvalds { 7351da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 7381da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL); 739*e2401654SSalam Noureddine in_dev_put(in_dev); 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds static void igmp_ifc_timer_expire(unsigned long data) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 7471da177e4SLinus Torvalds if (in_dev->mr_ifc_count) { 7481da177e4SLinus Torvalds in_dev->mr_ifc_count--; 749cab70040SWilliam Manley igmp_ifc_start_timer(in_dev, 750cab70040SWilliam Manley unsolicited_report_interval(in_dev)); 7511da177e4SLinus Torvalds } 752*e2401654SSalam Noureddine in_dev_put(in_dev); 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 7561da177e4SLinus Torvalds { 7571da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 7581da177e4SLinus Torvalds return; 7591da177e4SLinus Torvalds in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv : 7601da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 7611da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds static void igmp_timer_expire(unsigned long data) 7661da177e4SLinus Torvalds { 7671da177e4SLinus Torvalds struct ip_mc_list *im=(struct ip_mc_list *)data; 7681da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds spin_lock(&im->lock); 7711da177e4SLinus Torvalds im->tm_running = 0; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds if (im->unsolicit_count) { 7741da177e4SLinus Torvalds im->unsolicit_count--; 775cab70040SWilliam Manley igmp_start_timer(im, unsolicited_report_interval(in_dev)); 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds im->reporter = 1; 7781da177e4SLinus Torvalds spin_unlock(&im->lock); 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 7811da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 7821da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 7831da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 7841da177e4SLinus Torvalds else 7851da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds ip_ma_put(im); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 790ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 791ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 7921da177e4SLinus Torvalds { 7931da177e4SLinus Torvalds struct ip_sf_list *psf; 7941da177e4SLinus Torvalds int i, scount; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds scount = 0; 7971da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 7981da177e4SLinus Torvalds if (scount == nsrcs) 7991da177e4SLinus Torvalds break; 800ad12583fSDavid L Stevens for (i=0; i<nsrcs; i++) { 801ad12583fSDavid L Stevens /* skip inactive filters */ 802e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] || 803ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 804ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 805ce713ee5SRongQing.Li break; 806ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 807ad12583fSDavid L Stevens scount++; 808ad12583fSDavid L Stevens break; 809ad12583fSDavid L Stevens } 810ad12583fSDavid L Stevens } 811ad12583fSDavid L Stevens } 812ad12583fSDavid L Stevens pmc->gsquery = 0; 813ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 814ad12583fSDavid L Stevens return 0; 815ad12583fSDavid L Stevens return 1; 816ad12583fSDavid L Stevens } 817ad12583fSDavid L Stevens 818ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 819ad12583fSDavid L Stevens { 820ad12583fSDavid L Stevens struct ip_sf_list *psf; 821ad12583fSDavid L Stevens int i, scount; 822ad12583fSDavid L Stevens 823ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 824ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 825ad12583fSDavid L Stevens 826ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 827ad12583fSDavid L Stevens scount = 0; 828ad12583fSDavid L Stevens for (psf=pmc->sources; psf; psf=psf->sf_next) { 829ad12583fSDavid L Stevens if (scount == nsrcs) 830ad12583fSDavid L Stevens break; 8311da177e4SLinus Torvalds for (i=0; i<nsrcs; i++) 8321da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 8331da177e4SLinus Torvalds psf->sf_gsresp = 1; 8341da177e4SLinus Torvalds scount++; 8351da177e4SLinus Torvalds break; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds } 838ad12583fSDavid L Stevens if (!scount) { 839ad12583fSDavid L Stevens pmc->gsquery = 0; 840ad12583fSDavid L Stevens return 0; 841ad12583fSDavid L Stevens } 842ad12583fSDavid L Stevens pmc->gsquery = 1; 843ad12583fSDavid L Stevens return 1; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 846d679c532SEric Dumazet /* return true if packet was dropped */ 847d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group) 8481da177e4SLinus Torvalds { 8491da177e4SLinus Torvalds struct ip_mc_list *im; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 854d679c532SEric Dumazet return false; 8551da177e4SLinus Torvalds 8561d7138deSEric Dumazet rcu_read_lock(); 8571d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 8581da177e4SLinus Torvalds if (im->multiaddr == group) { 8591da177e4SLinus Torvalds igmp_stop_timer(im); 8601da177e4SLinus Torvalds break; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds } 8631d7138deSEric Dumazet rcu_read_unlock(); 864d679c532SEric Dumazet return false; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 867d679c532SEric Dumazet /* return true if packet was dropped */ 868d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 8691da177e4SLinus Torvalds int len) 8701da177e4SLinus Torvalds { 871d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 872d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 8731da177e4SLinus Torvalds struct ip_mc_list *im; 87463007727SAl Viro __be32 group = ih->group; 8751da177e4SLinus Torvalds int max_delay; 8761da177e4SLinus Torvalds int mark = 0; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds 8795b7c8406SDavid Stevens if (len == 8) { 8801da177e4SLinus Torvalds if (ih->code == 0) { 8811da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds max_delay = IGMP_Query_Response_Interval; 8841da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 8851da177e4SLinus Torvalds IGMP_V1_Router_Present_Timeout; 8861da177e4SLinus Torvalds group = 0; 8871da177e4SLinus Torvalds } else { 8881da177e4SLinus Torvalds /* v2 router present */ 8891da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 8901da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 8911da177e4SLinus Torvalds IGMP_V2_Router_Present_Timeout; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds /* cancel the interface change timer */ 8941da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 8951da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 8961da177e4SLinus Torvalds __in_dev_put(in_dev); 8971da177e4SLinus Torvalds /* clear deleted report items */ 8981da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 8991da177e4SLinus Torvalds } else if (len < 12) { 900d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */ 9015b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) { 9025b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */ 9035b7c8406SDavid Stevens max_delay = IGMP_Query_Response_Interval; 9045b7c8406SDavid Stevens group = 0; 9055b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) { 9065b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present; 9075b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here. 9085b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a 9095b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely 9105b7c8406SDavid Stevens * to be intended in a v3 query. 9115b7c8406SDavid Stevens */ 9125b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 913a8c1f65cSBen Hutchings if (!max_delay) 914a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */ 9151da177e4SLinus Torvalds } else { /* v3 */ 9161da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 917d679c532SEric Dumazet return true; 9181da177e4SLinus Torvalds 919d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9201da177e4SLinus Torvalds if (ih3->nsrcs) { 9211da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 92263007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 923d679c532SEric Dumazet return true; 924d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 9281da177e4SLinus Torvalds if (!max_delay) 9291da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 9301da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 9311da177e4SLinus Torvalds if (ih3->qrv) 9321da177e4SLinus Torvalds in_dev->mr_qrv = ih3->qrv; 9331da177e4SLinus Torvalds if (!group) { /* general query */ 9341da177e4SLinus Torvalds if (ih3->nsrcs) 935d679c532SEric Dumazet return false; /* no sources allowed */ 9361da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 937d679c532SEric Dumazet return false; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 9401da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds /* 9441da177e4SLinus Torvalds * - Start the timers in all of our membership records 9451da177e4SLinus Torvalds * that the query applies to for the interface on 9461da177e4SLinus Torvalds * which the query arrived excl. those that belong 9471da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 9481da177e4SLinus Torvalds * - For timers already running check if they need to 9491da177e4SLinus Torvalds * be reset. 9501da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 9511da177e4SLinus Torvalds * delay possible 9521da177e4SLinus Torvalds */ 9531d7138deSEric Dumazet rcu_read_lock(); 9541d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 955ad12583fSDavid L Stevens int changed; 956ad12583fSDavid L Stevens 9571da177e4SLinus Torvalds if (group && group != im->multiaddr) 9581da177e4SLinus Torvalds continue; 9591da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 9601da177e4SLinus Torvalds continue; 9611da177e4SLinus Torvalds spin_lock_bh(&im->lock); 9621da177e4SLinus Torvalds if (im->tm_running) 9631da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 9641da177e4SLinus Torvalds else 9651da177e4SLinus Torvalds im->gsquery = mark; 966ad12583fSDavid L Stevens changed = !im->gsquery || 9671da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 9681da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 969ad12583fSDavid L Stevens if (changed) 9701da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 9711da177e4SLinus Torvalds } 9721d7138deSEric Dumazet rcu_read_unlock(); 973d679c532SEric Dumazet return false; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9769a57a9d2SEric Dumazet /* called in rcu_read_lock() section */ 9771da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 9801da177e4SLinus Torvalds struct igmphdr *ih; 9819a57a9d2SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(skb->dev); 9821da177e4SLinus Torvalds int len = skb->len; 983d679c532SEric Dumazet bool dropped = true; 9841da177e4SLinus Torvalds 985cd557bc1SDenis V. Lunev if (in_dev == NULL) 986cd557bc1SDenis V. Lunev goto drop; 9871da177e4SLinus Torvalds 988fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 9899a57a9d2SEric Dumazet goto drop; 990fb286bb2SHerbert Xu 991fb286bb2SHerbert Xu switch (skb->ip_summed) { 99284fa7933SPatrick McHardy case CHECKSUM_COMPLETE: 993d3bc23e7SAl Viro if (!csum_fold(skb->csum)) 994fb286bb2SHerbert Xu break; 995fb286bb2SHerbert Xu /* fall through */ 996fb286bb2SHerbert Xu case CHECKSUM_NONE: 997fb286bb2SHerbert Xu skb->csum = 0; 998fb286bb2SHerbert Xu if (__skb_checksum_complete(skb)) 9999a57a9d2SEric Dumazet goto drop; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 1002d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 10031da177e4SLinus Torvalds switch (ih->type) { 10041da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 1005d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len); 10061da177e4SLinus Torvalds break; 10071da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 10081da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 10091da177e4SLinus Torvalds /* Is it our report looped back? */ 1010c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) 10111da177e4SLinus Torvalds break; 101224c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 101324c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 101424c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 1015d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group); 10161da177e4SLinus Torvalds break; 10171da177e4SLinus Torvalds case IGMP_PIM: 10181da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 10191da177e4SLinus Torvalds return pim_rcv_v1(skb); 10201da177e4SLinus Torvalds #endif 1021c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 10221da177e4SLinus Torvalds case IGMP_DVMRP: 10231da177e4SLinus Torvalds case IGMP_TRACE: 10241da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 10251da177e4SLinus Torvalds case IGMP_MTRACE: 10261da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 10271da177e4SLinus Torvalds break; 10281da177e4SLinus Torvalds default: 1029dd1c1853SLinus Torvalds break; 10301da177e4SLinus Torvalds } 1031fb286bb2SHerbert Xu 1032cd557bc1SDenis V. Lunev drop: 1033d679c532SEric Dumazet if (dropped) 10341da177e4SLinus Torvalds kfree_skb(skb); 1035d679c532SEric Dumazet else 1036d679c532SEric Dumazet consume_skb(skb); 10371da177e4SLinus Torvalds return 0; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds #endif 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds /* 10441da177e4SLinus Torvalds * Add a filter to a device 10451da177e4SLinus Torvalds */ 10461da177e4SLinus Torvalds 104763007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 10481da177e4SLinus Torvalds { 10491da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10501da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 10531da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 1054b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode 10551da177e4SLinus Torvalds routine. Something sort of: 10561da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 10571da177e4SLinus Torvalds --ANK 10581da177e4SLinus Torvalds */ 10591da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 106022bedad3SJiri Pirko dev_mc_add(dev, buf); 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds /* 10641da177e4SLinus Torvalds * Remove a filter from a device 10651da177e4SLinus Torvalds */ 10661da177e4SLinus Torvalds 106763007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 10681da177e4SLinus Torvalds { 10691da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10701da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 107322bedad3SJiri Pirko dev_mc_del(dev, buf); 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 10771da177e4SLinus Torvalds /* 10781da177e4SLinus Torvalds * deleted ip_mc_list manipulation 10791da177e4SLinus Torvalds */ 10801da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im) 10811da177e4SLinus Torvalds { 10821da177e4SLinus Torvalds struct ip_mc_list *pmc; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 10851da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 10861da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 10871da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 10881da177e4SLinus Torvalds * non-deleted or query-response MCA's. 10891da177e4SLinus Torvalds */ 10900da974f4SPanagiotis Issaris pmc = kzalloc(sizeof(*pmc), GFP_KERNEL); 10911da177e4SLinus Torvalds if (!pmc) 10921da177e4SLinus Torvalds return; 10931da177e4SLinus Torvalds spin_lock_bh(&im->lock); 10941da177e4SLinus Torvalds pmc->interface = im->interface; 10951da177e4SLinus Torvalds in_dev_hold(in_dev); 10961da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 10971da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 10981da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 10991da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 11001da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 11011da177e4SLinus Torvalds struct ip_sf_list *psf; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds pmc->tomb = im->tomb; 11041da177e4SLinus Torvalds pmc->sources = im->sources; 11051da177e4SLinus Torvalds im->tomb = im->sources = NULL; 11061da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) 11071da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11121da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 11131da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 11141da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 111763007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr) 11181da177e4SLinus Torvalds { 11191da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 11201da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11231da177e4SLinus Torvalds pmc_prev = NULL; 11241da177e4SLinus Torvalds for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) { 11251da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 11261da177e4SLinus Torvalds break; 11271da177e4SLinus Torvalds pmc_prev = pmc; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds if (pmc) { 11301da177e4SLinus Torvalds if (pmc_prev) 11311da177e4SLinus Torvalds pmc_prev->next = pmc->next; 11321da177e4SLinus Torvalds else 11331da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11361da177e4SLinus Torvalds if (pmc) { 11371da177e4SLinus Torvalds for (psf=pmc->tomb; psf; psf=psf_next) { 11381da177e4SLinus Torvalds psf_next = psf->sf_next; 11391da177e4SLinus Torvalds kfree(psf); 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds in_dev_put(pmc->interface); 11421da177e4SLinus Torvalds kfree(pmc); 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 11471da177e4SLinus Torvalds { 11481da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11511da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 11521da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 11531da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 11561da177e4SLinus Torvalds nextpmc = pmc->next; 11571da177e4SLinus Torvalds ip_mc_clear_src(pmc); 11581da177e4SLinus Torvalds in_dev_put(pmc->interface); 11591da177e4SLinus Torvalds kfree(pmc); 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds /* clear dead sources, too */ 11621d7138deSEric Dumazet rcu_read_lock(); 11631d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 11641da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 11671da177e4SLinus Torvalds psf = pmc->tomb; 11681da177e4SLinus Torvalds pmc->tomb = NULL; 11691da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 11701da177e4SLinus Torvalds for (; psf; psf=psf_next) { 11711da177e4SLinus Torvalds psf_next = psf->sf_next; 11721da177e4SLinus Torvalds kfree(psf); 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds } 11751d7138deSEric Dumazet rcu_read_unlock(); 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds #endif 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im) 11801da177e4SLinus Torvalds { 11811da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 11821da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11831da177e4SLinus Torvalds int reporter; 11841da177e4SLinus Torvalds #endif 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds if (im->loaded) { 11871da177e4SLinus Torvalds im->loaded = 0; 11881da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11921da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 11931da177e4SLinus Torvalds return; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds reporter = im->reporter; 11961da177e4SLinus Torvalds igmp_stop_timer(im); 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds if (!in_dev->dead) { 11991da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 120024cf3af3SVeaceslav Falico return; 12011da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 12021da177e4SLinus Torvalds if (reporter) 12031da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 120424cf3af3SVeaceslav Falico return; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds /* IGMPv3 */ 12071da177e4SLinus Torvalds igmpv3_add_delrec(in_dev, im); 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds igmp_ifc_event(in_dev); 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds #endif 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds static void igmp_group_added(struct ip_mc_list *im) 12151da177e4SLinus Torvalds { 12161da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (im->loaded == 0) { 12191da177e4SLinus Torvalds im->loaded = 1; 12201da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12241da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 12251da177e4SLinus Torvalds return; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds if (in_dev->dead) 12281da177e4SLinus Torvalds return; 12291da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 12301da177e4SLinus Torvalds spin_lock_bh(&im->lock); 12311da177e4SLinus Torvalds igmp_start_timer(im, IGMP_Initial_Report_Delay); 12321da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 12331da177e4SLinus Torvalds return; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds /* else, v3 */ 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 12381da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 12391da177e4SLinus Torvalds igmp_ifc_event(in_dev); 12401da177e4SLinus Torvalds #endif 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds /* 12451da177e4SLinus Torvalds * Multicast list managers 12461da177e4SLinus Torvalds */ 12471da177e4SLinus Torvalds 1248e9897071SEric Dumazet static u32 ip_mc_hash(const struct ip_mc_list *im) 1249e9897071SEric Dumazet { 1250c70eba74SEric Dumazet return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); 1251e9897071SEric Dumazet } 1252e9897071SEric Dumazet 1253e9897071SEric Dumazet static void ip_mc_hash_add(struct in_device *in_dev, 1254e9897071SEric Dumazet struct ip_mc_list *im) 1255e9897071SEric Dumazet { 1256e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 1257e9897071SEric Dumazet u32 hash; 1258e9897071SEric Dumazet 1259e9897071SEric Dumazet mc_hash = rtnl_dereference(in_dev->mc_hash); 1260e9897071SEric Dumazet if (mc_hash) { 1261e9897071SEric Dumazet hash = ip_mc_hash(im); 1262c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1263e9897071SEric Dumazet rcu_assign_pointer(mc_hash[hash], im); 1264e9897071SEric Dumazet return; 1265e9897071SEric Dumazet } 1266e9897071SEric Dumazet 1267e9897071SEric Dumazet /* do not use a hash table for small number of items */ 1268e9897071SEric Dumazet if (in_dev->mc_count < 4) 1269e9897071SEric Dumazet return; 1270e9897071SEric Dumazet 1271e9897071SEric Dumazet mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG, 1272e9897071SEric Dumazet GFP_KERNEL); 1273e9897071SEric Dumazet if (!mc_hash) 1274e9897071SEric Dumazet return; 1275e9897071SEric Dumazet 1276e9897071SEric Dumazet for_each_pmc_rtnl(in_dev, im) { 1277e9897071SEric Dumazet hash = ip_mc_hash(im); 1278c70eba74SEric Dumazet im->next_hash = mc_hash[hash]; 1279e9897071SEric Dumazet RCU_INIT_POINTER(mc_hash[hash], im); 1280e9897071SEric Dumazet } 1281e9897071SEric Dumazet 1282e9897071SEric Dumazet rcu_assign_pointer(in_dev->mc_hash, mc_hash); 1283e9897071SEric Dumazet } 1284e9897071SEric Dumazet 1285e9897071SEric Dumazet static void ip_mc_hash_remove(struct in_device *in_dev, 1286e9897071SEric Dumazet struct ip_mc_list *im) 1287e9897071SEric Dumazet { 1288e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); 1289e9897071SEric Dumazet struct ip_mc_list *aux; 1290e9897071SEric Dumazet 1291e9897071SEric Dumazet if (!mc_hash) 1292e9897071SEric Dumazet return; 1293e9897071SEric Dumazet mc_hash += ip_mc_hash(im); 1294e9897071SEric Dumazet while ((aux = rtnl_dereference(*mc_hash)) != im) 1295e9897071SEric Dumazet mc_hash = &aux->next_hash; 1296e9897071SEric Dumazet *mc_hash = im->next_hash; 1297e9897071SEric Dumazet } 1298e9897071SEric Dumazet 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds /* 13011da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 13021da177e4SLinus Torvalds */ 13031da177e4SLinus Torvalds 13048f935bbdSAl Viro void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 13051da177e4SLinus Torvalds { 13061da177e4SLinus Torvalds struct ip_mc_list *im; 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds ASSERT_RTNL(); 13091da177e4SLinus Torvalds 13101d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) { 13111da177e4SLinus Torvalds if (im->multiaddr == addr) { 13121da177e4SLinus Torvalds im->users++; 13131da177e4SLinus Torvalds ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0); 13141da177e4SLinus Torvalds goto out; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds 13181d7138deSEric Dumazet im = kzalloc(sizeof(*im), GFP_KERNEL); 13191da177e4SLinus Torvalds if (!im) 13201da177e4SLinus Torvalds goto out; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds im->users = 1; 13231da177e4SLinus Torvalds im->interface = in_dev; 13241da177e4SLinus Torvalds in_dev_hold(in_dev); 13251da177e4SLinus Torvalds im->multiaddr = addr; 13261da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 13271da177e4SLinus Torvalds im->sfmode = MCAST_EXCLUDE; 13281da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE] = 1; 13291da177e4SLinus Torvalds atomic_set(&im->refcnt, 1); 13301da177e4SLinus Torvalds spin_lock_init(&im->lock); 13311da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1332b24b8a24SPavel Emelyanov setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im); 13331da177e4SLinus Torvalds im->unsolicit_count = IGMP_Unsolicited_Report_Count; 13341da177e4SLinus Torvalds #endif 13351d7138deSEric Dumazet 13361d7138deSEric Dumazet im->next_rcu = in_dev->mc_list; 1337b8bae41eSRami Rosen in_dev->mc_count++; 1338cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im); 13391d7138deSEric Dumazet 1340e9897071SEric Dumazet ip_mc_hash_add(in_dev, im); 1341e9897071SEric Dumazet 13421da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13431da177e4SLinus Torvalds igmpv3_del_delrec(in_dev, im->multiaddr); 13441da177e4SLinus Torvalds #endif 13451da177e4SLinus Torvalds igmp_group_added(im); 13461da177e4SLinus Torvalds if (!in_dev->dead) 13471da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 13481da177e4SLinus Torvalds out: 13491da177e4SLinus Torvalds return; 13501da177e4SLinus Torvalds } 13514bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group); 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds /* 13544aa5dee4SJiri Pirko * Resend IGMP JOIN report; used by netdev notifier. 1355a816c7c7SJay Vosburgh */ 13564aa5dee4SJiri Pirko static void ip_mc_rejoin_groups(struct in_device *in_dev) 1357a816c7c7SJay Vosburgh { 135808882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1359866f3b25SEric Dumazet struct ip_mc_list *im; 1360866f3b25SEric Dumazet int type; 1361a816c7c7SJay Vosburgh 13624aa5dee4SJiri Pirko ASSERT_RTNL(); 13634aa5dee4SJiri Pirko 13644aa5dee4SJiri Pirko for_each_pmc_rtnl(in_dev, im) { 1365a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1366866f3b25SEric Dumazet continue; 1367a816c7c7SJay Vosburgh 1368e12b4539SFlavio Leitner /* a failover is happening and switches 1369866f3b25SEric Dumazet * must be notified immediately 1370866f3b25SEric Dumazet */ 1371e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev)) 1372866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT; 1373e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev)) 1374866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT; 1375e12b4539SFlavio Leitner else 1376866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT; 1377866f3b25SEric Dumazet igmp_send_report(in_dev, im, type); 1378866f3b25SEric Dumazet } 1379a816c7c7SJay Vosburgh #endif 1380a816c7c7SJay Vosburgh } 1381a816c7c7SJay Vosburgh 1382a816c7c7SJay Vosburgh /* 13831da177e4SLinus Torvalds * A socket has left a multicast group on device dev 13841da177e4SLinus Torvalds */ 13851da177e4SLinus Torvalds 13868f935bbdSAl Viro void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) 13871da177e4SLinus Torvalds { 13881d7138deSEric Dumazet struct ip_mc_list *i; 13891d7138deSEric Dumazet struct ip_mc_list __rcu **ip; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds ASSERT_RTNL(); 13921da177e4SLinus Torvalds 13931d7138deSEric Dumazet for (ip = &in_dev->mc_list; 13941d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL; 13951d7138deSEric Dumazet ip = &i->next_rcu) { 13961da177e4SLinus Torvalds if (i->multiaddr == addr) { 13971da177e4SLinus Torvalds if (--i->users == 0) { 1398e9897071SEric Dumazet ip_mc_hash_remove(in_dev, i); 13991d7138deSEric Dumazet *ip = i->next_rcu; 1400b8bae41eSRami Rosen in_dev->mc_count--; 14011da177e4SLinus Torvalds igmp_group_dropped(i); 140224cf3af3SVeaceslav Falico ip_mc_clear_src(i); 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds if (!in_dev->dead) 14051da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds ip_ma_put(i); 14081da177e4SLinus Torvalds return; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds break; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds } 14144bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_dec_group); 14151da177e4SLinus Torvalds 141675c78500SMoni Shoua /* Device changing type */ 141775c78500SMoni Shoua 141875c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 141975c78500SMoni Shoua { 14201d7138deSEric Dumazet struct ip_mc_list *pmc; 142175c78500SMoni Shoua 142275c78500SMoni Shoua ASSERT_RTNL(); 142375c78500SMoni Shoua 14241d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14251d7138deSEric Dumazet igmp_group_dropped(pmc); 142675c78500SMoni Shoua } 142775c78500SMoni Shoua 142875c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 142975c78500SMoni Shoua { 14301d7138deSEric Dumazet struct ip_mc_list *pmc; 143175c78500SMoni Shoua 143275c78500SMoni Shoua ASSERT_RTNL(); 143375c78500SMoni Shoua 14341d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14351d7138deSEric Dumazet igmp_group_added(pmc); 143675c78500SMoni Shoua } 143775c78500SMoni Shoua 14381da177e4SLinus Torvalds /* Device going down */ 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 14411da177e4SLinus Torvalds { 14421d7138deSEric Dumazet struct ip_mc_list *pmc; 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds ASSERT_RTNL(); 14451da177e4SLinus Torvalds 14461d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14471d7138deSEric Dumazet igmp_group_dropped(pmc); 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 14501da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 14511da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 14521da177e4SLinus Torvalds __in_dev_put(in_dev); 14531da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 14541da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 14551da177e4SLinus Torvalds __in_dev_put(in_dev); 14561da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 14571da177e4SLinus Torvalds #endif 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 14631da177e4SLinus Torvalds { 14641da177e4SLinus Torvalds ASSERT_RTNL(); 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1467b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 1468b24b8a24SPavel Emelyanov (unsigned long)in_dev); 1469b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 1470b24b8a24SPavel Emelyanov (unsigned long)in_dev); 14711da177e4SLinus Torvalds in_dev->mr_qrv = IGMP_Unsolicited_Report_Count; 14721da177e4SLinus Torvalds #endif 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 14751da177e4SLinus Torvalds } 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds /* Device going up */ 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 14801da177e4SLinus Torvalds { 14811d7138deSEric Dumazet struct ip_mc_list *pmc; 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds ASSERT_RTNL(); 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 14861da177e4SLinus Torvalds 14871d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14881d7138deSEric Dumazet igmp_group_added(pmc); 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds /* 14921da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 14931da177e4SLinus Torvalds */ 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 14961da177e4SLinus Torvalds { 14971da177e4SLinus Torvalds struct ip_mc_list *i; 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds ASSERT_RTNL(); 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds /* Deactivate timers */ 15021da177e4SLinus Torvalds ip_mc_down(in_dev); 15031da177e4SLinus Torvalds 15041d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { 15051d7138deSEric Dumazet in_dev->mc_list = i->next_rcu; 1506b8bae41eSRami Rosen in_dev->mc_count--; 15071d7138deSEric Dumazet 150824cf3af3SVeaceslav Falico /* We've dropped the groups in ip_mc_down already */ 150924cf3af3SVeaceslav Falico ip_mc_clear_src(i); 15101da177e4SLinus Torvalds ip_ma_put(i); 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15149e917dcaSEric Dumazet /* RTNL is locked */ 1515877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 15161da177e4SLinus Torvalds { 15171da177e4SLinus Torvalds struct net_device *dev = NULL; 15181da177e4SLinus Torvalds struct in_device *idev = NULL; 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds if (imr->imr_ifindex) { 1521877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 15221da177e4SLinus Torvalds return idev; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 15259e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false); 15261da177e4SLinus Torvalds if (!dev) 15271da177e4SLinus Torvalds return NULL; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 1530b23dd4feSDavid S. Miller if (!dev) { 153178fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, 153278fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr, 153378fbfd8aSDavid S. Miller 0, 0, 0); 1534b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1535d8d1f30bSChangli Gao dev = rt->dst.dev; 15361da177e4SLinus Torvalds ip_rt_put(rt); 15371da177e4SLinus Torvalds } 1538b23dd4feSDavid S. Miller } 15391da177e4SLinus Torvalds if (dev) { 15401da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1541e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds return idev; 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds /* 15471da177e4SLinus Torvalds * Join a socket to a group 15481da177e4SLinus Torvalds */ 1549ab32ea5dSBrian Haley int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS; 1550ab32ea5dSBrian Haley int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 15548f935bbdSAl Viro __be32 *psfsrc) 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 15571da177e4SLinus Torvalds int rv = 0; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds psf_prev = NULL; 15601da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 15611da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 15621da177e4SLinus Torvalds break; 15631da177e4SLinus Torvalds psf_prev = psf; 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 15661da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 15671da177e4SLinus Torvalds return -ESRCH; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds psf->sf_count[sfmode]--; 15701da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 15711da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 15741da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15751da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 15761da177e4SLinus Torvalds #endif 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds /* no more filters for this source */ 15791da177e4SLinus Torvalds if (psf_prev) 15801da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 15811da177e4SLinus Torvalds else 15821da177e4SLinus Torvalds pmc->sources = psf->sf_next; 15831da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15841da177e4SLinus Torvalds if (psf->sf_oldin && 15851da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 15861da177e4SLinus Torvalds psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 15871da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 15881da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 15891da177e4SLinus Torvalds pmc->tomb = psf; 15901da177e4SLinus Torvalds rv = 1; 15911da177e4SLinus Torvalds } else 15921da177e4SLinus Torvalds #endif 15931da177e4SLinus Torvalds kfree(psf); 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds return rv; 15961da177e4SLinus Torvalds } 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 15991da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 16001da177e4SLinus Torvalds #endif 16011da177e4SLinus Torvalds 16028f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 16038f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 16041da177e4SLinus Torvalds { 16051da177e4SLinus Torvalds struct ip_mc_list *pmc; 16061da177e4SLinus Torvalds int changerec = 0; 16071da177e4SLinus Torvalds int i, err; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds if (!in_dev) 16101da177e4SLinus Torvalds return -ENODEV; 16111d7138deSEric Dumazet rcu_read_lock(); 16121d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 16131da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 16141da177e4SLinus Torvalds break; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds if (!pmc) { 16171da177e4SLinus Torvalds /* MCA not found?? bug */ 16181d7138deSEric Dumazet rcu_read_unlock(); 16191da177e4SLinus Torvalds return -ESRCH; 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 16221d7138deSEric Dumazet rcu_read_unlock(); 16231da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16241da177e4SLinus Torvalds sf_markstate(pmc); 16251da177e4SLinus Torvalds #endif 16261da177e4SLinus Torvalds if (!delta) { 16271da177e4SLinus Torvalds err = -EINVAL; 16281da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 16291da177e4SLinus Torvalds goto out_unlock; 16301da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds err = 0; 16331da177e4SLinus Torvalds for (i=0; i<sfcount; i++) { 16341da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds changerec |= rv > 0; 16371da177e4SLinus Torvalds if (!err && rv < 0) 16381da177e4SLinus Torvalds err = rv; 16391da177e4SLinus Torvalds } 16401da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 16411da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 16421da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 16431da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16441da177e4SLinus Torvalds struct ip_sf_list *psf; 16451da177e4SLinus Torvalds #endif 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds /* filter mode change */ 16481da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 16491da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16501da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 16511da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 16521da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 16531da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf = psf->sf_next) 16541da177e4SLinus Torvalds psf->sf_crcount = 0; 16551da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 16561da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 16571da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 16581da177e4SLinus Torvalds #endif 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds out_unlock: 16611da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 16621da177e4SLinus Torvalds return err; 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds /* 16661da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 16671da177e4SLinus Torvalds */ 16681da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 16695eb81e89SJun Zhao __be32 *psfsrc) 16701da177e4SLinus Torvalds { 16711da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds psf_prev = NULL; 16741da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 16751da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 16761da177e4SLinus Torvalds break; 16771da177e4SLinus Torvalds psf_prev = psf; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds if (!psf) { 16800da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 16811da177e4SLinus Torvalds if (!psf) 16821da177e4SLinus Torvalds return -ENOBUFS; 16831da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 16841da177e4SLinus Torvalds if (psf_prev) { 16851da177e4SLinus Torvalds psf_prev->sf_next = psf; 16861da177e4SLinus Torvalds } else 16871da177e4SLinus Torvalds pmc->sources = psf; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds psf->sf_count[sfmode]++; 16901da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 16911da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds return 0; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16971da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 16981da177e4SLinus Torvalds { 16991da177e4SLinus Torvalds struct ip_sf_list *psf; 17001da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) 17031da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 17041da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 17051da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 17061da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 17071da177e4SLinus Torvalds } else 17081da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 17121da177e4SLinus Torvalds { 1713ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 17141da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 17151da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 17161da177e4SLinus Torvalds int new_in, rv; 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds rv = 0; 17191da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 17201da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 17211da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 17221da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 17231da177e4SLinus Torvalds } else 17241da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 1725ad12583fSDavid L Stevens if (new_in) { 1726ad12583fSDavid L Stevens if (!psf->sf_oldin) { 172776edc605SAl Viro struct ip_sf_list *prev = NULL; 1728ad12583fSDavid L Stevens 1729ad12583fSDavid L Stevens for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) { 1730ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1731ad12583fSDavid L Stevens break; 1732ad12583fSDavid L Stevens prev = dpsf; 1733ad12583fSDavid L Stevens } 1734ad12583fSDavid L Stevens if (dpsf) { 1735ad12583fSDavid L Stevens if (prev) 1736ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 1737ad12583fSDavid L Stevens else 1738ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 1739ad12583fSDavid L Stevens kfree(dpsf); 1740ad12583fSDavid L Stevens } 17411da177e4SLinus Torvalds psf->sf_crcount = qrv; 17421da177e4SLinus Torvalds rv++; 17431da177e4SLinus Torvalds } 1744ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 1745ad12583fSDavid L Stevens 1746ad12583fSDavid L Stevens psf->sf_crcount = 0; 1747ad12583fSDavid L Stevens /* 1748ad12583fSDavid L Stevens * add or update "delete" records if an active filter 1749ad12583fSDavid L Stevens * is now inactive 1750ad12583fSDavid L Stevens */ 1751ad12583fSDavid L Stevens for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) 1752ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1753ad12583fSDavid L Stevens break; 1754ad12583fSDavid L Stevens if (!dpsf) { 17553ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); 1756ad12583fSDavid L Stevens if (!dpsf) 1757ad12583fSDavid L Stevens continue; 1758ad12583fSDavid L Stevens *dpsf = *psf; 1759ad12583fSDavid L Stevens /* pmc->lock held by callers */ 1760ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 1761ad12583fSDavid L Stevens pmc->tomb = dpsf; 1762ad12583fSDavid L Stevens } 1763ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 1764ad12583fSDavid L Stevens rv++; 1765ad12583fSDavid L Stevens } 17661da177e4SLinus Torvalds } 17671da177e4SLinus Torvalds return rv; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds #endif 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /* 17721da177e4SLinus Torvalds * Add multicast source filter list to the interface list 17731da177e4SLinus Torvalds */ 17748f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 17758f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 17761da177e4SLinus Torvalds { 17771da177e4SLinus Torvalds struct ip_mc_list *pmc; 17781da177e4SLinus Torvalds int isexclude; 17791da177e4SLinus Torvalds int i, err; 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds if (!in_dev) 17821da177e4SLinus Torvalds return -ENODEV; 17831d7138deSEric Dumazet rcu_read_lock(); 17841d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 17851da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 17861da177e4SLinus Torvalds break; 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds if (!pmc) { 17891da177e4SLinus Torvalds /* MCA not found?? bug */ 17901d7138deSEric Dumazet rcu_read_unlock(); 17911da177e4SLinus Torvalds return -ESRCH; 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 17941d7138deSEric Dumazet rcu_read_unlock(); 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17971da177e4SLinus Torvalds sf_markstate(pmc); 17981da177e4SLinus Torvalds #endif 17991da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 18001da177e4SLinus Torvalds if (!delta) 18011da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 18021da177e4SLinus Torvalds err = 0; 18031da177e4SLinus Torvalds for (i=0; i<sfcount; i++) { 18045eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); 18051da177e4SLinus Torvalds if (err) 18061da177e4SLinus Torvalds break; 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds if (err) { 18091da177e4SLinus Torvalds int j; 18101da177e4SLinus Torvalds 1811685f94e6SJun Zhao if (!delta) 18121da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 18131da177e4SLinus Torvalds for (j=0; j<i; j++) 1814a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); 18151da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 18161da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18171da177e4SLinus Torvalds struct ip_sf_list *psf; 1818cfcabdccSStephen Hemminger in_dev = pmc->interface; 18191da177e4SLinus Torvalds #endif 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds /* filter mode change */ 18221da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 18231da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 18241da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 18251da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 18261da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 18271da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 18301da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 18311da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 18321da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf = psf->sf_next) 18331da177e4SLinus Torvalds psf->sf_crcount = 0; 18341da177e4SLinus Torvalds igmp_ifc_event(in_dev); 18351da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 18361da177e4SLinus Torvalds igmp_ifc_event(in_dev); 18371da177e4SLinus Torvalds #endif 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 18401da177e4SLinus Torvalds return err; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 18441da177e4SLinus Torvalds { 18451da177e4SLinus Torvalds struct ip_sf_list *psf, *nextpsf; 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds for (psf=pmc->tomb; psf; psf=nextpsf) { 18481da177e4SLinus Torvalds nextpsf = psf->sf_next; 18491da177e4SLinus Torvalds kfree(psf); 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds pmc->tomb = NULL; 18521da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=nextpsf) { 18531da177e4SLinus Torvalds nextpsf = psf->sf_next; 18541da177e4SLinus Torvalds kfree(psf); 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds pmc->sources = NULL; 18571da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 1858de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 18591da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds /* 18641da177e4SLinus Torvalds * Join a multicast group 18651da177e4SLinus Torvalds */ 18661da177e4SLinus Torvalds int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr) 18671da177e4SLinus Torvalds { 18681da177e4SLinus Torvalds int err; 18698f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 1870ca9b907dSDavid L Stevens struct ip_mc_socklist *iml = NULL, *i; 18711da177e4SLinus Torvalds struct in_device *in_dev; 18721da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1873877acedcSDaniel Lezcano struct net *net = sock_net(sk); 1874ca9b907dSDavid L Stevens int ifindex; 18751da177e4SLinus Torvalds int count = 0; 18761da177e4SLinus Torvalds 1877f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 18781da177e4SLinus Torvalds return -EINVAL; 18791da177e4SLinus Torvalds 18806756ae4bSStephen Hemminger rtnl_lock(); 18811da177e4SLinus Torvalds 1882877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds if (!in_dev) { 18851da177e4SLinus Torvalds iml = NULL; 18861da177e4SLinus Torvalds err = -ENODEV; 18871da177e4SLinus Torvalds goto done; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds err = -EADDRINUSE; 1891ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 18921d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 1893ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 1894ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 18951da177e4SLinus Torvalds goto done; 18961da177e4SLinus Torvalds count++; 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds err = -ENOBUFS; 1899ca9b907dSDavid L Stevens if (count >= sysctl_igmp_max_memberships) 19001da177e4SLinus Torvalds goto done; 19018b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 1902ca9b907dSDavid L Stevens if (iml == NULL) 1903ca9b907dSDavid L Stevens goto done; 1904ca9b907dSDavid L Stevens 19051da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 19061d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 19071da177e4SLinus Torvalds iml->sflist = NULL; 19081da177e4SLinus Torvalds iml->sfmode = MCAST_EXCLUDE; 1909cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 19101da177e4SLinus Torvalds ip_mc_inc_group(in_dev, addr); 19111da177e4SLinus Torvalds err = 0; 19121da177e4SLinus Torvalds done: 19136756ae4bSStephen Hemminger rtnl_unlock(); 19141da177e4SLinus Torvalds return err; 19151da177e4SLinus Torvalds } 19164bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 19191da177e4SLinus Torvalds struct in_device *in_dev) 19201da177e4SLinus Torvalds { 19211d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 19221da177e4SLinus Torvalds int err; 19231da177e4SLinus Torvalds 1924c85bb41eSFlavio Leitner if (psf == NULL) { 19251da177e4SLinus Torvalds /* any-source empty exclude case */ 19261da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 19271da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 1930c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 1931a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 1932c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 1933c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); 19347519cce4SLai Jiangshan kfree_rcu(psf, rcu); 19351da177e4SLinus Torvalds return err; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds /* 19391da177e4SLinus Torvalds * Ask a socket to leave a group. 19401da177e4SLinus Torvalds */ 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 19431da177e4SLinus Torvalds { 19441da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19451d7138deSEric Dumazet struct ip_mc_socklist *iml; 19461d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 194784b42baeSDavid L Stevens struct in_device *in_dev; 1948877acedcSDaniel Lezcano struct net *net = sock_net(sk); 19498f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 195084b42baeSDavid L Stevens u32 ifindex; 1951acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds rtnl_lock(); 1954877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 195584b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 19561d7138deSEric Dumazet for (imlp = &inet->mc_list; 19571d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 19581d7138deSEric Dumazet imlp = &iml->next_rcu) { 1959acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 1960acd6e00bSDavid L Stevens continue; 1961acd6e00bSDavid L Stevens if (ifindex) { 1962acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 1963acd6e00bSDavid L Stevens continue; 1964acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 1965acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 1966acd6e00bSDavid L Stevens continue; 1967acd6e00bSDavid L Stevens 19681da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 19691da177e4SLinus Torvalds 19701d7138deSEric Dumazet *imlp = iml->next_rcu; 19711da177e4SLinus Torvalds 1972acd6e00bSDavid L Stevens if (in_dev) 197384b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 19741da177e4SLinus Torvalds rtnl_unlock(); 1975c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 1976c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 197710d50e74SLai Jiangshan kfree_rcu(iml, rcu); 19781da177e4SLinus Torvalds return 0; 19791da177e4SLinus Torvalds } 1980acd6e00bSDavid L Stevens if (!in_dev) 1981acd6e00bSDavid L Stevens ret = -ENODEV; 19821da177e4SLinus Torvalds rtnl_unlock(); 1983acd6e00bSDavid L Stevens return ret; 19841da177e4SLinus Torvalds } 1985193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 19881da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 19891da177e4SLinus Torvalds { 19901da177e4SLinus Torvalds int err; 19911da177e4SLinus Torvalds struct ip_mreqn imr; 199263007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 19931da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 19941da177e4SLinus Torvalds struct in_device *in_dev = NULL; 19951da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19961da177e4SLinus Torvalds struct ip_sf_socklist *psl; 1997877acedcSDaniel Lezcano struct net *net = sock_net(sk); 19988cdaaa15SDavid L Stevens int leavegroup = 0; 19991da177e4SLinus Torvalds int i, j, rv; 20001da177e4SLinus Torvalds 2001f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 20021da177e4SLinus Torvalds return -EINVAL; 20031da177e4SLinus Torvalds 20046756ae4bSStephen Hemminger rtnl_lock(); 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 20071da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 20081da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2009877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds if (!in_dev) { 20121da177e4SLinus Torvalds err = -ENODEV; 20131da177e4SLinus Torvalds goto done; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 20161da177e4SLinus Torvalds 20171d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 2018f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 2019f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 2020f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 20211da177e4SLinus Torvalds break; 20221da177e4SLinus Torvalds } 2023917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2024917f2f10SDavid L Stevens err = -EINVAL; 20251da177e4SLinus Torvalds goto done; 2026917f2f10SDavid L Stevens } 20271da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 20281da177e4SLinus Torvalds if (pmc->sflist) { 2029917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 2030917f2f10SDavid L Stevens err = -EINVAL; 20311da177e4SLinus Torvalds goto done; 2032917f2f10SDavid L Stevens } 20331da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 20341da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 20351da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 20361da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 20371da177e4SLinus Torvalds NULL, 0); 20381da177e4SLinus Torvalds pmc->sfmode = omode; 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds 20411d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 20421da177e4SLinus Torvalds if (!add) { 20431da177e4SLinus Torvalds if (!psl) 2044917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 20451da177e4SLinus Torvalds rv = !0; 20461da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 20471da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 204863007727SAl Viro sizeof(__be32)); 20491da177e4SLinus Torvalds if (rv == 0) 20501da177e4SLinus Torvalds break; 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds if (rv) /* source not found */ 2053917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 20541da177e4SLinus Torvalds 20558cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 20568cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 20578cdaaa15SDavid L Stevens leavegroup = 1; 20588cdaaa15SDavid L Stevens goto done; 20598cdaaa15SDavid L Stevens } 20608cdaaa15SDavid L Stevens 20611da177e4SLinus Torvalds /* update the interface filter */ 20621da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 20631da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds for (j=i+1; j<psl->sl_count; j++) 20661da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 20671da177e4SLinus Torvalds psl->sl_count--; 20681da177e4SLinus Torvalds err = 0; 20691da177e4SLinus Torvalds goto done; 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds /* else, add a new source to the filter */ 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds if (psl && psl->sl_count >= sysctl_igmp_max_msf) { 20741da177e4SLinus Torvalds err = -ENOBUFS; 20751da177e4SLinus Torvalds goto done; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 20781da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 20791da177e4SLinus Torvalds int count = IP_SFBLOCK; 20801da177e4SLinus Torvalds 20811da177e4SLinus Torvalds if (psl) 20821da177e4SLinus Torvalds count += psl->sl_max; 20838b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 20841da177e4SLinus Torvalds if (!newpsl) { 20851da177e4SLinus Torvalds err = -ENOBUFS; 20861da177e4SLinus Torvalds goto done; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds newpsl->sl_max = count; 20891da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 20901da177e4SLinus Torvalds if (psl) { 20911da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) 20921da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2093c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2094c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 20957519cce4SLai Jiangshan kfree_rcu(psl, rcu); 20961da177e4SLinus Torvalds } 2097cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2098c85bb41eSFlavio Leitner psl = newpsl; 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 21011da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 21021da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 210363007727SAl Viro sizeof(__be32)); 21041da177e4SLinus Torvalds if (rv == 0) 21051da177e4SLinus Torvalds break; 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 21081da177e4SLinus Torvalds goto done; 21091da177e4SLinus Torvalds for (j=psl->sl_count-1; j>=i; j--) 21101da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 21111da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 21121da177e4SLinus Torvalds psl->sl_count++; 21131da177e4SLinus Torvalds err = 0; 21141da177e4SLinus Torvalds /* update the interface list */ 21151da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 21161da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 21171da177e4SLinus Torvalds done: 21186756ae4bSStephen Hemminger rtnl_unlock(); 21198cdaaa15SDavid L Stevens if (leavegroup) 21208cdaaa15SDavid L Stevens return ip_mc_leave_group(sk, &imr); 21211da177e4SLinus Torvalds return err; 21221da177e4SLinus Torvalds } 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 21251da177e4SLinus Torvalds { 21269951f036SDavid L Stevens int err = 0; 21271da177e4SLinus Torvalds struct ip_mreqn imr; 212863007727SAl Viro __be32 addr = msf->imsf_multiaddr; 21291da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 21301da177e4SLinus Torvalds struct in_device *in_dev; 21311da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21321da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2133877acedcSDaniel Lezcano struct net *net = sock_net(sk); 21349951f036SDavid L Stevens int leavegroup = 0; 21351da177e4SLinus Torvalds 2136f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21371da177e4SLinus Torvalds return -EINVAL; 21381da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 21391da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 21401da177e4SLinus Torvalds return -EINVAL; 21411da177e4SLinus Torvalds 21426756ae4bSStephen Hemminger rtnl_lock(); 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 21451da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 21461da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2147877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds if (!in_dev) { 21501da177e4SLinus Torvalds err = -ENODEV; 21511da177e4SLinus Torvalds goto done; 21521da177e4SLinus Torvalds } 21531da177e4SLinus Torvalds 21549951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 21559951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 21569951f036SDavid L Stevens leavegroup = 1; 21579951f036SDavid L Stevens goto done; 21589951f036SDavid L Stevens } 21599951f036SDavid L Stevens 21601d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 21611da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 21621da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 21631da177e4SLinus Torvalds break; 21641da177e4SLinus Torvalds } 2165917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2166917f2f10SDavid L Stevens err = -EINVAL; 21671da177e4SLinus Torvalds goto done; 2168917f2f10SDavid L Stevens } 21691da177e4SLinus Torvalds if (msf->imsf_numsrc) { 21708b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 21718b3a7005SKris Katterjohn GFP_KERNEL); 21721da177e4SLinus Torvalds if (!newpsl) { 21731da177e4SLinus Torvalds err = -ENOBUFS; 21741da177e4SLinus Torvalds goto done; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 21771da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 21781da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 21791da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 21801da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 21811da177e4SLinus Torvalds if (err) { 21821da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 21831da177e4SLinus Torvalds goto done; 21841da177e4SLinus Torvalds } 21858713dbf0SYan Zheng } else { 21861da177e4SLinus Torvalds newpsl = NULL; 21878713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 21888713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 21898713dbf0SYan Zheng } 21901d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 21911da177e4SLinus Torvalds if (psl) { 21921da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 21931da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2194c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2195c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 21967519cce4SLai Jiangshan kfree_rcu(psl, rcu); 21971da177e4SLinus Torvalds } else 21981da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 21991da177e4SLinus Torvalds 0, NULL, 0); 2200cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 22011da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2202917f2f10SDavid L Stevens err = 0; 22031da177e4SLinus Torvalds done: 22046756ae4bSStephen Hemminger rtnl_unlock(); 22059951f036SDavid L Stevens if (leavegroup) 22069951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 22071da177e4SLinus Torvalds return err; 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 22111da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 22121da177e4SLinus Torvalds { 22131da177e4SLinus Torvalds int err, len, count, copycount; 22141da177e4SLinus Torvalds struct ip_mreqn imr; 221563007727SAl Viro __be32 addr = msf->imsf_multiaddr; 22161da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22171da177e4SLinus Torvalds struct in_device *in_dev; 22181da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22191da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2220877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22211da177e4SLinus Torvalds 2222f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 22231da177e4SLinus Torvalds return -EINVAL; 22241da177e4SLinus Torvalds 22256756ae4bSStephen Hemminger rtnl_lock(); 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 22281da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 22291da177e4SLinus Torvalds imr.imr_ifindex = 0; 2230877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds if (!in_dev) { 22331da177e4SLinus Torvalds err = -ENODEV; 22341da177e4SLinus Torvalds goto done; 22351da177e4SLinus Torvalds } 22361da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 22371da177e4SLinus Torvalds 22381d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 22391da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 22401da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 22411da177e4SLinus Torvalds break; 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 22441da177e4SLinus Torvalds goto done; 22451da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 22461d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 22476756ae4bSStephen Hemminger rtnl_unlock(); 22481da177e4SLinus Torvalds if (!psl) { 22491da177e4SLinus Torvalds len = 0; 22501da177e4SLinus Torvalds count = 0; 22511da177e4SLinus Torvalds } else { 22521da177e4SLinus Torvalds count = psl->sl_count; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 22551da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 22561da177e4SLinus Torvalds msf->imsf_numsrc = count; 22571da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 22581da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 22591da177e4SLinus Torvalds return -EFAULT; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds if (len && 22621da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 22631da177e4SLinus Torvalds return -EFAULT; 22641da177e4SLinus Torvalds return 0; 22651da177e4SLinus Torvalds done: 22666756ae4bSStephen Hemminger rtnl_unlock(); 22671da177e4SLinus Torvalds return err; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 22711da177e4SLinus Torvalds struct group_filter __user *optval, int __user *optlen) 22721da177e4SLinus Torvalds { 22731da177e4SLinus Torvalds int err, i, count, copycount; 22741da177e4SLinus Torvalds struct sockaddr_in *psin; 227563007727SAl Viro __be32 addr; 22761da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22771da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22781da177e4SLinus Torvalds struct ip_sf_socklist *psl; 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 22811da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 22821da177e4SLinus Torvalds return -EINVAL; 22831da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2284f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 22851da177e4SLinus Torvalds return -EINVAL; 22861da177e4SLinus Torvalds 22876756ae4bSStephen Hemminger rtnl_lock(); 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 22901da177e4SLinus Torvalds 22911d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 22921da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 22931da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 22941da177e4SLinus Torvalds break; 22951da177e4SLinus Torvalds } 22961da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 22971da177e4SLinus Torvalds goto done; 22981da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 22991d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 23006756ae4bSStephen Hemminger rtnl_unlock(); 23011da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 23021da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 23031da177e4SLinus Torvalds gsf->gf_numsrc = count; 23041da177e4SLinus Torvalds if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 23051da177e4SLinus Torvalds copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 23061da177e4SLinus Torvalds return -EFAULT; 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds for (i=0; i<copycount; i++) { 23091da177e4SLinus Torvalds struct sockaddr_storage ss; 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 23121da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 23131da177e4SLinus Torvalds psin->sin_family = AF_INET; 23141da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 23151da177e4SLinus Torvalds if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 23161da177e4SLinus Torvalds return -EFAULT; 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds return 0; 23191da177e4SLinus Torvalds done: 23206756ae4bSStephen Hemminger rtnl_unlock(); 23211da177e4SLinus Torvalds return err; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds /* 23251da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 23261da177e4SLinus Torvalds */ 2327c0cda068SAl Viro int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif) 23281da177e4SLinus Torvalds { 23291da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23301da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 23311da177e4SLinus Torvalds struct ip_sf_socklist *psl; 23321da177e4SLinus Torvalds int i; 2333c85bb41eSFlavio Leitner int ret; 23341da177e4SLinus Torvalds 2335c85bb41eSFlavio Leitner ret = 1; 2336f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2337c85bb41eSFlavio Leitner goto out; 23381da177e4SLinus Torvalds 2339c85bb41eSFlavio Leitner rcu_read_lock(); 23401d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 23411da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 23421da177e4SLinus Torvalds pmc->multi.imr_ifindex == dif) 23431da177e4SLinus Torvalds break; 23441da177e4SLinus Torvalds } 2345c85bb41eSFlavio Leitner ret = inet->mc_all; 23461da177e4SLinus Torvalds if (!pmc) 2347c85bb41eSFlavio Leitner goto unlock; 23481d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2349c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 23501da177e4SLinus Torvalds if (!psl) 2351c85bb41eSFlavio Leitner goto unlock; 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 23541da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 23551da177e4SLinus Torvalds break; 23561da177e4SLinus Torvalds } 2357c85bb41eSFlavio Leitner ret = 0; 23581da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2359c85bb41eSFlavio Leitner goto unlock; 23601da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2361c85bb41eSFlavio Leitner goto unlock; 2362c85bb41eSFlavio Leitner ret = 1; 2363c85bb41eSFlavio Leitner unlock: 2364c85bb41eSFlavio Leitner rcu_read_unlock(); 2365c85bb41eSFlavio Leitner out: 2366c85bb41eSFlavio Leitner return ret; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds /* 23701da177e4SLinus Torvalds * A socket is closing. 23711da177e4SLinus Torvalds */ 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 23741da177e4SLinus Torvalds { 23751da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23761da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2377877acedcSDaniel Lezcano struct net *net = sock_net(sk); 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds if (inet->mc_list == NULL) 23801da177e4SLinus Torvalds return; 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds rtnl_lock(); 23831d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 23841da177e4SLinus Torvalds struct in_device *in_dev; 23851da177e4SLinus Torvalds 23861d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2387877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 23881da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 238918943d29SEric Dumazet if (in_dev != NULL) 23901da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2391c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2392c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 239310d50e74SLai Jiangshan kfree_rcu(iml, rcu); 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds rtnl_unlock(); 23961da177e4SLinus Torvalds } 23971da177e4SLinus Torvalds 2398dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 2399dbdd9a52SDavid S. Miller int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto) 24001da177e4SLinus Torvalds { 24011da177e4SLinus Torvalds struct ip_mc_list *im; 2402e9897071SEric Dumazet struct ip_mc_list __rcu **mc_hash; 24031da177e4SLinus Torvalds struct ip_sf_list *psf; 24041da177e4SLinus Torvalds int rv = 0; 24051da177e4SLinus Torvalds 2406e9897071SEric Dumazet mc_hash = rcu_dereference(in_dev->mc_hash); 2407e9897071SEric Dumazet if (mc_hash) { 2408c70eba74SEric Dumazet u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); 2409e9897071SEric Dumazet 2410e9897071SEric Dumazet for (im = rcu_dereference(mc_hash[hash]); 2411e9897071SEric Dumazet im != NULL; 2412e9897071SEric Dumazet im = rcu_dereference(im->next_hash)) { 2413e9897071SEric Dumazet if (im->multiaddr == mc_addr) 2414e9897071SEric Dumazet break; 2415e9897071SEric Dumazet } 2416e9897071SEric Dumazet } else { 24171d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 24181da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 24191da177e4SLinus Torvalds break; 24201da177e4SLinus Torvalds } 2421e9897071SEric Dumazet } 24221da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 24231da177e4SLinus Torvalds rv = 1; 24241da177e4SLinus Torvalds } else if (im) { 24251da177e4SLinus Torvalds if (src_addr) { 24261da177e4SLinus Torvalds for (psf=im->sources; psf; psf=psf->sf_next) { 24271da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 24281da177e4SLinus Torvalds break; 24291da177e4SLinus Torvalds } 24301da177e4SLinus Torvalds if (psf) 24311da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 24321da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 24331da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 24341da177e4SLinus Torvalds else 24351da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 24361da177e4SLinus Torvalds } else 24371da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds return rv; 24401da177e4SLinus Torvalds } 24411da177e4SLinus Torvalds 24421da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 24431da177e4SLinus Torvalds struct igmp_mc_iter_state { 24447091e728SAlexey Dobriyan struct seq_net_private p; 24451da177e4SLinus Torvalds struct net_device *dev; 24461da177e4SLinus Torvalds struct in_device *in_dev; 24471da177e4SLinus Torvalds }; 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 24521da177e4SLinus Torvalds { 24537091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 24541da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 24551da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24561da177e4SLinus Torvalds 24577562f876SPavel Emelianov state->in_dev = NULL; 245861fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 24591da177e4SLinus Torvalds struct in_device *in_dev; 24606baff150SEric Dumazet 24616baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 24621da177e4SLinus Torvalds if (!in_dev) 24631da177e4SLinus Torvalds continue; 24641d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 24651da177e4SLinus Torvalds if (im) { 24661da177e4SLinus Torvalds state->in_dev = in_dev; 24671da177e4SLinus Torvalds break; 24681da177e4SLinus Torvalds } 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds return im; 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds 24731da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 24741da177e4SLinus Torvalds { 24751da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24766baff150SEric Dumazet 24771d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 24781d7138deSEric Dumazet while (!im) { 24796baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 24801da177e4SLinus Torvalds if (!state->dev) { 24811da177e4SLinus Torvalds state->in_dev = NULL; 24821da177e4SLinus Torvalds break; 24831da177e4SLinus Torvalds } 24846baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 24851da177e4SLinus Torvalds if (!state->in_dev) 24861da177e4SLinus Torvalds continue; 24871d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds return im; 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 24931da177e4SLinus Torvalds { 24941da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 24951da177e4SLinus Torvalds if (im) 24961da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 24971da177e4SLinus Torvalds --pos; 24981da177e4SLinus Torvalds return pos ? NULL : im; 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 250261fbab77Sstephen hemminger __acquires(rcu) 25031da177e4SLinus Torvalds { 250461fbab77Sstephen hemminger rcu_read_lock(); 25051da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 25061da177e4SLinus Torvalds } 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25091da177e4SLinus Torvalds { 25101da177e4SLinus Torvalds struct ip_mc_list *im; 25111da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 25121da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 25131da177e4SLinus Torvalds else 25141da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 25151da177e4SLinus Torvalds ++*pos; 25161da177e4SLinus Torvalds return im; 25171da177e4SLinus Torvalds } 25181da177e4SLinus Torvalds 25191da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 252061fbab77Sstephen hemminger __releases(rcu) 25211da177e4SLinus Torvalds { 25221da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 25231d7138deSEric Dumazet 25241da177e4SLinus Torvalds state->in_dev = NULL; 25251da177e4SLinus Torvalds state->dev = NULL; 252661fbab77Sstephen hemminger rcu_read_unlock(); 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 25301da177e4SLinus Torvalds { 25311da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 25321da177e4SLinus Torvalds seq_puts(seq, 25331da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 25341da177e4SLinus Torvalds else { 25351da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 25361da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 25371da177e4SLinus Torvalds char *querier; 2538a399a805SEric Dumazet long delta; 2539a399a805SEric Dumazet 25401da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 25411da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 25421da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 25431da177e4SLinus Torvalds "V3"; 25441da177e4SLinus Torvalds #else 25451da177e4SLinus Torvalds querier = "NONE"; 25461da177e4SLinus Torvalds #endif 25471da177e4SLinus Torvalds 25481d7138deSEric Dumazet if (rcu_dereference(state->in_dev->mc_list) == im) { 25491da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2550b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds 2553a399a805SEric Dumazet delta = im->timer.expires - jiffies; 25541da177e4SLinus Torvalds seq_printf(seq, 2555338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 25561da177e4SLinus Torvalds im->multiaddr, im->users, 2557a399a805SEric Dumazet im->tm_running, 2558a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 25591da177e4SLinus Torvalds im->reporter); 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds return 0; 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds 2564f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 25651da177e4SLinus Torvalds .start = igmp_mc_seq_start, 25661da177e4SLinus Torvalds .next = igmp_mc_seq_next, 25671da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 25681da177e4SLinus Torvalds .show = igmp_mc_seq_show, 25691da177e4SLinus Torvalds }; 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file) 25721da177e4SLinus Torvalds { 25737091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mc_seq_ops, 2574cf7732e4SPavel Emelyanov sizeof(struct igmp_mc_iter_state)); 25751da177e4SLinus Torvalds } 25761da177e4SLinus Torvalds 25779a32144eSArjan van de Ven static const struct file_operations igmp_mc_seq_fops = { 25781da177e4SLinus Torvalds .owner = THIS_MODULE, 25791da177e4SLinus Torvalds .open = igmp_mc_seq_open, 25801da177e4SLinus Torvalds .read = seq_read, 25811da177e4SLinus Torvalds .llseek = seq_lseek, 25827091e728SAlexey Dobriyan .release = seq_release_net, 25831da177e4SLinus Torvalds }; 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds struct igmp_mcf_iter_state { 25867091e728SAlexey Dobriyan struct seq_net_private p; 25871da177e4SLinus Torvalds struct net_device *dev; 25881da177e4SLinus Torvalds struct in_device *idev; 25891da177e4SLinus Torvalds struct ip_mc_list *im; 25901da177e4SLinus Torvalds }; 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 25931da177e4SLinus Torvalds 25941da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 25951da177e4SLinus Torvalds { 25967091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 25971da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 25981da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 25991da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26001da177e4SLinus Torvalds 26017562f876SPavel Emelianov state->idev = NULL; 26027562f876SPavel Emelianov state->im = NULL; 260361fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 26041da177e4SLinus Torvalds struct in_device *idev; 26056baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 26061da177e4SLinus Torvalds if (unlikely(idev == NULL)) 26071da177e4SLinus Torvalds continue; 26081d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 26091da177e4SLinus Torvalds if (likely(im != NULL)) { 26101da177e4SLinus Torvalds spin_lock_bh(&im->lock); 26111da177e4SLinus Torvalds psf = im->sources; 26121da177e4SLinus Torvalds if (likely(psf != NULL)) { 26131da177e4SLinus Torvalds state->im = im; 26141da177e4SLinus Torvalds state->idev = idev; 26151da177e4SLinus Torvalds break; 26161da177e4SLinus Torvalds } 26171da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 26181da177e4SLinus Torvalds } 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds return psf; 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 26241da177e4SLinus Torvalds { 26251da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds psf = psf->sf_next; 26281da177e4SLinus Torvalds while (!psf) { 26291da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 26301da177e4SLinus Torvalds state->im = state->im->next; 26311da177e4SLinus Torvalds while (!state->im) { 26326baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 26331da177e4SLinus Torvalds if (!state->dev) { 26341da177e4SLinus Torvalds state->idev = NULL; 26351da177e4SLinus Torvalds goto out; 26361da177e4SLinus Torvalds } 26376baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 26381da177e4SLinus Torvalds if (!state->idev) 26391da177e4SLinus Torvalds continue; 26401d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds if (!state->im) 26431da177e4SLinus Torvalds break; 26441da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 26451da177e4SLinus Torvalds psf = state->im->sources; 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds out: 26481da177e4SLinus Torvalds return psf; 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 26521da177e4SLinus Torvalds { 26531da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 26541da177e4SLinus Torvalds if (psf) 26551da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 26561da177e4SLinus Torvalds --pos; 26571da177e4SLinus Torvalds return pos ? NULL : psf; 26581da177e4SLinus Torvalds } 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 266161fbab77Sstephen hemminger __acquires(rcu) 26621da177e4SLinus Torvalds { 266361fbab77Sstephen hemminger rcu_read_lock(); 26641da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26681da177e4SLinus Torvalds { 26691da177e4SLinus Torvalds struct ip_sf_list *psf; 26701da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 26711da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 26721da177e4SLinus Torvalds else 26731da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 26741da177e4SLinus Torvalds ++*pos; 26751da177e4SLinus Torvalds return psf; 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 267961fbab77Sstephen hemminger __releases(rcu) 26801da177e4SLinus Torvalds { 26811da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26821da177e4SLinus Torvalds if (likely(state->im != NULL)) { 26831da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 26841da177e4SLinus Torvalds state->im = NULL; 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds state->idev = NULL; 26871da177e4SLinus Torvalds state->dev = NULL; 268861fbab77Sstephen hemminger rcu_read_unlock(); 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 26921da177e4SLinus Torvalds { 26931da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 26941da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 26971da177e4SLinus Torvalds seq_printf(seq, 26981da177e4SLinus Torvalds "%3s %6s " 26991da177e4SLinus Torvalds "%10s %10s %6s %6s\n", "Idx", 27001da177e4SLinus Torvalds "Device", "MCA", 27011da177e4SLinus Torvalds "SRC", "INC", "EXC"); 27021da177e4SLinus Torvalds } else { 27031da177e4SLinus Torvalds seq_printf(seq, 27041da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 27051da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 27061da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 27071da177e4SLinus Torvalds ntohl(state->im->multiaddr), 27081da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 27091da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 27101da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds return 0; 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds 2715f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 27161da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 27171da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 27181da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 27191da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 27201da177e4SLinus Torvalds }; 27211da177e4SLinus Torvalds 27221da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file) 27231da177e4SLinus Torvalds { 27247091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mcf_seq_ops, 2725cf7732e4SPavel Emelyanov sizeof(struct igmp_mcf_iter_state)); 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 27289a32144eSArjan van de Ven static const struct file_operations igmp_mcf_seq_fops = { 27291da177e4SLinus Torvalds .owner = THIS_MODULE, 27301da177e4SLinus Torvalds .open = igmp_mcf_seq_open, 27311da177e4SLinus Torvalds .read = seq_read, 27321da177e4SLinus Torvalds .llseek = seq_lseek, 27337091e728SAlexey Dobriyan .release = seq_release_net, 27347091e728SAlexey Dobriyan }; 27357091e728SAlexey Dobriyan 27362c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 27377091e728SAlexey Dobriyan { 27387091e728SAlexey Dobriyan struct proc_dir_entry *pde; 27397091e728SAlexey Dobriyan 2740d4beaa66SGao feng pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops); 27417091e728SAlexey Dobriyan if (!pde) 27427091e728SAlexey Dobriyan goto out_igmp; 2743d4beaa66SGao feng pde = proc_create("mcfilter", S_IRUGO, net->proc_net, 2744d4beaa66SGao feng &igmp_mcf_seq_fops); 27457091e728SAlexey Dobriyan if (!pde) 27467091e728SAlexey Dobriyan goto out_mcfilter; 27477091e728SAlexey Dobriyan return 0; 27487091e728SAlexey Dobriyan 27497091e728SAlexey Dobriyan out_mcfilter: 2750ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 27517091e728SAlexey Dobriyan out_igmp: 27527091e728SAlexey Dobriyan return -ENOMEM; 27537091e728SAlexey Dobriyan } 27547091e728SAlexey Dobriyan 27552c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 27567091e728SAlexey Dobriyan { 2757ece31ffdSGao feng remove_proc_entry("mcfilter", net->proc_net); 2758ece31ffdSGao feng remove_proc_entry("igmp", net->proc_net); 27597091e728SAlexey Dobriyan } 27607091e728SAlexey Dobriyan 27617091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 27627091e728SAlexey Dobriyan .init = igmp_net_init, 27637091e728SAlexey Dobriyan .exit = igmp_net_exit, 27641da177e4SLinus Torvalds }; 27651da177e4SLinus Torvalds 27664aa5dee4SJiri Pirko static int igmp_netdev_event(struct notifier_block *this, 27674aa5dee4SJiri Pirko unsigned long event, void *ptr) 27684aa5dee4SJiri Pirko { 27694aa5dee4SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 27704aa5dee4SJiri Pirko struct in_device *in_dev; 27714aa5dee4SJiri Pirko 27724aa5dee4SJiri Pirko switch (event) { 27734aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 27744aa5dee4SJiri Pirko in_dev = __in_dev_get_rtnl(dev); 27754aa5dee4SJiri Pirko if (in_dev) 27764aa5dee4SJiri Pirko ip_mc_rejoin_groups(in_dev); 27774aa5dee4SJiri Pirko break; 27784aa5dee4SJiri Pirko default: 27794aa5dee4SJiri Pirko break; 27804aa5dee4SJiri Pirko } 27814aa5dee4SJiri Pirko return NOTIFY_DONE; 27824aa5dee4SJiri Pirko } 27834aa5dee4SJiri Pirko 27844aa5dee4SJiri Pirko static struct notifier_block igmp_notifier = { 27854aa5dee4SJiri Pirko .notifier_call = igmp_netdev_event, 27864aa5dee4SJiri Pirko }; 27874aa5dee4SJiri Pirko 27881da177e4SLinus Torvalds int __init igmp_mc_proc_init(void) 27891da177e4SLinus Torvalds { 27904aa5dee4SJiri Pirko int err; 27914aa5dee4SJiri Pirko 27924aa5dee4SJiri Pirko err = register_pernet_subsys(&igmp_net_ops); 27934aa5dee4SJiri Pirko if (err) 27944aa5dee4SJiri Pirko return err; 27954aa5dee4SJiri Pirko err = register_netdevice_notifier(&igmp_notifier); 27964aa5dee4SJiri Pirko if (err) 27974aa5dee4SJiri Pirko goto reg_notif_fail; 27984aa5dee4SJiri Pirko return 0; 27994aa5dee4SJiri Pirko 28004aa5dee4SJiri Pirko reg_notif_fail: 28014aa5dee4SJiri Pirko unregister_pernet_subsys(&igmp_net_ops); 28024aa5dee4SJiri Pirko return err; 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds #endif 2805