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> 741da177e4SLinus Torvalds #include <asm/uaccess.h> 751da177e4SLinus Torvalds #include <asm/system.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); 1551da177e4SLinus Torvalds kfree(im); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* 1621da177e4SLinus Torvalds * Timer management 1631da177e4SLinus Torvalds */ 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds static __inline__ void igmp_stop_timer(struct ip_mc_list *im) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds spin_lock_bh(&im->lock); 1681da177e4SLinus Torvalds if (del_timer(&im->timer)) 1691da177e4SLinus Torvalds atomic_dec(&im->refcnt); 1701da177e4SLinus Torvalds im->tm_running = 0; 1711da177e4SLinus Torvalds im->reporter = 0; 1721da177e4SLinus Torvalds im->unsolicit_count = 0; 1731da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds /* It must be called with locked im->lock */ 1771da177e4SLinus Torvalds static void igmp_start_timer(struct ip_mc_list *im, int max_delay) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds int tv = net_random() % max_delay; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds im->tm_running = 1; 1821da177e4SLinus Torvalds if (!mod_timer(&im->timer, jiffies+tv+2)) 1831da177e4SLinus Torvalds atomic_inc(&im->refcnt); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds static void igmp_gq_start_timer(struct in_device *in_dev) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds int tv = net_random() % in_dev->mr_maxdelay; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds in_dev->mr_gq_running = 1; 1911da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2)) 1921da177e4SLinus Torvalds in_dev_hold(in_dev); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds int tv = net_random() % delay; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) 2001da177e4SLinus Torvalds in_dev_hold(in_dev); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds spin_lock_bh(&im->lock); 2061da177e4SLinus Torvalds im->unsolicit_count = 0; 2071da177e4SLinus Torvalds if (del_timer(&im->timer)) { 2081da177e4SLinus Torvalds if ((long)(im->timer.expires-jiffies) < max_delay) { 2091da177e4SLinus Torvalds add_timer(&im->timer); 2101da177e4SLinus Torvalds im->tm_running = 1; 2111da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2121da177e4SLinus Torvalds return; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds atomic_dec(&im->refcnt); 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds igmp_start_timer(im, max_delay); 2171da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* 2221da177e4SLinus Torvalds * Send an IGMP report. 2231da177e4SLinus Torvalds */ 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, 2291da177e4SLinus Torvalds int gdeleted, int sdeleted) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds switch (type) { 2321da177e4SLinus Torvalds case IGMPV3_MODE_IS_INCLUDE: 2331da177e4SLinus Torvalds case IGMPV3_MODE_IS_EXCLUDE: 2341da177e4SLinus Torvalds if (gdeleted || sdeleted) 2351da177e4SLinus Torvalds return 0; 236ad12583fSDavid L Stevens if (!(pmc->gsquery && !psf->sf_gsresp)) { 237ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_INCLUDE) 238ad12583fSDavid L Stevens return 1; 239ad12583fSDavid L Stevens /* don't include if this source is excluded 240ad12583fSDavid L Stevens * in all filters 241ad12583fSDavid L Stevens */ 242ad12583fSDavid L Stevens if (psf->sf_count[MCAST_INCLUDE]) 243ad12583fSDavid L Stevens return type == IGMPV3_MODE_IS_INCLUDE; 244ad12583fSDavid L Stevens return pmc->sfcount[MCAST_EXCLUDE] == 245ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]; 246ad12583fSDavid L Stevens } 247ad12583fSDavid L Stevens return 0; 2481da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_INCLUDE: 2491da177e4SLinus Torvalds if (gdeleted || sdeleted) 2501da177e4SLinus Torvalds return 0; 2511da177e4SLinus Torvalds return psf->sf_count[MCAST_INCLUDE] != 0; 2521da177e4SLinus Torvalds case IGMPV3_CHANGE_TO_EXCLUDE: 2531da177e4SLinus Torvalds if (gdeleted || sdeleted) 2541da177e4SLinus Torvalds return 0; 2551da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE] == 0 || 2561da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE]) 2571da177e4SLinus Torvalds return 0; 2581da177e4SLinus Torvalds return pmc->sfcount[MCAST_EXCLUDE] == 2591da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]; 2601da177e4SLinus Torvalds case IGMPV3_ALLOW_NEW_SOURCES: 2611da177e4SLinus Torvalds if (gdeleted || !psf->sf_crcount) 2621da177e4SLinus Torvalds return 0; 2631da177e4SLinus Torvalds return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; 2641da177e4SLinus Torvalds case IGMPV3_BLOCK_OLD_SOURCES: 2651da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) 2661da177e4SLinus Torvalds return gdeleted || (psf->sf_crcount && sdeleted); 2671da177e4SLinus Torvalds return psf->sf_crcount && !gdeleted && !sdeleted; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds return 0; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds static int 2731da177e4SLinus Torvalds igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) 2741da177e4SLinus Torvalds { 2751da177e4SLinus Torvalds struct ip_sf_list *psf; 2761da177e4SLinus Torvalds int scount = 0; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 2791da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) 2801da177e4SLinus Torvalds continue; 2811da177e4SLinus Torvalds scount++; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds return scount; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds struct sk_buff *skb; 2891da177e4SLinus Torvalds struct rtable *rt; 2901da177e4SLinus Torvalds struct iphdr *pip; 2911da177e4SLinus Torvalds struct igmpv3_report *pig; 292877acedcSDaniel Lezcano struct net *net = dev_net(dev); 2931da177e4SLinus Torvalds 294f5184d26SJohannes Berg skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC); 2951da177e4SLinus Torvalds if (skb == NULL) 2961da177e4SLinus Torvalds return NULL; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds struct flowi fl = { .oif = dev->ifindex, 3001da177e4SLinus Torvalds .nl_u = { .ip4_u = { 3011da177e4SLinus Torvalds .daddr = IGMPV3_ALL_MCR } }, 3021da177e4SLinus Torvalds .proto = IPPROTO_IGMP }; 303877acedcSDaniel Lezcano if (ip_route_output_key(net, &rt, &fl)) { 3041da177e4SLinus Torvalds kfree_skb(skb); 3051da177e4SLinus Torvalds return NULL; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds if (rt->rt_src == 0) { 3091da177e4SLinus Torvalds kfree_skb(skb); 3101da177e4SLinus Torvalds ip_rt_put(rt); 3111da177e4SLinus Torvalds return NULL; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 314adf30907SEric Dumazet skb_dst_set(skb, &rt->u.dst); 3151da177e4SLinus Torvalds skb->dev = dev; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds skb_reserve(skb, LL_RESERVED_SPACE(dev)); 3181da177e4SLinus Torvalds 3197e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 320eddc9ec5SArnaldo Carvalho de Melo pip = ip_hdr(skb); 3217e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds pip->version = 4; 3241da177e4SLinus Torvalds pip->ihl = (sizeof(struct iphdr)+4)>>2; 3251da177e4SLinus Torvalds pip->tos = 0xc0; 3261da177e4SLinus Torvalds pip->frag_off = htons(IP_DF); 3271da177e4SLinus Torvalds pip->ttl = 1; 3281da177e4SLinus Torvalds pip->daddr = rt->rt_dst; 3291da177e4SLinus Torvalds pip->saddr = rt->rt_src; 3301da177e4SLinus Torvalds pip->protocol = IPPROTO_IGMP; 3311da177e4SLinus Torvalds pip->tot_len = 0; /* filled in later */ 3321da177e4SLinus Torvalds ip_select_ident(pip, &rt->u.dst, NULL); 3331da177e4SLinus Torvalds ((u8*)&pip[1])[0] = IPOPT_RA; 3341da177e4SLinus Torvalds ((u8*)&pip[1])[1] = 4; 3351da177e4SLinus Torvalds ((u8*)&pip[1])[2] = 0; 3361da177e4SLinus Torvalds ((u8*)&pip[1])[3] = 0; 3371da177e4SLinus Torvalds 338b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; 339d10ba34bSArnaldo Carvalho de Melo skb_put(skb, sizeof(*pig)); 340d9edf9e2SArnaldo Carvalho de Melo pig = igmpv3_report_hdr(skb); 3411da177e4SLinus Torvalds pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; 3421da177e4SLinus Torvalds pig->resv1 = 0; 3431da177e4SLinus Torvalds pig->csum = 0; 3441da177e4SLinus Torvalds pig->resv2 = 0; 3451da177e4SLinus Torvalds pig->ngrec = 0; 3461da177e4SLinus Torvalds return skb; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds static int igmpv3_sendpack(struct sk_buff *skb) 3501da177e4SLinus Torvalds { 351d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *pig = igmp_hdr(skb); 35227a884dcSArnaldo Carvalho de Melo const int igmplen = skb->tail - skb->transport_header; 3531da177e4SLinus Torvalds 354d9edf9e2SArnaldo Carvalho de Melo pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); 3551da177e4SLinus Torvalds 356c439cb2eSHerbert Xu return ip_local_out(skb); 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) 3601da177e4SLinus Torvalds { 3611da177e4SLinus Torvalds return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, 3651da177e4SLinus Torvalds int type, struct igmpv3_grec **ppgr) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 3681da177e4SLinus Torvalds struct igmpv3_report *pih; 3691da177e4SLinus Torvalds struct igmpv3_grec *pgr; 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds if (!skb) 3721da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 3731da177e4SLinus Torvalds if (!skb) 3741da177e4SLinus Torvalds return NULL; 3751da177e4SLinus Torvalds pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec)); 3761da177e4SLinus Torvalds pgr->grec_type = type; 3771da177e4SLinus Torvalds pgr->grec_auxwords = 0; 3781da177e4SLinus Torvalds pgr->grec_nsrcs = 0; 3791da177e4SLinus Torvalds pgr->grec_mca = pmc->multiaddr; 380d9edf9e2SArnaldo Carvalho de Melo pih = igmpv3_report_hdr(skb); 3811da177e4SLinus Torvalds pih->ngrec = htons(ntohs(pih->ngrec)+1); 3821da177e4SLinus Torvalds *ppgr = pgr; 3831da177e4SLinus Torvalds return skb; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \ 3871da177e4SLinus Torvalds skb_tailroom(skb)) : 0) 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, 3901da177e4SLinus Torvalds int type, int gdeleted, int sdeleted) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds struct net_device *dev = pmc->interface->dev; 3931da177e4SLinus Torvalds struct igmpv3_report *pih; 3941da177e4SLinus Torvalds struct igmpv3_grec *pgr = NULL; 3951da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; 396ad12583fSDavid L Stevens int scount, stotal, first, isquery, truncate; 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 3991da177e4SLinus Torvalds return skb; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds isquery = type == IGMPV3_MODE_IS_INCLUDE || 4021da177e4SLinus Torvalds type == IGMPV3_MODE_IS_EXCLUDE; 4031da177e4SLinus Torvalds truncate = type == IGMPV3_MODE_IS_EXCLUDE || 4041da177e4SLinus Torvalds type == IGMPV3_CHANGE_TO_EXCLUDE; 4051da177e4SLinus Torvalds 406ad12583fSDavid L Stevens stotal = scount = 0; 407ad12583fSDavid L Stevens 4081da177e4SLinus Torvalds psf_list = sdeleted ? &pmc->tomb : &pmc->sources; 4091da177e4SLinus Torvalds 410ad12583fSDavid L Stevens if (!*psf_list) 411ad12583fSDavid L Stevens goto empty_source; 412ad12583fSDavid L Stevens 413d9edf9e2SArnaldo Carvalho de Melo pih = skb ? igmpv3_report_hdr(skb) : NULL; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* EX and TO_EX get a fresh packet, if needed */ 4161da177e4SLinus Torvalds if (truncate) { 4171da177e4SLinus Torvalds if (pih && pih->ngrec && 4181da177e4SLinus Torvalds AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { 4191da177e4SLinus Torvalds if (skb) 4201da177e4SLinus Torvalds igmpv3_sendpack(skb); 4211da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds first = 1; 4251da177e4SLinus Torvalds psf_prev = NULL; 4261da177e4SLinus Torvalds for (psf=*psf_list; psf; psf=psf_next) { 427ea4d9e72SAl Viro __be32 *psrc; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds psf_next = psf->sf_next; 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { 4321da177e4SLinus Torvalds psf_prev = psf; 4331da177e4SLinus Torvalds continue; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* clear marks on query responses */ 4371da177e4SLinus Torvalds if (isquery) 4381da177e4SLinus Torvalds psf->sf_gsresp = 0; 4391da177e4SLinus Torvalds 44063007727SAl Viro if (AVAILABLE(skb) < sizeof(__be32) + 4411da177e4SLinus Torvalds first*sizeof(struct igmpv3_grec)) { 4421da177e4SLinus Torvalds if (truncate && !first) 4431da177e4SLinus Torvalds break; /* truncate these */ 4441da177e4SLinus Torvalds if (pgr) 4451da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 4461da177e4SLinus Torvalds if (skb) 4471da177e4SLinus Torvalds igmpv3_sendpack(skb); 4481da177e4SLinus Torvalds skb = igmpv3_newpack(dev, dev->mtu); 4491da177e4SLinus Torvalds first = 1; 4501da177e4SLinus Torvalds scount = 0; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds if (first) { 4531da177e4SLinus Torvalds skb = add_grhead(skb, pmc, type, &pgr); 4541da177e4SLinus Torvalds first = 0; 4551da177e4SLinus Torvalds } 456cc63f70bSAlexey Dobriyan if (!skb) 457cc63f70bSAlexey Dobriyan return NULL; 45863007727SAl Viro psrc = (__be32 *)skb_put(skb, sizeof(__be32)); 4591da177e4SLinus Torvalds *psrc = psf->sf_inaddr; 460ad12583fSDavid L Stevens scount++; stotal++; 4611da177e4SLinus Torvalds if ((type == IGMPV3_ALLOW_NEW_SOURCES || 4621da177e4SLinus Torvalds type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { 4631da177e4SLinus Torvalds psf->sf_crcount--; 4641da177e4SLinus Torvalds if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { 4651da177e4SLinus Torvalds if (psf_prev) 4661da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 4671da177e4SLinus Torvalds else 4681da177e4SLinus Torvalds *psf_list = psf->sf_next; 4691da177e4SLinus Torvalds kfree(psf); 4701da177e4SLinus Torvalds continue; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds psf_prev = psf; 4741da177e4SLinus Torvalds } 475ad12583fSDavid L Stevens 476ad12583fSDavid L Stevens empty_source: 477ad12583fSDavid L Stevens if (!stotal) { 478ad12583fSDavid L Stevens if (type == IGMPV3_ALLOW_NEW_SOURCES || 479ad12583fSDavid L Stevens type == IGMPV3_BLOCK_OLD_SOURCES) 480ad12583fSDavid L Stevens return skb; 481ad12583fSDavid L Stevens if (pmc->crcount || isquery) { 482ad12583fSDavid L Stevens /* make sure we have room for group header */ 483ad12583fSDavid L Stevens if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) { 484ad12583fSDavid L Stevens igmpv3_sendpack(skb); 485ad12583fSDavid L Stevens skb = NULL; /* add_grhead will get a new one */ 486ad12583fSDavid L Stevens } 487ad12583fSDavid L Stevens skb = add_grhead(skb, pmc, type, &pgr); 488ad12583fSDavid L Stevens } 489ad12583fSDavid L Stevens } 4901da177e4SLinus Torvalds if (pgr) 4911da177e4SLinus Torvalds pgr->grec_nsrcs = htons(scount); 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds if (isquery) 4941da177e4SLinus Torvalds pmc->gsquery = 0; /* clear query state on report */ 4951da177e4SLinus Torvalds return skb; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct sk_buff *skb = NULL; 5011da177e4SLinus Torvalds int type; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (!pmc) { 5041da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 5051da177e4SLinus Torvalds for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) { 5061da177e4SLinus Torvalds if (pmc->multiaddr == IGMP_ALL_HOSTS) 5071da177e4SLinus Torvalds continue; 5081da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5091da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5101da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5111da177e4SLinus Torvalds else 5121da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5131da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5141da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 5171da177e4SLinus Torvalds } else { 5181da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5191da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 5201da177e4SLinus Torvalds type = IGMPV3_MODE_IS_EXCLUDE; 5211da177e4SLinus Torvalds else 5221da177e4SLinus Torvalds type = IGMPV3_MODE_IS_INCLUDE; 5231da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 5241da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds if (!skb) 5271da177e4SLinus Torvalds return 0; 5281da177e4SLinus Torvalds return igmpv3_sendpack(skb); 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* 5321da177e4SLinus Torvalds * remove zero-count source records from a source filter list 5331da177e4SLinus Torvalds */ 5341da177e4SLinus Torvalds static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds struct ip_sf_list *psf_prev, *psf_next, *psf; 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds psf_prev = NULL; 5391da177e4SLinus Torvalds for (psf=*ppsf; psf; psf = psf_next) { 5401da177e4SLinus Torvalds psf_next = psf->sf_next; 5411da177e4SLinus Torvalds if (psf->sf_crcount == 0) { 5421da177e4SLinus Torvalds if (psf_prev) 5431da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 5441da177e4SLinus Torvalds else 5451da177e4SLinus Torvalds *ppsf = psf->sf_next; 5461da177e4SLinus Torvalds kfree(psf); 5471da177e4SLinus Torvalds } else 5481da177e4SLinus Torvalds psf_prev = psf; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds static void igmpv3_send_cr(struct in_device *in_dev) 5531da177e4SLinus Torvalds { 5541da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev, *pmc_next; 5551da177e4SLinus Torvalds struct sk_buff *skb = NULL; 5561da177e4SLinus Torvalds int type, dtype; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 5591da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds /* deleted MCA's */ 5621da177e4SLinus Torvalds pmc_prev = NULL; 5631da177e4SLinus Torvalds for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) { 5641da177e4SLinus Torvalds pmc_next = pmc->next; 5651da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 5661da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 5671da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 5681da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 5691da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 1, 1); 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds if (pmc->crcount) { 5721da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) { 5731da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 5741da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 1, 0); 5751da177e4SLinus Torvalds } 576ad12583fSDavid L Stevens pmc->crcount--; 5771da177e4SLinus Torvalds if (pmc->crcount == 0) { 5781da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->tomb); 5791da177e4SLinus Torvalds igmpv3_clear_zeros(&pmc->sources); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { 5831da177e4SLinus Torvalds if (pmc_prev) 5841da177e4SLinus Torvalds pmc_prev->next = pmc_next; 5851da177e4SLinus Torvalds else 5861da177e4SLinus Torvalds in_dev->mc_tomb = pmc_next; 5871da177e4SLinus Torvalds in_dev_put(pmc->interface); 5881da177e4SLinus Torvalds kfree(pmc); 5891da177e4SLinus Torvalds } else 5901da177e4SLinus Torvalds pmc_prev = pmc; 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds /* change recs */ 5951da177e4SLinus Torvalds for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) { 5961da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 5971da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 5981da177e4SLinus Torvalds type = IGMPV3_BLOCK_OLD_SOURCES; 5991da177e4SLinus Torvalds dtype = IGMPV3_ALLOW_NEW_SOURCES; 6001da177e4SLinus Torvalds } else { 6011da177e4SLinus Torvalds type = IGMPV3_ALLOW_NEW_SOURCES; 6021da177e4SLinus Torvalds dtype = IGMPV3_BLOCK_OLD_SOURCES; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 6051da177e4SLinus Torvalds skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* filter mode changes */ 6081da177e4SLinus Torvalds if (pmc->crcount) { 6091da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE) 6101da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_EXCLUDE; 6111da177e4SLinus Torvalds else 6121da177e4SLinus Torvalds type = IGMPV3_CHANGE_TO_INCLUDE; 6131da177e4SLinus Torvalds skb = add_grec(skb, pmc, type, 0, 0); 614ad12583fSDavid L Stevens pmc->crcount--; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds if (!skb) 6211da177e4SLinus Torvalds return; 6221da177e4SLinus Torvalds (void) igmpv3_sendpack(skb); 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, 6261da177e4SLinus Torvalds int type) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds struct sk_buff *skb; 6291da177e4SLinus Torvalds struct iphdr *iph; 6301da177e4SLinus Torvalds struct igmphdr *ih; 6311da177e4SLinus Torvalds struct rtable *rt; 6321da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 633877acedcSDaniel Lezcano struct net *net = dev_net(dev); 63463007727SAl Viro __be32 group = pmc ? pmc->multiaddr : 0; 63563007727SAl Viro __be32 dst; 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) 6381da177e4SLinus Torvalds return igmpv3_send_report(in_dev, pmc); 6391da177e4SLinus Torvalds else if (type == IGMP_HOST_LEAVE_MESSAGE) 6401da177e4SLinus Torvalds dst = IGMP_ALL_ROUTER; 6411da177e4SLinus Torvalds else 6421da177e4SLinus Torvalds dst = group; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds { 6451da177e4SLinus Torvalds struct flowi fl = { .oif = dev->ifindex, 6461da177e4SLinus Torvalds .nl_u = { .ip4_u = { .daddr = dst } }, 6471da177e4SLinus Torvalds .proto = IPPROTO_IGMP }; 648877acedcSDaniel Lezcano if (ip_route_output_key(net, &rt, &fl)) 6491da177e4SLinus Torvalds return -1; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds if (rt->rt_src == 0) { 6521da177e4SLinus Torvalds ip_rt_put(rt); 6531da177e4SLinus Torvalds return -1; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 656f5184d26SJohannes Berg skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC); 6571da177e4SLinus Torvalds if (skb == NULL) { 6581da177e4SLinus Torvalds ip_rt_put(rt); 6591da177e4SLinus Torvalds return -1; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds 662adf30907SEric Dumazet skb_dst_set(skb, &rt->u.dst); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds skb_reserve(skb, LL_RESERVED_SPACE(dev)); 6651da177e4SLinus Torvalds 6667e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 667eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 6687e28ecc2SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + 4); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds iph->version = 4; 6711da177e4SLinus Torvalds iph->ihl = (sizeof(struct iphdr)+4)>>2; 6721da177e4SLinus Torvalds iph->tos = 0xc0; 6731da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 6741da177e4SLinus Torvalds iph->ttl = 1; 6751da177e4SLinus Torvalds iph->daddr = dst; 6761da177e4SLinus Torvalds iph->saddr = rt->rt_src; 6771da177e4SLinus Torvalds iph->protocol = IPPROTO_IGMP; 6781da177e4SLinus Torvalds ip_select_ident(iph, &rt->u.dst, NULL); 6791da177e4SLinus Torvalds ((u8*)&iph[1])[0] = IPOPT_RA; 6801da177e4SLinus Torvalds ((u8*)&iph[1])[1] = 4; 6811da177e4SLinus Torvalds ((u8*)&iph[1])[2] = 0; 6821da177e4SLinus Torvalds ((u8*)&iph[1])[3] = 0; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 6851da177e4SLinus Torvalds ih->type = type; 6861da177e4SLinus Torvalds ih->code = 0; 6871da177e4SLinus Torvalds ih->csum = 0; 6881da177e4SLinus Torvalds ih->group = group; 6891da177e4SLinus Torvalds ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 6901da177e4SLinus Torvalds 691c439cb2eSHerbert Xu return ip_local_out(skb); 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds static void igmp_gq_timer_expire(unsigned long data) 6951da177e4SLinus Torvalds { 6961da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 6991da177e4SLinus Torvalds igmpv3_send_report(in_dev, NULL); 7001da177e4SLinus Torvalds __in_dev_put(in_dev); 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds static void igmp_ifc_timer_expire(unsigned long data) 7041da177e4SLinus Torvalds { 7051da177e4SLinus Torvalds struct in_device *in_dev = (struct in_device *)data; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds igmpv3_send_cr(in_dev); 7081da177e4SLinus Torvalds if (in_dev->mr_ifc_count) { 7091da177e4SLinus Torvalds in_dev->mr_ifc_count--; 7101da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval); 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds __in_dev_put(in_dev); 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds static void igmp_ifc_event(struct in_device *in_dev) 7161da177e4SLinus Torvalds { 7171da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) 7181da177e4SLinus Torvalds return; 7191da177e4SLinus Torvalds in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv : 7201da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 7211da177e4SLinus Torvalds igmp_ifc_start_timer(in_dev, 1); 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds static void igmp_timer_expire(unsigned long data) 7261da177e4SLinus Torvalds { 7271da177e4SLinus Torvalds struct ip_mc_list *im=(struct ip_mc_list *)data; 7281da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds spin_lock(&im->lock); 7311da177e4SLinus Torvalds im->tm_running = 0; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds if (im->unsolicit_count) { 7341da177e4SLinus Torvalds im->unsolicit_count--; 7351da177e4SLinus Torvalds igmp_start_timer(im, IGMP_Unsolicited_Report_Interval); 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds im->reporter = 1; 7381da177e4SLinus Torvalds spin_unlock(&im->lock); 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 7411da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); 7421da177e4SLinus Torvalds else if (IGMP_V2_SEEN(in_dev)) 7431da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); 7441da177e4SLinus Torvalds else 7451da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds ip_ma_put(im); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 750ad12583fSDavid L Stevens /* mark EXCLUDE-mode sources */ 751ea4d9e72SAl Viro static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 7521da177e4SLinus Torvalds { 7531da177e4SLinus Torvalds struct ip_sf_list *psf; 7541da177e4SLinus Torvalds int i, scount; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds scount = 0; 7571da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 7581da177e4SLinus Torvalds if (scount == nsrcs) 7591da177e4SLinus Torvalds break; 760ad12583fSDavid L Stevens for (i=0; i<nsrcs; i++) { 761ad12583fSDavid L Stevens /* skip inactive filters */ 762ad12583fSDavid L Stevens if (pmc->sfcount[MCAST_INCLUDE] || 763ad12583fSDavid L Stevens pmc->sfcount[MCAST_EXCLUDE] != 764ad12583fSDavid L Stevens psf->sf_count[MCAST_EXCLUDE]) 765ad12583fSDavid L Stevens continue; 766ad12583fSDavid L Stevens if (srcs[i] == psf->sf_inaddr) { 767ad12583fSDavid L Stevens scount++; 768ad12583fSDavid L Stevens break; 769ad12583fSDavid L Stevens } 770ad12583fSDavid L Stevens } 771ad12583fSDavid L Stevens } 772ad12583fSDavid L Stevens pmc->gsquery = 0; 773ad12583fSDavid L Stevens if (scount == nsrcs) /* all sources excluded */ 774ad12583fSDavid L Stevens return 0; 775ad12583fSDavid L Stevens return 1; 776ad12583fSDavid L Stevens } 777ad12583fSDavid L Stevens 778ea4d9e72SAl Viro static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) 779ad12583fSDavid L Stevens { 780ad12583fSDavid L Stevens struct ip_sf_list *psf; 781ad12583fSDavid L Stevens int i, scount; 782ad12583fSDavid L Stevens 783ad12583fSDavid L Stevens if (pmc->sfmode == MCAST_EXCLUDE) 784ad12583fSDavid L Stevens return igmp_xmarksources(pmc, nsrcs, srcs); 785ad12583fSDavid L Stevens 786ad12583fSDavid L Stevens /* mark INCLUDE-mode sources */ 787ad12583fSDavid L Stevens scount = 0; 788ad12583fSDavid L Stevens for (psf=pmc->sources; psf; psf=psf->sf_next) { 789ad12583fSDavid L Stevens if (scount == nsrcs) 790ad12583fSDavid L Stevens break; 7911da177e4SLinus Torvalds for (i=0; i<nsrcs; i++) 7921da177e4SLinus Torvalds if (srcs[i] == psf->sf_inaddr) { 7931da177e4SLinus Torvalds psf->sf_gsresp = 1; 7941da177e4SLinus Torvalds scount++; 7951da177e4SLinus Torvalds break; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds } 798ad12583fSDavid L Stevens if (!scount) { 799ad12583fSDavid L Stevens pmc->gsquery = 0; 800ad12583fSDavid L Stevens return 0; 801ad12583fSDavid L Stevens } 802ad12583fSDavid L Stevens pmc->gsquery = 1; 803ad12583fSDavid L Stevens return 1; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 80663007727SAl Viro static void igmp_heard_report(struct in_device *in_dev, __be32 group) 8071da177e4SLinus Torvalds { 8081da177e4SLinus Torvalds struct ip_mc_list *im; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds /* Timers are only set for non-local groups */ 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds if (group == IGMP_ALL_HOSTS) 8131da177e4SLinus Torvalds return; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 8161da177e4SLinus Torvalds for (im=in_dev->mc_list; im!=NULL; im=im->next) { 8171da177e4SLinus Torvalds if (im->multiaddr == group) { 8181da177e4SLinus Torvalds igmp_stop_timer(im); 8191da177e4SLinus Torvalds break; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, 8261da177e4SLinus Torvalds int len) 8271da177e4SLinus Torvalds { 828d9edf9e2SArnaldo Carvalho de Melo struct igmphdr *ih = igmp_hdr(skb); 829d9edf9e2SArnaldo Carvalho de Melo struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); 8301da177e4SLinus Torvalds struct ip_mc_list *im; 83163007727SAl Viro __be32 group = ih->group; 8321da177e4SLinus Torvalds int max_delay; 8331da177e4SLinus Torvalds int mark = 0; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds if (len == 8) { 8371da177e4SLinus Torvalds if (ih->code == 0) { 8381da177e4SLinus Torvalds /* Alas, old v1 router presents here. */ 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds max_delay = IGMP_Query_Response_Interval; 8411da177e4SLinus Torvalds in_dev->mr_v1_seen = jiffies + 8421da177e4SLinus Torvalds IGMP_V1_Router_Present_Timeout; 8431da177e4SLinus Torvalds group = 0; 8441da177e4SLinus Torvalds } else { 8451da177e4SLinus Torvalds /* v2 router present */ 8461da177e4SLinus Torvalds max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); 8471da177e4SLinus Torvalds in_dev->mr_v2_seen = jiffies + 8481da177e4SLinus Torvalds IGMP_V2_Router_Present_Timeout; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds /* cancel the interface change timer */ 8511da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 8521da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 8531da177e4SLinus Torvalds __in_dev_put(in_dev); 8541da177e4SLinus Torvalds /* clear deleted report items */ 8551da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 8561da177e4SLinus Torvalds } else if (len < 12) { 8571da177e4SLinus Torvalds return; /* ignore bogus packet; freed by caller */ 8581da177e4SLinus Torvalds } else { /* v3 */ 8591da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) 8601da177e4SLinus Torvalds return; 8611da177e4SLinus Torvalds 862d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 8631da177e4SLinus Torvalds if (ih3->nsrcs) { 8641da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 86563007727SAl Viro + ntohs(ih3->nsrcs)*sizeof(__be32))) 8661da177e4SLinus Torvalds return; 867d9edf9e2SArnaldo Carvalho de Melo ih3 = igmpv3_query_hdr(skb); 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); 8711da177e4SLinus Torvalds if (!max_delay) 8721da177e4SLinus Torvalds max_delay = 1; /* can't mod w/ 0 */ 8731da177e4SLinus Torvalds in_dev->mr_maxdelay = max_delay; 8741da177e4SLinus Torvalds if (ih3->qrv) 8751da177e4SLinus Torvalds in_dev->mr_qrv = ih3->qrv; 8761da177e4SLinus Torvalds if (!group) { /* general query */ 8771da177e4SLinus Torvalds if (ih3->nsrcs) 8781da177e4SLinus Torvalds return; /* no sources allowed */ 8791da177e4SLinus Torvalds igmp_gq_start_timer(in_dev); 8801da177e4SLinus Torvalds return; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds /* mark sources to include, if group & source-specific */ 8831da177e4SLinus Torvalds mark = ih3->nsrcs != 0; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds /* 8871da177e4SLinus Torvalds * - Start the timers in all of our membership records 8881da177e4SLinus Torvalds * that the query applies to for the interface on 8891da177e4SLinus Torvalds * which the query arrived excl. those that belong 8901da177e4SLinus Torvalds * to a "local" group (224.0.0.X) 8911da177e4SLinus Torvalds * - For timers already running check if they need to 8921da177e4SLinus Torvalds * be reset. 8931da177e4SLinus Torvalds * - Use the igmp->igmp_code field as the maximum 8941da177e4SLinus Torvalds * delay possible 8951da177e4SLinus Torvalds */ 8961da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 8971da177e4SLinus Torvalds for (im=in_dev->mc_list; im!=NULL; im=im->next) { 898ad12583fSDavid L Stevens int changed; 899ad12583fSDavid L Stevens 9001da177e4SLinus Torvalds if (group && group != im->multiaddr) 9011da177e4SLinus Torvalds continue; 9021da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 9031da177e4SLinus Torvalds continue; 9041da177e4SLinus Torvalds spin_lock_bh(&im->lock); 9051da177e4SLinus Torvalds if (im->tm_running) 9061da177e4SLinus Torvalds im->gsquery = im->gsquery && mark; 9071da177e4SLinus Torvalds else 9081da177e4SLinus Torvalds im->gsquery = mark; 909ad12583fSDavid L Stevens changed = !im->gsquery || 9101da177e4SLinus Torvalds igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); 9111da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 912ad12583fSDavid L Stevens if (changed) 9131da177e4SLinus Torvalds igmp_mod_timer(im, max_delay); 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds int igmp_rcv(struct sk_buff *skb) 9191da177e4SLinus Torvalds { 9201da177e4SLinus Torvalds /* This basically follows the spec line by line -- see RFC1112 */ 9211da177e4SLinus Torvalds struct igmphdr *ih; 9221da177e4SLinus Torvalds struct in_device *in_dev = in_dev_get(skb->dev); 9231da177e4SLinus Torvalds int len = skb->len; 9241da177e4SLinus Torvalds 925cd557bc1SDenis V. Lunev if (in_dev == NULL) 926cd557bc1SDenis V. Lunev goto drop; 9271da177e4SLinus Torvalds 928fb286bb2SHerbert Xu if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 929cd557bc1SDenis V. Lunev goto drop_ref; 930fb286bb2SHerbert Xu 931fb286bb2SHerbert Xu switch (skb->ip_summed) { 93284fa7933SPatrick McHardy case CHECKSUM_COMPLETE: 933d3bc23e7SAl Viro if (!csum_fold(skb->csum)) 934fb286bb2SHerbert Xu break; 935fb286bb2SHerbert Xu /* fall through */ 936fb286bb2SHerbert Xu case CHECKSUM_NONE: 937fb286bb2SHerbert Xu skb->csum = 0; 938fb286bb2SHerbert Xu if (__skb_checksum_complete(skb)) 939cd557bc1SDenis V. Lunev goto drop_ref; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 942d9edf9e2SArnaldo Carvalho de Melo ih = igmp_hdr(skb); 9431da177e4SLinus Torvalds switch (ih->type) { 9441da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_QUERY: 9451da177e4SLinus Torvalds igmp_heard_query(in_dev, skb, len); 9461da177e4SLinus Torvalds break; 9471da177e4SLinus Torvalds case IGMP_HOST_MEMBERSHIP_REPORT: 9481da177e4SLinus Torvalds case IGMPV2_HOST_MEMBERSHIP_REPORT: 9491da177e4SLinus Torvalds /* Is it our report looped back? */ 950511c3f92SEric Dumazet if (skb_rtable(skb)->fl.iif == 0) 9511da177e4SLinus Torvalds break; 95224c69275SDavid Stevens /* don't rely on MC router hearing unicast reports */ 95324c69275SDavid Stevens if (skb->pkt_type == PACKET_MULTICAST || 95424c69275SDavid Stevens skb->pkt_type == PACKET_BROADCAST) 9551da177e4SLinus Torvalds igmp_heard_report(in_dev, ih->group); 9561da177e4SLinus Torvalds break; 9571da177e4SLinus Torvalds case IGMP_PIM: 9581da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1 9591da177e4SLinus Torvalds in_dev_put(in_dev); 9601da177e4SLinus Torvalds return pim_rcv_v1(skb); 9611da177e4SLinus Torvalds #endif 962*c6b471e6SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 9631da177e4SLinus Torvalds case IGMP_DVMRP: 9641da177e4SLinus Torvalds case IGMP_TRACE: 9651da177e4SLinus Torvalds case IGMP_HOST_LEAVE_MESSAGE: 9661da177e4SLinus Torvalds case IGMP_MTRACE: 9671da177e4SLinus Torvalds case IGMP_MTRACE_RESP: 9681da177e4SLinus Torvalds break; 9691da177e4SLinus Torvalds default: 970dd1c1853SLinus Torvalds break; 9711da177e4SLinus Torvalds } 972fb286bb2SHerbert Xu 973cd557bc1SDenis V. Lunev drop_ref: 9741da177e4SLinus Torvalds in_dev_put(in_dev); 975cd557bc1SDenis V. Lunev drop: 9761da177e4SLinus Torvalds kfree_skb(skb); 9771da177e4SLinus Torvalds return 0; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds #endif 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds /* 9841da177e4SLinus Torvalds * Add a filter to a device 9851da177e4SLinus Torvalds */ 9861da177e4SLinus Torvalds 98763007727SAl Viro static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) 9881da177e4SLinus Torvalds { 9891da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 9901da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. 9931da177e4SLinus Torvalds We will get multicast token leakage, when IFF_MULTICAST 9941da177e4SLinus Torvalds is changed. This check should be done in dev->set_multicast_list 9951da177e4SLinus Torvalds routine. Something sort of: 9961da177e4SLinus Torvalds if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } 9971da177e4SLinus Torvalds --ANK 9981da177e4SLinus Torvalds */ 9991da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 10001da177e4SLinus Torvalds dev_mc_add(dev, buf, dev->addr_len, 0); 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* 10041da177e4SLinus Torvalds * Remove a filter from a device 10051da177e4SLinus Torvalds */ 10061da177e4SLinus Torvalds 100763007727SAl Viro static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds char buf[MAX_ADDR_LEN]; 10101da177e4SLinus Torvalds struct net_device *dev = in_dev->dev; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds if (arp_mc_map(addr, buf, dev, 0) == 0) 10131da177e4SLinus Torvalds dev_mc_delete(dev, buf, dev->addr_len, 0); 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 10171da177e4SLinus Torvalds /* 10181da177e4SLinus Torvalds * deleted ip_mc_list manipulation 10191da177e4SLinus Torvalds */ 10201da177e4SLinus Torvalds static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im) 10211da177e4SLinus Torvalds { 10221da177e4SLinus Torvalds struct ip_mc_list *pmc; 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds /* this is an "ip_mc_list" for convenience; only the fields below 10251da177e4SLinus Torvalds * are actually used. In particular, the refcnt and users are not 10261da177e4SLinus Torvalds * used for management of the delete list. Using the same structure 10271da177e4SLinus Torvalds * for deleted items allows change reports to use common code with 10281da177e4SLinus Torvalds * non-deleted or query-response MCA's. 10291da177e4SLinus Torvalds */ 10300da974f4SPanagiotis Issaris pmc = kzalloc(sizeof(*pmc), GFP_KERNEL); 10311da177e4SLinus Torvalds if (!pmc) 10321da177e4SLinus Torvalds return; 10331da177e4SLinus Torvalds spin_lock_bh(&im->lock); 10341da177e4SLinus Torvalds pmc->interface = im->interface; 10351da177e4SLinus Torvalds in_dev_hold(in_dev); 10361da177e4SLinus Torvalds pmc->multiaddr = im->multiaddr; 10371da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 10381da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 10391da177e4SLinus Torvalds pmc->sfmode = im->sfmode; 10401da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE) { 10411da177e4SLinus Torvalds struct ip_sf_list *psf; 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds pmc->tomb = im->tomb; 10441da177e4SLinus Torvalds pmc->sources = im->sources; 10451da177e4SLinus Torvalds im->tomb = im->sources = NULL; 10461da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) 10471da177e4SLinus Torvalds psf->sf_crcount = pmc->crcount; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 10521da177e4SLinus Torvalds pmc->next = in_dev->mc_tomb; 10531da177e4SLinus Torvalds in_dev->mc_tomb = pmc; 10541da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 105763007727SAl Viro static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr) 10581da177e4SLinus Torvalds { 10591da177e4SLinus Torvalds struct ip_mc_list *pmc, *pmc_prev; 10601da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 10631da177e4SLinus Torvalds pmc_prev = NULL; 10641da177e4SLinus Torvalds for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) { 10651da177e4SLinus Torvalds if (pmc->multiaddr == multiaddr) 10661da177e4SLinus Torvalds break; 10671da177e4SLinus Torvalds pmc_prev = pmc; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds if (pmc) { 10701da177e4SLinus Torvalds if (pmc_prev) 10711da177e4SLinus Torvalds pmc_prev->next = pmc->next; 10721da177e4SLinus Torvalds else 10731da177e4SLinus Torvalds in_dev->mc_tomb = pmc->next; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 10761da177e4SLinus Torvalds if (pmc) { 10771da177e4SLinus Torvalds for (psf=pmc->tomb; psf; psf=psf_next) { 10781da177e4SLinus Torvalds psf_next = psf->sf_next; 10791da177e4SLinus Torvalds kfree(psf); 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds in_dev_put(pmc->interface); 10821da177e4SLinus Torvalds kfree(pmc); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds static void igmpv3_clear_delrec(struct in_device *in_dev) 10871da177e4SLinus Torvalds { 10881da177e4SLinus Torvalds struct ip_mc_list *pmc, *nextpmc; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds spin_lock_bh(&in_dev->mc_tomb_lock); 10911da177e4SLinus Torvalds pmc = in_dev->mc_tomb; 10921da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 10931da177e4SLinus Torvalds spin_unlock_bh(&in_dev->mc_tomb_lock); 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds for (; pmc; pmc = nextpmc) { 10961da177e4SLinus Torvalds nextpmc = pmc->next; 10971da177e4SLinus Torvalds ip_mc_clear_src(pmc); 10981da177e4SLinus Torvalds in_dev_put(pmc->interface); 10991da177e4SLinus Torvalds kfree(pmc); 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds /* clear dead sources, too */ 11021da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 11031da177e4SLinus Torvalds for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) { 11041da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_next; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 11071da177e4SLinus Torvalds psf = pmc->tomb; 11081da177e4SLinus Torvalds pmc->tomb = NULL; 11091da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 11101da177e4SLinus Torvalds for (; psf; psf=psf_next) { 11111da177e4SLinus Torvalds psf_next = psf->sf_next; 11121da177e4SLinus Torvalds kfree(psf); 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds #endif 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds static void igmp_group_dropped(struct ip_mc_list *im) 11201da177e4SLinus Torvalds { 11211da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 11221da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11231da177e4SLinus Torvalds int reporter; 11241da177e4SLinus Torvalds #endif 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds if (im->loaded) { 11271da177e4SLinus Torvalds im->loaded = 0; 11281da177e4SLinus Torvalds ip_mc_filter_del(in_dev, im->multiaddr); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11321da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 11331da177e4SLinus Torvalds return; 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds reporter = im->reporter; 11361da177e4SLinus Torvalds igmp_stop_timer(im); 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds if (!in_dev->dead) { 11391da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev)) 11401da177e4SLinus Torvalds goto done; 11411da177e4SLinus Torvalds if (IGMP_V2_SEEN(in_dev)) { 11421da177e4SLinus Torvalds if (reporter) 11431da177e4SLinus Torvalds igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); 11441da177e4SLinus Torvalds goto done; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds /* IGMPv3 */ 11471da177e4SLinus Torvalds igmpv3_add_delrec(in_dev, im); 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds igmp_ifc_event(in_dev); 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds done: 11521da177e4SLinus Torvalds #endif 11531da177e4SLinus Torvalds ip_mc_clear_src(im); 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds static void igmp_group_added(struct ip_mc_list *im) 11571da177e4SLinus Torvalds { 11581da177e4SLinus Torvalds struct in_device *in_dev = im->interface; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (im->loaded == 0) { 11611da177e4SLinus Torvalds im->loaded = 1; 11621da177e4SLinus Torvalds ip_mc_filter_add(in_dev, im->multiaddr); 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 11661da177e4SLinus Torvalds if (im->multiaddr == IGMP_ALL_HOSTS) 11671da177e4SLinus Torvalds return; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds if (in_dev->dead) 11701da177e4SLinus Torvalds return; 11711da177e4SLinus Torvalds if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 11721da177e4SLinus Torvalds spin_lock_bh(&im->lock); 11731da177e4SLinus Torvalds igmp_start_timer(im, IGMP_Initial_Report_Delay); 11741da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 11751da177e4SLinus Torvalds return; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds /* else, v3 */ 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 11801da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 11811da177e4SLinus Torvalds igmp_ifc_event(in_dev); 11821da177e4SLinus Torvalds #endif 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds /* 11871da177e4SLinus Torvalds * Multicast list managers 11881da177e4SLinus Torvalds */ 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds /* 11921da177e4SLinus Torvalds * A socket has joined a multicast group on device dev. 11931da177e4SLinus Torvalds */ 11941da177e4SLinus Torvalds 11958f935bbdSAl Viro void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) 11961da177e4SLinus Torvalds { 11971da177e4SLinus Torvalds struct ip_mc_list *im; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds ASSERT_RTNL(); 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds for (im=in_dev->mc_list; im; im=im->next) { 12021da177e4SLinus Torvalds if (im->multiaddr == addr) { 12031da177e4SLinus Torvalds im->users++; 12041da177e4SLinus Torvalds ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0); 12051da177e4SLinus Torvalds goto out; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 12098b3a7005SKris Katterjohn im = kmalloc(sizeof(*im), GFP_KERNEL); 12101da177e4SLinus Torvalds if (!im) 12111da177e4SLinus Torvalds goto out; 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds im->users = 1; 12141da177e4SLinus Torvalds im->interface = in_dev; 12151da177e4SLinus Torvalds in_dev_hold(in_dev); 12161da177e4SLinus Torvalds im->multiaddr = addr; 12171da177e4SLinus Torvalds /* initial mode is (EX, empty) */ 12181da177e4SLinus Torvalds im->sfmode = MCAST_EXCLUDE; 12191da177e4SLinus Torvalds im->sfcount[MCAST_INCLUDE] = 0; 12201da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE] = 1; 12211da177e4SLinus Torvalds im->sources = NULL; 12221da177e4SLinus Torvalds im->tomb = NULL; 12231da177e4SLinus Torvalds im->crcount = 0; 12241da177e4SLinus Torvalds atomic_set(&im->refcnt, 1); 12251da177e4SLinus Torvalds spin_lock_init(&im->lock); 12261da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12271da177e4SLinus Torvalds im->tm_running = 0; 1228b24b8a24SPavel Emelyanov setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im); 12291da177e4SLinus Torvalds im->unsolicit_count = IGMP_Unsolicited_Report_Count; 12301da177e4SLinus Torvalds im->reporter = 0; 12311da177e4SLinus Torvalds im->gsquery = 0; 12321da177e4SLinus Torvalds #endif 12331da177e4SLinus Torvalds im->loaded = 0; 12341da177e4SLinus Torvalds write_lock_bh(&in_dev->mc_list_lock); 12351da177e4SLinus Torvalds im->next = in_dev->mc_list; 12361da177e4SLinus Torvalds in_dev->mc_list = im; 1237b8bae41eSRami Rosen in_dev->mc_count++; 12381da177e4SLinus Torvalds write_unlock_bh(&in_dev->mc_list_lock); 12391da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 12401da177e4SLinus Torvalds igmpv3_del_delrec(in_dev, im->multiaddr); 12411da177e4SLinus Torvalds #endif 12421da177e4SLinus Torvalds igmp_group_added(im); 12431da177e4SLinus Torvalds if (!in_dev->dead) 12441da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 12451da177e4SLinus Torvalds out: 12461da177e4SLinus Torvalds return; 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds /* 1250a816c7c7SJay Vosburgh * Resend IGMP JOIN report; used for bonding. 1251a816c7c7SJay Vosburgh */ 1252a816c7c7SJay Vosburgh void ip_mc_rejoin_group(struct ip_mc_list *im) 1253a816c7c7SJay Vosburgh { 125408882669SGeert Uytterhoeven #ifdef CONFIG_IP_MULTICAST 1255a816c7c7SJay Vosburgh struct in_device *in_dev = im->interface; 1256a816c7c7SJay Vosburgh 1257a816c7c7SJay Vosburgh if (im->multiaddr == IGMP_ALL_HOSTS) 1258a816c7c7SJay Vosburgh return; 1259a816c7c7SJay Vosburgh 1260a816c7c7SJay Vosburgh if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { 1261a816c7c7SJay Vosburgh igmp_mod_timer(im, IGMP_Initial_Report_Delay); 1262a816c7c7SJay Vosburgh return; 1263a816c7c7SJay Vosburgh } 1264a816c7c7SJay Vosburgh /* else, v3 */ 1265a816c7c7SJay Vosburgh im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 1266a816c7c7SJay Vosburgh IGMP_Unsolicited_Report_Count; 1267a816c7c7SJay Vosburgh igmp_ifc_event(in_dev); 1268a816c7c7SJay Vosburgh #endif 1269a816c7c7SJay Vosburgh } 1270a816c7c7SJay Vosburgh 1271a816c7c7SJay Vosburgh /* 12721da177e4SLinus Torvalds * A socket has left a multicast group on device dev 12731da177e4SLinus Torvalds */ 12741da177e4SLinus Torvalds 12758f935bbdSAl Viro void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) 12761da177e4SLinus Torvalds { 12771da177e4SLinus Torvalds struct ip_mc_list *i, **ip; 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds ASSERT_RTNL(); 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) { 12821da177e4SLinus Torvalds if (i->multiaddr == addr) { 12831da177e4SLinus Torvalds if (--i->users == 0) { 12841da177e4SLinus Torvalds write_lock_bh(&in_dev->mc_list_lock); 12851da177e4SLinus Torvalds *ip = i->next; 1286b8bae41eSRami Rosen in_dev->mc_count--; 12871da177e4SLinus Torvalds write_unlock_bh(&in_dev->mc_list_lock); 12881da177e4SLinus Torvalds igmp_group_dropped(i); 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds if (!in_dev->dead) 12911da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds ip_ma_put(i); 12941da177e4SLinus Torvalds return; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds break; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 130175c78500SMoni Shoua /* Device changing type */ 130275c78500SMoni Shoua 130375c78500SMoni Shoua void ip_mc_unmap(struct in_device *in_dev) 130475c78500SMoni Shoua { 130575c78500SMoni Shoua struct ip_mc_list *i; 130675c78500SMoni Shoua 130775c78500SMoni Shoua ASSERT_RTNL(); 130875c78500SMoni Shoua 130975c78500SMoni Shoua for (i = in_dev->mc_list; i; i = i->next) 131075c78500SMoni Shoua igmp_group_dropped(i); 131175c78500SMoni Shoua } 131275c78500SMoni Shoua 131375c78500SMoni Shoua void ip_mc_remap(struct in_device *in_dev) 131475c78500SMoni Shoua { 131575c78500SMoni Shoua struct ip_mc_list *i; 131675c78500SMoni Shoua 131775c78500SMoni Shoua ASSERT_RTNL(); 131875c78500SMoni Shoua 131975c78500SMoni Shoua for (i = in_dev->mc_list; i; i = i->next) 132075c78500SMoni Shoua igmp_group_added(i); 132175c78500SMoni Shoua } 132275c78500SMoni Shoua 13231da177e4SLinus Torvalds /* Device going down */ 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds void ip_mc_down(struct in_device *in_dev) 13261da177e4SLinus Torvalds { 13271da177e4SLinus Torvalds struct ip_mc_list *i; 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds ASSERT_RTNL(); 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds for (i=in_dev->mc_list; i; i=i->next) 13321da177e4SLinus Torvalds igmp_group_dropped(i); 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13351da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 13361da177e4SLinus Torvalds if (del_timer(&in_dev->mr_ifc_timer)) 13371da177e4SLinus Torvalds __in_dev_put(in_dev); 13381da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 13391da177e4SLinus Torvalds if (del_timer(&in_dev->mr_gq_timer)) 13401da177e4SLinus Torvalds __in_dev_put(in_dev); 13411da177e4SLinus Torvalds igmpv3_clear_delrec(in_dev); 13421da177e4SLinus Torvalds #endif 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds void ip_mc_init_dev(struct in_device *in_dev) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds ASSERT_RTNL(); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds in_dev->mc_tomb = NULL; 13521da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 13531da177e4SLinus Torvalds in_dev->mr_gq_running = 0; 1354b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 1355b24b8a24SPavel Emelyanov (unsigned long)in_dev); 13561da177e4SLinus Torvalds in_dev->mr_ifc_count = 0; 1357b8bae41eSRami Rosen in_dev->mc_count = 0; 1358b24b8a24SPavel Emelyanov setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 1359b24b8a24SPavel Emelyanov (unsigned long)in_dev); 13601da177e4SLinus Torvalds in_dev->mr_qrv = IGMP_Unsolicited_Report_Count; 13611da177e4SLinus Torvalds #endif 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds rwlock_init(&in_dev->mc_list_lock); 13641da177e4SLinus Torvalds spin_lock_init(&in_dev->mc_tomb_lock); 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds /* Device going up */ 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds void ip_mc_up(struct in_device *in_dev) 13701da177e4SLinus Torvalds { 13711da177e4SLinus Torvalds struct ip_mc_list *i; 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds ASSERT_RTNL(); 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds for (i=in_dev->mc_list; i; i=i->next) 13781da177e4SLinus Torvalds igmp_group_added(i); 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds /* 13821da177e4SLinus Torvalds * Device is about to be destroyed: clean up. 13831da177e4SLinus Torvalds */ 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds void ip_mc_destroy_dev(struct in_device *in_dev) 13861da177e4SLinus Torvalds { 13871da177e4SLinus Torvalds struct ip_mc_list *i; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds ASSERT_RTNL(); 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /* Deactivate timers */ 13921da177e4SLinus Torvalds ip_mc_down(in_dev); 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds write_lock_bh(&in_dev->mc_list_lock); 13951da177e4SLinus Torvalds while ((i = in_dev->mc_list) != NULL) { 13961da177e4SLinus Torvalds in_dev->mc_list = i->next; 1397b8bae41eSRami Rosen in_dev->mc_count--; 13981da177e4SLinus Torvalds write_unlock_bh(&in_dev->mc_list_lock); 13991da177e4SLinus Torvalds igmp_group_dropped(i); 14001da177e4SLinus Torvalds ip_ma_put(i); 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds write_lock_bh(&in_dev->mc_list_lock); 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds write_unlock_bh(&in_dev->mc_list_lock); 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 1407877acedcSDaniel Lezcano static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) 14081da177e4SLinus Torvalds { 14091da177e4SLinus Torvalds struct flowi fl = { .nl_u = { .ip4_u = 14101da177e4SLinus Torvalds { .daddr = imr->imr_multiaddr.s_addr } } }; 14111da177e4SLinus Torvalds struct rtable *rt; 14121da177e4SLinus Torvalds struct net_device *dev = NULL; 14131da177e4SLinus Torvalds struct in_device *idev = NULL; 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds if (imr->imr_ifindex) { 1416877acedcSDaniel Lezcano idev = inetdev_by_index(net, imr->imr_ifindex); 14171da177e4SLinus Torvalds if (idev) 14181da177e4SLinus Torvalds __in_dev_put(idev); 14191da177e4SLinus Torvalds return idev; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds if (imr->imr_address.s_addr) { 1422877acedcSDaniel Lezcano dev = ip_dev_find(net, imr->imr_address.s_addr); 14231da177e4SLinus Torvalds if (!dev) 14241da177e4SLinus Torvalds return NULL; 142515333061SStephen Hemminger dev_put(dev); 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds 1428877acedcSDaniel Lezcano if (!dev && !ip_route_output_key(net, &rt, &fl)) { 14291da177e4SLinus Torvalds dev = rt->u.dst.dev; 14301da177e4SLinus Torvalds ip_rt_put(rt); 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds if (dev) { 14331da177e4SLinus Torvalds imr->imr_ifindex = dev->ifindex; 1434e5ed6399SHerbert Xu idev = __in_dev_get_rtnl(dev); 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds return idev; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds /* 14401da177e4SLinus Torvalds * Join a socket to a group 14411da177e4SLinus Torvalds */ 1442ab32ea5dSBrian Haley int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS; 1443ab32ea5dSBrian Haley int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, 14478f935bbdSAl Viro __be32 *psfsrc) 14481da177e4SLinus Torvalds { 14491da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 14501da177e4SLinus Torvalds int rv = 0; 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds psf_prev = NULL; 14531da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 14541da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 14551da177e4SLinus Torvalds break; 14561da177e4SLinus Torvalds psf_prev = psf; 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds if (!psf || psf->sf_count[sfmode] == 0) { 14591da177e4SLinus Torvalds /* source filter not found, or count wrong => bug */ 14601da177e4SLinus Torvalds return -ESRCH; 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds psf->sf_count[sfmode]--; 14631da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 0) { 14641da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { 14671da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 14681da177e4SLinus Torvalds struct in_device *in_dev = pmc->interface; 14691da177e4SLinus Torvalds #endif 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds /* no more filters for this source */ 14721da177e4SLinus Torvalds if (psf_prev) 14731da177e4SLinus Torvalds psf_prev->sf_next = psf->sf_next; 14741da177e4SLinus Torvalds else 14751da177e4SLinus Torvalds pmc->sources = psf->sf_next; 14761da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 14771da177e4SLinus Torvalds if (psf->sf_oldin && 14781da177e4SLinus Torvalds !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { 14791da177e4SLinus Torvalds psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 14801da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 14811da177e4SLinus Torvalds psf->sf_next = pmc->tomb; 14821da177e4SLinus Torvalds pmc->tomb = psf; 14831da177e4SLinus Torvalds rv = 1; 14841da177e4SLinus Torvalds } else 14851da177e4SLinus Torvalds #endif 14861da177e4SLinus Torvalds kfree(psf); 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds return rv; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTICAST 14921da177e4SLinus Torvalds #define igmp_ifc_event(x) do { } while (0) 14931da177e4SLinus Torvalds #endif 14941da177e4SLinus Torvalds 14958f935bbdSAl Viro static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 14968f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 14971da177e4SLinus Torvalds { 14981da177e4SLinus Torvalds struct ip_mc_list *pmc; 14991da177e4SLinus Torvalds int changerec = 0; 15001da177e4SLinus Torvalds int i, err; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds if (!in_dev) 15031da177e4SLinus Torvalds return -ENODEV; 15041da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 15051da177e4SLinus Torvalds for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) { 15061da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 15071da177e4SLinus Torvalds break; 15081da177e4SLinus Torvalds } 15091da177e4SLinus Torvalds if (!pmc) { 15101da177e4SLinus Torvalds /* MCA not found?? bug */ 15111da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 15121da177e4SLinus Torvalds return -ESRCH; 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 15151da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 15161da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15171da177e4SLinus Torvalds sf_markstate(pmc); 15181da177e4SLinus Torvalds #endif 15191da177e4SLinus Torvalds if (!delta) { 15201da177e4SLinus Torvalds err = -EINVAL; 15211da177e4SLinus Torvalds if (!pmc->sfcount[sfmode]) 15221da177e4SLinus Torvalds goto out_unlock; 15231da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds err = 0; 15261da177e4SLinus Torvalds for (i=0; i<sfcount; i++) { 15271da177e4SLinus Torvalds int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds changerec |= rv > 0; 15301da177e4SLinus Torvalds if (!err && rv < 0) 15311da177e4SLinus Torvalds err = rv; 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && 15341da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] == 0 && 15351da177e4SLinus Torvalds pmc->sfcount[MCAST_INCLUDE]) { 15361da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15371da177e4SLinus Torvalds struct ip_sf_list *psf; 15381da177e4SLinus Torvalds #endif 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds /* filter mode change */ 15411da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 15421da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15431da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 15441da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 15451da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 15461da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf = psf->sf_next) 15471da177e4SLinus Torvalds psf->sf_crcount = 0; 15481da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 15491da177e4SLinus Torvalds } else if (sf_setstate(pmc) || changerec) { 15501da177e4SLinus Torvalds igmp_ifc_event(pmc->interface); 15511da177e4SLinus Torvalds #endif 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds out_unlock: 15541da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 15551da177e4SLinus Torvalds return err; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds /* 15591da177e4SLinus Torvalds * Add multicast single-source filter to the interface list 15601da177e4SLinus Torvalds */ 15611da177e4SLinus Torvalds static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, 15628f935bbdSAl Viro __be32 *psfsrc, int delta) 15631da177e4SLinus Torvalds { 15641da177e4SLinus Torvalds struct ip_sf_list *psf, *psf_prev; 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds psf_prev = NULL; 15671da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 15681da177e4SLinus Torvalds if (psf->sf_inaddr == *psfsrc) 15691da177e4SLinus Torvalds break; 15701da177e4SLinus Torvalds psf_prev = psf; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds if (!psf) { 15730da974f4SPanagiotis Issaris psf = kzalloc(sizeof(*psf), GFP_ATOMIC); 15741da177e4SLinus Torvalds if (!psf) 15751da177e4SLinus Torvalds return -ENOBUFS; 15761da177e4SLinus Torvalds psf->sf_inaddr = *psfsrc; 15771da177e4SLinus Torvalds if (psf_prev) { 15781da177e4SLinus Torvalds psf_prev->sf_next = psf; 15791da177e4SLinus Torvalds } else 15801da177e4SLinus Torvalds pmc->sources = psf; 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds psf->sf_count[sfmode]++; 15831da177e4SLinus Torvalds if (psf->sf_count[sfmode] == 1) { 15841da177e4SLinus Torvalds ip_rt_multicast_event(pmc->interface); 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds return 0; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 15901da177e4SLinus Torvalds static void sf_markstate(struct ip_mc_list *pmc) 15911da177e4SLinus Torvalds { 15921da177e4SLinus Torvalds struct ip_sf_list *psf; 15931da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) 15961da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 15971da177e4SLinus Torvalds psf->sf_oldin = mca_xcount == 15981da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] && 15991da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 16001da177e4SLinus Torvalds } else 16011da177e4SLinus Torvalds psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds static int sf_setstate(struct ip_mc_list *pmc) 16051da177e4SLinus Torvalds { 1606ad12583fSDavid L Stevens struct ip_sf_list *psf, *dpsf; 16071da177e4SLinus Torvalds int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; 16081da177e4SLinus Torvalds int qrv = pmc->interface->mr_qrv; 16091da177e4SLinus Torvalds int new_in, rv; 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds rv = 0; 16121da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=psf->sf_next) { 16131da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) { 16141da177e4SLinus Torvalds new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && 16151da177e4SLinus Torvalds !psf->sf_count[MCAST_INCLUDE]; 16161da177e4SLinus Torvalds } else 16171da177e4SLinus Torvalds new_in = psf->sf_count[MCAST_INCLUDE] != 0; 1618ad12583fSDavid L Stevens if (new_in) { 1619ad12583fSDavid L Stevens if (!psf->sf_oldin) { 162076edc605SAl Viro struct ip_sf_list *prev = NULL; 1621ad12583fSDavid L Stevens 1622ad12583fSDavid L Stevens for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) { 1623ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1624ad12583fSDavid L Stevens break; 1625ad12583fSDavid L Stevens prev = dpsf; 1626ad12583fSDavid L Stevens } 1627ad12583fSDavid L Stevens if (dpsf) { 1628ad12583fSDavid L Stevens if (prev) 1629ad12583fSDavid L Stevens prev->sf_next = dpsf->sf_next; 1630ad12583fSDavid L Stevens else 1631ad12583fSDavid L Stevens pmc->tomb = dpsf->sf_next; 1632ad12583fSDavid L Stevens kfree(dpsf); 1633ad12583fSDavid L Stevens } 16341da177e4SLinus Torvalds psf->sf_crcount = qrv; 16351da177e4SLinus Torvalds rv++; 16361da177e4SLinus Torvalds } 1637ad12583fSDavid L Stevens } else if (psf->sf_oldin) { 1638ad12583fSDavid L Stevens 1639ad12583fSDavid L Stevens psf->sf_crcount = 0; 1640ad12583fSDavid L Stevens /* 1641ad12583fSDavid L Stevens * add or update "delete" records if an active filter 1642ad12583fSDavid L Stevens * is now inactive 1643ad12583fSDavid L Stevens */ 1644ad12583fSDavid L Stevens for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) 1645ad12583fSDavid L Stevens if (dpsf->sf_inaddr == psf->sf_inaddr) 1646ad12583fSDavid L Stevens break; 1647ad12583fSDavid L Stevens if (!dpsf) { 1648ad12583fSDavid L Stevens dpsf = (struct ip_sf_list *) 1649ad12583fSDavid L Stevens kmalloc(sizeof(*dpsf), GFP_ATOMIC); 1650ad12583fSDavid L Stevens if (!dpsf) 1651ad12583fSDavid L Stevens continue; 1652ad12583fSDavid L Stevens *dpsf = *psf; 1653ad12583fSDavid L Stevens /* pmc->lock held by callers */ 1654ad12583fSDavid L Stevens dpsf->sf_next = pmc->tomb; 1655ad12583fSDavid L Stevens pmc->tomb = dpsf; 1656ad12583fSDavid L Stevens } 1657ad12583fSDavid L Stevens dpsf->sf_crcount = qrv; 1658ad12583fSDavid L Stevens rv++; 1659ad12583fSDavid L Stevens } 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds return rv; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds #endif 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds /* 16661da177e4SLinus Torvalds * Add multicast source filter list to the interface list 16671da177e4SLinus Torvalds */ 16688f935bbdSAl Viro static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, 16698f935bbdSAl Viro int sfcount, __be32 *psfsrc, int delta) 16701da177e4SLinus Torvalds { 16711da177e4SLinus Torvalds struct ip_mc_list *pmc; 16721da177e4SLinus Torvalds int isexclude; 16731da177e4SLinus Torvalds int i, err; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds if (!in_dev) 16761da177e4SLinus Torvalds return -ENODEV; 16771da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 16781da177e4SLinus Torvalds for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) { 16791da177e4SLinus Torvalds if (*pmca == pmc->multiaddr) 16801da177e4SLinus Torvalds break; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds if (!pmc) { 16831da177e4SLinus Torvalds /* MCA not found?? bug */ 16841da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 16851da177e4SLinus Torvalds return -ESRCH; 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds spin_lock_bh(&pmc->lock); 16881da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 16911da177e4SLinus Torvalds sf_markstate(pmc); 16921da177e4SLinus Torvalds #endif 16931da177e4SLinus Torvalds isexclude = pmc->sfmode == MCAST_EXCLUDE; 16941da177e4SLinus Torvalds if (!delta) 16951da177e4SLinus Torvalds pmc->sfcount[sfmode]++; 16961da177e4SLinus Torvalds err = 0; 16971da177e4SLinus Torvalds for (i=0; i<sfcount; i++) { 16981da177e4SLinus Torvalds err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta); 16991da177e4SLinus Torvalds if (err) 17001da177e4SLinus Torvalds break; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds if (err) { 17031da177e4SLinus Torvalds int j; 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds pmc->sfcount[sfmode]--; 17061da177e4SLinus Torvalds for (j=0; j<i; j++) 17071da177e4SLinus Torvalds (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); 17081da177e4SLinus Torvalds } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { 17091da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17101da177e4SLinus Torvalds struct ip_sf_list *psf; 1711cfcabdccSStephen Hemminger in_dev = pmc->interface; 17121da177e4SLinus Torvalds #endif 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds /* filter mode change */ 17151da177e4SLinus Torvalds if (pmc->sfcount[MCAST_EXCLUDE]) 17161da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 17171da177e4SLinus Torvalds else if (pmc->sfcount[MCAST_INCLUDE]) 17181da177e4SLinus Torvalds pmc->sfmode = MCAST_INCLUDE; 17191da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 17201da177e4SLinus Torvalds /* else no filters; keep old mode for reports */ 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 17231da177e4SLinus Torvalds IGMP_Unsolicited_Report_Count; 17241da177e4SLinus Torvalds in_dev->mr_ifc_count = pmc->crcount; 17251da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf = psf->sf_next) 17261da177e4SLinus Torvalds psf->sf_crcount = 0; 17271da177e4SLinus Torvalds igmp_ifc_event(in_dev); 17281da177e4SLinus Torvalds } else if (sf_setstate(pmc)) { 17291da177e4SLinus Torvalds igmp_ifc_event(in_dev); 17301da177e4SLinus Torvalds #endif 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds spin_unlock_bh(&pmc->lock); 17331da177e4SLinus Torvalds return err; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds static void ip_mc_clear_src(struct ip_mc_list *pmc) 17371da177e4SLinus Torvalds { 17381da177e4SLinus Torvalds struct ip_sf_list *psf, *nextpsf; 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds for (psf=pmc->tomb; psf; psf=nextpsf) { 17411da177e4SLinus Torvalds nextpsf = psf->sf_next; 17421da177e4SLinus Torvalds kfree(psf); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds pmc->tomb = NULL; 17451da177e4SLinus Torvalds for (psf=pmc->sources; psf; psf=nextpsf) { 17461da177e4SLinus Torvalds nextpsf = psf->sf_next; 17471da177e4SLinus Torvalds kfree(psf); 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds pmc->sources = NULL; 17501da177e4SLinus Torvalds pmc->sfmode = MCAST_EXCLUDE; 1751de9daad9SDenis Lukianov pmc->sfcount[MCAST_INCLUDE] = 0; 17521da177e4SLinus Torvalds pmc->sfcount[MCAST_EXCLUDE] = 1; 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds /* 17571da177e4SLinus Torvalds * Join a multicast group 17581da177e4SLinus Torvalds */ 17591da177e4SLinus Torvalds int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr) 17601da177e4SLinus Torvalds { 17611da177e4SLinus Torvalds int err; 17628f935bbdSAl Viro __be32 addr = imr->imr_multiaddr.s_addr; 1763ca9b907dSDavid L Stevens struct ip_mc_socklist *iml = NULL, *i; 17641da177e4SLinus Torvalds struct in_device *in_dev; 17651da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1766877acedcSDaniel Lezcano struct net *net = sock_net(sk); 1767ca9b907dSDavid L Stevens int ifindex; 17681da177e4SLinus Torvalds int count = 0; 17691da177e4SLinus Torvalds 1770f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 17711da177e4SLinus Torvalds return -EINVAL; 17721da177e4SLinus Torvalds 17736756ae4bSStephen Hemminger rtnl_lock(); 17741da177e4SLinus Torvalds 1775877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds if (!in_dev) { 17781da177e4SLinus Torvalds iml = NULL; 17791da177e4SLinus Torvalds err = -ENODEV; 17801da177e4SLinus Torvalds goto done; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds err = -EADDRINUSE; 1784ca9b907dSDavid L Stevens ifindex = imr->imr_ifindex; 17851da177e4SLinus Torvalds for (i = inet->mc_list; i; i = i->next) { 1786ca9b907dSDavid L Stevens if (i->multi.imr_multiaddr.s_addr == addr && 1787ca9b907dSDavid L Stevens i->multi.imr_ifindex == ifindex) 17881da177e4SLinus Torvalds goto done; 17891da177e4SLinus Torvalds count++; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds err = -ENOBUFS; 1792ca9b907dSDavid L Stevens if (count >= sysctl_igmp_max_memberships) 17931da177e4SLinus Torvalds goto done; 17948b3a7005SKris Katterjohn iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 1795ca9b907dSDavid L Stevens if (iml == NULL) 1796ca9b907dSDavid L Stevens goto done; 1797ca9b907dSDavid L Stevens 17981da177e4SLinus Torvalds memcpy(&iml->multi, imr, sizeof(*imr)); 17991da177e4SLinus Torvalds iml->next = inet->mc_list; 18001da177e4SLinus Torvalds iml->sflist = NULL; 18011da177e4SLinus Torvalds iml->sfmode = MCAST_EXCLUDE; 18021da177e4SLinus Torvalds inet->mc_list = iml; 18031da177e4SLinus Torvalds ip_mc_inc_group(in_dev, addr); 18041da177e4SLinus Torvalds err = 0; 18051da177e4SLinus Torvalds done: 18066756ae4bSStephen Hemminger rtnl_unlock(); 18071da177e4SLinus Torvalds return err; 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, 18111da177e4SLinus Torvalds struct in_device *in_dev) 18121da177e4SLinus Torvalds { 18131da177e4SLinus Torvalds int err; 18141da177e4SLinus Torvalds 1815cfcabdccSStephen Hemminger if (iml->sflist == NULL) { 18161da177e4SLinus Torvalds /* any-source empty exclude case */ 18171da177e4SLinus Torvalds return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 18181da177e4SLinus Torvalds iml->sfmode, 0, NULL, 0); 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 18211da177e4SLinus Torvalds iml->sfmode, iml->sflist->sl_count, 18221da177e4SLinus Torvalds iml->sflist->sl_addr, 0); 18231da177e4SLinus Torvalds sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max)); 18241da177e4SLinus Torvalds iml->sflist = NULL; 18251da177e4SLinus Torvalds return err; 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds /* 18291da177e4SLinus Torvalds * Ask a socket to leave a group. 18301da177e4SLinus Torvalds */ 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 18331da177e4SLinus Torvalds { 18341da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 18351da177e4SLinus Torvalds struct ip_mc_socklist *iml, **imlp; 183684b42baeSDavid L Stevens struct in_device *in_dev; 1837877acedcSDaniel Lezcano struct net *net = sock_net(sk); 18388f935bbdSAl Viro __be32 group = imr->imr_multiaddr.s_addr; 183984b42baeSDavid L Stevens u32 ifindex; 1840acd6e00bSDavid L Stevens int ret = -EADDRNOTAVAIL; 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds rtnl_lock(); 1843877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, imr); 184484b42baeSDavid L Stevens ifindex = imr->imr_ifindex; 18451da177e4SLinus Torvalds for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) { 1846acd6e00bSDavid L Stevens if (iml->multi.imr_multiaddr.s_addr != group) 1847acd6e00bSDavid L Stevens continue; 1848acd6e00bSDavid L Stevens if (ifindex) { 1849acd6e00bSDavid L Stevens if (iml->multi.imr_ifindex != ifindex) 1850acd6e00bSDavid L Stevens continue; 1851acd6e00bSDavid L Stevens } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != 1852acd6e00bSDavid L Stevens iml->multi.imr_address.s_addr) 1853acd6e00bSDavid L Stevens continue; 1854acd6e00bSDavid L Stevens 18551da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds *imlp = iml->next; 18581da177e4SLinus Torvalds 1859acd6e00bSDavid L Stevens if (in_dev) 186084b42baeSDavid L Stevens ip_mc_dec_group(in_dev, group); 18611da177e4SLinus Torvalds rtnl_unlock(); 18621da177e4SLinus Torvalds sock_kfree_s(sk, iml, sizeof(*iml)); 18631da177e4SLinus Torvalds return 0; 18641da177e4SLinus Torvalds } 1865acd6e00bSDavid L Stevens if (!in_dev) 1866acd6e00bSDavid L Stevens ret = -ENODEV; 18671da177e4SLinus Torvalds rtnl_unlock(); 1868acd6e00bSDavid L Stevens return ret; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds int ip_mc_source(int add, int omode, struct sock *sk, struct 18721da177e4SLinus Torvalds ip_mreq_source *mreqs, int ifindex) 18731da177e4SLinus Torvalds { 18741da177e4SLinus Torvalds int err; 18751da177e4SLinus Torvalds struct ip_mreqn imr; 187663007727SAl Viro __be32 addr = mreqs->imr_multiaddr; 18771da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 18781da177e4SLinus Torvalds struct in_device *in_dev = NULL; 18791da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 18801da177e4SLinus Torvalds struct ip_sf_socklist *psl; 1881877acedcSDaniel Lezcano struct net *net = sock_net(sk); 18828cdaaa15SDavid L Stevens int leavegroup = 0; 18831da177e4SLinus Torvalds int i, j, rv; 18841da177e4SLinus Torvalds 1885f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 18861da177e4SLinus Torvalds return -EINVAL; 18871da177e4SLinus Torvalds 18886756ae4bSStephen Hemminger rtnl_lock(); 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 18911da177e4SLinus Torvalds imr.imr_address.s_addr = mreqs->imr_interface; 18921da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 1893877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds if (!in_dev) { 18961da177e4SLinus Torvalds err = -ENODEV; 18971da177e4SLinus Torvalds goto done; 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 1902f64f9e71SJoe Perches if ((pmc->multi.imr_multiaddr.s_addr == 1903f64f9e71SJoe Perches imr.imr_multiaddr.s_addr) && 1904f64f9e71SJoe Perches (pmc->multi.imr_ifindex == imr.imr_ifindex)) 19051da177e4SLinus Torvalds break; 19061da177e4SLinus Torvalds } 1907917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 1908917f2f10SDavid L Stevens err = -EINVAL; 19091da177e4SLinus Torvalds goto done; 1910917f2f10SDavid L Stevens } 19111da177e4SLinus Torvalds /* if a source filter was set, must be the same mode as before */ 19121da177e4SLinus Torvalds if (pmc->sflist) { 1913917f2f10SDavid L Stevens if (pmc->sfmode != omode) { 1914917f2f10SDavid L Stevens err = -EINVAL; 19151da177e4SLinus Torvalds goto done; 1916917f2f10SDavid L Stevens } 19171da177e4SLinus Torvalds } else if (pmc->sfmode != omode) { 19181da177e4SLinus Torvalds /* allow mode switches for empty-set filters */ 19191da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); 19201da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 19211da177e4SLinus Torvalds NULL, 0); 19221da177e4SLinus Torvalds pmc->sfmode = omode; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds psl = pmc->sflist; 19261da177e4SLinus Torvalds if (!add) { 19271da177e4SLinus Torvalds if (!psl) 1928917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 19291da177e4SLinus Torvalds rv = !0; 19301da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 19311da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 193263007727SAl Viro sizeof(__be32)); 19331da177e4SLinus Torvalds if (rv == 0) 19341da177e4SLinus Torvalds break; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds if (rv) /* source not found */ 1937917f2f10SDavid L Stevens goto done; /* err = -EADDRNOTAVAIL */ 19381da177e4SLinus Torvalds 19398cdaaa15SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 19408cdaaa15SDavid L Stevens if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { 19418cdaaa15SDavid L Stevens leavegroup = 1; 19428cdaaa15SDavid L Stevens goto done; 19438cdaaa15SDavid L Stevens } 19448cdaaa15SDavid L Stevens 19451da177e4SLinus Torvalds /* update the interface filter */ 19461da177e4SLinus Torvalds ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 19471da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds for (j=i+1; j<psl->sl_count; j++) 19501da177e4SLinus Torvalds psl->sl_addr[j-1] = psl->sl_addr[j]; 19511da177e4SLinus Torvalds psl->sl_count--; 19521da177e4SLinus Torvalds err = 0; 19531da177e4SLinus Torvalds goto done; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds /* else, add a new source to the filter */ 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds if (psl && psl->sl_count >= sysctl_igmp_max_msf) { 19581da177e4SLinus Torvalds err = -ENOBUFS; 19591da177e4SLinus Torvalds goto done; 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds if (!psl || psl->sl_count == psl->sl_max) { 19621da177e4SLinus Torvalds struct ip_sf_socklist *newpsl; 19631da177e4SLinus Torvalds int count = IP_SFBLOCK; 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds if (psl) 19661da177e4SLinus Torvalds count += psl->sl_max; 19678b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); 19681da177e4SLinus Torvalds if (!newpsl) { 19691da177e4SLinus Torvalds err = -ENOBUFS; 19701da177e4SLinus Torvalds goto done; 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds newpsl->sl_max = count; 19731da177e4SLinus Torvalds newpsl->sl_count = count - IP_SFBLOCK; 19741da177e4SLinus Torvalds if (psl) { 19751da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) 19761da177e4SLinus Torvalds newpsl->sl_addr[i] = psl->sl_addr[i]; 19771da177e4SLinus Torvalds sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max)); 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds pmc->sflist = psl = newpsl; 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds rv = 1; /* > 0 for insert logic below if sl_count is 0 */ 19821da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 19831da177e4SLinus Torvalds rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, 198463007727SAl Viro sizeof(__be32)); 19851da177e4SLinus Torvalds if (rv == 0) 19861da177e4SLinus Torvalds break; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds if (rv == 0) /* address already there is an error */ 19891da177e4SLinus Torvalds goto done; 19901da177e4SLinus Torvalds for (j=psl->sl_count-1; j>=i; j--) 19911da177e4SLinus Torvalds psl->sl_addr[j+1] = psl->sl_addr[j]; 19921da177e4SLinus Torvalds psl->sl_addr[i] = mreqs->imr_sourceaddr; 19931da177e4SLinus Torvalds psl->sl_count++; 19941da177e4SLinus Torvalds err = 0; 19951da177e4SLinus Torvalds /* update the interface list */ 19961da177e4SLinus Torvalds ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 19971da177e4SLinus Torvalds &mreqs->imr_sourceaddr, 1); 19981da177e4SLinus Torvalds done: 19996756ae4bSStephen Hemminger rtnl_unlock(); 20008cdaaa15SDavid L Stevens if (leavegroup) 20018cdaaa15SDavid L Stevens return ip_mc_leave_group(sk, &imr); 20021da177e4SLinus Torvalds return err; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) 20061da177e4SLinus Torvalds { 20079951f036SDavid L Stevens int err = 0; 20081da177e4SLinus Torvalds struct ip_mreqn imr; 200963007727SAl Viro __be32 addr = msf->imsf_multiaddr; 20101da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 20111da177e4SLinus Torvalds struct in_device *in_dev; 20121da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 20131da177e4SLinus Torvalds struct ip_sf_socklist *newpsl, *psl; 2014877acedcSDaniel Lezcano struct net *net = sock_net(sk); 20159951f036SDavid L Stevens int leavegroup = 0; 20161da177e4SLinus Torvalds 2017f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 20181da177e4SLinus Torvalds return -EINVAL; 20191da177e4SLinus Torvalds if (msf->imsf_fmode != MCAST_INCLUDE && 20201da177e4SLinus Torvalds msf->imsf_fmode != MCAST_EXCLUDE) 20211da177e4SLinus Torvalds return -EINVAL; 20221da177e4SLinus Torvalds 20236756ae4bSStephen Hemminger rtnl_lock(); 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 20261da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 20271da177e4SLinus Torvalds imr.imr_ifindex = ifindex; 2028877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds if (!in_dev) { 20311da177e4SLinus Torvalds err = -ENODEV; 20321da177e4SLinus Torvalds goto done; 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds 20359951f036SDavid L Stevens /* special case - (INCLUDE, empty) == LEAVE_GROUP */ 20369951f036SDavid L Stevens if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { 20379951f036SDavid L Stevens leavegroup = 1; 20389951f036SDavid L Stevens goto done; 20399951f036SDavid L Stevens } 20409951f036SDavid L Stevens 20411da177e4SLinus Torvalds for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 20421da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 20431da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 20441da177e4SLinus Torvalds break; 20451da177e4SLinus Torvalds } 2046917f2f10SDavid L Stevens if (!pmc) { /* must have a prior join */ 2047917f2f10SDavid L Stevens err = -EINVAL; 20481da177e4SLinus Torvalds goto done; 2049917f2f10SDavid L Stevens } 20501da177e4SLinus Torvalds if (msf->imsf_numsrc) { 20518b3a7005SKris Katterjohn newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), 20528b3a7005SKris Katterjohn GFP_KERNEL); 20531da177e4SLinus Torvalds if (!newpsl) { 20541da177e4SLinus Torvalds err = -ENOBUFS; 20551da177e4SLinus Torvalds goto done; 20561da177e4SLinus Torvalds } 20571da177e4SLinus Torvalds newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; 20581da177e4SLinus Torvalds memcpy(newpsl->sl_addr, msf->imsf_slist, 20591da177e4SLinus Torvalds msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); 20601da177e4SLinus Torvalds err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 20611da177e4SLinus Torvalds msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); 20621da177e4SLinus Torvalds if (err) { 20631da177e4SLinus Torvalds sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); 20641da177e4SLinus Torvalds goto done; 20651da177e4SLinus Torvalds } 20668713dbf0SYan Zheng } else { 20671da177e4SLinus Torvalds newpsl = NULL; 20688713dbf0SYan Zheng (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, 20698713dbf0SYan Zheng msf->imsf_fmode, 0, NULL, 0); 20708713dbf0SYan Zheng } 20711da177e4SLinus Torvalds psl = pmc->sflist; 20721da177e4SLinus Torvalds if (psl) { 20731da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 20741da177e4SLinus Torvalds psl->sl_count, psl->sl_addr, 0); 20751da177e4SLinus Torvalds sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max)); 20761da177e4SLinus Torvalds } else 20771da177e4SLinus Torvalds (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, 20781da177e4SLinus Torvalds 0, NULL, 0); 20791da177e4SLinus Torvalds pmc->sflist = newpsl; 20801da177e4SLinus Torvalds pmc->sfmode = msf->imsf_fmode; 2081917f2f10SDavid L Stevens err = 0; 20821da177e4SLinus Torvalds done: 20836756ae4bSStephen Hemminger rtnl_unlock(); 20849951f036SDavid L Stevens if (leavegroup) 20859951f036SDavid L Stevens err = ip_mc_leave_group(sk, &imr); 20861da177e4SLinus Torvalds return err; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, 20901da177e4SLinus Torvalds struct ip_msfilter __user *optval, int __user *optlen) 20911da177e4SLinus Torvalds { 20921da177e4SLinus Torvalds int err, len, count, copycount; 20931da177e4SLinus Torvalds struct ip_mreqn imr; 209463007727SAl Viro __be32 addr = msf->imsf_multiaddr; 20951da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 20961da177e4SLinus Torvalds struct in_device *in_dev; 20971da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 20981da177e4SLinus Torvalds struct ip_sf_socklist *psl; 2099877acedcSDaniel Lezcano struct net *net = sock_net(sk); 21001da177e4SLinus Torvalds 2101f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21021da177e4SLinus Torvalds return -EINVAL; 21031da177e4SLinus Torvalds 21046756ae4bSStephen Hemminger rtnl_lock(); 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 21071da177e4SLinus Torvalds imr.imr_address.s_addr = msf->imsf_interface; 21081da177e4SLinus Torvalds imr.imr_ifindex = 0; 2109877acedcSDaniel Lezcano in_dev = ip_mc_find_dev(net, &imr); 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds if (!in_dev) { 21121da177e4SLinus Torvalds err = -ENODEV; 21131da177e4SLinus Torvalds goto done; 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 21181da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && 21191da177e4SLinus Torvalds pmc->multi.imr_ifindex == imr.imr_ifindex) 21201da177e4SLinus Torvalds break; 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 21231da177e4SLinus Torvalds goto done; 21241da177e4SLinus Torvalds msf->imsf_fmode = pmc->sfmode; 21251da177e4SLinus Torvalds psl = pmc->sflist; 21266756ae4bSStephen Hemminger rtnl_unlock(); 21271da177e4SLinus Torvalds if (!psl) { 21281da177e4SLinus Torvalds len = 0; 21291da177e4SLinus Torvalds count = 0; 21301da177e4SLinus Torvalds } else { 21311da177e4SLinus Torvalds count = psl->sl_count; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; 21341da177e4SLinus Torvalds len = copycount * sizeof(psl->sl_addr[0]); 21351da177e4SLinus Torvalds msf->imsf_numsrc = count; 21361da177e4SLinus Torvalds if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || 21371da177e4SLinus Torvalds copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { 21381da177e4SLinus Torvalds return -EFAULT; 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds if (len && 21411da177e4SLinus Torvalds copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) 21421da177e4SLinus Torvalds return -EFAULT; 21431da177e4SLinus Torvalds return 0; 21441da177e4SLinus Torvalds done: 21456756ae4bSStephen Hemminger rtnl_unlock(); 21461da177e4SLinus Torvalds return err; 21471da177e4SLinus Torvalds } 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, 21501da177e4SLinus Torvalds struct group_filter __user *optval, int __user *optlen) 21511da177e4SLinus Torvalds { 21521da177e4SLinus Torvalds int err, i, count, copycount; 21531da177e4SLinus Torvalds struct sockaddr_in *psin; 215463007727SAl Viro __be32 addr; 21551da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 21561da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21571da177e4SLinus Torvalds struct ip_sf_socklist *psl; 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds psin = (struct sockaddr_in *)&gsf->gf_group; 21601da177e4SLinus Torvalds if (psin->sin_family != AF_INET) 21611da177e4SLinus Torvalds return -EINVAL; 21621da177e4SLinus Torvalds addr = psin->sin_addr.s_addr; 2163f97c1e0cSJoe Perches if (!ipv4_is_multicast(addr)) 21641da177e4SLinus Torvalds return -EINVAL; 21651da177e4SLinus Torvalds 21666756ae4bSStephen Hemminger rtnl_lock(); 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 21711da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == addr && 21721da177e4SLinus Torvalds pmc->multi.imr_ifindex == gsf->gf_interface) 21731da177e4SLinus Torvalds break; 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds if (!pmc) /* must have a prior join */ 21761da177e4SLinus Torvalds goto done; 21771da177e4SLinus Torvalds gsf->gf_fmode = pmc->sfmode; 21781da177e4SLinus Torvalds psl = pmc->sflist; 21796756ae4bSStephen Hemminger rtnl_unlock(); 21801da177e4SLinus Torvalds count = psl ? psl->sl_count : 0; 21811da177e4SLinus Torvalds copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; 21821da177e4SLinus Torvalds gsf->gf_numsrc = count; 21831da177e4SLinus Torvalds if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || 21841da177e4SLinus Torvalds copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { 21851da177e4SLinus Torvalds return -EFAULT; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds for (i=0; i<copycount; i++) { 21881da177e4SLinus Torvalds struct sockaddr_storage ss; 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds psin = (struct sockaddr_in *)&ss; 21911da177e4SLinus Torvalds memset(&ss, 0, sizeof(ss)); 21921da177e4SLinus Torvalds psin->sin_family = AF_INET; 21931da177e4SLinus Torvalds psin->sin_addr.s_addr = psl->sl_addr[i]; 21941da177e4SLinus Torvalds if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) 21951da177e4SLinus Torvalds return -EFAULT; 21961da177e4SLinus Torvalds } 21971da177e4SLinus Torvalds return 0; 21981da177e4SLinus Torvalds done: 21996756ae4bSStephen Hemminger rtnl_unlock(); 22001da177e4SLinus Torvalds return err; 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds /* 22041da177e4SLinus Torvalds * check if a multicast source filter allows delivery for a given <src,dst,intf> 22051da177e4SLinus Torvalds */ 2206c0cda068SAl Viro int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif) 22071da177e4SLinus Torvalds { 22081da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22091da177e4SLinus Torvalds struct ip_mc_socklist *pmc; 22101da177e4SLinus Torvalds struct ip_sf_socklist *psl; 22111da177e4SLinus Torvalds int i; 22121da177e4SLinus Torvalds 2213f97c1e0cSJoe Perches if (!ipv4_is_multicast(loc_addr)) 22141da177e4SLinus Torvalds return 1; 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds for (pmc=inet->mc_list; pmc; pmc=pmc->next) { 22171da177e4SLinus Torvalds if (pmc->multi.imr_multiaddr.s_addr == loc_addr && 22181da177e4SLinus Torvalds pmc->multi.imr_ifindex == dif) 22191da177e4SLinus Torvalds break; 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds if (!pmc) 2222f771bef9SNivedita Singhvi return inet->mc_all; 22231da177e4SLinus Torvalds psl = pmc->sflist; 22241da177e4SLinus Torvalds if (!psl) 22251da177e4SLinus Torvalds return pmc->sfmode == MCAST_EXCLUDE; 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds for (i=0; i<psl->sl_count; i++) { 22281da177e4SLinus Torvalds if (psl->sl_addr[i] == rmt_addr) 22291da177e4SLinus Torvalds break; 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) 22321da177e4SLinus Torvalds return 0; 22331da177e4SLinus Torvalds if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) 22341da177e4SLinus Torvalds return 0; 22351da177e4SLinus Torvalds return 1; 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds /* 22391da177e4SLinus Torvalds * A socket is closing. 22401da177e4SLinus Torvalds */ 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds void ip_mc_drop_socket(struct sock *sk) 22431da177e4SLinus Torvalds { 22441da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 22451da177e4SLinus Torvalds struct ip_mc_socklist *iml; 2246877acedcSDaniel Lezcano struct net *net = sock_net(sk); 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds if (inet->mc_list == NULL) 22491da177e4SLinus Torvalds return; 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds rtnl_lock(); 22521da177e4SLinus Torvalds while ((iml = inet->mc_list) != NULL) { 22531da177e4SLinus Torvalds struct in_device *in_dev; 22541da177e4SLinus Torvalds inet->mc_list = iml->next; 22551da177e4SLinus Torvalds 2256877acedcSDaniel Lezcano in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 22571da177e4SLinus Torvalds (void) ip_mc_leave_src(sk, iml, in_dev); 2258bb699cbcSMichal Ruzicka if (in_dev != NULL) { 22591da177e4SLinus Torvalds ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 22601da177e4SLinus Torvalds in_dev_put(in_dev); 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds sock_kfree_s(sk, iml, sizeof(*iml)); 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds rtnl_unlock(); 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 2267a60c4923SAl Viro int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto) 22681da177e4SLinus Torvalds { 22691da177e4SLinus Torvalds struct ip_mc_list *im; 22701da177e4SLinus Torvalds struct ip_sf_list *psf; 22711da177e4SLinus Torvalds int rv = 0; 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 22741da177e4SLinus Torvalds for (im=in_dev->mc_list; im; im=im->next) { 22751da177e4SLinus Torvalds if (im->multiaddr == mc_addr) 22761da177e4SLinus Torvalds break; 22771da177e4SLinus Torvalds } 22781da177e4SLinus Torvalds if (im && proto == IPPROTO_IGMP) { 22791da177e4SLinus Torvalds rv = 1; 22801da177e4SLinus Torvalds } else if (im) { 22811da177e4SLinus Torvalds if (src_addr) { 22821da177e4SLinus Torvalds for (psf=im->sources; psf; psf=psf->sf_next) { 22831da177e4SLinus Torvalds if (psf->sf_inaddr == src_addr) 22841da177e4SLinus Torvalds break; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds if (psf) 22871da177e4SLinus Torvalds rv = psf->sf_count[MCAST_INCLUDE] || 22881da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE] != 22891da177e4SLinus Torvalds im->sfcount[MCAST_EXCLUDE]; 22901da177e4SLinus Torvalds else 22911da177e4SLinus Torvalds rv = im->sfcount[MCAST_EXCLUDE] != 0; 22921da177e4SLinus Torvalds } else 22931da177e4SLinus Torvalds rv = 1; /* unspecified source; tentatively allow */ 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 22961da177e4SLinus Torvalds return rv; 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS) 23001da177e4SLinus Torvalds struct igmp_mc_iter_state { 23017091e728SAlexey Dobriyan struct seq_net_private p; 23021da177e4SLinus Torvalds struct net_device *dev; 23031da177e4SLinus Torvalds struct in_device *in_dev; 23041da177e4SLinus Torvalds }; 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) 23091da177e4SLinus Torvalds { 23107091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 23111da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 23121da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 23131da177e4SLinus Torvalds 23147562f876SPavel Emelianov state->in_dev = NULL; 231561fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 23161da177e4SLinus Torvalds struct in_device *in_dev; 23176baff150SEric Dumazet 23186baff150SEric Dumazet in_dev = __in_dev_get_rcu(state->dev); 23191da177e4SLinus Torvalds if (!in_dev) 23201da177e4SLinus Torvalds continue; 23211da177e4SLinus Torvalds read_lock(&in_dev->mc_list_lock); 23221da177e4SLinus Torvalds im = in_dev->mc_list; 23231da177e4SLinus Torvalds if (im) { 23241da177e4SLinus Torvalds state->in_dev = in_dev; 23251da177e4SLinus Torvalds break; 23261da177e4SLinus Torvalds } 23271da177e4SLinus Torvalds read_unlock(&in_dev->mc_list_lock); 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds return im; 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) 23331da177e4SLinus Torvalds { 23341da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 23351da177e4SLinus Torvalds im = im->next; 23361da177e4SLinus Torvalds while (!im) { 23376baff150SEric Dumazet if (likely(state->in_dev != NULL)) 23381da177e4SLinus Torvalds read_unlock(&state->in_dev->mc_list_lock); 23396baff150SEric Dumazet 23406baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 23411da177e4SLinus Torvalds if (!state->dev) { 23421da177e4SLinus Torvalds state->in_dev = NULL; 23431da177e4SLinus Torvalds break; 23441da177e4SLinus Torvalds } 23456baff150SEric Dumazet state->in_dev = __in_dev_get_rcu(state->dev); 23461da177e4SLinus Torvalds if (!state->in_dev) 23471da177e4SLinus Torvalds continue; 23481da177e4SLinus Torvalds read_lock(&state->in_dev->mc_list_lock); 23491da177e4SLinus Torvalds im = state->in_dev->mc_list; 23501da177e4SLinus Torvalds } 23511da177e4SLinus Torvalds return im; 23521da177e4SLinus Torvalds } 23531da177e4SLinus Torvalds 23541da177e4SLinus Torvalds static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) 23551da177e4SLinus Torvalds { 23561da177e4SLinus Torvalds struct ip_mc_list *im = igmp_mc_get_first(seq); 23571da177e4SLinus Torvalds if (im) 23581da177e4SLinus Torvalds while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) 23591da177e4SLinus Torvalds --pos; 23601da177e4SLinus Torvalds return pos ? NULL : im; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) 236461fbab77Sstephen hemminger __acquires(rcu) 23651da177e4SLinus Torvalds { 236661fbab77Sstephen hemminger rcu_read_lock(); 23671da177e4SLinus Torvalds return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 23711da177e4SLinus Torvalds { 23721da177e4SLinus Torvalds struct ip_mc_list *im; 23731da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 23741da177e4SLinus Torvalds im = igmp_mc_get_first(seq); 23751da177e4SLinus Torvalds else 23761da177e4SLinus Torvalds im = igmp_mc_get_next(seq, v); 23771da177e4SLinus Torvalds ++*pos; 23781da177e4SLinus Torvalds return im; 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds static void igmp_mc_seq_stop(struct seq_file *seq, void *v) 238261fbab77Sstephen hemminger __releases(rcu) 23831da177e4SLinus Torvalds { 23841da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 23851da177e4SLinus Torvalds if (likely(state->in_dev != NULL)) { 23861da177e4SLinus Torvalds read_unlock(&state->in_dev->mc_list_lock); 23871da177e4SLinus Torvalds state->in_dev = NULL; 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds state->dev = NULL; 239061fbab77Sstephen hemminger rcu_read_unlock(); 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds static int igmp_mc_seq_show(struct seq_file *seq, void *v) 23941da177e4SLinus Torvalds { 23951da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 23961da177e4SLinus Torvalds seq_puts(seq, 23971da177e4SLinus Torvalds "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); 23981da177e4SLinus Torvalds else { 23991da177e4SLinus Torvalds struct ip_mc_list *im = (struct ip_mc_list *)v; 24001da177e4SLinus Torvalds struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); 24011da177e4SLinus Torvalds char *querier; 24021da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST 24031da177e4SLinus Torvalds querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : 24041da177e4SLinus Torvalds IGMP_V2_SEEN(state->in_dev) ? "V2" : 24051da177e4SLinus Torvalds "V3"; 24061da177e4SLinus Torvalds #else 24071da177e4SLinus Torvalds querier = "NONE"; 24081da177e4SLinus Torvalds #endif 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds if (state->in_dev->mc_list == im) { 24111da177e4SLinus Torvalds seq_printf(seq, "%d\t%-10s: %5d %7s\n", 2412b8bae41eSRami Rosen state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); 24131da177e4SLinus Torvalds } 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds seq_printf(seq, 2416338fcf98SAlexey Dobriyan "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", 24171da177e4SLinus Torvalds im->multiaddr, im->users, 24181da177e4SLinus Torvalds im->tm_running, im->tm_running ? 24191da177e4SLinus Torvalds jiffies_to_clock_t(im->timer.expires-jiffies) : 0, 24201da177e4SLinus Torvalds im->reporter); 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds return 0; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds 2425f690808eSStephen Hemminger static const struct seq_operations igmp_mc_seq_ops = { 24261da177e4SLinus Torvalds .start = igmp_mc_seq_start, 24271da177e4SLinus Torvalds .next = igmp_mc_seq_next, 24281da177e4SLinus Torvalds .stop = igmp_mc_seq_stop, 24291da177e4SLinus Torvalds .show = igmp_mc_seq_show, 24301da177e4SLinus Torvalds }; 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds static int igmp_mc_seq_open(struct inode *inode, struct file *file) 24331da177e4SLinus Torvalds { 24347091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mc_seq_ops, 2435cf7732e4SPavel Emelyanov sizeof(struct igmp_mc_iter_state)); 24361da177e4SLinus Torvalds } 24371da177e4SLinus Torvalds 24389a32144eSArjan van de Ven static const struct file_operations igmp_mc_seq_fops = { 24391da177e4SLinus Torvalds .owner = THIS_MODULE, 24401da177e4SLinus Torvalds .open = igmp_mc_seq_open, 24411da177e4SLinus Torvalds .read = seq_read, 24421da177e4SLinus Torvalds .llseek = seq_lseek, 24437091e728SAlexey Dobriyan .release = seq_release_net, 24441da177e4SLinus Torvalds }; 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds struct igmp_mcf_iter_state { 24477091e728SAlexey Dobriyan struct seq_net_private p; 24481da177e4SLinus Torvalds struct net_device *dev; 24491da177e4SLinus Torvalds struct in_device *idev; 24501da177e4SLinus Torvalds struct ip_mc_list *im; 24511da177e4SLinus Torvalds }; 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) 24561da177e4SLinus Torvalds { 24577091e728SAlexey Dobriyan struct net *net = seq_file_net(seq); 24581da177e4SLinus Torvalds struct ip_sf_list *psf = NULL; 24591da177e4SLinus Torvalds struct ip_mc_list *im = NULL; 24601da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 24611da177e4SLinus Torvalds 24627562f876SPavel Emelianov state->idev = NULL; 24637562f876SPavel Emelianov state->im = NULL; 246461fbab77Sstephen hemminger for_each_netdev_rcu(net, state->dev) { 24651da177e4SLinus Torvalds struct in_device *idev; 24666baff150SEric Dumazet idev = __in_dev_get_rcu(state->dev); 24671da177e4SLinus Torvalds if (unlikely(idev == NULL)) 24681da177e4SLinus Torvalds continue; 24691da177e4SLinus Torvalds read_lock(&idev->mc_list_lock); 24701da177e4SLinus Torvalds im = idev->mc_list; 24711da177e4SLinus Torvalds if (likely(im != NULL)) { 24721da177e4SLinus Torvalds spin_lock_bh(&im->lock); 24731da177e4SLinus Torvalds psf = im->sources; 24741da177e4SLinus Torvalds if (likely(psf != NULL)) { 24751da177e4SLinus Torvalds state->im = im; 24761da177e4SLinus Torvalds state->idev = idev; 24771da177e4SLinus Torvalds break; 24781da177e4SLinus Torvalds } 24791da177e4SLinus Torvalds spin_unlock_bh(&im->lock); 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds read_unlock(&idev->mc_list_lock); 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds return psf; 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) 24871da177e4SLinus Torvalds { 24881da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds psf = psf->sf_next; 24911da177e4SLinus Torvalds while (!psf) { 24921da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 24931da177e4SLinus Torvalds state->im = state->im->next; 24941da177e4SLinus Torvalds while (!state->im) { 24956baff150SEric Dumazet if (likely(state->idev != NULL)) 24961da177e4SLinus Torvalds read_unlock(&state->idev->mc_list_lock); 24976baff150SEric Dumazet 24986baff150SEric Dumazet state->dev = next_net_device_rcu(state->dev); 24991da177e4SLinus Torvalds if (!state->dev) { 25001da177e4SLinus Torvalds state->idev = NULL; 25011da177e4SLinus Torvalds goto out; 25021da177e4SLinus Torvalds } 25036baff150SEric Dumazet state->idev = __in_dev_get_rcu(state->dev); 25041da177e4SLinus Torvalds if (!state->idev) 25051da177e4SLinus Torvalds continue; 25061da177e4SLinus Torvalds read_lock(&state->idev->mc_list_lock); 25071da177e4SLinus Torvalds state->im = state->idev->mc_list; 25081da177e4SLinus Torvalds } 25091da177e4SLinus Torvalds if (!state->im) 25101da177e4SLinus Torvalds break; 25111da177e4SLinus Torvalds spin_lock_bh(&state->im->lock); 25121da177e4SLinus Torvalds psf = state->im->sources; 25131da177e4SLinus Torvalds } 25141da177e4SLinus Torvalds out: 25151da177e4SLinus Torvalds return psf; 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) 25191da177e4SLinus Torvalds { 25201da177e4SLinus Torvalds struct ip_sf_list *psf = igmp_mcf_get_first(seq); 25211da177e4SLinus Torvalds if (psf) 25221da177e4SLinus Torvalds while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) 25231da177e4SLinus Torvalds --pos; 25241da177e4SLinus Torvalds return pos ? NULL : psf; 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) 252861fbab77Sstephen hemminger __acquires(rcu) 25291da177e4SLinus Torvalds { 253061fbab77Sstephen hemminger rcu_read_lock(); 25311da177e4SLinus Torvalds return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 25321da177e4SLinus Torvalds } 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25351da177e4SLinus Torvalds { 25361da177e4SLinus Torvalds struct ip_sf_list *psf; 25371da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 25381da177e4SLinus Torvalds psf = igmp_mcf_get_first(seq); 25391da177e4SLinus Torvalds else 25401da177e4SLinus Torvalds psf = igmp_mcf_get_next(seq, v); 25411da177e4SLinus Torvalds ++*pos; 25421da177e4SLinus Torvalds return psf; 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) 254661fbab77Sstephen hemminger __releases(rcu) 25471da177e4SLinus Torvalds { 25481da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 25491da177e4SLinus Torvalds if (likely(state->im != NULL)) { 25501da177e4SLinus Torvalds spin_unlock_bh(&state->im->lock); 25511da177e4SLinus Torvalds state->im = NULL; 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds if (likely(state->idev != NULL)) { 25541da177e4SLinus Torvalds read_unlock(&state->idev->mc_list_lock); 25551da177e4SLinus Torvalds state->idev = NULL; 25561da177e4SLinus Torvalds } 25571da177e4SLinus Torvalds state->dev = NULL; 255861fbab77Sstephen hemminger rcu_read_unlock(); 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds static int igmp_mcf_seq_show(struct seq_file *seq, void *v) 25621da177e4SLinus Torvalds { 25631da177e4SLinus Torvalds struct ip_sf_list *psf = (struct ip_sf_list *)v; 25641da177e4SLinus Torvalds struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 25651da177e4SLinus Torvalds 25661da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 25671da177e4SLinus Torvalds seq_printf(seq, 25681da177e4SLinus Torvalds "%3s %6s " 25691da177e4SLinus Torvalds "%10s %10s %6s %6s\n", "Idx", 25701da177e4SLinus Torvalds "Device", "MCA", 25711da177e4SLinus Torvalds "SRC", "INC", "EXC"); 25721da177e4SLinus Torvalds } else { 25731da177e4SLinus Torvalds seq_printf(seq, 25741da177e4SLinus Torvalds "%3d %6.6s 0x%08x " 25751da177e4SLinus Torvalds "0x%08x %6lu %6lu\n", 25761da177e4SLinus Torvalds state->dev->ifindex, state->dev->name, 25771da177e4SLinus Torvalds ntohl(state->im->multiaddr), 25781da177e4SLinus Torvalds ntohl(psf->sf_inaddr), 25791da177e4SLinus Torvalds psf->sf_count[MCAST_INCLUDE], 25801da177e4SLinus Torvalds psf->sf_count[MCAST_EXCLUDE]); 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds return 0; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 2585f690808eSStephen Hemminger static const struct seq_operations igmp_mcf_seq_ops = { 25861da177e4SLinus Torvalds .start = igmp_mcf_seq_start, 25871da177e4SLinus Torvalds .next = igmp_mcf_seq_next, 25881da177e4SLinus Torvalds .stop = igmp_mcf_seq_stop, 25891da177e4SLinus Torvalds .show = igmp_mcf_seq_show, 25901da177e4SLinus Torvalds }; 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds static int igmp_mcf_seq_open(struct inode *inode, struct file *file) 25931da177e4SLinus Torvalds { 25947091e728SAlexey Dobriyan return seq_open_net(inode, file, &igmp_mcf_seq_ops, 2595cf7732e4SPavel Emelyanov sizeof(struct igmp_mcf_iter_state)); 25961da177e4SLinus Torvalds } 25971da177e4SLinus Torvalds 25989a32144eSArjan van de Ven static const struct file_operations igmp_mcf_seq_fops = { 25991da177e4SLinus Torvalds .owner = THIS_MODULE, 26001da177e4SLinus Torvalds .open = igmp_mcf_seq_open, 26011da177e4SLinus Torvalds .read = seq_read, 26021da177e4SLinus Torvalds .llseek = seq_lseek, 26037091e728SAlexey Dobriyan .release = seq_release_net, 26047091e728SAlexey Dobriyan }; 26057091e728SAlexey Dobriyan 26067091e728SAlexey Dobriyan static int igmp_net_init(struct net *net) 26077091e728SAlexey Dobriyan { 26087091e728SAlexey Dobriyan struct proc_dir_entry *pde; 26097091e728SAlexey Dobriyan 26107091e728SAlexey Dobriyan pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops); 26117091e728SAlexey Dobriyan if (!pde) 26127091e728SAlexey Dobriyan goto out_igmp; 26137091e728SAlexey Dobriyan pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops); 26147091e728SAlexey Dobriyan if (!pde) 26157091e728SAlexey Dobriyan goto out_mcfilter; 26167091e728SAlexey Dobriyan return 0; 26177091e728SAlexey Dobriyan 26187091e728SAlexey Dobriyan out_mcfilter: 26197091e728SAlexey Dobriyan proc_net_remove(net, "igmp"); 26207091e728SAlexey Dobriyan out_igmp: 26217091e728SAlexey Dobriyan return -ENOMEM; 26227091e728SAlexey Dobriyan } 26237091e728SAlexey Dobriyan 26247091e728SAlexey Dobriyan static void igmp_net_exit(struct net *net) 26257091e728SAlexey Dobriyan { 26267091e728SAlexey Dobriyan proc_net_remove(net, "mcfilter"); 26277091e728SAlexey Dobriyan proc_net_remove(net, "igmp"); 26287091e728SAlexey Dobriyan } 26297091e728SAlexey Dobriyan 26307091e728SAlexey Dobriyan static struct pernet_operations igmp_net_ops = { 26317091e728SAlexey Dobriyan .init = igmp_net_init, 26327091e728SAlexey Dobriyan .exit = igmp_net_exit, 26331da177e4SLinus Torvalds }; 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds int __init igmp_mc_proc_init(void) 26361da177e4SLinus Torvalds { 26377091e728SAlexey Dobriyan return register_pernet_subsys(&igmp_net_ops); 26381da177e4SLinus Torvalds } 26391da177e4SLinus Torvalds #endif 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds EXPORT_SYMBOL(ip_mc_dec_group); 26421da177e4SLinus Torvalds EXPORT_SYMBOL(ip_mc_inc_group); 26431da177e4SLinus Torvalds EXPORT_SYMBOL(ip_mc_join_group); 2644a816c7c7SJay Vosburgh EXPORT_SYMBOL(ip_mc_rejoin_group); 2645