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> 9114c85021SArnaldo Carvalho de Melo 92457c4cbcSEric W. Biederman #include <net/net_namespace.h> 9314c85021SArnaldo Carvalho de Melo #include <net/arp.h> 941da177e4SLinus Torvalds #include <net/ip.h> 951da177e4SLinus Torvalds #include <net/protocol.h> 961da177e4SLinus Torvalds #include <net/route.h> 971da177e4SLinus Torvalds #include <net/sock.h> 981da177e4SLinus Torvalds #include <net/checksum.h> 991da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 1001da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1011da177e4SLinus Torvalds #include <linux/mroute.h> 1021da177e4SLinus Torvalds #endif 1031da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1041da177e4SLinus Torvalds #include <linux/proc_fs.h> 1051da177e4SLinus Torvalds #include <linux/seq_file.h> 1061da177e4SLinus Torvalds #endif 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds #define IP_MAX_MEMBERSHIPS 20 1091da177e4SLinus Torvalds #define IP_MAX_MSF 10 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1121da177e4SLinus Torvalds /* Parameter names and values are taken from igmp-v2-06 draft */ 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #define IGMP_V1_Router_Present_Timeout (400*HZ) 1151da177e4SLinus Torvalds #define IGMP_V2_Router_Present_Timeout (400*HZ) 1161da177e4SLinus Torvalds #define IGMP_Unsolicited_Report_Interval (10*HZ) 1171da177e4SLinus Torvalds #define IGMP_Query_Response_Interval (10*HZ) 1181da177e4SLinus Torvalds #define IGMP_Unsolicited_Report_Count 2 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds #define IGMP_Initial_Report_Delay (1) 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* IGMP_Initial_Report_Delay is not from IGMP specs! 1241da177e4SLinus Torvalds * IGMP specs require to report membership immediately after 1251da177e4SLinus Torvalds * joining a group, but we delay the first report by a 1261da177e4SLinus Torvalds * small interval. It seems more natural and still does not 1271da177e4SLinus Torvalds * contradict to specs provided this delay is small enough. 1281da177e4SLinus Torvalds */ 1291da177e4SLinus Torvalds 13042f811b8SHerbert Xu #define IGMP_V1_SEEN(in_dev) \ 131c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ 13242f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ 1331da177e4SLinus Torvalds ((in_dev)->mr_v1_seen && \ 1341da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v1_seen))) 13542f811b8SHerbert Xu #define IGMP_V2_SEEN(in_dev) \ 136c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ 13742f811b8SHerbert Xu IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ 1381da177e4SLinus Torvalds ((in_dev)->mr_v2_seen && \ 1391da177e4SLinus Torvalds time_before(jiffies, (in_dev)->mr_v2_seen))) 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im); 14263007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr); 1431da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev); 1441da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc); 1451da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc); 1461da177e4SLinus Torvalds #endif 1471da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc); 1488f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 1498f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta); 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static void ip_ma_put(struct ip_mc_list *im) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds if (atomic_dec_and_test(&im->refcnt)) { 1541da177e4SLinus Torvalds in_dev_put(im->interface); 15542ea299dSLai Jiangshan kfree_rcu(im, rcu); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 159d9aa9380SDavid S. Miller #define for_each_pmc_rcu(in_dev, pmc) \ 160d9aa9380SDavid S. Miller for (pmc = rcu_dereference(in_dev->mc_list); \ 161d9aa9380SDavid S. Miller pmc != NULL; \ 162d9aa9380SDavid S. Miller pmc = rcu_dereference(pmc->next_rcu)) 163d9aa9380SDavid S. Miller 164d9aa9380SDavid S. Miller #define for_each_pmc_rtnl(in_dev, pmc) \ 165d9aa9380SDavid S. Miller for (pmc = rtnl_dereference(in_dev->mc_list); \ 166d9aa9380SDavid S. Miller pmc != NULL; \ 167d9aa9380SDavid S. Miller pmc = rtnl_dereference(pmc->next_rcu)) 168d9aa9380SDavid S. Miller 1691da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds /* 1721da177e4SLinus Torvalds * Timer management 1731da177e4SLinus Torvalds */ 1741da177e4SLinus Torvalds 1751d7138deSEric Dumazet static void igmp_stop_timer(struct ip_mc_list *im) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds spin_lock_bh(&im->lock); 1781da177e4SLinus Torvalds if (del_timer(&im->timer)) 1791da177e4SLinus Torvalds atomic_dec(&im->refcnt); 1801da177e4SLinus Torvalds im->tm_running = 0; 1811da177e4SLinus Torvalds im->reporter = 0; 1821da177e4SLinus Torvalds im->unsolicit_count = 0; 1831da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds /* It must be called with locked im->lock */ 1871da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds int tv = net_random() % max_delay; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds im->tm_running = 1; 1921da177e4SLinus Torvalds if (!mod_timer(&im->timer, jiffies+tv+2)) 1931da177e4SLinus Torvalds atomic_inc(&im->refcnt); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds int tv = net_random() % in_dev->mr_maxdelay; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds in_dev->mr_gq_running = 1; 2011da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2)) 2021da177e4SLinus Torvalds in_dev_hold(in_dev); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds int tv = net_random() % delay; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) 2101da177e4SLinus Torvalds in_dev_hold(in_dev); 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2161da177e4SLinus Torvalds im->unsolicit_count = 0; 2171da177e4SLinus Torvalds if (del_timer(&im->timer)) { 2181da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) { 2191da177e4SLinus Torvalds add_timer(&im->timer); 2201da177e4SLinus Torvalds im->tm_running = 1; 2211da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2221da177e4SLinus Torvalds return; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds atomic_dec(&im->refcnt); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds igmp_start_timer(im, max_delay); 2271da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* 2321da177e4SLinus Torvalds * Send an IGMP report. 2331da177e4SLinus Torvalds */ 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, 2391da177e4SLinus Torvalds int gdeleted, int sdeleted) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds switch (type) { 2421da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE: 2431da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE: 2441da177e4SLinus Torvalds if (gdeleted || sdeleted) 2451da177e4SLinus Torvalds return 0; 246ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) { 247ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE) 248ad12583fSDavid L Stevens return 1; 249ad12583fSDavid L Stevens /* don't include if this source is excluded 250ad12583fSDavid L Stevens * in all filters 251ad12583fSDavid L Stevens */ 252ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE]) 253ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE; 254ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] == 255ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]; 256ad12583fSDavid L Stevens } 257ad12583fSDavid L Stevens return 0; 2581da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE: 2591da177e4SLinus Torvalds if (gdeleted || sdeleted) 2601da177e4SLinus Torvalds return 0; 2611da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0; 2621da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE: 2631da177e4SLinus Torvalds if (gdeleted || sdeleted) 2641da177e4SLinus Torvalds return 0; 2651da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 || 2661da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE]) 2671da177e4SLinus Torvalds return 0; 2681da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] == 2691da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]; 2701da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES: 2711da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount) 2721da177e4SLinus Torvalds return 0; 2731da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; 2741da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES: 2751da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) 2761da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted); 2771da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds return 0; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds static int 2831da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) 2841da177e4SLinus Torvalds { 2851da177e4SLinus Torvalds struct ip_sf_list *psf; 2861da177e4SLinus Torvalds int scount = 0; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 2891da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 2901da177e4SLinus Torvalds continue; 2911da177e4SLinus Torvalds scount++; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds return scount; 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 29657e1ab6eSEric Dumazet #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb)) 29757e1ab6eSEric Dumazet 2981da177e4SLinus Torvalds static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds struct sk_buff *skb; 3011da177e4SLinus Torvalds struct rtable *rt; 3021da177e4SLinus Torvalds struct iphdr *pip; 3031da177e4SLinus Torvalds struct igmpv3_report *pig; 304877acedcSDaniel Lezcano struct net *net = dev_net(dev); 30531e4543dSDavid S. Miller struct flowi4 fl4; 30666088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 30766088243SHerbert Xu int tlen = dev->needed_tailroom; 3081da177e4SLinus Torvalds 30957e1ab6eSEric Dumazet while (1) { 31066088243SHerbert Xu skb = alloc_skb(size + hlen + tlen, 31157e1ab6eSEric Dumazet GFP_ATOMIC | __GFP_NOWARN); 31257e1ab6eSEric Dumazet if (skb) 31357e1ab6eSEric Dumazet break; 31457e1ab6eSEric Dumazet size >>= 1; 31557e1ab6eSEric Dumazet if (size < 256) 3161da177e4SLinus Torvalds return NULL; 31757e1ab6eSEric Dumazet } 31857e1ab6eSEric Dumazet igmp_skb_size(skb) = size; 3191da177e4SLinus Torvalds 32031e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, 32178fbfd8aSDavid S. Miller 0, 0, 32278fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 323b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 3241da177e4SLinus Torvalds kfree_skb(skb); 3251da177e4SLinus Torvalds return NULL; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 328d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 3291da177e4SLinus Torvalds skb->dev = dev; 3301da177e4SLinus Torvalds 33166088243SHerbert Xu skb_reserve(skb, hlen); 3321da177e4SLinus Torvalds 3337e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 334eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb); 3357e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds pip->version = 4; 3381da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2; 3391da177e4SLinus Torvalds pip->tos = 0xc0; 3401da177e4SLinus Torvalds pip->frag_off = htons(IP_DF); 3411da177e4SLinus Torvalds pip->ttl = 1; 342492f64ceSDavid S. Miller pip->daddr = fl4.daddr; 343492f64ceSDavid S. Miller pip->saddr = fl4.saddr; 3441da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP; 3451da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */ 346d8d1f30bSChangli Gao ip_select_ident(pip, &rt->dst, NULL); 3471da177e4SLinus Torvalds ((u8 *)&pip[1])[0] = IPOPT_RA; 3481da177e4SLinus Torvalds ((u8 *)&pip[1])[1] = 4; 3491da177e4SLinus Torvalds ((u8 *)&pip[1])[2] = 0; 3501da177e4SLinus Torvalds ((u8 *)&pip[1])[3] = 0; 3511da177e4SLinus Torvalds 352b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; 353d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig)); 354d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb); 3551da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; 3561da177e4SLinus Torvalds pig->resv1 = 0; 3571da177e4SLinus Torvalds pig->csum = 0; 3581da177e4SLinus Torvalds pig->resv2 = 0; 3591da177e4SLinus Torvalds pig->ngrec = 0; 3601da177e4SLinus Torvalds return skb; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb) 3641da177e4SLinus Torvalds { 365d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb); 36627a884dcSArnaldo Carvalho de Melo const int igmplen = skb->tail - skb->transport_header; 3671da177e4SLinus Torvalds 368d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); 3691da177e4SLinus Torvalds 370c439cb2eSHerbert Xu return ip_local_out(skb); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) 3741da177e4SLinus Torvalds { 3751da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, 3791da177e4SLinus Torvalds int type, struct igmpv3_grec **ppgr) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 3821da177e4SLinus Torvalds struct igmpv3_report *pih; 3831da177e4SLinus Torvalds struct igmpv3_grec *pgr; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds if (!skb) 3861da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 3871da177e4SLinus Torvalds if (!skb) 3881da177e4SLinus Torvalds return NULL; 3891da177e4SLinus Torvalds pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec)); 3901da177e4SLinus Torvalds pgr->grec_type = type; 3911da177e4SLinus Torvalds pgr->grec_auxwords = 0; 3921da177e4SLinus Torvalds pgr->grec_nsrcs = 0; 3931da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr; 394d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb); 3951da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1); 3961da177e4SLinus Torvalds *ppgr = pgr; 3971da177e4SLinus Torvalds return skb; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 40057e1ab6eSEric Dumazet #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \ 4011da177e4SLinus Torvalds skb_tailroom(skb)) : 0) 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, 4041da177e4SLinus Torvalds int type, int gdeleted, int sdeleted) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 4071da177e4SLinus Torvalds struct igmpv3_report *pih; 4081da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL; 4091da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; 410ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate; 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 4131da177e4SLinus Torvalds return skb; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds isquery = type == IGMPV3_MODE_IS_INCLUDE || 4161da177e4SLinus Torvalds type == IGMPV3_MODE_IS_EXCLUDE; 4171da177e4SLinus Torvalds truncate = type == IGMPV3_MODE_IS_EXCLUDE || 4181da177e4SLinus Torvalds type == IGMPV3_CHANGE_TO_EXCLUDE; 4191da177e4SLinus Torvalds 420ad12583fSDavid L Stevens stotal = scount = 0; 421ad12583fSDavid L Stevens 4221da177e4SLinus Torvalds psf_list = sdeleted ? &pmc->tomb : &pmc->sources; 4231da177e4SLinus Torvalds 424ad12583fSDavid L Stevens if (!*psf_list) 425ad12583fSDavid L Stevens goto empty_source; 426ad12583fSDavid L Stevens 427d9edf9e2SArnaldo Carvalho de Melo pih = skb ? igmpv3_report_hdr(skb) : NULL; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* EX and TO_EX get a fresh packet, if needed */ 4301da177e4SLinus Torvalds if (truncate) { 4311da177e4SLinus Torvalds if (pih && pih->ngrec && 4321da177e4SLinus Torvalds AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { 4331da177e4SLinus Torvalds if (skb) 4341da177e4SLinus Torvalds igmpv3_sendpack(skb); 4351da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds first = 1; 4391da177e4SLinus Torvalds psf_prev = NULL; 4401da177e4SLinus Torvalds for (psf=*psf_list; psf; psf=psf_next) { 441ea4d9e72SAl Viro __be32 *psrc; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds psf_next = psf->sf_next; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { 4461da177e4SLinus Torvalds psf_prev = psf; 4471da177e4SLinus Torvalds continue; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds /* clear marks on query responses */ 4511da177e4SLinus Torvalds if (isquery) 4521da177e4SLinus Torvalds psf->sf_gsresp = 0; 4531da177e4SLinus Torvalds 45463007727SAl Viro if (AVAILABLE(skb) < sizeof(__be32) + 4551da177e4SLinus Torvalds first*sizeof(struct igmpv3_grec)) { 4561da177e4SLinus Torvalds if (truncate && !first) 4571da177e4SLinus Torvalds break; /* truncate these */ 4581da177e4SLinus Torvalds if (pgr) 4591da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 4601da177e4SLinus Torvalds if (skb) 4611da177e4SLinus Torvalds igmpv3_sendpack(skb); 4621da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4631da177e4SLinus Torvalds first = 1; 4641da177e4SLinus Torvalds scount = 0; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds if (first) { 4671da177e4SLinus Torvalds skb = add_grhead(skb, pmc, type, &pgr); 4681da177e4SLinus Torvalds first = 0; 4691da177e4SLinus Torvalds } 470cc63f70bSAlexey Dobriyan if (!skb) 471cc63f70bSAlexey Dobriyan return NULL; 47263007727SAl Viro psrc = (__be32 *)skb_put(skb, sizeof(__be32)); 4731da177e4SLinus Torvalds *psrc = psf->sf_inaddr; 474ad12583fSDavid L Stevens scount++; stotal++; 4751da177e4SLinus Torvalds if ((type == IGMPV3_ALLOW_NEW_SOURCES || 4761da177e4SLinus Torvalds type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { 4771da177e4SLinus Torvalds psf->sf_crcount--; 4781da177e4SLinus Torvalds if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { 4791da177e4SLinus Torvalds if (psf_prev) 4801da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 4811da177e4SLinus Torvalds else 4821da177e4SLinus Torvalds *psf_list = psf->sf_next; 4831da177e4SLinus Torvalds kfree(psf); 4841da177e4SLinus Torvalds continue; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds psf_prev = psf; 4881da177e4SLinus Torvalds } 489ad12583fSDavid L Stevens 490ad12583fSDavid L Stevens empty_source: 491ad12583fSDavid L Stevens if (!stotal) { 492ad12583fSDavid L Stevens if (type == IGMPV3_ALLOW_NEW_SOURCES || 493ad12583fSDavid L Stevens type == IGMPV3_BLOCK_OLD_SOURCES) 494ad12583fSDavid L Stevens return skb; 495ad12583fSDavid L Stevens if (pmc->crcount || isquery) { 496ad12583fSDavid L Stevens /* make sure we have room for group header */ 497ad12583fSDavid L Stevens if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) { 498ad12583fSDavid L Stevens igmpv3_sendpack(skb); 499ad12583fSDavid L Stevens skb = NULL; /* add_grhead will get a new one */ 500ad12583fSDavid L Stevens } 501ad12583fSDavid L Stevens skb = add_grhead(skb, pmc, type, &pgr); 502ad12583fSDavid L Stevens } 503ad12583fSDavid L Stevens } 5041da177e4SLinus Torvalds if (pgr) 5051da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds if (isquery) 5081da177e4SLinus Torvalds pmc->gsquery = 0; /* clear query state on report */ 5091da177e4SLinus Torvalds return skb; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) 5131da177e4SLinus Torvalds { 5141da177e4SLinus Torvalds struct sk_buff *skb = NULL; 5151da177e4SLinus Torvalds int type; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds if (!pmc) { 5181d7138deSEric Dumazet rcu_read_lock(); 5191d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 5201da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 5211da177e4SLinus Torvalds continue; 5221da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5231da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5241da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5251da177e4SLinus Torvalds else 5261da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5271da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5281da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5291da177e4SLinus Torvalds } 5301d7138deSEric Dumazet rcu_read_unlock(); 5311da177e4SLinus Torvalds } else { 5321da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5331da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5341da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5351da177e4SLinus Torvalds else 5361da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5371da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5381da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds if (!skb) 5411da177e4SLinus Torvalds return 0; 5421da177e4SLinus Torvalds return igmpv3_sendpack(skb); 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* 5461da177e4SLinus Torvalds * remove zero-count source records from a source filter list 5471da177e4SLinus Torvalds */ 5481da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) 5491da177e4SLinus Torvalds { 5501da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf; 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds psf_prev = NULL; 5531da177e4SLinus Torvalds for (psf=*ppsf; psf; psf = psf_next) { 5541da177e4SLinus Torvalds psf_next = psf->sf_next; 5551da177e4SLinus Torvalds if (psf->sf_crcount == 0) { 5561da177e4SLinus Torvalds if (psf_prev) 5571da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5581da177e4SLinus Torvalds else 5591da177e4SLinus Torvalds *ppsf = psf->sf_next; 5601da177e4SLinus Torvalds kfree(psf); 5611da177e4SLinus Torvalds } else 5621da177e4SLinus Torvalds psf_prev = psf; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next; 5691da177e4SLinus Torvalds struct sk_buff *skb = NULL; 5701da177e4SLinus Torvalds int type, dtype; 5711da177e4SLinus Torvalds 5721d7138deSEric Dumazet rcu_read_lock(); 5731da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* deleted MCA's */ 5761da177e4SLinus Torvalds pmc_prev = NULL; 5771da177e4SLinus Torvalds for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) { 5781da177e4SLinus Torvalds pmc_next = pmc->next; 5791da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 5801da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 5811da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 5821da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 5831da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1); 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds if (pmc->crcount) { 5861da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) { 5871da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 5881da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 5891da177e4SLinus Torvalds } 590ad12583fSDavid L Stevens pmc->crcount--; 5911da177e4SLinus Torvalds if (pmc->crcount == 0) { 5921da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb); 5931da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources); 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { 5971da177e4SLinus Torvalds if (pmc_prev) 5981da177e4SLinus Torvalds pmc_prev->next = pmc_next; 5991da177e4SLinus Torvalds else 6001da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next; 6011da177e4SLinus Torvalds in_dev_put(pmc->interface); 6021da177e4SLinus Torvalds kfree(pmc); 6031da177e4SLinus Torvalds } else 6041da177e4SLinus Torvalds pmc_prev = pmc; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /* change recs */ 6091d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 6101da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 6111da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 6121da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 6131da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES; 6141da177e4SLinus Torvalds } else { 6151da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES; 6161da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6191da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* filter mode changes */ 6221da177e4SLinus Torvalds if (pmc->crcount) { 6231da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) 6241da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE; 6251da177e4SLinus Torvalds else 6261da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6271da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 628ad12583fSDavid L Stevens pmc->crcount--; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6311da177e4SLinus Torvalds } 6321d7138deSEric Dumazet rcu_read_unlock(); 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds if (!skb) 6351da177e4SLinus Torvalds return; 6361da177e4SLinus Torvalds (void) igmpv3_sendpack(skb); 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, 6401da177e4SLinus Torvalds int type) 6411da177e4SLinus Torvalds { 6421da177e4SLinus Torvalds struct sk_buff *skb; 6431da177e4SLinus Torvalds struct iphdr *iph; 6441da177e4SLinus Torvalds struct igmphdr *ih; 6451da177e4SLinus Torvalds struct rtable *rt; 6461da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 647877acedcSDaniel Lezcano struct net *net = dev_net(dev); 64863007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0; 64931e4543dSDavid S. Miller struct flowi4 fl4; 65063007727SAl Viro __be32 dst; 65166088243SHerbert Xu int hlen, tlen; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) 6541da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc); 6551da177e4SLinus Torvalds else if (type == IGMP_HOST_LEAVE_MESSAGE) 6561da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER; 6571da177e4SLinus Torvalds else 6581da177e4SLinus Torvalds dst = group; 6591da177e4SLinus Torvalds 66031e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, 66178fbfd8aSDavid S. Miller 0, 0, 66278fbfd8aSDavid S. Miller IPPROTO_IGMP, 0, dev->ifindex); 663b23dd4feSDavid S. Miller if (IS_ERR(rt)) 6641da177e4SLinus Torvalds return -1; 66578fbfd8aSDavid S. Miller 66666088243SHerbert Xu hlen = LL_RESERVED_SPACE(dev); 66766088243SHerbert Xu tlen = dev->needed_tailroom; 66866088243SHerbert Xu skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); 6691da177e4SLinus Torvalds if (skb == NULL) { 6701da177e4SLinus Torvalds ip_rt_put(rt); 6711da177e4SLinus Torvalds return -1; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 674d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 6751da177e4SLinus Torvalds 67666088243SHerbert Xu skb_reserve(skb, hlen); 6771da177e4SLinus Torvalds 6787e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 679eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 6807e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds iph->version = 4; 6831da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2; 6841da177e4SLinus Torvalds iph->tos = 0xc0; 6851da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 6861da177e4SLinus Torvalds iph->ttl = 1; 6871da177e4SLinus Torvalds iph->daddr = dst; 688492f64ceSDavid S. Miller iph->saddr = fl4.saddr; 6891da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP; 690d8d1f30bSChangli Gao ip_select_ident(iph, &rt->dst, NULL); 6911da177e4SLinus Torvalds ((u8 *)&iph[1])[0] = IPOPT_RA; 6921da177e4SLinus Torvalds ((u8 *)&iph[1])[1] = 4; 6931da177e4SLinus Torvalds ((u8 *)&iph[1])[2] = 0; 6941da177e4SLinus Torvalds ((u8 *)&iph[1])[3] = 0; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 6971da177e4SLinus Torvalds ih->type = type; 6981da177e4SLinus Torvalds ih->code = 0; 6991da177e4SLinus Torvalds ih->csum = 0; 7001da177e4SLinus Torvalds ih->group = group; 7011da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 7021da177e4SLinus Torvalds 703c439cb2eSHerbert Xu return ip_local_out(skb); 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds static void igmp_gq_timer_expire(unsigned long data) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 7111da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL); 7121da177e4SLinus Torvalds __in_dev_put(in_dev); 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds static void igmp_ifc_timer_expire(unsigned long data) 7161da177e4SLinus Torvalds { 7171da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 7201da177e4SLinus Torvalds if (in_dev->mr_ifc_count) { 7211da177e4SLinus Torvalds in_dev->mr_ifc_count--; 7221da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval); 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds __in_dev_put(in_dev); 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 7281da177e4SLinus Torvalds { 7291da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 7301da177e4SLinus Torvalds return; 7311da177e4SLinus Torvalds in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv : 7321da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 7331da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds static void igmp_timer_expire(unsigned long data) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds struct ip_mc_list *im=(struct ip_mc_list *)data; 7401da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds spin_lock(&im->lock); 7431da177e4SLinus Torvalds im->tm_running = 0; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds if (im->unsolicit_count) { 7461da177e4SLinus Torvalds im->unsolicit_count--; 7471da177e4SLinus Torvalds igmp_start_timer(im, IGMP_Unsolicited_Report_Interval); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds im->reporter = 1; 7501da177e4SLinus Torvalds spin_unlock(&im->lock); 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 7531da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 7541da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 7551da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 7561da177e4SLinus Torvalds else 7571da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds ip_ma_put(im); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 762ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 763ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 7641da177e4SLinus Torvalds { 7651da177e4SLinus Torvalds struct ip_sf_list *psf; 7661da177e4SLinus Torvalds int i, scount; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds scount = 0; 7691da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 7701da177e4SLinus Torvalds if (scount == nsrcs) 7711da177e4SLinus Torvalds break; 772ad12583fSDavid L Stevens for (i=0; i<nsrcs; i++) { 773ad12583fSDavid L Stevens /* skip inactive filters */ 774e05c4ad3SYan, Zheng if (psf->sf_count[MCAST_INCLUDE] || 775ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 776ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 777ce713ee5SRongQing.Li break; 778ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 779ad12583fSDavid L Stevens scount++; 780ad12583fSDavid L Stevens break; 781ad12583fSDavid L Stevens } 782ad12583fSDavid L Stevens } 783ad12583fSDavid L Stevens } 784ad12583fSDavid L Stevens pmc->gsquery = 0; 785ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 786ad12583fSDavid L Stevens return 0; 787ad12583fSDavid L Stevens return 1; 788ad12583fSDavid L Stevens } 789ad12583fSDavid L Stevens 790ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 791ad12583fSDavid L Stevens { 792ad12583fSDavid L Stevens struct ip_sf_list *psf; 793ad12583fSDavid L Stevens int i, scount; 794ad12583fSDavid L Stevens 795ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 796ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 797ad12583fSDavid L Stevens 798ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 799ad12583fSDavid L Stevens scount = 0; 800ad12583fSDavid L Stevens for (psf=pmc->sources; psf; psf=psf->sf_next) { 801ad12583fSDavid L Stevens if (scount == nsrcs) 802ad12583fSDavid L Stevens break; 8031da177e4SLinus Torvalds for (i=0; i<nsrcs; i++) 8041da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 8051da177e4SLinus Torvalds psf->sf_gsresp = 1; 8061da177e4SLinus Torvalds scount++; 8071da177e4SLinus Torvalds break; 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds } 810ad12583fSDavid L Stevens if (!scount) { 811ad12583fSDavid L Stevens pmc->gsquery = 0; 812ad12583fSDavid L Stevens return 0; 813ad12583fSDavid L Stevens } 814ad12583fSDavid L Stevens pmc->gsquery = 1; 815ad12583fSDavid L Stevens return 1; 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 818d679c532SEric Dumazet /* return true if packet was dropped */ 819d679c532SEric Dumazet static bool igmp_heard_report(struct in_device *in_dev, __be32 group) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds struct ip_mc_list *im; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 826d679c532SEric Dumazet return false; 8271da177e4SLinus Torvalds 8281d7138deSEric Dumazet rcu_read_lock(); 8291d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 8301da177e4SLinus Torvalds if (im->multiaddr == group) { 8311da177e4SLinus Torvalds igmp_stop_timer(im); 8321da177e4SLinus Torvalds break; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds } 8351d7138deSEric Dumazet rcu_read_unlock(); 836d679c532SEric Dumazet return false; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 839d679c532SEric Dumazet /* return true if packet was dropped */ 840d679c532SEric Dumazet static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 8411da177e4SLinus Torvalds int len) 8421da177e4SLinus Torvalds { 843d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 844d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 8451da177e4SLinus Torvalds struct ip_mc_list *im; 84663007727SAl Viro __be32 group = ih->group; 8471da177e4SLinus Torvalds int max_delay; 8481da177e4SLinus Torvalds int mark = 0; 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds 8515b7c8406SDavid Stevens if (len == 8) { 8521da177e4SLinus Torvalds if (ih->code == 0) { 8531da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds max_delay = IGMP_Query_Response_Interval; 8561da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 8571da177e4SLinus Torvalds IGMP_V1_Router_Present_Timeout; 8581da177e4SLinus Torvalds group = 0; 8591da177e4SLinus Torvalds } else { 8601da177e4SLinus Torvalds /* v2 router present */ 8611da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 8621da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 8631da177e4SLinus Torvalds IGMP_V2_Router_Present_Timeout; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds /* cancel the interface change timer */ 8661da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 8671da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 8681da177e4SLinus Torvalds __in_dev_put(in_dev); 8691da177e4SLinus Torvalds /* clear deleted report items */ 8701da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 8711da177e4SLinus Torvalds } else if (len < 12) { 872d679c532SEric Dumazet return true; /* ignore bogus packet; freed by caller */ 8735b7c8406SDavid Stevens } else if (IGMP_V1_SEEN(in_dev)) { 8745b7c8406SDavid Stevens /* This is a v3 query with v1 queriers present */ 8755b7c8406SDavid Stevens max_delay = IGMP_Query_Response_Interval; 8765b7c8406SDavid Stevens group = 0; 8775b7c8406SDavid Stevens } else if (IGMP_V2_SEEN(in_dev)) { 8785b7c8406SDavid Stevens /* this is a v3 query with v2 queriers present; 8795b7c8406SDavid Stevens * Interpretation of the max_delay code is problematic here. 8805b7c8406SDavid Stevens * A real v2 host would use ih_code directly, while v3 has a 8815b7c8406SDavid Stevens * different encoding. We use the v3 encoding as more likely 8825b7c8406SDavid Stevens * to be intended in a v3 query. 8835b7c8406SDavid Stevens */ 8845b7c8406SDavid Stevens max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 885a8c1f65cSBen Hutchings if (!max_delay) 886a8c1f65cSBen Hutchings max_delay = 1; /* can't mod w/ 0 */ 8871da177e4SLinus Torvalds } else { /* v3 */ 8881da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 889d679c532SEric Dumazet return true; 8901da177e4SLinus Torvalds 891d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 8921da177e4SLinus Torvalds if (ih3->nsrcs) { 8931da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 89463007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 895d679c532SEric Dumazet return true; 896d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 9001da177e4SLinus Torvalds if (!max_delay) 9011da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 9021da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 9031da177e4SLinus Torvalds if (ih3->qrv) 9041da177e4SLinus Torvalds in_dev->mr_qrv = ih3->qrv; 9051da177e4SLinus Torvalds if (!group) { /* general query */ 9061da177e4SLinus Torvalds if (ih3->nsrcs) 907d679c532SEric Dumazet return false; /* no sources allowed */ 9081da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 909d679c532SEric Dumazet return false; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 9121da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds /* 9161da177e4SLinus Torvalds * - Start the timers in all of our membership records 9171da177e4SLinus Torvalds * that the query applies to for the interface on 9181da177e4SLinus Torvalds * which the query arrived excl. those that belong 9191da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 9201da177e4SLinus Torvalds * - For timers already running check if they need to 9211da177e4SLinus Torvalds * be reset. 9221da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 9231da177e4SLinus Torvalds * delay possible 9241da177e4SLinus Torvalds */ 9251d7138deSEric Dumazet rcu_read_lock(); 9261d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 927ad12583fSDavid L Stevens int changed; 928ad12583fSDavid L Stevens 9291da177e4SLinus Torvalds if (group && group != im->multiaddr) 9301da177e4SLinus Torvalds continue; 9311da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 9321da177e4SLinus Torvalds continue; 9331da177e4SLinus Torvalds spin_lock_bh(&im->lock); 9341da177e4SLinus Torvalds if (im->tm_running) 9351da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 9361da177e4SLinus Torvalds else 9371da177e4SLinus Torvalds im->gsquery = mark; 938ad12583fSDavid L Stevens changed = !im->gsquery || 9391da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 9401da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 941ad12583fSDavid L Stevens if (changed) 9421da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 9431da177e4SLinus Torvalds } 9441d7138deSEric Dumazet rcu_read_unlock(); 945d679c532SEric Dumazet return false; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds 9489a57a9d2SEric Dumazet /* called in rcu_read_lock() section */ 9491da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 9501da177e4SLinus Torvalds { 9511da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 9521da177e4SLinus Torvalds struct igmphdr *ih; 9539a57a9d2SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(skb->dev); 9541da177e4SLinus Torvalds int len = skb->len; 955d679c532SEric Dumazet bool dropped = true; 9561da177e4SLinus Torvalds 957cd557bc1SDenis V. Lunev if (in_dev == NULL) 958cd557bc1SDenis V. Lunev goto drop; 9591da177e4SLinus Torvalds 960fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 9619a57a9d2SEric Dumazet goto drop; 962fb286bb2SHerbert Xu 963fb286bb2SHerbert Xu switch (skb->ip_summed) { 96484fa7933SPatrick McHardy case CHECKSUM_COMPLETE: 965d3bc23e7SAl Viro if (!csum_fold(skb->csum)) 966fb286bb2SHerbert Xu break; 967fb286bb2SHerbert Xu /* fall through */ 968fb286bb2SHerbert Xu case CHECKSUM_NONE: 969fb286bb2SHerbert Xu skb->csum = 0; 970fb286bb2SHerbert Xu if (__skb_checksum_complete(skb)) 9719a57a9d2SEric Dumazet goto drop; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 974d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 9751da177e4SLinus Torvalds switch (ih->type) { 9761da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 977d679c532SEric Dumazet dropped = igmp_heard_query(in_dev, skb, len); 9781da177e4SLinus Torvalds break; 9791da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 9801da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 9811da177e4SLinus Torvalds /* Is it our report looped back? */ 982c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) 9831da177e4SLinus Torvalds break; 98424c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 98524c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 98624c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 987d679c532SEric Dumazet dropped = igmp_heard_report(in_dev, ih->group); 9881da177e4SLinus Torvalds break; 9891da177e4SLinus Torvalds case IGMP_PIM: 9901da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 9911da177e4SLinus Torvalds return pim_rcv_v1(skb); 9921da177e4SLinus Torvalds #endif 993c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 9941da177e4SLinus Torvalds case IGMP_DVMRP: 9951da177e4SLinus Torvalds case IGMP_TRACE: 9961da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 9971da177e4SLinus Torvalds case IGMP_MTRACE: 9981da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 9991da177e4SLinus Torvalds break; 10001da177e4SLinus Torvalds default: 1001dd1c1853SLinus Torvalds break; 10021da177e4SLinus Torvalds } 1003fb286bb2SHerbert Xu 1004cd557bc1SDenis V. Lunev drop: 1005d679c532SEric Dumazet if (dropped) 10061da177e4SLinus Torvalds kfree_skb(skb); 1007d679c532SEric Dumazet else 1008d679c532SEric Dumazet consume_skb(skb); 10091da177e4SLinus Torvalds return 0; 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds #endif 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds /* 10161da177e4SLinus Torvalds * Add a filter to a device 10171da177e4SLinus Torvalds */ 10181da177e4SLinus Torvalds 101963007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 10201da177e4SLinus Torvalds { 10211da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10221da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 10251da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 1026b81693d9SJiri Pirko is changed. This check should be done in ndo_set_rx_mode 10271da177e4SLinus Torvalds routine. Something sort of: 10281da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 10291da177e4SLinus Torvalds --ANK 10301da177e4SLinus Torvalds */ 10311da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 103222bedad3SJiri Pirko dev_mc_add(dev, buf); 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds /* 10361da177e4SLinus Torvalds * Remove a filter from a device 10371da177e4SLinus Torvalds */ 10381da177e4SLinus Torvalds 103963007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 10401da177e4SLinus Torvalds { 10411da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10421da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 104522bedad3SJiri Pirko dev_mc_del(dev, buf); 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 10491da177e4SLinus Torvalds /* 10501da177e4SLinus Torvalds * deleted ip_mc_list manipulation 10511da177e4SLinus Torvalds */ 10521da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im) 10531da177e4SLinus Torvalds { 10541da177e4SLinus Torvalds struct ip_mc_list *pmc; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 10571da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 10581da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 10591da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 10601da177e4SLinus Torvalds * non-deleted or query-response MCA's. 10611da177e4SLinus Torvalds */ 10620da974f4SPanagiotis Issaris pmc = kzalloc(sizeof(*pmc), GFP_KERNEL); 10631da177e4SLinus Torvalds if (!pmc) 10641da177e4SLinus Torvalds return; 10651da177e4SLinus Torvalds spin_lock_bh(&im->lock); 10661da177e4SLinus Torvalds pmc->interface = im->interface; 10671da177e4SLinus Torvalds in_dev_hold(in_dev); 10681da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 10691da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 10701da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 10711da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 10721da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 10731da177e4SLinus Torvalds struct ip_sf_list *psf; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds pmc->tomb = im->tomb; 10761da177e4SLinus Torvalds pmc->sources = im->sources; 10771da177e4SLinus Torvalds im->tomb = im->sources = NULL; 10781da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) 10791da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 10841da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 10851da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 10861da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 108963007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr) 10901da177e4SLinus Torvalds { 10911da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 10921da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 10951da177e4SLinus Torvalds pmc_prev = NULL; 10961da177e4SLinus Torvalds for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) { 10971da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 10981da177e4SLinus Torvalds break; 10991da177e4SLinus Torvalds pmc_prev = pmc; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds if (pmc) { 11021da177e4SLinus Torvalds if (pmc_prev) 11031da177e4SLinus Torvalds pmc_prev->next = pmc->next; 11041da177e4SLinus Torvalds else 11051da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11081da177e4SLinus Torvalds if (pmc) { 11091da177e4SLinus Torvalds for (psf=pmc->tomb; psf; psf=psf_next) { 11101da177e4SLinus Torvalds psf_next = psf->sf_next; 11111da177e4SLinus Torvalds kfree(psf); 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds in_dev_put(pmc->interface); 11141da177e4SLinus Torvalds kfree(pmc); 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 11191da177e4SLinus Torvalds { 11201da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 11231da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 11241da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 11251da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 11281da177e4SLinus Torvalds nextpmc = pmc->next; 11291da177e4SLinus Torvalds ip_mc_clear_src(pmc); 11301da177e4SLinus Torvalds in_dev_put(pmc->interface); 11311da177e4SLinus Torvalds kfree(pmc); 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds /* clear dead sources, too */ 11341d7138deSEric Dumazet rcu_read_lock(); 11351d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 11361da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 11391da177e4SLinus Torvalds psf = pmc->tomb; 11401da177e4SLinus Torvalds pmc->tomb = NULL; 11411da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 11421da177e4SLinus Torvalds for (; psf; psf=psf_next) { 11431da177e4SLinus Torvalds psf_next = psf->sf_next; 11441da177e4SLinus Torvalds kfree(psf); 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds } 11471d7138deSEric Dumazet rcu_read_unlock(); 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds #endif 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im) 11521da177e4SLinus Torvalds { 11531da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 11541da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11551da177e4SLinus Torvalds int reporter; 11561da177e4SLinus Torvalds #endif 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds if (im->loaded) { 11591da177e4SLinus Torvalds im->loaded = 0; 11601da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11641da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 11651da177e4SLinus Torvalds return; 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds reporter = im->reporter; 11681da177e4SLinus Torvalds igmp_stop_timer(im); 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds if (!in_dev->dead) { 11711da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 117224cf3af3SVeaceslav Falico return; 11731da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 11741da177e4SLinus Torvalds if (reporter) 11751da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 117624cf3af3SVeaceslav Falico return; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds /* IGMPv3 */ 11791da177e4SLinus Torvalds igmpv3_add_delrec(in_dev, im); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds igmp_ifc_event(in_dev); 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds #endif 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds static void igmp_group_added(struct ip_mc_list *im) 11871da177e4SLinus Torvalds { 11881da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds if (im->loaded == 0) { 11911da177e4SLinus Torvalds im->loaded = 1; 11921da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11961da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 11971da177e4SLinus Torvalds return; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds if (in_dev->dead) 12001da177e4SLinus Torvalds return; 12011da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 12021da177e4SLinus Torvalds spin_lock_bh(&im->lock); 12031da177e4SLinus Torvalds igmp_start_timer(im, IGMP_Initial_Report_Delay); 12041da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 12051da177e4SLinus Torvalds return; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds /* else, v3 */ 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 12101da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 12111da177e4SLinus Torvalds igmp_ifc_event(in_dev); 12121da177e4SLinus Torvalds #endif 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds /* 12171da177e4SLinus Torvalds * Multicast list managers 12181da177e4SLinus Torvalds */ 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* 12221da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 12231da177e4SLinus Torvalds */ 12241da177e4SLinus Torvalds 12258f935bbdSAl Viro void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds struct ip_mc_list *im; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds ASSERT_RTNL(); 12301da177e4SLinus Torvalds 12311d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, im) { 12321da177e4SLinus Torvalds if (im->multiaddr == addr) { 12331da177e4SLinus Torvalds im->users++; 12341da177e4SLinus Torvalds ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0); 12351da177e4SLinus Torvalds goto out; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds 12391d7138deSEric Dumazet im = kzalloc(sizeof(*im), GFP_KERNEL); 12401da177e4SLinus Torvalds if (!im) 12411da177e4SLinus Torvalds goto out; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds im->users = 1; 12441da177e4SLinus Torvalds im->interface = in_dev; 12451da177e4SLinus Torvalds in_dev_hold(in_dev); 12461da177e4SLinus Torvalds im->multiaddr = addr; 12471da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 12481da177e4SLinus Torvalds im->sfmode = MCAST_EXCLUDE; 12491da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE] = 1; 12501da177e4SLinus Torvalds atomic_set(&im->refcnt, 1); 12511da177e4SLinus Torvalds spin_lock_init(&im->lock); 12521da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1253b24b8a24SPavel Emelyanov setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im); 12541da177e4SLinus Torvalds im->unsolicit_count = IGMP_Unsolicited_Report_Count; 12551da177e4SLinus Torvalds #endif 12561d7138deSEric Dumazet 12571d7138deSEric Dumazet im->next_rcu = in_dev->mc_list; 1258b8bae41eSRami Rosen in_dev->mc_count++; 1259cf778b00SEric Dumazet rcu_assign_pointer(in_dev->mc_list, im); 12601d7138deSEric Dumazet 12611da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12621da177e4SLinus Torvalds igmpv3_del_delrec(in_dev, im->multiaddr); 12631da177e4SLinus Torvalds #endif 12641da177e4SLinus Torvalds igmp_group_added(im); 12651da177e4SLinus Torvalds if (!in_dev->dead) 12661da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 12671da177e4SLinus Torvalds out: 12681da177e4SLinus Torvalds return; 12691da177e4SLinus Torvalds } 12704bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_inc_group); 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds /* 1273a816c7c7SJay Vosburgh * Resend IGMP JOIN report; used for bonding. 1274866f3b25SEric Dumazet * Called with rcu_read_lock() 1275a816c7c7SJay Vosburgh */ 1276866f3b25SEric Dumazet void ip_mc_rejoin_groups(struct in_device *in_dev) 1277a816c7c7SJay Vosburgh { 127808882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1279866f3b25SEric Dumazet struct ip_mc_list *im; 1280866f3b25SEric Dumazet int type; 1281a816c7c7SJay Vosburgh 1282866f3b25SEric Dumazet for_each_pmc_rcu(in_dev, im) { 1283a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1284866f3b25SEric Dumazet continue; 1285a816c7c7SJay Vosburgh 1286e12b4539SFlavio Leitner /* a failover is happening and switches 1287866f3b25SEric Dumazet * must be notified immediately 1288866f3b25SEric Dumazet */ 1289e12b4539SFlavio Leitner if (IGMP_V1_SEEN(in_dev)) 1290866f3b25SEric Dumazet type = IGMP_HOST_MEMBERSHIP_REPORT; 1291e12b4539SFlavio Leitner else if (IGMP_V2_SEEN(in_dev)) 1292866f3b25SEric Dumazet type = IGMPV2_HOST_MEMBERSHIP_REPORT; 1293e12b4539SFlavio Leitner else 1294866f3b25SEric Dumazet type = IGMPV3_HOST_MEMBERSHIP_REPORT; 1295866f3b25SEric Dumazet igmp_send_report(in_dev, im, type); 1296866f3b25SEric Dumazet } 1297a816c7c7SJay Vosburgh #endif 1298a816c7c7SJay Vosburgh } 1299866f3b25SEric Dumazet EXPORT_SYMBOL(ip_mc_rejoin_groups); 1300a816c7c7SJay Vosburgh 1301a816c7c7SJay Vosburgh /* 13021da177e4SLinus Torvalds * A socket has left a multicast group on device dev 13031da177e4SLinus Torvalds */ 13041da177e4SLinus Torvalds 13058f935bbdSAl Viro void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) 13061da177e4SLinus Torvalds { 13071d7138deSEric Dumazet struct ip_mc_list *i; 13081d7138deSEric Dumazet struct ip_mc_list __rcu **ip; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds ASSERT_RTNL(); 13111da177e4SLinus Torvalds 13121d7138deSEric Dumazet for (ip = &in_dev->mc_list; 13131d7138deSEric Dumazet (i = rtnl_dereference(*ip)) != NULL; 13141d7138deSEric Dumazet ip = &i->next_rcu) { 13151da177e4SLinus Torvalds if (i->multiaddr == addr) { 13161da177e4SLinus Torvalds if (--i->users == 0) { 13171d7138deSEric Dumazet *ip = i->next_rcu; 1318b8bae41eSRami Rosen in_dev->mc_count--; 13191da177e4SLinus Torvalds igmp_group_dropped(i); 132024cf3af3SVeaceslav Falico ip_mc_clear_src(i); 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds if (!in_dev->dead) 13231da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds ip_ma_put(i); 13261da177e4SLinus Torvalds return; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds break; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds } 13324bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_dec_group); 13331da177e4SLinus Torvalds 133475c78500SMoni Shoua /* Device changing type */ 133575c78500SMoni Shoua 133675c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 133775c78500SMoni Shoua { 13381d7138deSEric Dumazet struct ip_mc_list *pmc; 133975c78500SMoni Shoua 134075c78500SMoni Shoua ASSERT_RTNL(); 134175c78500SMoni Shoua 13421d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 13431d7138deSEric Dumazet igmp_group_dropped(pmc); 134475c78500SMoni Shoua } 134575c78500SMoni Shoua 134675c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 134775c78500SMoni Shoua { 13481d7138deSEric Dumazet struct ip_mc_list *pmc; 134975c78500SMoni Shoua 135075c78500SMoni Shoua ASSERT_RTNL(); 135175c78500SMoni Shoua 13521d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 13531d7138deSEric Dumazet igmp_group_added(pmc); 135475c78500SMoni Shoua } 135575c78500SMoni Shoua 13561da177e4SLinus Torvalds /* Device going down */ 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 13591da177e4SLinus Torvalds { 13601d7138deSEric Dumazet struct ip_mc_list *pmc; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds ASSERT_RTNL(); 13631da177e4SLinus Torvalds 13641d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 13651d7138deSEric Dumazet igmp_group_dropped(pmc); 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13681da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 13691da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 13701da177e4SLinus Torvalds __in_dev_put(in_dev); 13711da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 13721da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 13731da177e4SLinus Torvalds __in_dev_put(in_dev); 13741da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 13751da177e4SLinus Torvalds #endif 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 13811da177e4SLinus Torvalds { 13821da177e4SLinus Torvalds ASSERT_RTNL(); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 13851da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13861da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 1387b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 1388b24b8a24SPavel Emelyanov (unsigned long)in_dev); 13891da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 1390b8bae41eSRami Rosen in_dev->mc_count = 0; 1391b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 1392b24b8a24SPavel Emelyanov (unsigned long)in_dev); 13931da177e4SLinus Torvalds in_dev->mr_qrv = IGMP_Unsolicited_Report_Count; 13941da177e4SLinus Torvalds #endif 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds /* Device going up */ 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 14021da177e4SLinus Torvalds { 14031d7138deSEric Dumazet struct ip_mc_list *pmc; 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds ASSERT_RTNL(); 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 14081da177e4SLinus Torvalds 14091d7138deSEric Dumazet for_each_pmc_rtnl(in_dev, pmc) 14101d7138deSEric Dumazet igmp_group_added(pmc); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 14141da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 14151da177e4SLinus Torvalds */ 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 14181da177e4SLinus Torvalds { 14191da177e4SLinus Torvalds struct ip_mc_list *i; 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds ASSERT_RTNL(); 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds /* Deactivate timers */ 14241da177e4SLinus Torvalds ip_mc_down(in_dev); 14251da177e4SLinus Torvalds 14261d7138deSEric Dumazet while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { 14271d7138deSEric Dumazet in_dev->mc_list = i->next_rcu; 1428b8bae41eSRami Rosen in_dev->mc_count--; 14291d7138deSEric Dumazet 143024cf3af3SVeaceslav Falico /* We've dropped the groups in ip_mc_down already */ 143124cf3af3SVeaceslav Falico ip_mc_clear_src(i); 14321da177e4SLinus Torvalds ip_ma_put(i); 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 14369e917dcaSEric Dumazet /* RTNL is locked */ 1437877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 14381da177e4SLinus Torvalds { 14391da177e4SLinus Torvalds struct net_device *dev = NULL; 14401da177e4SLinus Torvalds struct in_device *idev = NULL; 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds if (imr->imr_ifindex) { 1443877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 14441da177e4SLinus Torvalds return idev; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 14479e917dcaSEric Dumazet dev = __ip_dev_find(net, imr->imr_address.s_addr, false); 14481da177e4SLinus Torvalds if (!dev) 14491da177e4SLinus Torvalds return NULL; 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 1452b23dd4feSDavid S. Miller if (!dev) { 145378fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, 145478fbfd8aSDavid S. Miller imr->imr_multiaddr.s_addr, 145578fbfd8aSDavid S. Miller 0, 0, 0); 1456b23dd4feSDavid S. Miller if (!IS_ERR(rt)) { 1457d8d1f30bSChangli Gao dev = rt->dst.dev; 14581da177e4SLinus Torvalds ip_rt_put(rt); 14591da177e4SLinus Torvalds } 1460b23dd4feSDavid S. Miller } 14611da177e4SLinus Torvalds if (dev) { 14621da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1463e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds return idev; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds /* 14691da177e4SLinus Torvalds * Join a socket to a group 14701da177e4SLinus Torvalds */ 1471ab32ea5dSBrian Haley int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS; 1472ab32ea5dSBrian Haley int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 14768f935bbdSAl Viro __be32 *psfsrc) 14771da177e4SLinus Torvalds { 14781da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 14791da177e4SLinus Torvalds int rv = 0; 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds psf_prev = NULL; 14821da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 14831da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 14841da177e4SLinus Torvalds break; 14851da177e4SLinus Torvalds psf_prev = psf; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 14881da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 14891da177e4SLinus Torvalds return -ESRCH; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds psf->sf_count[sfmode]--; 14921da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 14931da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 14961da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 14971da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 14981da177e4SLinus Torvalds #endif 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds /* no more filters for this source */ 15011da177e4SLinus Torvalds if (psf_prev) 15021da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 15031da177e4SLinus Torvalds else 15041da177e4SLinus Torvalds pmc->sources = psf->sf_next; 15051da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15061da177e4SLinus Torvalds if (psf->sf_oldin && 15071da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 15081da177e4SLinus Torvalds psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 15091da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 15101da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 15111da177e4SLinus Torvalds pmc->tomb = psf; 15121da177e4SLinus Torvalds rv = 1; 15131da177e4SLinus Torvalds } else 15141da177e4SLinus Torvalds #endif 15151da177e4SLinus Torvalds kfree(psf); 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds return rv; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 15211da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 15221da177e4SLinus Torvalds #endif 15231da177e4SLinus Torvalds 15248f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 15258f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 15261da177e4SLinus Torvalds { 15271da177e4SLinus Torvalds struct ip_mc_list *pmc; 15281da177e4SLinus Torvalds int changerec = 0; 15291da177e4SLinus Torvalds int i, err; 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds if (!in_dev) 15321da177e4SLinus Torvalds return -ENODEV; 15331d7138deSEric Dumazet rcu_read_lock(); 15341d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 15351da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 15361da177e4SLinus Torvalds break; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds if (!pmc) { 15391da177e4SLinus Torvalds /* MCA not found?? bug */ 15401d7138deSEric Dumazet rcu_read_unlock(); 15411da177e4SLinus Torvalds return -ESRCH; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 15441d7138deSEric Dumazet rcu_read_unlock(); 15451da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15461da177e4SLinus Torvalds sf_markstate(pmc); 15471da177e4SLinus Torvalds #endif 15481da177e4SLinus Torvalds if (!delta) { 15491da177e4SLinus Torvalds err = -EINVAL; 15501da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 15511da177e4SLinus Torvalds goto out_unlock; 15521da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds err = 0; 15551da177e4SLinus Torvalds for (i=0; i<sfcount; i++) { 15561da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds changerec |= rv > 0; 15591da177e4SLinus Torvalds if (!err && rv < 0) 15601da177e4SLinus Torvalds err = rv; 15611da177e4SLinus Torvalds } 15621da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 15631da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 15641da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 15651da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15661da177e4SLinus Torvalds struct ip_sf_list *psf; 15671da177e4SLinus Torvalds #endif 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds /* filter mode change */ 15701da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 15711da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15721da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 15731da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 15741da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 15751da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf = psf->sf_next) 15761da177e4SLinus Torvalds psf->sf_crcount = 0; 15771da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 15781da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 15791da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 15801da177e4SLinus Torvalds #endif 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds out_unlock: 15831da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 15841da177e4SLinus Torvalds return err; 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds /* 15881da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 15891da177e4SLinus Torvalds */ 15901da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 15915eb81e89SJun Zhao __be32 *psfsrc) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds psf_prev = NULL; 15961da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 15971da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 15981da177e4SLinus Torvalds break; 15991da177e4SLinus Torvalds psf_prev = psf; 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds if (!psf) { 16020da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 16031da177e4SLinus Torvalds if (!psf) 16041da177e4SLinus Torvalds return -ENOBUFS; 16051da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 16061da177e4SLinus Torvalds if (psf_prev) { 16071da177e4SLinus Torvalds psf_prev->sf_next = psf; 16081da177e4SLinus Torvalds } else 16091da177e4SLinus Torvalds pmc->sources = psf; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds psf->sf_count[sfmode]++; 16121da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 16131da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds return 0; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16191da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 16201da177e4SLinus Torvalds { 16211da177e4SLinus Torvalds struct ip_sf_list *psf; 16221da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) 16251da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 16261da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 16271da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 16281da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 16291da177e4SLinus Torvalds } else 16301da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 16341da177e4SLinus Torvalds { 1635ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 16361da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 16371da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 16381da177e4SLinus Torvalds int new_in, rv; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds rv = 0; 16411da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 16421da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 16431da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 16441da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 16451da177e4SLinus Torvalds } else 16461da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 1647ad12583fSDavid L Stevens if (new_in) { 1648ad12583fSDavid L Stevens if (!psf->sf_oldin) { 164976edc605SAl Viro struct ip_sf_list *prev = NULL; 1650ad12583fSDavid L Stevens 1651ad12583fSDavid L Stevens for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) { 1652ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1653ad12583fSDavid L Stevens break; 1654ad12583fSDavid L Stevens prev = dpsf; 1655ad12583fSDavid L Stevens } 1656ad12583fSDavid L Stevens if (dpsf) { 1657ad12583fSDavid L Stevens if (prev) 1658ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 1659ad12583fSDavid L Stevens else 1660ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 1661ad12583fSDavid L Stevens kfree(dpsf); 1662ad12583fSDavid L Stevens } 16631da177e4SLinus Torvalds psf->sf_crcount = qrv; 16641da177e4SLinus Torvalds rv++; 16651da177e4SLinus Torvalds } 1666ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 1667ad12583fSDavid L Stevens 1668ad12583fSDavid L Stevens psf->sf_crcount = 0; 1669ad12583fSDavid L Stevens /* 1670ad12583fSDavid L Stevens * add or update "delete" records if an active filter 1671ad12583fSDavid L Stevens * is now inactive 1672ad12583fSDavid L Stevens */ 1673ad12583fSDavid L Stevens for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) 1674ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1675ad12583fSDavid L Stevens break; 1676ad12583fSDavid L Stevens if (!dpsf) { 16773ed37a6fSJoe Perches dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); 1678ad12583fSDavid L Stevens if (!dpsf) 1679ad12583fSDavid L Stevens continue; 1680ad12583fSDavid L Stevens *dpsf = *psf; 1681ad12583fSDavid L Stevens /* pmc->lock held by callers */ 1682ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 1683ad12583fSDavid L Stevens pmc->tomb = dpsf; 1684ad12583fSDavid L Stevens } 1685ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 1686ad12583fSDavid L Stevens rv++; 1687ad12583fSDavid L Stevens } 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds return rv; 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds #endif 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds /* 16941da177e4SLinus Torvalds * Add multicast source filter list to the interface list 16951da177e4SLinus Torvalds */ 16968f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 16978f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 16981da177e4SLinus Torvalds { 16991da177e4SLinus Torvalds struct ip_mc_list *pmc; 17001da177e4SLinus Torvalds int isexclude; 17011da177e4SLinus Torvalds int i, err; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds if (!in_dev) 17041da177e4SLinus Torvalds return -ENODEV; 17051d7138deSEric Dumazet rcu_read_lock(); 17061d7138deSEric Dumazet for_each_pmc_rcu(in_dev, pmc) { 17071da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 17081da177e4SLinus Torvalds break; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds if (!pmc) { 17111da177e4SLinus Torvalds /* MCA not found?? bug */ 17121d7138deSEric Dumazet rcu_read_unlock(); 17131da177e4SLinus Torvalds return -ESRCH; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 17161d7138deSEric Dumazet rcu_read_unlock(); 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17191da177e4SLinus Torvalds sf_markstate(pmc); 17201da177e4SLinus Torvalds #endif 17211da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 17221da177e4SLinus Torvalds if (!delta) 17231da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 17241da177e4SLinus Torvalds err = 0; 17251da177e4SLinus Torvalds for (i=0; i<sfcount; i++) { 17265eb81e89SJun Zhao err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); 17271da177e4SLinus Torvalds if (err) 17281da177e4SLinus Torvalds break; 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds if (err) { 17311da177e4SLinus Torvalds int j; 17321da177e4SLinus Torvalds 1733685f94e6SJun Zhao if (!delta) 17341da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 17351da177e4SLinus Torvalds for (j=0; j<i; j++) 1736a1889c0dSJulia Lawall (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); 17371da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 17381da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17391da177e4SLinus Torvalds struct ip_sf_list *psf; 1740cfcabdccSStephen Hemminger in_dev = pmc->interface; 17411da177e4SLinus Torvalds #endif 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /* filter mode change */ 17441da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 17451da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 17461da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 17471da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 17481da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17491da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 17521da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 17531da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 17541da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf = psf->sf_next) 17551da177e4SLinus Torvalds psf->sf_crcount = 0; 17561da177e4SLinus Torvalds igmp_ifc_event(in_dev); 17571da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 17581da177e4SLinus Torvalds igmp_ifc_event(in_dev); 17591da177e4SLinus Torvalds #endif 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 17621da177e4SLinus Torvalds return err; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds struct ip_sf_list *psf, *nextpsf; 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds for (psf=pmc->tomb; psf; psf=nextpsf) { 17701da177e4SLinus Torvalds nextpsf = psf->sf_next; 17711da177e4SLinus Torvalds kfree(psf); 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds pmc->tomb = NULL; 17741da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=nextpsf) { 17751da177e4SLinus Torvalds nextpsf = psf->sf_next; 17761da177e4SLinus Torvalds kfree(psf); 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds pmc->sources = NULL; 17791da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 1780de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 17811da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds /* 17861da177e4SLinus Torvalds * Join a multicast group 17871da177e4SLinus Torvalds */ 17881da177e4SLinus Torvalds int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr) 17891da177e4SLinus Torvalds { 17901da177e4SLinus Torvalds int err; 17918f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 1792ca9b907dSDavid L Stevens struct ip_mc_socklist *iml = NULL, *i; 17931da177e4SLinus Torvalds struct in_device *in_dev; 17941da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1795877acedcSDaniel Lezcano struct net *net = sock_net(sk); 1796ca9b907dSDavid L Stevens int ifindex; 17971da177e4SLinus Torvalds int count = 0; 17981da177e4SLinus Torvalds 1799f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 18001da177e4SLinus Torvalds return -EINVAL; 18011da177e4SLinus Torvalds 18026756ae4bSStephen Hemminger rtnl_lock(); 18031da177e4SLinus Torvalds 1804877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds if (!in_dev) { 18071da177e4SLinus Torvalds iml = NULL; 18081da177e4SLinus Torvalds err = -ENODEV; 18091da177e4SLinus Torvalds goto done; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds err = -EADDRINUSE; 1813ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 18141d7138deSEric Dumazet for_each_pmc_rtnl(inet, i) { 1815ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 1816ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 18171da177e4SLinus Torvalds goto done; 18181da177e4SLinus Torvalds count++; 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds err = -ENOBUFS; 1821ca9b907dSDavid L Stevens if (count >= sysctl_igmp_max_memberships) 18221da177e4SLinus Torvalds goto done; 18238b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 1824ca9b907dSDavid L Stevens if (iml == NULL) 1825ca9b907dSDavid L Stevens goto done; 1826ca9b907dSDavid L Stevens 18271da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 18281d7138deSEric Dumazet iml->next_rcu = inet->mc_list; 18291da177e4SLinus Torvalds iml->sflist = NULL; 18301da177e4SLinus Torvalds iml->sfmode = MCAST_EXCLUDE; 1831cf778b00SEric Dumazet rcu_assign_pointer(inet->mc_list, iml); 18321da177e4SLinus Torvalds ip_mc_inc_group(in_dev, addr); 18331da177e4SLinus Torvalds err = 0; 18341da177e4SLinus Torvalds done: 18356756ae4bSStephen Hemminger rtnl_unlock(); 18361da177e4SLinus Torvalds return err; 18371da177e4SLinus Torvalds } 18384bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_mc_join_group); 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 18411da177e4SLinus Torvalds struct in_device *in_dev) 18421da177e4SLinus Torvalds { 18431d7138deSEric Dumazet struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 18441da177e4SLinus Torvalds int err; 18451da177e4SLinus Torvalds 1846c85bb41eSFlavio Leitner if (psf == NULL) { 18471da177e4SLinus Torvalds /* any-source empty exclude case */ 18481da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 18491da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 1852c85bb41eSFlavio Leitner iml->sfmode, psf->sl_count, psf->sl_addr, 0); 1853a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(iml->sflist, NULL); 1854c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 1855c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); 18567519cce4SLai Jiangshan kfree_rcu(psf, rcu); 18571da177e4SLinus Torvalds return err; 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds /* 18611da177e4SLinus Torvalds * Ask a socket to leave a group. 18621da177e4SLinus Torvalds */ 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 18651da177e4SLinus Torvalds { 18661da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 18671d7138deSEric Dumazet struct ip_mc_socklist *iml; 18681d7138deSEric Dumazet struct ip_mc_socklist __rcu **imlp; 186984b42baeSDavid L Stevens struct in_device *in_dev; 1870877acedcSDaniel Lezcano struct net *net = sock_net(sk); 18718f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 187284b42baeSDavid L Stevens u32 ifindex; 1873acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds rtnl_lock(); 1876877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 187784b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 18781d7138deSEric Dumazet for (imlp = &inet->mc_list; 18791d7138deSEric Dumazet (iml = rtnl_dereference(*imlp)) != NULL; 18801d7138deSEric Dumazet imlp = &iml->next_rcu) { 1881acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 1882acd6e00bSDavid L Stevens continue; 1883acd6e00bSDavid L Stevens if (ifindex) { 1884acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 1885acd6e00bSDavid L Stevens continue; 1886acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 1887acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 1888acd6e00bSDavid L Stevens continue; 1889acd6e00bSDavid L Stevens 18901da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 18911da177e4SLinus Torvalds 18921d7138deSEric Dumazet *imlp = iml->next_rcu; 18931da177e4SLinus Torvalds 1894acd6e00bSDavid L Stevens if (in_dev) 189584b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 18961da177e4SLinus Torvalds rtnl_unlock(); 1897c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 1898c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 189910d50e74SLai Jiangshan kfree_rcu(iml, rcu); 19001da177e4SLinus Torvalds return 0; 19011da177e4SLinus Torvalds } 1902acd6e00bSDavid L Stevens if (!in_dev) 1903acd6e00bSDavid L Stevens ret = -ENODEV; 19041da177e4SLinus Torvalds rtnl_unlock(); 1905acd6e00bSDavid L Stevens return ret; 19061da177e4SLinus Torvalds } 1907193ba924Sstephen hemminger EXPORT_SYMBOL(ip_mc_leave_group); 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 19101da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 19111da177e4SLinus Torvalds { 19121da177e4SLinus Torvalds int err; 19131da177e4SLinus Torvalds struct ip_mreqn imr; 191463007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 19151da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 19161da177e4SLinus Torvalds struct in_device *in_dev = NULL; 19171da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19181da177e4SLinus Torvalds struct ip_sf_socklist *psl; 1919877acedcSDaniel Lezcano struct net *net = sock_net(sk); 19208cdaaa15SDavid L Stevens int leavegroup = 0; 19211da177e4SLinus Torvalds int i, j, rv; 19221da177e4SLinus Torvalds 1923f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 19241da177e4SLinus Torvalds return -EINVAL; 19251da177e4SLinus Torvalds 19266756ae4bSStephen Hemminger rtnl_lock(); 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 19291da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 19301da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 1931877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds if (!in_dev) { 19341da177e4SLinus Torvalds err = -ENODEV; 19351da177e4SLinus Torvalds goto done; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 19381da177e4SLinus Torvalds 19391d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 1940f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 1941f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 1942f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 19431da177e4SLinus Torvalds break; 19441da177e4SLinus Torvalds } 1945917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 1946917f2f10SDavid L Stevens err = -EINVAL; 19471da177e4SLinus Torvalds goto done; 1948917f2f10SDavid L Stevens } 19491da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 19501da177e4SLinus Torvalds if (pmc->sflist) { 1951917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 1952917f2f10SDavid L Stevens err = -EINVAL; 19531da177e4SLinus Torvalds goto done; 1954917f2f10SDavid L Stevens } 19551da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 19561da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 19571da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 19581da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 19591da177e4SLinus Torvalds NULL, 0); 19601da177e4SLinus Torvalds pmc->sfmode = omode; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds 19631d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 19641da177e4SLinus Torvalds if (!add) { 19651da177e4SLinus Torvalds if (!psl) 1966917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 19671da177e4SLinus Torvalds rv = !0; 19681da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 19691da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 197063007727SAl Viro sizeof(__be32)); 19711da177e4SLinus Torvalds if (rv == 0) 19721da177e4SLinus Torvalds break; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds if (rv) /* source not found */ 1975917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 19761da177e4SLinus Torvalds 19778cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 19788cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 19798cdaaa15SDavid L Stevens leavegroup = 1; 19808cdaaa15SDavid L Stevens goto done; 19818cdaaa15SDavid L Stevens } 19828cdaaa15SDavid L Stevens 19831da177e4SLinus Torvalds /* update the interface filter */ 19841da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 19851da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds for (j=i+1; j<psl->sl_count; j++) 19881da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 19891da177e4SLinus Torvalds psl->sl_count--; 19901da177e4SLinus Torvalds err = 0; 19911da177e4SLinus Torvalds goto done; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds /* else, add a new source to the filter */ 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds if (psl && psl->sl_count >= sysctl_igmp_max_msf) { 19961da177e4SLinus Torvalds err = -ENOBUFS; 19971da177e4SLinus Torvalds goto done; 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 20001da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 20011da177e4SLinus Torvalds int count = IP_SFBLOCK; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (psl) 20041da177e4SLinus Torvalds count += psl->sl_max; 20058b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 20061da177e4SLinus Torvalds if (!newpsl) { 20071da177e4SLinus Torvalds err = -ENOBUFS; 20081da177e4SLinus Torvalds goto done; 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds newpsl->sl_max = count; 20111da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 20121da177e4SLinus Torvalds if (psl) { 20131da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) 20141da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 2015c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2016c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 20177519cce4SLai Jiangshan kfree_rcu(psl, rcu); 20181da177e4SLinus Torvalds } 2019cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 2020c85bb41eSFlavio Leitner psl = newpsl; 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 20231da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 20241da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 202563007727SAl Viro sizeof(__be32)); 20261da177e4SLinus Torvalds if (rv == 0) 20271da177e4SLinus Torvalds break; 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 20301da177e4SLinus Torvalds goto done; 20311da177e4SLinus Torvalds for (j=psl->sl_count-1; j>=i; j--) 20321da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 20331da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 20341da177e4SLinus Torvalds psl->sl_count++; 20351da177e4SLinus Torvalds err = 0; 20361da177e4SLinus Torvalds /* update the interface list */ 20371da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 20381da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 20391da177e4SLinus Torvalds done: 20406756ae4bSStephen Hemminger rtnl_unlock(); 20418cdaaa15SDavid L Stevens if (leavegroup) 20428cdaaa15SDavid L Stevens return ip_mc_leave_group(sk, &imr); 20431da177e4SLinus Torvalds return err; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 20471da177e4SLinus Torvalds { 20489951f036SDavid L Stevens int err = 0; 20491da177e4SLinus Torvalds struct ip_mreqn imr; 205063007727SAl Viro __be32 addr = msf->imsf_multiaddr; 20511da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 20521da177e4SLinus Torvalds struct in_device *in_dev; 20531da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 20541da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2055877acedcSDaniel Lezcano struct net *net = sock_net(sk); 20569951f036SDavid L Stevens int leavegroup = 0; 20571da177e4SLinus Torvalds 2058f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 20591da177e4SLinus Torvalds return -EINVAL; 20601da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 20611da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 20621da177e4SLinus Torvalds return -EINVAL; 20631da177e4SLinus Torvalds 20646756ae4bSStephen Hemminger rtnl_lock(); 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 20671da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 20681da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2069877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds if (!in_dev) { 20721da177e4SLinus Torvalds err = -ENODEV; 20731da177e4SLinus Torvalds goto done; 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 20769951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 20779951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 20789951f036SDavid L Stevens leavegroup = 1; 20799951f036SDavid L Stevens goto done; 20809951f036SDavid L Stevens } 20819951f036SDavid L Stevens 20821d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 20831da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 20841da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 20851da177e4SLinus Torvalds break; 20861da177e4SLinus Torvalds } 2087917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2088917f2f10SDavid L Stevens err = -EINVAL; 20891da177e4SLinus Torvalds goto done; 2090917f2f10SDavid L Stevens } 20911da177e4SLinus Torvalds if (msf->imsf_numsrc) { 20928b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 20938b3a7005SKris Katterjohn GFP_KERNEL); 20941da177e4SLinus Torvalds if (!newpsl) { 20951da177e4SLinus Torvalds err = -ENOBUFS; 20961da177e4SLinus Torvalds goto done; 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 20991da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 21001da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 21011da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 21021da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 21031da177e4SLinus Torvalds if (err) { 21041da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 21051da177e4SLinus Torvalds goto done; 21061da177e4SLinus Torvalds } 21078713dbf0SYan Zheng } else { 21081da177e4SLinus Torvalds newpsl = NULL; 21098713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 21108713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 21118713dbf0SYan Zheng } 21121d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 21131da177e4SLinus Torvalds if (psl) { 21141da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 21151da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 2116c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2117c85bb41eSFlavio Leitner atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); 21187519cce4SLai Jiangshan kfree_rcu(psl, rcu); 21191da177e4SLinus Torvalds } else 21201da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 21211da177e4SLinus Torvalds 0, NULL, 0); 2122cf778b00SEric Dumazet rcu_assign_pointer(pmc->sflist, newpsl); 21231da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2124917f2f10SDavid L Stevens err = 0; 21251da177e4SLinus Torvalds done: 21266756ae4bSStephen Hemminger rtnl_unlock(); 21279951f036SDavid L Stevens if (leavegroup) 21289951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 21291da177e4SLinus Torvalds return err; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 21331da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 21341da177e4SLinus Torvalds { 21351da177e4SLinus Torvalds int err, len, count, copycount; 21361da177e4SLinus Torvalds struct ip_mreqn imr; 213763007727SAl Viro __be32 addr = msf->imsf_multiaddr; 21381da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 21391da177e4SLinus Torvalds struct in_device *in_dev; 21401da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21411da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2142877acedcSDaniel Lezcano struct net *net = sock_net(sk); 21431da177e4SLinus Torvalds 2144f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21451da177e4SLinus Torvalds return -EINVAL; 21461da177e4SLinus Torvalds 21476756ae4bSStephen Hemminger rtnl_lock(); 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 21501da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 21511da177e4SLinus Torvalds imr.imr_ifindex = 0; 2152877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 21531da177e4SLinus Torvalds 21541da177e4SLinus Torvalds if (!in_dev) { 21551da177e4SLinus Torvalds err = -ENODEV; 21561da177e4SLinus Torvalds goto done; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 21591da177e4SLinus Torvalds 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 } 21651da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 21661da177e4SLinus Torvalds goto done; 21671da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 21681d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 21696756ae4bSStephen Hemminger rtnl_unlock(); 21701da177e4SLinus Torvalds if (!psl) { 21711da177e4SLinus Torvalds len = 0; 21721da177e4SLinus Torvalds count = 0; 21731da177e4SLinus Torvalds } else { 21741da177e4SLinus Torvalds count = psl->sl_count; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 21771da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 21781da177e4SLinus Torvalds msf->imsf_numsrc = count; 21791da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 21801da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 21811da177e4SLinus Torvalds return -EFAULT; 21821da177e4SLinus Torvalds } 21831da177e4SLinus Torvalds if (len && 21841da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 21851da177e4SLinus Torvalds return -EFAULT; 21861da177e4SLinus Torvalds return 0; 21871da177e4SLinus Torvalds done: 21886756ae4bSStephen Hemminger rtnl_unlock(); 21891da177e4SLinus Torvalds return err; 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 21931da177e4SLinus Torvalds struct group_filter __user *optval, int __user *optlen) 21941da177e4SLinus Torvalds { 21951da177e4SLinus Torvalds int err, i, count, copycount; 21961da177e4SLinus Torvalds struct sockaddr_in *psin; 219763007727SAl Viro __be32 addr; 21981da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 21991da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22001da177e4SLinus Torvalds struct ip_sf_socklist *psl; 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 22031da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 22041da177e4SLinus Torvalds return -EINVAL; 22051da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2206f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 22071da177e4SLinus Torvalds return -EINVAL; 22081da177e4SLinus Torvalds 22096756ae4bSStephen Hemminger rtnl_lock(); 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 22121da177e4SLinus Torvalds 22131d7138deSEric Dumazet for_each_pmc_rtnl(inet, pmc) { 22141da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 22151da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 22161da177e4SLinus Torvalds break; 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 22191da177e4SLinus Torvalds goto done; 22201da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 22211d7138deSEric Dumazet psl = rtnl_dereference(pmc->sflist); 22226756ae4bSStephen Hemminger rtnl_unlock(); 22231da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 22241da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 22251da177e4SLinus Torvalds gsf->gf_numsrc = count; 22261da177e4SLinus Torvalds if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 22271da177e4SLinus Torvalds copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 22281da177e4SLinus Torvalds return -EFAULT; 22291da177e4SLinus Torvalds } 22301da177e4SLinus Torvalds for (i=0; i<copycount; i++) { 22311da177e4SLinus Torvalds struct sockaddr_storage ss; 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 22341da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 22351da177e4SLinus Torvalds psin->sin_family = AF_INET; 22361da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 22371da177e4SLinus Torvalds if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 22381da177e4SLinus Torvalds return -EFAULT; 22391da177e4SLinus Torvalds } 22401da177e4SLinus Torvalds return 0; 22411da177e4SLinus Torvalds done: 22426756ae4bSStephen Hemminger rtnl_unlock(); 22431da177e4SLinus Torvalds return err; 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds /* 22471da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 22481da177e4SLinus Torvalds */ 2249c0cda068SAl Viro int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif) 22501da177e4SLinus Torvalds { 22511da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22521da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22531da177e4SLinus Torvalds struct ip_sf_socklist *psl; 22541da177e4SLinus Torvalds int i; 2255c85bb41eSFlavio Leitner int ret; 22561da177e4SLinus Torvalds 2257c85bb41eSFlavio Leitner ret = 1; 2258f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 2259c85bb41eSFlavio Leitner goto out; 22601da177e4SLinus Torvalds 2261c85bb41eSFlavio Leitner rcu_read_lock(); 22621d7138deSEric Dumazet for_each_pmc_rcu(inet, pmc) { 22631da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 22641da177e4SLinus Torvalds pmc->multi.imr_ifindex == dif) 22651da177e4SLinus Torvalds break; 22661da177e4SLinus Torvalds } 2267c85bb41eSFlavio Leitner ret = inet->mc_all; 22681da177e4SLinus Torvalds if (!pmc) 2269c85bb41eSFlavio Leitner goto unlock; 22701d7138deSEric Dumazet psl = rcu_dereference(pmc->sflist); 2271c85bb41eSFlavio Leitner ret = (pmc->sfmode == MCAST_EXCLUDE); 22721da177e4SLinus Torvalds if (!psl) 2273c85bb41eSFlavio Leitner goto unlock; 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 22761da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 22771da177e4SLinus Torvalds break; 22781da177e4SLinus Torvalds } 2279c85bb41eSFlavio Leitner ret = 0; 22801da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 2281c85bb41eSFlavio Leitner goto unlock; 22821da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 2283c85bb41eSFlavio Leitner goto unlock; 2284c85bb41eSFlavio Leitner ret = 1; 2285c85bb41eSFlavio Leitner unlock: 2286c85bb41eSFlavio Leitner rcu_read_unlock(); 2287c85bb41eSFlavio Leitner out: 2288c85bb41eSFlavio Leitner return ret; 22891da177e4SLinus Torvalds } 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds /* 22921da177e4SLinus Torvalds * A socket is closing. 22931da177e4SLinus Torvalds */ 22941da177e4SLinus Torvalds 22951da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 22961da177e4SLinus Torvalds { 22971da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22981da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2299877acedcSDaniel Lezcano struct net *net = sock_net(sk); 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds if (inet->mc_list == NULL) 23021da177e4SLinus Torvalds return; 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds rtnl_lock(); 23051d7138deSEric Dumazet while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { 23061da177e4SLinus Torvalds struct in_device *in_dev; 23071da177e4SLinus Torvalds 23081d7138deSEric Dumazet inet->mc_list = iml->next_rcu; 2309877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 23101da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 231118943d29SEric Dumazet if (in_dev != NULL) 23121da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2313c85bb41eSFlavio Leitner /* decrease mem now to avoid the memleak warning */ 2314c85bb41eSFlavio Leitner atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 231510d50e74SLai Jiangshan kfree_rcu(iml, rcu); 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds rtnl_unlock(); 23181da177e4SLinus Torvalds } 23191da177e4SLinus Torvalds 2320dbdd9a52SDavid S. Miller /* called with rcu_read_lock() */ 2321dbdd9a52SDavid S. Miller int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto) 23221da177e4SLinus Torvalds { 23231da177e4SLinus Torvalds struct ip_mc_list *im; 23241da177e4SLinus Torvalds struct ip_sf_list *psf; 23251da177e4SLinus Torvalds int rv = 0; 23261da177e4SLinus Torvalds 23271d7138deSEric Dumazet for_each_pmc_rcu(in_dev, im) { 23281da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 23291da177e4SLinus Torvalds break; 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 23321da177e4SLinus Torvalds rv = 1; 23331da177e4SLinus Torvalds } else if (im) { 23341da177e4SLinus Torvalds if (src_addr) { 23351da177e4SLinus Torvalds for (psf=im->sources; psf; psf=psf->sf_next) { 23361da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 23371da177e4SLinus Torvalds break; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds if (psf) 23401da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 23411da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 23421da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 23431da177e4SLinus Torvalds else 23441da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 23451da177e4SLinus Torvalds } else 23461da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds return rv; 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 23521da177e4SLinus Torvalds struct igmp_mc_iter_state { 23537091e728SAlexey Dobriyan struct seq_net_private p; 23541da177e4SLinus Torvalds struct net_device *dev; 23551da177e4SLinus Torvalds struct in_device *in_dev; 23561da177e4SLinus Torvalds }; 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 23611da177e4SLinus Torvalds { 23627091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 23631da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 23641da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 23651da177e4SLinus Torvalds 23667562f876SPavel Emelianov state->in_dev = NULL; 236761fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 23681da177e4SLinus Torvalds struct in_device *in_dev; 23696baff150SEric Dumazet 23706baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 23711da177e4SLinus Torvalds if (!in_dev) 23721da177e4SLinus Torvalds continue; 23731d7138deSEric Dumazet im = rcu_dereference(in_dev->mc_list); 23741da177e4SLinus Torvalds if (im) { 23751da177e4SLinus Torvalds state->in_dev = in_dev; 23761da177e4SLinus Torvalds break; 23771da177e4SLinus Torvalds } 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds return im; 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 23831da177e4SLinus Torvalds { 23841da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 23856baff150SEric Dumazet 23861d7138deSEric Dumazet im = rcu_dereference(im->next_rcu); 23871d7138deSEric Dumazet while (!im) { 23886baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 23891da177e4SLinus Torvalds if (!state->dev) { 23901da177e4SLinus Torvalds state->in_dev = NULL; 23911da177e4SLinus Torvalds break; 23921da177e4SLinus Torvalds } 23936baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 23941da177e4SLinus Torvalds if (!state->in_dev) 23951da177e4SLinus Torvalds continue; 23961d7138deSEric Dumazet im = rcu_dereference(state->in_dev->mc_list); 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds return im; 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 24021da177e4SLinus Torvalds { 24031da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 24041da177e4SLinus Torvalds if (im) 24051da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 24061da177e4SLinus Torvalds --pos; 24071da177e4SLinus Torvalds return pos ? NULL : im; 24081da177e4SLinus Torvalds } 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 241161fbab77Sstephen hemminger __acquires(rcu) 24121da177e4SLinus Torvalds { 241361fbab77Sstephen hemminger rcu_read_lock(); 24141da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 24181da177e4SLinus Torvalds { 24191da177e4SLinus Torvalds struct ip_mc_list *im; 24201da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 24211da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 24221da177e4SLinus Torvalds else 24231da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 24241da177e4SLinus Torvalds ++*pos; 24251da177e4SLinus Torvalds return im; 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 242961fbab77Sstephen hemminger __releases(rcu) 24301da177e4SLinus Torvalds { 24311da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24321d7138deSEric Dumazet 24331da177e4SLinus Torvalds state->in_dev = NULL; 24341da177e4SLinus Torvalds state->dev = NULL; 243561fbab77Sstephen hemminger rcu_read_unlock(); 24361da177e4SLinus Torvalds } 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 24391da177e4SLinus Torvalds { 24401da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 24411da177e4SLinus Torvalds seq_puts(seq, 24421da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 24431da177e4SLinus Torvalds else { 24441da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 24451da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24461da177e4SLinus Torvalds char *querier; 2447a399a805SEric Dumazet long delta; 2448a399a805SEric Dumazet 24491da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 24501da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 24511da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 24521da177e4SLinus Torvalds "V3"; 24531da177e4SLinus Torvalds #else 24541da177e4SLinus Torvalds querier = "NONE"; 24551da177e4SLinus Torvalds #endif 24561da177e4SLinus Torvalds 24571d7138deSEric Dumazet if (rcu_dereference(state->in_dev->mc_list) == im) { 24581da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2459b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 2462a399a805SEric Dumazet delta = im->timer.expires - jiffies; 24631da177e4SLinus Torvalds seq_printf(seq, 2464338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 24651da177e4SLinus Torvalds im->multiaddr, im->users, 2466a399a805SEric Dumazet im->tm_running, 2467a399a805SEric Dumazet im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, 24681da177e4SLinus Torvalds im->reporter); 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds return 0; 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds 2473f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 24741da177e4SLinus Torvalds .start = igmp_mc_seq_start, 24751da177e4SLinus Torvalds .next = igmp_mc_seq_next, 24761da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 24771da177e4SLinus Torvalds .show = igmp_mc_seq_show, 24781da177e4SLinus Torvalds }; 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file) 24811da177e4SLinus Torvalds { 24827091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mc_seq_ops, 2483cf7732e4SPavel Emelyanov sizeof(struct igmp_mc_iter_state)); 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds 24869a32144eSArjan van de Ven static const struct file_operations igmp_mc_seq_fops = { 24871da177e4SLinus Torvalds .owner = THIS_MODULE, 24881da177e4SLinus Torvalds .open = igmp_mc_seq_open, 24891da177e4SLinus Torvalds .read = seq_read, 24901da177e4SLinus Torvalds .llseek = seq_lseek, 24917091e728SAlexey Dobriyan .release = seq_release_net, 24921da177e4SLinus Torvalds }; 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds struct igmp_mcf_iter_state { 24957091e728SAlexey Dobriyan struct seq_net_private p; 24961da177e4SLinus Torvalds struct net_device *dev; 24971da177e4SLinus Torvalds struct in_device *idev; 24981da177e4SLinus Torvalds struct ip_mc_list *im; 24991da177e4SLinus Torvalds }; 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 25041da177e4SLinus Torvalds { 25057091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 25061da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 25071da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 25081da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 25091da177e4SLinus Torvalds 25107562f876SPavel Emelianov state->idev = NULL; 25117562f876SPavel Emelianov state->im = NULL; 251261fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 25131da177e4SLinus Torvalds struct in_device *idev; 25146baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 25151da177e4SLinus Torvalds if (unlikely(idev == NULL)) 25161da177e4SLinus Torvalds continue; 25171d7138deSEric Dumazet im = rcu_dereference(idev->mc_list); 25181da177e4SLinus Torvalds if (likely(im != NULL)) { 25191da177e4SLinus Torvalds spin_lock_bh(&im->lock); 25201da177e4SLinus Torvalds psf = im->sources; 25211da177e4SLinus Torvalds if (likely(psf != NULL)) { 25221da177e4SLinus Torvalds state->im = im; 25231da177e4SLinus Torvalds state->idev = idev; 25241da177e4SLinus Torvalds break; 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds } 25291da177e4SLinus Torvalds return psf; 25301da177e4SLinus Torvalds } 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 25331da177e4SLinus Torvalds { 25341da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds psf = psf->sf_next; 25371da177e4SLinus Torvalds while (!psf) { 25381da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 25391da177e4SLinus Torvalds state->im = state->im->next; 25401da177e4SLinus Torvalds while (!state->im) { 25416baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 25421da177e4SLinus Torvalds if (!state->dev) { 25431da177e4SLinus Torvalds state->idev = NULL; 25441da177e4SLinus Torvalds goto out; 25451da177e4SLinus Torvalds } 25466baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 25471da177e4SLinus Torvalds if (!state->idev) 25481da177e4SLinus Torvalds continue; 25491d7138deSEric Dumazet state->im = rcu_dereference(state->idev->mc_list); 25501da177e4SLinus Torvalds } 25511da177e4SLinus Torvalds if (!state->im) 25521da177e4SLinus Torvalds break; 25531da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 25541da177e4SLinus Torvalds psf = state->im->sources; 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds out: 25571da177e4SLinus Torvalds return psf; 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 25611da177e4SLinus Torvalds { 25621da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 25631da177e4SLinus Torvalds if (psf) 25641da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 25651da177e4SLinus Torvalds --pos; 25661da177e4SLinus Torvalds return pos ? NULL : psf; 25671da177e4SLinus Torvalds } 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 257061fbab77Sstephen hemminger __acquires(rcu) 25711da177e4SLinus Torvalds { 257261fbab77Sstephen hemminger rcu_read_lock(); 25731da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25771da177e4SLinus Torvalds { 25781da177e4SLinus Torvalds struct ip_sf_list *psf; 25791da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 25801da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 25811da177e4SLinus Torvalds else 25821da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 25831da177e4SLinus Torvalds ++*pos; 25841da177e4SLinus Torvalds return psf; 25851da177e4SLinus Torvalds } 25861da177e4SLinus Torvalds 25871da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 258861fbab77Sstephen hemminger __releases(rcu) 25891da177e4SLinus Torvalds { 25901da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 25911da177e4SLinus Torvalds if (likely(state->im != NULL)) { 25921da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 25931da177e4SLinus Torvalds state->im = NULL; 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds state->idev = NULL; 25961da177e4SLinus Torvalds state->dev = NULL; 259761fbab77Sstephen hemminger rcu_read_unlock(); 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 26011da177e4SLinus Torvalds { 26021da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 26031da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 26061da177e4SLinus Torvalds seq_printf(seq, 26071da177e4SLinus Torvalds "%3s %6s " 26081da177e4SLinus Torvalds "%10s %10s %6s %6s\n", "Idx", 26091da177e4SLinus Torvalds "Device", "MCA", 26101da177e4SLinus Torvalds "SRC", "INC", "EXC"); 26111da177e4SLinus Torvalds } else { 26121da177e4SLinus Torvalds seq_printf(seq, 26131da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 26141da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 26151da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 26161da177e4SLinus Torvalds ntohl(state->im->multiaddr), 26171da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 26181da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 26191da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds return 0; 26221da177e4SLinus Torvalds } 26231da177e4SLinus Torvalds 2624f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 26251da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 26261da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 26271da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 26281da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 26291da177e4SLinus Torvalds }; 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file) 26321da177e4SLinus Torvalds { 26337091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mcf_seq_ops, 2634cf7732e4SPavel Emelyanov sizeof(struct igmp_mcf_iter_state)); 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 26379a32144eSArjan van de Ven static const struct file_operations igmp_mcf_seq_fops = { 26381da177e4SLinus Torvalds .owner = THIS_MODULE, 26391da177e4SLinus Torvalds .open = igmp_mcf_seq_open, 26401da177e4SLinus Torvalds .read = seq_read, 26411da177e4SLinus Torvalds .llseek = seq_lseek, 26427091e728SAlexey Dobriyan .release = seq_release_net, 26437091e728SAlexey Dobriyan }; 26447091e728SAlexey Dobriyan 26452c8c1e72SAlexey Dobriyan static int __net_init igmp_net_init(struct net *net) 26467091e728SAlexey Dobriyan { 26477091e728SAlexey Dobriyan struct proc_dir_entry *pde; 26487091e728SAlexey Dobriyan 2649*d4beaa66SGao feng pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops); 26507091e728SAlexey Dobriyan if (!pde) 26517091e728SAlexey Dobriyan goto out_igmp; 2652*d4beaa66SGao feng pde = proc_create("mcfilter", S_IRUGO, net->proc_net, 2653*d4beaa66SGao feng &igmp_mcf_seq_fops); 26547091e728SAlexey Dobriyan if (!pde) 26557091e728SAlexey Dobriyan goto out_mcfilter; 26567091e728SAlexey Dobriyan return 0; 26577091e728SAlexey Dobriyan 26587091e728SAlexey Dobriyan out_mcfilter: 26597091e728SAlexey Dobriyan proc_net_remove(net, "igmp"); 26607091e728SAlexey Dobriyan out_igmp: 26617091e728SAlexey Dobriyan return -ENOMEM; 26627091e728SAlexey Dobriyan } 26637091e728SAlexey Dobriyan 26642c8c1e72SAlexey Dobriyan static void __net_exit igmp_net_exit(struct net *net) 26657091e728SAlexey Dobriyan { 26667091e728SAlexey Dobriyan proc_net_remove(net, "mcfilter"); 26677091e728SAlexey Dobriyan proc_net_remove(net, "igmp"); 26687091e728SAlexey Dobriyan } 26697091e728SAlexey Dobriyan 26707091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 26717091e728SAlexey Dobriyan .init = igmp_net_init, 26727091e728SAlexey Dobriyan .exit = igmp_net_exit, 26731da177e4SLinus Torvalds }; 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds int __init igmp_mc_proc_init(void) 26761da177e4SLinus Torvalds { 26777091e728SAlexey Dobriyan return register_pernet_subsys(&igmp_net_ops); 26781da177e4SLinus Torvalds } 26791da177e4SLinus Torvalds #endif 2680