Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux NET3: Internet Group Management Protocol [IGMP] |
| 3 | * |
| 4 | * This code implements the IGMP protocol as defined in RFC1112. There has |
| 5 | * been a further revision of this protocol since which is now supported. |
| 6 | * |
| 7 | * If you have trouble with this module be careful what gcc you have used, |
| 8 | * the older version didn't come out right using gcc 2.5.8, the newer one |
| 9 | * seems to fall out with gcc 2.6.2. |
| 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Authors: |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 12 | * Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; either version |
| 17 | * 2 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * Fixes: |
| 20 | * |
| 21 | * Alan Cox : Added lots of __inline__ to optimise |
| 22 | * the memory usage of all the tiny little |
| 23 | * functions. |
| 24 | * Alan Cox : Dumped the header building experiment. |
| 25 | * Alan Cox : Minor tweaks ready for multicast routing |
| 26 | * and extended IGMP protocol. |
| 27 | * Alan Cox : Removed a load of inline directives. Gcc 2.5.8 |
| 28 | * writes utterly bogus code otherwise (sigh) |
| 29 | * fixed IGMP loopback to behave in the manner |
| 30 | * desired by mrouted, fixed the fact it has been |
| 31 | * broken since 1.3.6 and cleaned up a few minor |
| 32 | * points. |
| 33 | * |
| 34 | * Chih-Jen Chang : Tried to revise IGMP to Version 2 |
| 35 | * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 36 | * The enhancements are mainly based on Steve Deering's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * ipmulti-3.5 source code. |
| 38 | * Chih-Jen Chang : Added the igmp_get_mrouter_info and |
| 39 | * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of |
| 40 | * the mrouted version on that device. |
| 41 | * Chih-Jen Chang : Added the max_resp_time parameter to |
| 42 | * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter |
| 43 | * to identify the multicast router version |
| 44 | * and do what the IGMP version 2 specified. |
| 45 | * Chih-Jen Chang : Added a timer to revert to IGMP V2 router |
| 46 | * Tsu-Sheng Tsao if the specified time expired. |
| 47 | * Alan Cox : Stop IGMP from 0.0.0.0 being accepted. |
| 48 | * Alan Cox : Use GFP_ATOMIC in the right places. |
| 49 | * Christian Daudt : igmp timer wasn't set for local group |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 50 | * memberships but was being deleted, |
| 51 | * which caused a "del_timer() called |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * from %p with timer not initialized\n" |
| 53 | * message (960131). |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 54 | * Christian Daudt : removed del_timer from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | * igmp_timer_expire function (960205). |
| 56 | * Christian Daudt : igmp_heard_report now only calls |
| 57 | * igmp_timer_expire if tm->running is |
| 58 | * true (960216). |
| 59 | * Malcolm Beattie : ttl comparison wrong in igmp_rcv made |
| 60 | * igmp_heard_query never trigger. Expiry |
| 61 | * miscalculation fixed in igmp_heard_query |
| 62 | * and random() made to return unsigned to |
| 63 | * prevent negative expiry times. |
| 64 | * Alexey Kuznetsov: Wrong group leaving behaviour, backport |
| 65 | * fix from pending 2.1.x patches. |
| 66 | * Alan Cox: Forget to enable FDDI support earlier. |
| 67 | * Alexey Kuznetsov: Fixed leaving groups on device down. |
| 68 | * Alexey Kuznetsov: Accordance to igmp-v2-06 draft. |
| 69 | * David L Stevens: IGMPv3 support, with help from |
| 70 | * Vinay Kulkarni |
| 71 | */ |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 74 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #include <linux/types.h> |
| 77 | #include <linux/kernel.h> |
| 78 | #include <linux/jiffies.h> |
| 79 | #include <linux/string.h> |
| 80 | #include <linux/socket.h> |
| 81 | #include <linux/sockios.h> |
| 82 | #include <linux/in.h> |
| 83 | #include <linux/inet.h> |
| 84 | #include <linux/netdevice.h> |
| 85 | #include <linux/skbuff.h> |
| 86 | #include <linux/inetdevice.h> |
| 87 | #include <linux/igmp.h> |
| 88 | #include <linux/if_arp.h> |
| 89 | #include <linux/rtnetlink.h> |
| 90 | #include <linux/times.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 91 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 92 | #include <net/net_namespace.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 93 | #include <net/arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #include <net/ip.h> |
| 95 | #include <net/protocol.h> |
| 96 | #include <net/route.h> |
| 97 | #include <net/sock.h> |
| 98 | #include <net/checksum.h> |
| 99 | #include <linux/netfilter_ipv4.h> |
| 100 | #ifdef CONFIG_IP_MROUTE |
| 101 | #include <linux/mroute.h> |
| 102 | #endif |
| 103 | #ifdef CONFIG_PROC_FS |
| 104 | #include <linux/proc_fs.h> |
| 105 | #include <linux/seq_file.h> |
| 106 | #endif |
| 107 | |
| 108 | #define IP_MAX_MEMBERSHIPS 20 |
| 109 | #define IP_MAX_MSF 10 |
| 110 | |
| 111 | #ifdef CONFIG_IP_MULTICAST |
| 112 | /* Parameter names and values are taken from igmp-v2-06 draft */ |
| 113 | |
| 114 | #define IGMP_V1_Router_Present_Timeout (400*HZ) |
| 115 | #define IGMP_V2_Router_Present_Timeout (400*HZ) |
| 116 | #define IGMP_Unsolicited_Report_Interval (10*HZ) |
| 117 | #define IGMP_Query_Response_Interval (10*HZ) |
| 118 | #define IGMP_Unsolicited_Report_Count 2 |
| 119 | |
| 120 | |
| 121 | #define IGMP_Initial_Report_Delay (1) |
| 122 | |
| 123 | /* IGMP_Initial_Report_Delay is not from IGMP specs! |
| 124 | * IGMP specs require to report membership immediately after |
| 125 | * joining a group, but we delay the first report by a |
| 126 | * small interval. It seems more natural and still does not |
| 127 | * contradict to specs provided this delay is small enough. |
| 128 | */ |
| 129 | |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 130 | #define IGMP_V1_SEEN(in_dev) \ |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 131 | (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 132 | IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ |
| 133 | ((in_dev)->mr_v1_seen && \ |
| 134 | time_before(jiffies, (in_dev)->mr_v1_seen))) |
| 135 | #define IGMP_V2_SEEN(in_dev) \ |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 136 | (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 137 | IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ |
| 138 | ((in_dev)->mr_v2_seen && \ |
| 139 | time_before(jiffies, (in_dev)->mr_v2_seen))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im); |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 142 | static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | static void igmpv3_clear_delrec(struct in_device *in_dev); |
| 144 | static int sf_setstate(struct ip_mc_list *pmc); |
| 145 | static void sf_markstate(struct ip_mc_list *pmc); |
| 146 | #endif |
| 147 | static void ip_mc_clear_src(struct ip_mc_list *pmc); |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 148 | static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, |
| 149 | int sfcount, __be32 *psfsrc, int delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | static void ip_ma_put(struct ip_mc_list *im) |
| 152 | { |
| 153 | if (atomic_dec_and_test(&im->refcnt)) { |
| 154 | in_dev_put(im->interface); |
Lai Jiangshan | 42ea299d | 2011-03-18 11:44:08 +0800 | [diff] [blame] | 155 | kfree_rcu(im, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
David S. Miller | d9aa938 | 2010-11-15 08:52:02 -0800 | [diff] [blame] | 159 | #define for_each_pmc_rcu(in_dev, pmc) \ |
| 160 | for (pmc = rcu_dereference(in_dev->mc_list); \ |
| 161 | pmc != NULL; \ |
| 162 | pmc = rcu_dereference(pmc->next_rcu)) |
| 163 | |
| 164 | #define for_each_pmc_rtnl(in_dev, pmc) \ |
| 165 | for (pmc = rtnl_dereference(in_dev->mc_list); \ |
| 166 | pmc != NULL; \ |
| 167 | pmc = rtnl_dereference(pmc->next_rcu)) |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | #ifdef CONFIG_IP_MULTICAST |
| 170 | |
| 171 | /* |
| 172 | * Timer management |
| 173 | */ |
| 174 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 175 | static void igmp_stop_timer(struct ip_mc_list *im) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | spin_lock_bh(&im->lock); |
| 178 | if (del_timer(&im->timer)) |
| 179 | atomic_dec(&im->refcnt); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 180 | im->tm_running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | im->reporter = 0; |
| 182 | im->unsolicit_count = 0; |
| 183 | spin_unlock_bh(&im->lock); |
| 184 | } |
| 185 | |
| 186 | /* It must be called with locked im->lock */ |
| 187 | static void igmp_start_timer(struct ip_mc_list *im, int max_delay) |
| 188 | { |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 189 | int tv = net_random() % max_delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 191 | im->tm_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (!mod_timer(&im->timer, jiffies+tv+2)) |
| 193 | atomic_inc(&im->refcnt); |
| 194 | } |
| 195 | |
| 196 | static void igmp_gq_start_timer(struct in_device *in_dev) |
| 197 | { |
| 198 | int tv = net_random() % in_dev->mr_maxdelay; |
| 199 | |
| 200 | in_dev->mr_gq_running = 1; |
| 201 | if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2)) |
| 202 | in_dev_hold(in_dev); |
| 203 | } |
| 204 | |
| 205 | static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) |
| 206 | { |
| 207 | int tv = net_random() % delay; |
| 208 | |
| 209 | if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) |
| 210 | in_dev_hold(in_dev); |
| 211 | } |
| 212 | |
| 213 | static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) |
| 214 | { |
| 215 | spin_lock_bh(&im->lock); |
| 216 | im->unsolicit_count = 0; |
| 217 | if (del_timer(&im->timer)) { |
| 218 | if ((long)(im->timer.expires-jiffies) < max_delay) { |
| 219 | add_timer(&im->timer); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 220 | im->tm_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | spin_unlock_bh(&im->lock); |
| 222 | return; |
| 223 | } |
| 224 | atomic_dec(&im->refcnt); |
| 225 | } |
| 226 | igmp_start_timer(im, max_delay); |
| 227 | spin_unlock_bh(&im->lock); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /* |
| 232 | * Send an IGMP report. |
| 233 | */ |
| 234 | |
| 235 | #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) |
| 236 | |
| 237 | |
| 238 | static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, |
| 239 | int gdeleted, int sdeleted) |
| 240 | { |
| 241 | switch (type) { |
| 242 | case IGMPV3_MODE_IS_INCLUDE: |
| 243 | case IGMPV3_MODE_IS_EXCLUDE: |
| 244 | if (gdeleted || sdeleted) |
| 245 | return 0; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 246 | if (!(pmc->gsquery && !psf->sf_gsresp)) { |
| 247 | if (pmc->sfmode == MCAST_INCLUDE) |
| 248 | return 1; |
| 249 | /* don't include if this source is excluded |
| 250 | * in all filters |
| 251 | */ |
| 252 | if (psf->sf_count[MCAST_INCLUDE]) |
| 253 | return type == IGMPV3_MODE_IS_INCLUDE; |
| 254 | return pmc->sfcount[MCAST_EXCLUDE] == |
| 255 | psf->sf_count[MCAST_EXCLUDE]; |
| 256 | } |
| 257 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | case IGMPV3_CHANGE_TO_INCLUDE: |
| 259 | if (gdeleted || sdeleted) |
| 260 | return 0; |
| 261 | return psf->sf_count[MCAST_INCLUDE] != 0; |
| 262 | case IGMPV3_CHANGE_TO_EXCLUDE: |
| 263 | if (gdeleted || sdeleted) |
| 264 | return 0; |
| 265 | if (pmc->sfcount[MCAST_EXCLUDE] == 0 || |
| 266 | psf->sf_count[MCAST_INCLUDE]) |
| 267 | return 0; |
| 268 | return pmc->sfcount[MCAST_EXCLUDE] == |
| 269 | psf->sf_count[MCAST_EXCLUDE]; |
| 270 | case IGMPV3_ALLOW_NEW_SOURCES: |
| 271 | if (gdeleted || !psf->sf_crcount) |
| 272 | return 0; |
| 273 | return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; |
| 274 | case IGMPV3_BLOCK_OLD_SOURCES: |
| 275 | if (pmc->sfmode == MCAST_INCLUDE) |
| 276 | return gdeleted || (psf->sf_crcount && sdeleted); |
| 277 | return psf->sf_crcount && !gdeleted && !sdeleted; |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int |
| 283 | igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) |
| 284 | { |
| 285 | struct ip_sf_list *psf; |
| 286 | int scount = 0; |
| 287 | |
| 288 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 289 | if (!is_in(pmc, psf, type, gdeleted, sdeleted)) |
| 290 | continue; |
| 291 | scount++; |
| 292 | } |
| 293 | return scount; |
| 294 | } |
| 295 | |
Eric Dumazet | 57e1ab6 | 2010-11-16 20:36:42 +0000 | [diff] [blame] | 296 | #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb)) |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size) |
| 299 | { |
| 300 | struct sk_buff *skb; |
| 301 | struct rtable *rt; |
| 302 | struct iphdr *pip; |
| 303 | struct igmpv3_report *pig; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 304 | struct net *net = dev_net(dev); |
David S. Miller | 31e4543 | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 305 | struct flowi4 fl4; |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 306 | int hlen = LL_RESERVED_SPACE(dev); |
| 307 | int tlen = dev->needed_tailroom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Eric Dumazet | 57e1ab6 | 2010-11-16 20:36:42 +0000 | [diff] [blame] | 309 | while (1) { |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 310 | skb = alloc_skb(size + hlen + tlen, |
Eric Dumazet | 57e1ab6 | 2010-11-16 20:36:42 +0000 | [diff] [blame] | 311 | GFP_ATOMIC | __GFP_NOWARN); |
| 312 | if (skb) |
| 313 | break; |
| 314 | size >>= 1; |
| 315 | if (size < 256) |
| 316 | return NULL; |
| 317 | } |
| 318 | igmp_skb_size(skb) = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
David S. Miller | 31e4543 | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 320 | rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 321 | 0, 0, |
| 322 | IPPROTO_IGMP, 0, dev->ifindex); |
| 323 | if (IS_ERR(rt)) { |
| 324 | kfree_skb(skb); |
| 325 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 328 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | skb->dev = dev; |
| 330 | |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 331 | skb_reserve(skb, hlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 333 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 334 | pip = ip_hdr(skb); |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 335 | skb_put(skb, sizeof(struct iphdr) + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | pip->version = 4; |
| 338 | pip->ihl = (sizeof(struct iphdr)+4)>>2; |
| 339 | pip->tos = 0xc0; |
| 340 | pip->frag_off = htons(IP_DF); |
| 341 | pip->ttl = 1; |
David S. Miller | 492f64c | 2011-05-03 20:53:12 -0700 | [diff] [blame] | 342 | pip->daddr = fl4.daddr; |
| 343 | pip->saddr = fl4.saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | pip->protocol = IPPROTO_IGMP; |
| 345 | pip->tot_len = 0; /* filled in later */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 346 | ip_select_ident(pip, &rt->dst, NULL); |
Daniel Baluta | 5e73ea1 | 2012-04-15 01:34:41 +0000 | [diff] [blame] | 347 | ((u8 *)&pip[1])[0] = IPOPT_RA; |
| 348 | ((u8 *)&pip[1])[1] = 4; |
| 349 | ((u8 *)&pip[1])[2] = 0; |
| 350 | ((u8 *)&pip[1])[3] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 352 | skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; |
Arnaldo Carvalho de Melo | d10ba34 | 2007-03-14 21:05:37 -0300 | [diff] [blame] | 353 | skb_put(skb, sizeof(*pig)); |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 354 | pig = igmpv3_report_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; |
| 356 | pig->resv1 = 0; |
| 357 | pig->csum = 0; |
| 358 | pig->resv2 = 0; |
| 359 | pig->ngrec = 0; |
| 360 | return skb; |
| 361 | } |
| 362 | |
| 363 | static int igmpv3_sendpack(struct sk_buff *skb) |
| 364 | { |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 365 | struct igmphdr *pig = igmp_hdr(skb); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 366 | const int igmplen = skb->tail - skb->transport_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 368 | pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 370 | return ip_local_out(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) |
| 374 | { |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 375 | return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, |
| 379 | int type, struct igmpv3_grec **ppgr) |
| 380 | { |
| 381 | struct net_device *dev = pmc->interface->dev; |
| 382 | struct igmpv3_report *pih; |
| 383 | struct igmpv3_grec *pgr; |
| 384 | |
| 385 | if (!skb) |
| 386 | skb = igmpv3_newpack(dev, dev->mtu); |
| 387 | if (!skb) |
| 388 | return NULL; |
| 389 | pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec)); |
| 390 | pgr->grec_type = type; |
| 391 | pgr->grec_auxwords = 0; |
| 392 | pgr->grec_nsrcs = 0; |
| 393 | pgr->grec_mca = pmc->multiaddr; |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 394 | pih = igmpv3_report_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | pih->ngrec = htons(ntohs(pih->ngrec)+1); |
| 396 | *ppgr = pgr; |
| 397 | return skb; |
| 398 | } |
| 399 | |
Eric Dumazet | 57e1ab6 | 2010-11-16 20:36:42 +0000 | [diff] [blame] | 400 | #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | skb_tailroom(skb)) : 0) |
| 402 | |
| 403 | static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, |
| 404 | int type, int gdeleted, int sdeleted) |
| 405 | { |
| 406 | struct net_device *dev = pmc->interface->dev; |
| 407 | struct igmpv3_report *pih; |
| 408 | struct igmpv3_grec *pgr = NULL; |
| 409 | struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 410 | int scount, stotal, first, isquery, truncate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | if (pmc->multiaddr == IGMP_ALL_HOSTS) |
| 413 | return skb; |
| 414 | |
| 415 | isquery = type == IGMPV3_MODE_IS_INCLUDE || |
| 416 | type == IGMPV3_MODE_IS_EXCLUDE; |
| 417 | truncate = type == IGMPV3_MODE_IS_EXCLUDE || |
| 418 | type == IGMPV3_CHANGE_TO_EXCLUDE; |
| 419 | |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 420 | stotal = scount = 0; |
| 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | psf_list = sdeleted ? &pmc->tomb : &pmc->sources; |
| 423 | |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 424 | if (!*psf_list) |
| 425 | goto empty_source; |
| 426 | |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 427 | pih = skb ? igmpv3_report_hdr(skb) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | /* EX and TO_EX get a fresh packet, if needed */ |
| 430 | if (truncate) { |
| 431 | if (pih && pih->ngrec && |
| 432 | AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { |
| 433 | if (skb) |
| 434 | igmpv3_sendpack(skb); |
| 435 | skb = igmpv3_newpack(dev, dev->mtu); |
| 436 | } |
| 437 | } |
| 438 | first = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | psf_prev = NULL; |
| 440 | for (psf=*psf_list; psf; psf=psf_next) { |
Al Viro | ea4d9e7 | 2006-09-27 18:30:52 -0700 | [diff] [blame] | 441 | __be32 *psrc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
| 443 | psf_next = psf->sf_next; |
| 444 | |
| 445 | if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { |
| 446 | psf_prev = psf; |
| 447 | continue; |
| 448 | } |
| 449 | |
| 450 | /* clear marks on query responses */ |
| 451 | if (isquery) |
| 452 | psf->sf_gsresp = 0; |
| 453 | |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 454 | if (AVAILABLE(skb) < sizeof(__be32) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | first*sizeof(struct igmpv3_grec)) { |
| 456 | if (truncate && !first) |
| 457 | break; /* truncate these */ |
| 458 | if (pgr) |
| 459 | pgr->grec_nsrcs = htons(scount); |
| 460 | if (skb) |
| 461 | igmpv3_sendpack(skb); |
| 462 | skb = igmpv3_newpack(dev, dev->mtu); |
| 463 | first = 1; |
| 464 | scount = 0; |
| 465 | } |
| 466 | if (first) { |
| 467 | skb = add_grhead(skb, pmc, type, &pgr); |
| 468 | first = 0; |
| 469 | } |
Alexey Dobriyan | cc63f70 | 2007-02-06 14:35:25 -0800 | [diff] [blame] | 470 | if (!skb) |
| 471 | return NULL; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 472 | psrc = (__be32 *)skb_put(skb, sizeof(__be32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | *psrc = psf->sf_inaddr; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 474 | scount++; stotal++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | if ((type == IGMPV3_ALLOW_NEW_SOURCES || |
| 476 | type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { |
| 477 | psf->sf_crcount--; |
| 478 | if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { |
| 479 | if (psf_prev) |
| 480 | psf_prev->sf_next = psf->sf_next; |
| 481 | else |
| 482 | *psf_list = psf->sf_next; |
| 483 | kfree(psf); |
| 484 | continue; |
| 485 | } |
| 486 | } |
| 487 | psf_prev = psf; |
| 488 | } |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 489 | |
| 490 | empty_source: |
| 491 | if (!stotal) { |
| 492 | if (type == IGMPV3_ALLOW_NEW_SOURCES || |
| 493 | type == IGMPV3_BLOCK_OLD_SOURCES) |
| 494 | return skb; |
| 495 | if (pmc->crcount || isquery) { |
| 496 | /* make sure we have room for group header */ |
| 497 | if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) { |
| 498 | igmpv3_sendpack(skb); |
| 499 | skb = NULL; /* add_grhead will get a new one */ |
| 500 | } |
| 501 | skb = add_grhead(skb, pmc, type, &pgr); |
| 502 | } |
| 503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (pgr) |
| 505 | pgr->grec_nsrcs = htons(scount); |
| 506 | |
| 507 | if (isquery) |
| 508 | pmc->gsquery = 0; /* clear query state on report */ |
| 509 | return skb; |
| 510 | } |
| 511 | |
| 512 | static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) |
| 513 | { |
| 514 | struct sk_buff *skb = NULL; |
| 515 | int type; |
| 516 | |
| 517 | if (!pmc) { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 518 | rcu_read_lock(); |
| 519 | for_each_pmc_rcu(in_dev, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | if (pmc->multiaddr == IGMP_ALL_HOSTS) |
| 521 | continue; |
| 522 | spin_lock_bh(&pmc->lock); |
| 523 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 524 | type = IGMPV3_MODE_IS_EXCLUDE; |
| 525 | else |
| 526 | type = IGMPV3_MODE_IS_INCLUDE; |
| 527 | skb = add_grec(skb, pmc, type, 0, 0); |
| 528 | spin_unlock_bh(&pmc->lock); |
| 529 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 530 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } else { |
| 532 | spin_lock_bh(&pmc->lock); |
| 533 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 534 | type = IGMPV3_MODE_IS_EXCLUDE; |
| 535 | else |
| 536 | type = IGMPV3_MODE_IS_INCLUDE; |
| 537 | skb = add_grec(skb, pmc, type, 0, 0); |
| 538 | spin_unlock_bh(&pmc->lock); |
| 539 | } |
| 540 | if (!skb) |
| 541 | return 0; |
| 542 | return igmpv3_sendpack(skb); |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * remove zero-count source records from a source filter list |
| 547 | */ |
| 548 | static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) |
| 549 | { |
| 550 | struct ip_sf_list *psf_prev, *psf_next, *psf; |
| 551 | |
| 552 | psf_prev = NULL; |
| 553 | for (psf=*ppsf; psf; psf = psf_next) { |
| 554 | psf_next = psf->sf_next; |
| 555 | if (psf->sf_crcount == 0) { |
| 556 | if (psf_prev) |
| 557 | psf_prev->sf_next = psf->sf_next; |
| 558 | else |
| 559 | *ppsf = psf->sf_next; |
| 560 | kfree(psf); |
| 561 | } else |
| 562 | psf_prev = psf; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | static void igmpv3_send_cr(struct in_device *in_dev) |
| 567 | { |
| 568 | struct ip_mc_list *pmc, *pmc_prev, *pmc_next; |
| 569 | struct sk_buff *skb = NULL; |
| 570 | int type, dtype; |
| 571 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 572 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 574 | |
| 575 | /* deleted MCA's */ |
| 576 | pmc_prev = NULL; |
| 577 | for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) { |
| 578 | pmc_next = pmc->next; |
| 579 | if (pmc->sfmode == MCAST_INCLUDE) { |
| 580 | type = IGMPV3_BLOCK_OLD_SOURCES; |
| 581 | dtype = IGMPV3_BLOCK_OLD_SOURCES; |
| 582 | skb = add_grec(skb, pmc, type, 1, 0); |
| 583 | skb = add_grec(skb, pmc, dtype, 1, 1); |
| 584 | } |
| 585 | if (pmc->crcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | if (pmc->sfmode == MCAST_EXCLUDE) { |
| 587 | type = IGMPV3_CHANGE_TO_INCLUDE; |
| 588 | skb = add_grec(skb, pmc, type, 1, 0); |
| 589 | } |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 590 | pmc->crcount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (pmc->crcount == 0) { |
| 592 | igmpv3_clear_zeros(&pmc->tomb); |
| 593 | igmpv3_clear_zeros(&pmc->sources); |
| 594 | } |
| 595 | } |
| 596 | if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { |
| 597 | if (pmc_prev) |
| 598 | pmc_prev->next = pmc_next; |
| 599 | else |
| 600 | in_dev->mc_tomb = pmc_next; |
| 601 | in_dev_put(pmc->interface); |
| 602 | kfree(pmc); |
| 603 | } else |
| 604 | pmc_prev = pmc; |
| 605 | } |
| 606 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 607 | |
| 608 | /* change recs */ |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 609 | for_each_pmc_rcu(in_dev, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | spin_lock_bh(&pmc->lock); |
| 611 | if (pmc->sfcount[MCAST_EXCLUDE]) { |
| 612 | type = IGMPV3_BLOCK_OLD_SOURCES; |
| 613 | dtype = IGMPV3_ALLOW_NEW_SOURCES; |
| 614 | } else { |
| 615 | type = IGMPV3_ALLOW_NEW_SOURCES; |
| 616 | dtype = IGMPV3_BLOCK_OLD_SOURCES; |
| 617 | } |
| 618 | skb = add_grec(skb, pmc, type, 0, 0); |
| 619 | skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ |
| 620 | |
| 621 | /* filter mode changes */ |
| 622 | if (pmc->crcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | if (pmc->sfmode == MCAST_EXCLUDE) |
| 624 | type = IGMPV3_CHANGE_TO_EXCLUDE; |
| 625 | else |
| 626 | type = IGMPV3_CHANGE_TO_INCLUDE; |
| 627 | skb = add_grec(skb, pmc, type, 0, 0); |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 628 | pmc->crcount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
| 630 | spin_unlock_bh(&pmc->lock); |
| 631 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 632 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | if (!skb) |
| 635 | return; |
| 636 | (void) igmpv3_sendpack(skb); |
| 637 | } |
| 638 | |
| 639 | static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, |
| 640 | int type) |
| 641 | { |
| 642 | struct sk_buff *skb; |
| 643 | struct iphdr *iph; |
| 644 | struct igmphdr *ih; |
| 645 | struct rtable *rt; |
| 646 | struct net_device *dev = in_dev->dev; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 647 | struct net *net = dev_net(dev); |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 648 | __be32 group = pmc ? pmc->multiaddr : 0; |
David S. Miller | 31e4543 | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 649 | struct flowi4 fl4; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 650 | __be32 dst; |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 651 | int hlen, tlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) |
| 654 | return igmpv3_send_report(in_dev, pmc); |
| 655 | else if (type == IGMP_HOST_LEAVE_MESSAGE) |
| 656 | dst = IGMP_ALL_ROUTER; |
| 657 | else |
| 658 | dst = group; |
| 659 | |
David S. Miller | 31e4543 | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 660 | rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 661 | 0, 0, |
| 662 | IPPROTO_IGMP, 0, dev->ifindex); |
| 663 | if (IS_ERR(rt)) |
| 664 | return -1; |
| 665 | |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 666 | hlen = LL_RESERVED_SPACE(dev); |
| 667 | tlen = dev->needed_tailroom; |
| 668 | skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | if (skb == NULL) { |
| 670 | ip_rt_put(rt); |
| 671 | return -1; |
| 672 | } |
| 673 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 674 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Herbert Xu | 6608824 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 676 | skb_reserve(skb, hlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 678 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 679 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 680 | skb_put(skb, sizeof(struct iphdr) + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
| 682 | iph->version = 4; |
| 683 | iph->ihl = (sizeof(struct iphdr)+4)>>2; |
| 684 | iph->tos = 0xc0; |
| 685 | iph->frag_off = htons(IP_DF); |
| 686 | iph->ttl = 1; |
| 687 | iph->daddr = dst; |
David S. Miller | 492f64c | 2011-05-03 20:53:12 -0700 | [diff] [blame] | 688 | iph->saddr = fl4.saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | iph->protocol = IPPROTO_IGMP; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 690 | ip_select_ident(iph, &rt->dst, NULL); |
Daniel Baluta | 5e73ea1 | 2012-04-15 01:34:41 +0000 | [diff] [blame] | 691 | ((u8 *)&iph[1])[0] = IPOPT_RA; |
| 692 | ((u8 *)&iph[1])[1] = 4; |
| 693 | ((u8 *)&iph[1])[2] = 0; |
| 694 | ((u8 *)&iph[1])[3] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
| 696 | ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 697 | ih->type = type; |
| 698 | ih->code = 0; |
| 699 | ih->csum = 0; |
| 700 | ih->group = group; |
| 701 | ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 703 | return ip_local_out(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static void igmp_gq_timer_expire(unsigned long data) |
| 707 | { |
| 708 | struct in_device *in_dev = (struct in_device *)data; |
| 709 | |
| 710 | in_dev->mr_gq_running = 0; |
| 711 | igmpv3_send_report(in_dev, NULL); |
| 712 | __in_dev_put(in_dev); |
| 713 | } |
| 714 | |
| 715 | static void igmp_ifc_timer_expire(unsigned long data) |
| 716 | { |
| 717 | struct in_device *in_dev = (struct in_device *)data; |
| 718 | |
| 719 | igmpv3_send_cr(in_dev); |
| 720 | if (in_dev->mr_ifc_count) { |
| 721 | in_dev->mr_ifc_count--; |
| 722 | igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval); |
| 723 | } |
| 724 | __in_dev_put(in_dev); |
| 725 | } |
| 726 | |
| 727 | static void igmp_ifc_event(struct in_device *in_dev) |
| 728 | { |
| 729 | if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) |
| 730 | return; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 731 | in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | IGMP_Unsolicited_Report_Count; |
| 733 | igmp_ifc_start_timer(in_dev, 1); |
| 734 | } |
| 735 | |
| 736 | |
| 737 | static void igmp_timer_expire(unsigned long data) |
| 738 | { |
| 739 | struct ip_mc_list *im=(struct ip_mc_list *)data; |
| 740 | struct in_device *in_dev = im->interface; |
| 741 | |
| 742 | spin_lock(&im->lock); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 743 | im->tm_running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
| 745 | if (im->unsolicit_count) { |
| 746 | im->unsolicit_count--; |
| 747 | igmp_start_timer(im, IGMP_Unsolicited_Report_Interval); |
| 748 | } |
| 749 | im->reporter = 1; |
| 750 | spin_unlock(&im->lock); |
| 751 | |
| 752 | if (IGMP_V1_SEEN(in_dev)) |
| 753 | igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); |
| 754 | else if (IGMP_V2_SEEN(in_dev)) |
| 755 | igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); |
| 756 | else |
| 757 | igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); |
| 758 | |
| 759 | ip_ma_put(im); |
| 760 | } |
| 761 | |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 762 | /* mark EXCLUDE-mode sources */ |
Al Viro | ea4d9e7 | 2006-09-27 18:30:52 -0700 | [diff] [blame] | 763 | static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
| 765 | struct ip_sf_list *psf; |
| 766 | int i, scount; |
| 767 | |
| 768 | scount = 0; |
| 769 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 770 | if (scount == nsrcs) |
| 771 | break; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 772 | for (i=0; i<nsrcs; i++) { |
| 773 | /* skip inactive filters */ |
Yan, Zheng | e05c4ad3 | 2011-08-23 22:54:37 +0000 | [diff] [blame] | 774 | if (psf->sf_count[MCAST_INCLUDE] || |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 775 | pmc->sfcount[MCAST_EXCLUDE] != |
| 776 | psf->sf_count[MCAST_EXCLUDE]) |
RongQing.Li | ce713ee | 2012-04-05 17:36:29 +0800 | [diff] [blame] | 777 | break; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 778 | if (srcs[i] == psf->sf_inaddr) { |
| 779 | scount++; |
| 780 | break; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | pmc->gsquery = 0; |
| 785 | if (scount == nsrcs) /* all sources excluded */ |
| 786 | return 0; |
| 787 | return 1; |
| 788 | } |
| 789 | |
Al Viro | ea4d9e7 | 2006-09-27 18:30:52 -0700 | [diff] [blame] | 790 | static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 791 | { |
| 792 | struct ip_sf_list *psf; |
| 793 | int i, scount; |
| 794 | |
| 795 | if (pmc->sfmode == MCAST_EXCLUDE) |
| 796 | return igmp_xmarksources(pmc, nsrcs, srcs); |
| 797 | |
| 798 | /* mark INCLUDE-mode sources */ |
| 799 | scount = 0; |
| 800 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 801 | if (scount == nsrcs) |
| 802 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | for (i=0; i<nsrcs; i++) |
| 804 | if (srcs[i] == psf->sf_inaddr) { |
| 805 | psf->sf_gsresp = 1; |
| 806 | scount++; |
| 807 | break; |
| 808 | } |
| 809 | } |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 810 | if (!scount) { |
| 811 | pmc->gsquery = 0; |
| 812 | return 0; |
| 813 | } |
| 814 | pmc->gsquery = 1; |
| 815 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 818 | /* return true if packet was dropped */ |
| 819 | static bool igmp_heard_report(struct in_device *in_dev, __be32 group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
| 821 | struct ip_mc_list *im; |
| 822 | |
| 823 | /* Timers are only set for non-local groups */ |
| 824 | |
| 825 | if (group == IGMP_ALL_HOSTS) |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 826 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 828 | rcu_read_lock(); |
| 829 | for_each_pmc_rcu(in_dev, im) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | if (im->multiaddr == group) { |
| 831 | igmp_stop_timer(im); |
| 832 | break; |
| 833 | } |
| 834 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 835 | rcu_read_unlock(); |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 836 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 839 | /* return true if packet was dropped */ |
| 840 | static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | int len) |
| 842 | { |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 843 | struct igmphdr *ih = igmp_hdr(skb); |
| 844 | struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | struct ip_mc_list *im; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 846 | __be32 group = ih->group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | int max_delay; |
| 848 | int mark = 0; |
| 849 | |
| 850 | |
David Stevens | 5b7c840 | 2010-09-30 14:29:40 +0000 | [diff] [blame] | 851 | if (len == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | if (ih->code == 0) { |
| 853 | /* Alas, old v1 router presents here. */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | max_delay = IGMP_Query_Response_Interval; |
| 856 | in_dev->mr_v1_seen = jiffies + |
| 857 | IGMP_V1_Router_Present_Timeout; |
| 858 | group = 0; |
| 859 | } else { |
| 860 | /* v2 router present */ |
| 861 | max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); |
| 862 | in_dev->mr_v2_seen = jiffies + |
| 863 | IGMP_V2_Router_Present_Timeout; |
| 864 | } |
| 865 | /* cancel the interface change timer */ |
| 866 | in_dev->mr_ifc_count = 0; |
| 867 | if (del_timer(&in_dev->mr_ifc_timer)) |
| 868 | __in_dev_put(in_dev); |
| 869 | /* clear deleted report items */ |
| 870 | igmpv3_clear_delrec(in_dev); |
| 871 | } else if (len < 12) { |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 872 | return true; /* ignore bogus packet; freed by caller */ |
David Stevens | 5b7c840 | 2010-09-30 14:29:40 +0000 | [diff] [blame] | 873 | } else if (IGMP_V1_SEEN(in_dev)) { |
| 874 | /* This is a v3 query with v1 queriers present */ |
| 875 | max_delay = IGMP_Query_Response_Interval; |
| 876 | group = 0; |
| 877 | } else if (IGMP_V2_SEEN(in_dev)) { |
| 878 | /* this is a v3 query with v2 queriers present; |
| 879 | * Interpretation of the max_delay code is problematic here. |
| 880 | * A real v2 host would use ih_code directly, while v3 has a |
| 881 | * different encoding. We use the v3 encoding as more likely |
| 882 | * to be intended in a v3 query. |
| 883 | */ |
| 884 | max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); |
Ben Hutchings | a8c1f65 | 2012-01-09 14:06:46 -0800 | [diff] [blame] | 885 | if (!max_delay) |
| 886 | max_delay = 1; /* can't mod w/ 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } else { /* v3 */ |
| 888 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 889 | return true; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 890 | |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 891 | ih3 = igmpv3_query_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | if (ih3->nsrcs) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 893 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 894 | + ntohs(ih3->nsrcs)*sizeof(__be32))) |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 895 | return true; |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 896 | ih3 = igmpv3_query_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); |
| 900 | if (!max_delay) |
| 901 | max_delay = 1; /* can't mod w/ 0 */ |
| 902 | in_dev->mr_maxdelay = max_delay; |
| 903 | if (ih3->qrv) |
| 904 | in_dev->mr_qrv = ih3->qrv; |
| 905 | if (!group) { /* general query */ |
| 906 | if (ih3->nsrcs) |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 907 | return false; /* no sources allowed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | igmp_gq_start_timer(in_dev); |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 909 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
| 911 | /* mark sources to include, if group & source-specific */ |
| 912 | mark = ih3->nsrcs != 0; |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * - Start the timers in all of our membership records |
| 917 | * that the query applies to for the interface on |
| 918 | * which the query arrived excl. those that belong |
| 919 | * to a "local" group (224.0.0.X) |
| 920 | * - For timers already running check if they need to |
| 921 | * be reset. |
| 922 | * - Use the igmp->igmp_code field as the maximum |
| 923 | * delay possible |
| 924 | */ |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 925 | rcu_read_lock(); |
| 926 | for_each_pmc_rcu(in_dev, im) { |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 927 | int changed; |
| 928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | if (group && group != im->multiaddr) |
| 930 | continue; |
| 931 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 932 | continue; |
| 933 | spin_lock_bh(&im->lock); |
| 934 | if (im->tm_running) |
| 935 | im->gsquery = im->gsquery && mark; |
| 936 | else |
| 937 | im->gsquery = mark; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 938 | changed = !im->gsquery || |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 939 | igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | spin_unlock_bh(&im->lock); |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 941 | if (changed) |
| 942 | igmp_mod_timer(im, max_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 944 | rcu_read_unlock(); |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 945 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Eric Dumazet | 9a57a9d | 2010-06-07 03:17:10 +0000 | [diff] [blame] | 948 | /* called in rcu_read_lock() section */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | int igmp_rcv(struct sk_buff *skb) |
| 950 | { |
| 951 | /* This basically follows the spec line by line -- see RFC1112 */ |
| 952 | struct igmphdr *ih; |
Eric Dumazet | 9a57a9d | 2010-06-07 03:17:10 +0000 | [diff] [blame] | 953 | struct in_device *in_dev = __in_dev_get_rcu(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | int len = skb->len; |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 955 | bool dropped = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
Denis V. Lunev | cd557bc | 2008-02-09 23:22:26 -0800 | [diff] [blame] | 957 | if (in_dev == NULL) |
| 958 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 960 | if (!pskb_may_pull(skb, sizeof(struct igmphdr))) |
Eric Dumazet | 9a57a9d | 2010-06-07 03:17:10 +0000 | [diff] [blame] | 961 | goto drop; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 962 | |
| 963 | switch (skb->ip_summed) { |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 964 | case CHECKSUM_COMPLETE: |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 965 | if (!csum_fold(skb->csum)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 966 | break; |
| 967 | /* fall through */ |
| 968 | case CHECKSUM_NONE: |
| 969 | skb->csum = 0; |
| 970 | if (__skb_checksum_complete(skb)) |
Eric Dumazet | 9a57a9d | 2010-06-07 03:17:10 +0000 | [diff] [blame] | 971 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 974 | ih = igmp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | switch (ih->type) { |
| 976 | case IGMP_HOST_MEMBERSHIP_QUERY: |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 977 | dropped = igmp_heard_query(in_dev, skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | break; |
| 979 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 980 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | /* Is it our report looped back? */ |
David S. Miller | c753796 | 2010-11-11 17:07:48 -0800 | [diff] [blame] | 982 | if (rt_is_output_route(skb_rtable(skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | break; |
David Stevens | 24c6927 | 2005-12-02 20:32:59 -0800 | [diff] [blame] | 984 | /* don't rely on MC router hearing unicast reports */ |
| 985 | if (skb->pkt_type == PACKET_MULTICAST || |
| 986 | skb->pkt_type == PACKET_BROADCAST) |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 987 | dropped = igmp_heard_report(in_dev, ih->group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | break; |
| 989 | case IGMP_PIM: |
| 990 | #ifdef CONFIG_IP_PIMSM_V1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | return pim_rcv_v1(skb); |
| 992 | #endif |
Herbert Xu | c6b471e | 2010-02-07 17:26:30 +0000 | [diff] [blame] | 993 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | case IGMP_DVMRP: |
| 995 | case IGMP_TRACE: |
| 996 | case IGMP_HOST_LEAVE_MESSAGE: |
| 997 | case IGMP_MTRACE: |
| 998 | case IGMP_MTRACE_RESP: |
| 999 | break; |
| 1000 | default: |
Linus Torvalds | dd1c185 | 2006-01-31 13:11:41 -0800 | [diff] [blame] | 1001 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 1003 | |
Denis V. Lunev | cd557bc | 2008-02-09 23:22:26 -0800 | [diff] [blame] | 1004 | drop: |
Eric Dumazet | d679c53 | 2012-09-06 20:37:06 +0000 | [diff] [blame] | 1005 | if (dropped) |
| 1006 | kfree_skb(skb); |
| 1007 | else |
| 1008 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | #endif |
| 1013 | |
| 1014 | |
| 1015 | /* |
| 1016 | * Add a filter to a device |
| 1017 | */ |
| 1018 | |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 1019 | static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | { |
| 1021 | char buf[MAX_ADDR_LEN]; |
| 1022 | struct net_device *dev = in_dev->dev; |
| 1023 | |
| 1024 | /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. |
| 1025 | We will get multicast token leakage, when IFF_MULTICAST |
Jiri Pirko | b81693d | 2011-08-16 06:29:02 +0000 | [diff] [blame] | 1026 | is changed. This check should be done in ndo_set_rx_mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | routine. Something sort of: |
| 1028 | if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } |
| 1029 | --ANK |
| 1030 | */ |
| 1031 | if (arp_mc_map(addr, buf, dev, 0) == 0) |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1032 | dev_mc_add(dev, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Remove a filter from a device |
| 1037 | */ |
| 1038 | |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 1039 | static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | { |
| 1041 | char buf[MAX_ADDR_LEN]; |
| 1042 | struct net_device *dev = in_dev->dev; |
| 1043 | |
| 1044 | if (arp_mc_map(addr, buf, dev, 0) == 0) |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1045 | dev_mc_del(dev, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | #ifdef CONFIG_IP_MULTICAST |
| 1049 | /* |
| 1050 | * deleted ip_mc_list manipulation |
| 1051 | */ |
| 1052 | static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im) |
| 1053 | { |
| 1054 | struct ip_mc_list *pmc; |
| 1055 | |
| 1056 | /* this is an "ip_mc_list" for convenience; only the fields below |
| 1057 | * are actually used. In particular, the refcnt and users are not |
| 1058 | * used for management of the delete list. Using the same structure |
| 1059 | * for deleted items allows change reports to use common code with |
| 1060 | * non-deleted or query-response MCA's. |
| 1061 | */ |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1062 | pmc = kzalloc(sizeof(*pmc), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | if (!pmc) |
| 1064 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | spin_lock_bh(&im->lock); |
| 1066 | pmc->interface = im->interface; |
| 1067 | in_dev_hold(in_dev); |
| 1068 | pmc->multiaddr = im->multiaddr; |
| 1069 | pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : |
| 1070 | IGMP_Unsolicited_Report_Count; |
| 1071 | pmc->sfmode = im->sfmode; |
| 1072 | if (pmc->sfmode == MCAST_INCLUDE) { |
| 1073 | struct ip_sf_list *psf; |
| 1074 | |
| 1075 | pmc->tomb = im->tomb; |
| 1076 | pmc->sources = im->sources; |
| 1077 | im->tomb = im->sources = NULL; |
| 1078 | for (psf=pmc->sources; psf; psf=psf->sf_next) |
| 1079 | psf->sf_crcount = pmc->crcount; |
| 1080 | } |
| 1081 | spin_unlock_bh(&im->lock); |
| 1082 | |
| 1083 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 1084 | pmc->next = in_dev->mc_tomb; |
| 1085 | in_dev->mc_tomb = pmc; |
| 1086 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 1087 | } |
| 1088 | |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 1089 | static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | { |
| 1091 | struct ip_mc_list *pmc, *pmc_prev; |
| 1092 | struct ip_sf_list *psf, *psf_next; |
| 1093 | |
| 1094 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 1095 | pmc_prev = NULL; |
| 1096 | for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) { |
| 1097 | if (pmc->multiaddr == multiaddr) |
| 1098 | break; |
| 1099 | pmc_prev = pmc; |
| 1100 | } |
| 1101 | if (pmc) { |
| 1102 | if (pmc_prev) |
| 1103 | pmc_prev->next = pmc->next; |
| 1104 | else |
| 1105 | in_dev->mc_tomb = pmc->next; |
| 1106 | } |
| 1107 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 1108 | if (pmc) { |
| 1109 | for (psf=pmc->tomb; psf; psf=psf_next) { |
| 1110 | psf_next = psf->sf_next; |
| 1111 | kfree(psf); |
| 1112 | } |
| 1113 | in_dev_put(pmc->interface); |
| 1114 | kfree(pmc); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | static void igmpv3_clear_delrec(struct in_device *in_dev) |
| 1119 | { |
| 1120 | struct ip_mc_list *pmc, *nextpmc; |
| 1121 | |
| 1122 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 1123 | pmc = in_dev->mc_tomb; |
| 1124 | in_dev->mc_tomb = NULL; |
| 1125 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 1126 | |
| 1127 | for (; pmc; pmc = nextpmc) { |
| 1128 | nextpmc = pmc->next; |
| 1129 | ip_mc_clear_src(pmc); |
| 1130 | in_dev_put(pmc->interface); |
| 1131 | kfree(pmc); |
| 1132 | } |
| 1133 | /* clear dead sources, too */ |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1134 | rcu_read_lock(); |
| 1135 | for_each_pmc_rcu(in_dev, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | struct ip_sf_list *psf, *psf_next; |
| 1137 | |
| 1138 | spin_lock_bh(&pmc->lock); |
| 1139 | psf = pmc->tomb; |
| 1140 | pmc->tomb = NULL; |
| 1141 | spin_unlock_bh(&pmc->lock); |
| 1142 | for (; psf; psf=psf_next) { |
| 1143 | psf_next = psf->sf_next; |
| 1144 | kfree(psf); |
| 1145 | } |
| 1146 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1147 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | } |
| 1149 | #endif |
| 1150 | |
| 1151 | static void igmp_group_dropped(struct ip_mc_list *im) |
| 1152 | { |
| 1153 | struct in_device *in_dev = im->interface; |
| 1154 | #ifdef CONFIG_IP_MULTICAST |
| 1155 | int reporter; |
| 1156 | #endif |
| 1157 | |
| 1158 | if (im->loaded) { |
| 1159 | im->loaded = 0; |
| 1160 | ip_mc_filter_del(in_dev, im->multiaddr); |
| 1161 | } |
| 1162 | |
| 1163 | #ifdef CONFIG_IP_MULTICAST |
| 1164 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 1165 | return; |
| 1166 | |
| 1167 | reporter = im->reporter; |
| 1168 | igmp_stop_timer(im); |
| 1169 | |
| 1170 | if (!in_dev->dead) { |
| 1171 | if (IGMP_V1_SEEN(in_dev)) |
Veaceslav Falico | 24cf3af | 2011-05-23 23:15:05 +0000 | [diff] [blame] | 1172 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | if (IGMP_V2_SEEN(in_dev)) { |
| 1174 | if (reporter) |
| 1175 | igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); |
Veaceslav Falico | 24cf3af | 2011-05-23 23:15:05 +0000 | [diff] [blame] | 1176 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
| 1178 | /* IGMPv3 */ |
| 1179 | igmpv3_add_delrec(in_dev, im); |
| 1180 | |
| 1181 | igmp_ifc_event(in_dev); |
| 1182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static void igmp_group_added(struct ip_mc_list *im) |
| 1187 | { |
| 1188 | struct in_device *in_dev = im->interface; |
| 1189 | |
| 1190 | if (im->loaded == 0) { |
| 1191 | im->loaded = 1; |
| 1192 | ip_mc_filter_add(in_dev, im->multiaddr); |
| 1193 | } |
| 1194 | |
| 1195 | #ifdef CONFIG_IP_MULTICAST |
| 1196 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 1197 | return; |
| 1198 | |
| 1199 | if (in_dev->dead) |
| 1200 | return; |
| 1201 | if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { |
| 1202 | spin_lock_bh(&im->lock); |
| 1203 | igmp_start_timer(im, IGMP_Initial_Report_Delay); |
| 1204 | spin_unlock_bh(&im->lock); |
| 1205 | return; |
| 1206 | } |
| 1207 | /* else, v3 */ |
| 1208 | |
| 1209 | im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : |
| 1210 | IGMP_Unsolicited_Report_Count; |
| 1211 | igmp_ifc_event(in_dev); |
| 1212 | #endif |
| 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | /* |
| 1217 | * Multicast list managers |
| 1218 | */ |
| 1219 | |
| 1220 | |
| 1221 | /* |
| 1222 | * A socket has joined a multicast group on device dev. |
| 1223 | */ |
| 1224 | |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1225 | void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | { |
| 1227 | struct ip_mc_list *im; |
| 1228 | |
| 1229 | ASSERT_RTNL(); |
| 1230 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1231 | for_each_pmc_rtnl(in_dev, im) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | if (im->multiaddr == addr) { |
| 1233 | im->users++; |
| 1234 | ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0); |
| 1235 | goto out; |
| 1236 | } |
| 1237 | } |
| 1238 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1239 | im = kzalloc(sizeof(*im), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | if (!im) |
| 1241 | goto out; |
| 1242 | |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1243 | im->users = 1; |
| 1244 | im->interface = in_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | in_dev_hold(in_dev); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1246 | im->multiaddr = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | /* initial mode is (EX, empty) */ |
| 1248 | im->sfmode = MCAST_EXCLUDE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | im->sfcount[MCAST_EXCLUDE] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | atomic_set(&im->refcnt, 1); |
| 1251 | spin_lock_init(&im->lock); |
| 1252 | #ifdef CONFIG_IP_MULTICAST |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1253 | setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | im->unsolicit_count = IGMP_Unsolicited_Report_Count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | #endif |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1256 | |
| 1257 | im->next_rcu = in_dev->mc_list; |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 1258 | in_dev->mc_count++; |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 1259 | rcu_assign_pointer(in_dev->mc_list, im); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | #ifdef CONFIG_IP_MULTICAST |
| 1262 | igmpv3_del_delrec(in_dev, im->multiaddr); |
| 1263 | #endif |
| 1264 | igmp_group_added(im); |
| 1265 | if (!in_dev->dead) |
| 1266 | ip_rt_multicast_event(in_dev); |
| 1267 | out: |
| 1268 | return; |
| 1269 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 1270 | EXPORT_SYMBOL(ip_mc_inc_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
| 1272 | /* |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1273 | * Resend IGMP JOIN report; used for bonding. |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1274 | * Called with rcu_read_lock() |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1275 | */ |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1276 | void ip_mc_rejoin_groups(struct in_device *in_dev) |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1277 | { |
Geert Uytterhoeven | 0888266 | 2007-03-12 17:02:37 -0700 | [diff] [blame] | 1278 | #ifdef CONFIG_IP_MULTICAST |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1279 | struct ip_mc_list *im; |
| 1280 | int type; |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1281 | |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1282 | for_each_pmc_rcu(in_dev, im) { |
| 1283 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 1284 | continue; |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1285 | |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1286 | /* a failover is happening and switches |
| 1287 | * must be notified immediately |
| 1288 | */ |
| 1289 | if (IGMP_V1_SEEN(in_dev)) |
| 1290 | type = IGMP_HOST_MEMBERSHIP_REPORT; |
| 1291 | else if (IGMP_V2_SEEN(in_dev)) |
| 1292 | type = IGMPV2_HOST_MEMBERSHIP_REPORT; |
| 1293 | else |
| 1294 | type = IGMPV3_HOST_MEMBERSHIP_REPORT; |
| 1295 | igmp_send_report(in_dev, im, type); |
| 1296 | } |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1297 | #endif |
| 1298 | } |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1299 | EXPORT_SYMBOL(ip_mc_rejoin_groups); |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1300 | |
| 1301 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | * A socket has left a multicast group on device dev |
| 1303 | */ |
| 1304 | |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1305 | void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1307 | struct ip_mc_list *i; |
| 1308 | struct ip_mc_list __rcu **ip; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | ASSERT_RTNL(); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1311 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1312 | for (ip = &in_dev->mc_list; |
| 1313 | (i = rtnl_dereference(*ip)) != NULL; |
| 1314 | ip = &i->next_rcu) { |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1315 | if (i->multiaddr == addr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | if (--i->users == 0) { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1317 | *ip = i->next_rcu; |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 1318 | in_dev->mc_count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | igmp_group_dropped(i); |
Veaceslav Falico | 24cf3af | 2011-05-23 23:15:05 +0000 | [diff] [blame] | 1320 | ip_mc_clear_src(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
| 1322 | if (!in_dev->dead) |
| 1323 | ip_rt_multicast_event(in_dev); |
| 1324 | |
| 1325 | ip_ma_put(i); |
| 1326 | return; |
| 1327 | } |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
| 1331 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 1332 | EXPORT_SYMBOL(ip_mc_dec_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1334 | /* Device changing type */ |
| 1335 | |
| 1336 | void ip_mc_unmap(struct in_device *in_dev) |
| 1337 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1338 | struct ip_mc_list *pmc; |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1339 | |
| 1340 | ASSERT_RTNL(); |
| 1341 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1342 | for_each_pmc_rtnl(in_dev, pmc) |
| 1343 | igmp_group_dropped(pmc); |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | void ip_mc_remap(struct in_device *in_dev) |
| 1347 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1348 | struct ip_mc_list *pmc; |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1349 | |
| 1350 | ASSERT_RTNL(); |
| 1351 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1352 | for_each_pmc_rtnl(in_dev, pmc) |
| 1353 | igmp_group_added(pmc); |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | /* Device going down */ |
| 1357 | |
| 1358 | void ip_mc_down(struct in_device *in_dev) |
| 1359 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1360 | struct ip_mc_list *pmc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
| 1362 | ASSERT_RTNL(); |
| 1363 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1364 | for_each_pmc_rtnl(in_dev, pmc) |
| 1365 | igmp_group_dropped(pmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
| 1367 | #ifdef CONFIG_IP_MULTICAST |
| 1368 | in_dev->mr_ifc_count = 0; |
| 1369 | if (del_timer(&in_dev->mr_ifc_timer)) |
| 1370 | __in_dev_put(in_dev); |
| 1371 | in_dev->mr_gq_running = 0; |
| 1372 | if (del_timer(&in_dev->mr_gq_timer)) |
| 1373 | __in_dev_put(in_dev); |
| 1374 | igmpv3_clear_delrec(in_dev); |
| 1375 | #endif |
| 1376 | |
| 1377 | ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); |
| 1378 | } |
| 1379 | |
| 1380 | void ip_mc_init_dev(struct in_device *in_dev) |
| 1381 | { |
| 1382 | ASSERT_RTNL(); |
| 1383 | |
| 1384 | in_dev->mc_tomb = NULL; |
| 1385 | #ifdef CONFIG_IP_MULTICAST |
| 1386 | in_dev->mr_gq_running = 0; |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1387 | setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire, |
| 1388 | (unsigned long)in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | in_dev->mr_ifc_count = 0; |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 1390 | in_dev->mc_count = 0; |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1391 | setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, |
| 1392 | (unsigned long)in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | in_dev->mr_qrv = IGMP_Unsolicited_Report_Count; |
| 1394 | #endif |
| 1395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | spin_lock_init(&in_dev->mc_tomb_lock); |
| 1397 | } |
| 1398 | |
| 1399 | /* Device going up */ |
| 1400 | |
| 1401 | void ip_mc_up(struct in_device *in_dev) |
| 1402 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1403 | struct ip_mc_list *pmc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
| 1405 | ASSERT_RTNL(); |
| 1406 | |
| 1407 | ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); |
| 1408 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1409 | for_each_pmc_rtnl(in_dev, pmc) |
| 1410 | igmp_group_added(pmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * Device is about to be destroyed: clean up. |
| 1415 | */ |
| 1416 | |
| 1417 | void ip_mc_destroy_dev(struct in_device *in_dev) |
| 1418 | { |
| 1419 | struct ip_mc_list *i; |
| 1420 | |
| 1421 | ASSERT_RTNL(); |
| 1422 | |
| 1423 | /* Deactivate timers */ |
| 1424 | ip_mc_down(in_dev); |
| 1425 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1426 | while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { |
| 1427 | in_dev->mc_list = i->next_rcu; |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 1428 | in_dev->mc_count--; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1429 | |
Veaceslav Falico | 24cf3af | 2011-05-23 23:15:05 +0000 | [diff] [blame] | 1430 | /* We've dropped the groups in ip_mc_down already */ |
| 1431 | ip_mc_clear_src(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | ip_ma_put(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Eric Dumazet | 9e917dc | 2010-10-19 00:39:18 +0000 | [diff] [blame] | 1436 | /* RTNL is locked */ |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1437 | static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | struct net_device *dev = NULL; |
| 1440 | struct in_device *idev = NULL; |
| 1441 | |
| 1442 | if (imr->imr_ifindex) { |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1443 | idev = inetdev_by_index(net, imr->imr_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | return idev; |
| 1445 | } |
| 1446 | if (imr->imr_address.s_addr) { |
Eric Dumazet | 9e917dc | 2010-10-19 00:39:18 +0000 | [diff] [blame] | 1447 | dev = __ip_dev_find(net, imr->imr_address.s_addr, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | if (!dev) |
| 1449 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 1452 | if (!dev) { |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 1453 | struct rtable *rt = ip_route_output(net, |
| 1454 | imr->imr_multiaddr.s_addr, |
| 1455 | 0, 0, 0); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 1456 | if (!IS_ERR(rt)) { |
| 1457 | dev = rt->dst.dev; |
| 1458 | ip_rt_put(rt); |
| 1459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | } |
| 1461 | if (dev) { |
| 1462 | imr->imr_ifindex = dev->ifindex; |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 1463 | idev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | } |
| 1465 | return idev; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Join a socket to a group |
| 1470 | */ |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 1471 | int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS; |
| 1472 | int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | |
| 1474 | |
| 1475 | static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1476 | __be32 *psfsrc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | { |
| 1478 | struct ip_sf_list *psf, *psf_prev; |
| 1479 | int rv = 0; |
| 1480 | |
| 1481 | psf_prev = NULL; |
| 1482 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 1483 | if (psf->sf_inaddr == *psfsrc) |
| 1484 | break; |
| 1485 | psf_prev = psf; |
| 1486 | } |
| 1487 | if (!psf || psf->sf_count[sfmode] == 0) { |
| 1488 | /* source filter not found, or count wrong => bug */ |
| 1489 | return -ESRCH; |
| 1490 | } |
| 1491 | psf->sf_count[sfmode]--; |
| 1492 | if (psf->sf_count[sfmode] == 0) { |
| 1493 | ip_rt_multicast_event(pmc->interface); |
| 1494 | } |
| 1495 | if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { |
| 1496 | #ifdef CONFIG_IP_MULTICAST |
| 1497 | struct in_device *in_dev = pmc->interface; |
| 1498 | #endif |
| 1499 | |
| 1500 | /* no more filters for this source */ |
| 1501 | if (psf_prev) |
| 1502 | psf_prev->sf_next = psf->sf_next; |
| 1503 | else |
| 1504 | pmc->sources = psf->sf_next; |
| 1505 | #ifdef CONFIG_IP_MULTICAST |
| 1506 | if (psf->sf_oldin && |
| 1507 | !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1508 | psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | IGMP_Unsolicited_Report_Count; |
| 1510 | psf->sf_next = pmc->tomb; |
| 1511 | pmc->tomb = psf; |
| 1512 | rv = 1; |
| 1513 | } else |
| 1514 | #endif |
| 1515 | kfree(psf); |
| 1516 | } |
| 1517 | return rv; |
| 1518 | } |
| 1519 | |
| 1520 | #ifndef CONFIG_IP_MULTICAST |
| 1521 | #define igmp_ifc_event(x) do { } while (0) |
| 1522 | #endif |
| 1523 | |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1524 | static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, |
| 1525 | int sfcount, __be32 *psfsrc, int delta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | { |
| 1527 | struct ip_mc_list *pmc; |
| 1528 | int changerec = 0; |
| 1529 | int i, err; |
| 1530 | |
| 1531 | if (!in_dev) |
| 1532 | return -ENODEV; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1533 | rcu_read_lock(); |
| 1534 | for_each_pmc_rcu(in_dev, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (*pmca == pmc->multiaddr) |
| 1536 | break; |
| 1537 | } |
| 1538 | if (!pmc) { |
| 1539 | /* MCA not found?? bug */ |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1540 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | return -ESRCH; |
| 1542 | } |
| 1543 | spin_lock_bh(&pmc->lock); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1544 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | #ifdef CONFIG_IP_MULTICAST |
| 1546 | sf_markstate(pmc); |
| 1547 | #endif |
| 1548 | if (!delta) { |
| 1549 | err = -EINVAL; |
| 1550 | if (!pmc->sfcount[sfmode]) |
| 1551 | goto out_unlock; |
| 1552 | pmc->sfcount[sfmode]--; |
| 1553 | } |
| 1554 | err = 0; |
| 1555 | for (i=0; i<sfcount; i++) { |
| 1556 | int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); |
| 1557 | |
| 1558 | changerec |= rv > 0; |
| 1559 | if (!err && rv < 0) |
| 1560 | err = rv; |
| 1561 | } |
| 1562 | if (pmc->sfmode == MCAST_EXCLUDE && |
| 1563 | pmc->sfcount[MCAST_EXCLUDE] == 0 && |
| 1564 | pmc->sfcount[MCAST_INCLUDE]) { |
| 1565 | #ifdef CONFIG_IP_MULTICAST |
| 1566 | struct ip_sf_list *psf; |
| 1567 | #endif |
| 1568 | |
| 1569 | /* filter mode change */ |
| 1570 | pmc->sfmode = MCAST_INCLUDE; |
| 1571 | #ifdef CONFIG_IP_MULTICAST |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1572 | pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | IGMP_Unsolicited_Report_Count; |
| 1574 | in_dev->mr_ifc_count = pmc->crcount; |
| 1575 | for (psf=pmc->sources; psf; psf = psf->sf_next) |
| 1576 | psf->sf_crcount = 0; |
| 1577 | igmp_ifc_event(pmc->interface); |
| 1578 | } else if (sf_setstate(pmc) || changerec) { |
| 1579 | igmp_ifc_event(pmc->interface); |
| 1580 | #endif |
| 1581 | } |
| 1582 | out_unlock: |
| 1583 | spin_unlock_bh(&pmc->lock); |
| 1584 | return err; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * Add multicast single-source filter to the interface list |
| 1589 | */ |
| 1590 | static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, |
Jun Zhao | 5eb81e89 | 2011-11-30 06:21:04 +0000 | [diff] [blame] | 1591 | __be32 *psfsrc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | { |
| 1593 | struct ip_sf_list *psf, *psf_prev; |
| 1594 | |
| 1595 | psf_prev = NULL; |
| 1596 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 1597 | if (psf->sf_inaddr == *psfsrc) |
| 1598 | break; |
| 1599 | psf_prev = psf; |
| 1600 | } |
| 1601 | if (!psf) { |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1602 | psf = kzalloc(sizeof(*psf), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | if (!psf) |
| 1604 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | psf->sf_inaddr = *psfsrc; |
| 1606 | if (psf_prev) { |
| 1607 | psf_prev->sf_next = psf; |
| 1608 | } else |
| 1609 | pmc->sources = psf; |
| 1610 | } |
| 1611 | psf->sf_count[sfmode]++; |
| 1612 | if (psf->sf_count[sfmode] == 1) { |
| 1613 | ip_rt_multicast_event(pmc->interface); |
| 1614 | } |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | #ifdef CONFIG_IP_MULTICAST |
| 1619 | static void sf_markstate(struct ip_mc_list *pmc) |
| 1620 | { |
| 1621 | struct ip_sf_list *psf; |
| 1622 | int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; |
| 1623 | |
| 1624 | for (psf=pmc->sources; psf; psf=psf->sf_next) |
| 1625 | if (pmc->sfcount[MCAST_EXCLUDE]) { |
| 1626 | psf->sf_oldin = mca_xcount == |
| 1627 | psf->sf_count[MCAST_EXCLUDE] && |
| 1628 | !psf->sf_count[MCAST_INCLUDE]; |
| 1629 | } else |
| 1630 | psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; |
| 1631 | } |
| 1632 | |
| 1633 | static int sf_setstate(struct ip_mc_list *pmc) |
| 1634 | { |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 1635 | struct ip_sf_list *psf, *dpsf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; |
| 1637 | int qrv = pmc->interface->mr_qrv; |
| 1638 | int new_in, rv; |
| 1639 | |
| 1640 | rv = 0; |
| 1641 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 1642 | if (pmc->sfcount[MCAST_EXCLUDE]) { |
| 1643 | new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && |
| 1644 | !psf->sf_count[MCAST_INCLUDE]; |
| 1645 | } else |
| 1646 | new_in = psf->sf_count[MCAST_INCLUDE] != 0; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 1647 | if (new_in) { |
| 1648 | if (!psf->sf_oldin) { |
Al Viro | 76edc60 | 2006-02-01 05:54:35 -0500 | [diff] [blame] | 1649 | struct ip_sf_list *prev = NULL; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 1650 | |
| 1651 | for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) { |
| 1652 | if (dpsf->sf_inaddr == psf->sf_inaddr) |
| 1653 | break; |
| 1654 | prev = dpsf; |
| 1655 | } |
| 1656 | if (dpsf) { |
| 1657 | if (prev) |
| 1658 | prev->sf_next = dpsf->sf_next; |
| 1659 | else |
| 1660 | pmc->tomb = dpsf->sf_next; |
| 1661 | kfree(dpsf); |
| 1662 | } |
| 1663 | psf->sf_crcount = qrv; |
| 1664 | rv++; |
| 1665 | } |
| 1666 | } else if (psf->sf_oldin) { |
| 1667 | |
| 1668 | psf->sf_crcount = 0; |
| 1669 | /* |
| 1670 | * add or update "delete" records if an active filter |
| 1671 | * is now inactive |
| 1672 | */ |
| 1673 | for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) |
| 1674 | if (dpsf->sf_inaddr == psf->sf_inaddr) |
| 1675 | break; |
| 1676 | if (!dpsf) { |
Joe Perches | 3ed37a6 | 2010-05-31 17:23:21 +0000 | [diff] [blame] | 1677 | dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 1678 | if (!dpsf) |
| 1679 | continue; |
| 1680 | *dpsf = *psf; |
| 1681 | /* pmc->lock held by callers */ |
| 1682 | dpsf->sf_next = pmc->tomb; |
| 1683 | pmc->tomb = dpsf; |
| 1684 | } |
| 1685 | dpsf->sf_crcount = qrv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | rv++; |
| 1687 | } |
| 1688 | } |
| 1689 | return rv; |
| 1690 | } |
| 1691 | #endif |
| 1692 | |
| 1693 | /* |
| 1694 | * Add multicast source filter list to the interface list |
| 1695 | */ |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1696 | static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, |
| 1697 | int sfcount, __be32 *psfsrc, int delta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | { |
| 1699 | struct ip_mc_list *pmc; |
| 1700 | int isexclude; |
| 1701 | int i, err; |
| 1702 | |
| 1703 | if (!in_dev) |
| 1704 | return -ENODEV; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1705 | rcu_read_lock(); |
| 1706 | for_each_pmc_rcu(in_dev, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | if (*pmca == pmc->multiaddr) |
| 1708 | break; |
| 1709 | } |
| 1710 | if (!pmc) { |
| 1711 | /* MCA not found?? bug */ |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1712 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | return -ESRCH; |
| 1714 | } |
| 1715 | spin_lock_bh(&pmc->lock); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1716 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | |
| 1718 | #ifdef CONFIG_IP_MULTICAST |
| 1719 | sf_markstate(pmc); |
| 1720 | #endif |
| 1721 | isexclude = pmc->sfmode == MCAST_EXCLUDE; |
| 1722 | if (!delta) |
| 1723 | pmc->sfcount[sfmode]++; |
| 1724 | err = 0; |
| 1725 | for (i=0; i<sfcount; i++) { |
Jun Zhao | 5eb81e89 | 2011-11-30 06:21:04 +0000 | [diff] [blame] | 1726 | err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | if (err) |
| 1728 | break; |
| 1729 | } |
| 1730 | if (err) { |
| 1731 | int j; |
| 1732 | |
Jun Zhao | 685f94e6 | 2011-11-22 17:19:03 +0000 | [diff] [blame] | 1733 | if (!delta) |
| 1734 | pmc->sfcount[sfmode]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | for (j=0; j<i; j++) |
Julia Lawall | a1889c0 | 2011-07-28 02:46:01 +0000 | [diff] [blame] | 1736 | (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { |
| 1738 | #ifdef CONFIG_IP_MULTICAST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | struct ip_sf_list *psf; |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 1740 | in_dev = pmc->interface; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | #endif |
| 1742 | |
| 1743 | /* filter mode change */ |
| 1744 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 1745 | pmc->sfmode = MCAST_EXCLUDE; |
| 1746 | else if (pmc->sfcount[MCAST_INCLUDE]) |
| 1747 | pmc->sfmode = MCAST_INCLUDE; |
| 1748 | #ifdef CONFIG_IP_MULTICAST |
| 1749 | /* else no filters; keep old mode for reports */ |
| 1750 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1751 | pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | IGMP_Unsolicited_Report_Count; |
| 1753 | in_dev->mr_ifc_count = pmc->crcount; |
| 1754 | for (psf=pmc->sources; psf; psf = psf->sf_next) |
| 1755 | psf->sf_crcount = 0; |
| 1756 | igmp_ifc_event(in_dev); |
| 1757 | } else if (sf_setstate(pmc)) { |
| 1758 | igmp_ifc_event(in_dev); |
| 1759 | #endif |
| 1760 | } |
| 1761 | spin_unlock_bh(&pmc->lock); |
| 1762 | return err; |
| 1763 | } |
| 1764 | |
| 1765 | static void ip_mc_clear_src(struct ip_mc_list *pmc) |
| 1766 | { |
| 1767 | struct ip_sf_list *psf, *nextpsf; |
| 1768 | |
| 1769 | for (psf=pmc->tomb; psf; psf=nextpsf) { |
| 1770 | nextpsf = psf->sf_next; |
| 1771 | kfree(psf); |
| 1772 | } |
| 1773 | pmc->tomb = NULL; |
| 1774 | for (psf=pmc->sources; psf; psf=nextpsf) { |
| 1775 | nextpsf = psf->sf_next; |
| 1776 | kfree(psf); |
| 1777 | } |
| 1778 | pmc->sources = NULL; |
| 1779 | pmc->sfmode = MCAST_EXCLUDE; |
Denis Lukianov | de9daad | 2005-09-14 20:53:42 -0700 | [diff] [blame] | 1780 | pmc->sfcount[MCAST_INCLUDE] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | pmc->sfcount[MCAST_EXCLUDE] = 1; |
| 1782 | } |
| 1783 | |
| 1784 | |
| 1785 | /* |
| 1786 | * Join a multicast group |
| 1787 | */ |
| 1788 | int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr) |
| 1789 | { |
| 1790 | int err; |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1791 | __be32 addr = imr->imr_multiaddr.s_addr; |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1792 | struct ip_mc_socklist *iml = NULL, *i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | struct in_device *in_dev; |
| 1794 | struct inet_sock *inet = inet_sk(sk); |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1795 | struct net *net = sock_net(sk); |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1796 | int ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | int count = 0; |
| 1798 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 1799 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | return -EINVAL; |
| 1801 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 1802 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1804 | in_dev = ip_mc_find_dev(net, imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
| 1806 | if (!in_dev) { |
| 1807 | iml = NULL; |
| 1808 | err = -ENODEV; |
| 1809 | goto done; |
| 1810 | } |
| 1811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | err = -EADDRINUSE; |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1813 | ifindex = imr->imr_ifindex; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1814 | for_each_pmc_rtnl(inet, i) { |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1815 | if (i->multi.imr_multiaddr.s_addr == addr && |
| 1816 | i->multi.imr_ifindex == ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | count++; |
| 1819 | } |
| 1820 | err = -ENOBUFS; |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1821 | if (count >= sysctl_igmp_max_memberships) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | goto done; |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1823 | iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1824 | if (iml == NULL) |
| 1825 | goto done; |
| 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | memcpy(&iml->multi, imr, sizeof(*imr)); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1828 | iml->next_rcu = inet->mc_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | iml->sflist = NULL; |
| 1830 | iml->sfmode = MCAST_EXCLUDE; |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 1831 | rcu_assign_pointer(inet->mc_list, iml); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | ip_mc_inc_group(in_dev, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 1835 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | return err; |
| 1837 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 1838 | EXPORT_SYMBOL(ip_mc_join_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | |
| 1840 | static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, |
| 1841 | struct in_device *in_dev) |
| 1842 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1843 | struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | int err; |
| 1845 | |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1846 | if (psf == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | /* any-source empty exclude case */ |
| 1848 | return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, |
| 1849 | iml->sfmode, 0, NULL, 0); |
| 1850 | } |
| 1851 | err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1852 | iml->sfmode, psf->sl_count, psf->sl_addr, 0); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 1853 | RCU_INIT_POINTER(iml->sflist, NULL); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1854 | /* decrease mem now to avoid the memleak warning */ |
| 1855 | atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); |
Lai Jiangshan | 7519cce | 2011-03-18 11:44:46 +0800 | [diff] [blame] | 1856 | kfree_rcu(psf, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | return err; |
| 1858 | } |
| 1859 | |
| 1860 | /* |
| 1861 | * Ask a socket to leave a group. |
| 1862 | */ |
| 1863 | |
| 1864 | int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) |
| 1865 | { |
| 1866 | struct inet_sock *inet = inet_sk(sk); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1867 | struct ip_mc_socklist *iml; |
| 1868 | struct ip_mc_socklist __rcu **imlp; |
David L Stevens | 84b42ba | 2005-07-08 17:48:38 -0700 | [diff] [blame] | 1869 | struct in_device *in_dev; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1870 | struct net *net = sock_net(sk); |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1871 | __be32 group = imr->imr_multiaddr.s_addr; |
David L Stevens | 84b42ba | 2005-07-08 17:48:38 -0700 | [diff] [blame] | 1872 | u32 ifindex; |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1873 | int ret = -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | |
| 1875 | rtnl_lock(); |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1876 | in_dev = ip_mc_find_dev(net, imr); |
David L Stevens | 84b42ba | 2005-07-08 17:48:38 -0700 | [diff] [blame] | 1877 | ifindex = imr->imr_ifindex; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1878 | for (imlp = &inet->mc_list; |
| 1879 | (iml = rtnl_dereference(*imlp)) != NULL; |
| 1880 | imlp = &iml->next_rcu) { |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1881 | if (iml->multi.imr_multiaddr.s_addr != group) |
| 1882 | continue; |
| 1883 | if (ifindex) { |
| 1884 | if (iml->multi.imr_ifindex != ifindex) |
| 1885 | continue; |
| 1886 | } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != |
| 1887 | iml->multi.imr_address.s_addr) |
| 1888 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1890 | (void) ip_mc_leave_src(sk, iml, in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1892 | *imlp = iml->next_rcu; |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1893 | |
| 1894 | if (in_dev) |
David L Stevens | 84b42ba | 2005-07-08 17:48:38 -0700 | [diff] [blame] | 1895 | ip_mc_dec_group(in_dev, group); |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1896 | rtnl_unlock(); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1897 | /* decrease mem now to avoid the memleak warning */ |
| 1898 | atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); |
Lai Jiangshan | 10d50e7 | 2011-03-18 11:45:08 +0800 | [diff] [blame] | 1899 | kfree_rcu(iml, rcu); |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1900 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | } |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1902 | if (!in_dev) |
| 1903 | ret = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | rtnl_unlock(); |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1905 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | } |
stephen hemminger | 193ba92 | 2012-10-01 12:32:34 +0000 | [diff] [blame] | 1907 | EXPORT_SYMBOL(ip_mc_leave_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | |
| 1909 | int ip_mc_source(int add, int omode, struct sock *sk, struct |
| 1910 | ip_mreq_source *mreqs, int ifindex) |
| 1911 | { |
| 1912 | int err; |
| 1913 | struct ip_mreqn imr; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 1914 | __be32 addr = mreqs->imr_multiaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | struct ip_mc_socklist *pmc; |
| 1916 | struct in_device *in_dev = NULL; |
| 1917 | struct inet_sock *inet = inet_sk(sk); |
| 1918 | struct ip_sf_socklist *psl; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1919 | struct net *net = sock_net(sk); |
David L Stevens | 8cdaaa1 | 2005-07-08 17:39:23 -0700 | [diff] [blame] | 1920 | int leavegroup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | int i, j, rv; |
| 1922 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 1923 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | return -EINVAL; |
| 1925 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 1926 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | |
| 1928 | imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; |
| 1929 | imr.imr_address.s_addr = mreqs->imr_interface; |
| 1930 | imr.imr_ifindex = ifindex; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1931 | in_dev = ip_mc_find_dev(net, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | |
| 1933 | if (!in_dev) { |
| 1934 | err = -ENODEV; |
| 1935 | goto done; |
| 1936 | } |
| 1937 | err = -EADDRNOTAVAIL; |
| 1938 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1939 | for_each_pmc_rtnl(inet, pmc) { |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1940 | if ((pmc->multi.imr_multiaddr.s_addr == |
| 1941 | imr.imr_multiaddr.s_addr) && |
| 1942 | (pmc->multi.imr_ifindex == imr.imr_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | break; |
| 1944 | } |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1945 | if (!pmc) { /* must have a prior join */ |
| 1946 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | /* if a source filter was set, must be the same mode as before */ |
| 1950 | if (pmc->sflist) { |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1951 | if (pmc->sfmode != omode) { |
| 1952 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | } else if (pmc->sfmode != omode) { |
| 1956 | /* allow mode switches for empty-set filters */ |
| 1957 | ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1958 | ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | NULL, 0); |
| 1960 | pmc->sfmode = omode; |
| 1961 | } |
| 1962 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1963 | psl = rtnl_dereference(pmc->sflist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | if (!add) { |
| 1965 | if (!psl) |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1966 | goto done; /* err = -EADDRNOTAVAIL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | rv = !0; |
| 1968 | for (i=0; i<psl->sl_count; i++) { |
| 1969 | rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 1970 | sizeof(__be32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | if (rv == 0) |
| 1972 | break; |
| 1973 | } |
| 1974 | if (rv) /* source not found */ |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1975 | goto done; /* err = -EADDRNOTAVAIL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | |
David L Stevens | 8cdaaa1 | 2005-07-08 17:39:23 -0700 | [diff] [blame] | 1977 | /* special case - (INCLUDE, empty) == LEAVE_GROUP */ |
| 1978 | if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { |
| 1979 | leavegroup = 1; |
| 1980 | goto done; |
| 1981 | } |
| 1982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | /* update the interface filter */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1984 | ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | &mreqs->imr_sourceaddr, 1); |
| 1986 | |
| 1987 | for (j=i+1; j<psl->sl_count; j++) |
| 1988 | psl->sl_addr[j-1] = psl->sl_addr[j]; |
| 1989 | psl->sl_count--; |
| 1990 | err = 0; |
| 1991 | goto done; |
| 1992 | } |
| 1993 | /* else, add a new source to the filter */ |
| 1994 | |
| 1995 | if (psl && psl->sl_count >= sysctl_igmp_max_msf) { |
| 1996 | err = -ENOBUFS; |
| 1997 | goto done; |
| 1998 | } |
| 1999 | if (!psl || psl->sl_count == psl->sl_max) { |
| 2000 | struct ip_sf_socklist *newpsl; |
| 2001 | int count = IP_SFBLOCK; |
| 2002 | |
| 2003 | if (psl) |
| 2004 | count += psl->sl_max; |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 2005 | newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | if (!newpsl) { |
| 2007 | err = -ENOBUFS; |
| 2008 | goto done; |
| 2009 | } |
| 2010 | newpsl->sl_max = count; |
| 2011 | newpsl->sl_count = count - IP_SFBLOCK; |
| 2012 | if (psl) { |
| 2013 | for (i=0; i<psl->sl_count; i++) |
| 2014 | newpsl->sl_addr[i] = psl->sl_addr[i]; |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2015 | /* decrease mem now to avoid the memleak warning */ |
| 2016 | atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); |
Lai Jiangshan | 7519cce | 2011-03-18 11:44:46 +0800 | [diff] [blame] | 2017 | kfree_rcu(psl, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | } |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 2019 | rcu_assign_pointer(pmc->sflist, newpsl); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2020 | psl = newpsl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | } |
| 2022 | rv = 1; /* > 0 for insert logic below if sl_count is 0 */ |
| 2023 | for (i=0; i<psl->sl_count; i++) { |
| 2024 | rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 2025 | sizeof(__be32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | if (rv == 0) |
| 2027 | break; |
| 2028 | } |
| 2029 | if (rv == 0) /* address already there is an error */ |
| 2030 | goto done; |
| 2031 | for (j=psl->sl_count-1; j>=i; j--) |
| 2032 | psl->sl_addr[j+1] = psl->sl_addr[j]; |
| 2033 | psl->sl_addr[i] = mreqs->imr_sourceaddr; |
| 2034 | psl->sl_count++; |
| 2035 | err = 0; |
| 2036 | /* update the interface list */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2037 | ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | &mreqs->imr_sourceaddr, 1); |
| 2039 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2040 | rtnl_unlock(); |
David L Stevens | 8cdaaa1 | 2005-07-08 17:39:23 -0700 | [diff] [blame] | 2041 | if (leavegroup) |
| 2042 | return ip_mc_leave_group(sk, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | return err; |
| 2044 | } |
| 2045 | |
| 2046 | int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) |
| 2047 | { |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 2048 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | struct ip_mreqn imr; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 2050 | __be32 addr = msf->imsf_multiaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | struct ip_mc_socklist *pmc; |
| 2052 | struct in_device *in_dev; |
| 2053 | struct inet_sock *inet = inet_sk(sk); |
| 2054 | struct ip_sf_socklist *newpsl, *psl; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2055 | struct net *net = sock_net(sk); |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 2056 | int leavegroup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 2058 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | return -EINVAL; |
| 2060 | if (msf->imsf_fmode != MCAST_INCLUDE && |
| 2061 | msf->imsf_fmode != MCAST_EXCLUDE) |
| 2062 | return -EINVAL; |
| 2063 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2064 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
| 2066 | imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; |
| 2067 | imr.imr_address.s_addr = msf->imsf_interface; |
| 2068 | imr.imr_ifindex = ifindex; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2069 | in_dev = ip_mc_find_dev(net, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
| 2071 | if (!in_dev) { |
| 2072 | err = -ENODEV; |
| 2073 | goto done; |
| 2074 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 2076 | /* special case - (INCLUDE, empty) == LEAVE_GROUP */ |
| 2077 | if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { |
| 2078 | leavegroup = 1; |
| 2079 | goto done; |
| 2080 | } |
| 2081 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2082 | for_each_pmc_rtnl(inet, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && |
| 2084 | pmc->multi.imr_ifindex == imr.imr_ifindex) |
| 2085 | break; |
| 2086 | } |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 2087 | if (!pmc) { /* must have a prior join */ |
| 2088 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 2090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | if (msf->imsf_numsrc) { |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 2092 | newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), |
| 2093 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | if (!newpsl) { |
| 2095 | err = -ENOBUFS; |
| 2096 | goto done; |
| 2097 | } |
| 2098 | newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; |
| 2099 | memcpy(newpsl->sl_addr, msf->imsf_slist, |
| 2100 | msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); |
| 2101 | err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, |
| 2102 | msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); |
| 2103 | if (err) { |
| 2104 | sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); |
| 2105 | goto done; |
| 2106 | } |
Yan Zheng | 8713dbf | 2005-10-28 08:02:08 +0800 | [diff] [blame] | 2107 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | newpsl = NULL; |
Yan Zheng | 8713dbf | 2005-10-28 08:02:08 +0800 | [diff] [blame] | 2109 | (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, |
| 2110 | msf->imsf_fmode, 0, NULL, 0); |
| 2111 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2112 | psl = rtnl_dereference(pmc->sflist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | if (psl) { |
| 2114 | (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, |
| 2115 | psl->sl_count, psl->sl_addr, 0); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2116 | /* decrease mem now to avoid the memleak warning */ |
| 2117 | atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); |
Lai Jiangshan | 7519cce | 2011-03-18 11:44:46 +0800 | [diff] [blame] | 2118 | kfree_rcu(psl, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | } else |
| 2120 | (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, |
| 2121 | 0, NULL, 0); |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 2122 | rcu_assign_pointer(pmc->sflist, newpsl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | pmc->sfmode = msf->imsf_fmode; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 2124 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2126 | rtnl_unlock(); |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 2127 | if (leavegroup) |
| 2128 | err = ip_mc_leave_group(sk, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | return err; |
| 2130 | } |
| 2131 | |
| 2132 | int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, |
| 2133 | struct ip_msfilter __user *optval, int __user *optlen) |
| 2134 | { |
| 2135 | int err, len, count, copycount; |
| 2136 | struct ip_mreqn imr; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 2137 | __be32 addr = msf->imsf_multiaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | struct ip_mc_socklist *pmc; |
| 2139 | struct in_device *in_dev; |
| 2140 | struct inet_sock *inet = inet_sk(sk); |
| 2141 | struct ip_sf_socklist *psl; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2142 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 2144 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | return -EINVAL; |
| 2146 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2147 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | |
| 2149 | imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; |
| 2150 | imr.imr_address.s_addr = msf->imsf_interface; |
| 2151 | imr.imr_ifindex = 0; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2152 | in_dev = ip_mc_find_dev(net, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
| 2154 | if (!in_dev) { |
| 2155 | err = -ENODEV; |
| 2156 | goto done; |
| 2157 | } |
| 2158 | err = -EADDRNOTAVAIL; |
| 2159 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2160 | for_each_pmc_rtnl(inet, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && |
| 2162 | pmc->multi.imr_ifindex == imr.imr_ifindex) |
| 2163 | break; |
| 2164 | } |
| 2165 | if (!pmc) /* must have a prior join */ |
| 2166 | goto done; |
| 2167 | msf->imsf_fmode = pmc->sfmode; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2168 | psl = rtnl_dereference(pmc->sflist); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2169 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | if (!psl) { |
| 2171 | len = 0; |
| 2172 | count = 0; |
| 2173 | } else { |
| 2174 | count = psl->sl_count; |
| 2175 | } |
| 2176 | copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; |
| 2177 | len = copycount * sizeof(psl->sl_addr[0]); |
| 2178 | msf->imsf_numsrc = count; |
| 2179 | if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || |
| 2180 | copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { |
| 2181 | return -EFAULT; |
| 2182 | } |
| 2183 | if (len && |
| 2184 | copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) |
| 2185 | return -EFAULT; |
| 2186 | return 0; |
| 2187 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2188 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | return err; |
| 2190 | } |
| 2191 | |
| 2192 | int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, |
| 2193 | struct group_filter __user *optval, int __user *optlen) |
| 2194 | { |
| 2195 | int err, i, count, copycount; |
| 2196 | struct sockaddr_in *psin; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 2197 | __be32 addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | struct ip_mc_socklist *pmc; |
| 2199 | struct inet_sock *inet = inet_sk(sk); |
| 2200 | struct ip_sf_socklist *psl; |
| 2201 | |
| 2202 | psin = (struct sockaddr_in *)&gsf->gf_group; |
| 2203 | if (psin->sin_family != AF_INET) |
| 2204 | return -EINVAL; |
| 2205 | addr = psin->sin_addr.s_addr; |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 2206 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | return -EINVAL; |
| 2208 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2209 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | |
| 2211 | err = -EADDRNOTAVAIL; |
| 2212 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2213 | for_each_pmc_rtnl(inet, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | if (pmc->multi.imr_multiaddr.s_addr == addr && |
| 2215 | pmc->multi.imr_ifindex == gsf->gf_interface) |
| 2216 | break; |
| 2217 | } |
| 2218 | if (!pmc) /* must have a prior join */ |
| 2219 | goto done; |
| 2220 | gsf->gf_fmode = pmc->sfmode; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2221 | psl = rtnl_dereference(pmc->sflist); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2222 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | count = psl ? psl->sl_count : 0; |
| 2224 | copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; |
| 2225 | gsf->gf_numsrc = count; |
| 2226 | if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || |
| 2227 | copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { |
| 2228 | return -EFAULT; |
| 2229 | } |
| 2230 | for (i=0; i<copycount; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | struct sockaddr_storage ss; |
| 2232 | |
| 2233 | psin = (struct sockaddr_in *)&ss; |
| 2234 | memset(&ss, 0, sizeof(ss)); |
| 2235 | psin->sin_family = AF_INET; |
| 2236 | psin->sin_addr.s_addr = psl->sl_addr[i]; |
| 2237 | if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) |
| 2238 | return -EFAULT; |
| 2239 | } |
| 2240 | return 0; |
| 2241 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2242 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | return err; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * check if a multicast source filter allows delivery for a given <src,dst,intf> |
| 2248 | */ |
Al Viro | c0cda06 | 2006-09-27 18:31:10 -0700 | [diff] [blame] | 2249 | int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | { |
| 2251 | struct inet_sock *inet = inet_sk(sk); |
| 2252 | struct ip_mc_socklist *pmc; |
| 2253 | struct ip_sf_socklist *psl; |
| 2254 | int i; |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2255 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2257 | ret = 1; |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 2258 | if (!ipv4_is_multicast(loc_addr)) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2259 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2261 | rcu_read_lock(); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2262 | for_each_pmc_rcu(inet, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | if (pmc->multi.imr_multiaddr.s_addr == loc_addr && |
| 2264 | pmc->multi.imr_ifindex == dif) |
| 2265 | break; |
| 2266 | } |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2267 | ret = inet->mc_all; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | if (!pmc) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2269 | goto unlock; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2270 | psl = rcu_dereference(pmc->sflist); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2271 | ret = (pmc->sfmode == MCAST_EXCLUDE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | if (!psl) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2273 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | |
| 2275 | for (i=0; i<psl->sl_count; i++) { |
| 2276 | if (psl->sl_addr[i] == rmt_addr) |
| 2277 | break; |
| 2278 | } |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2279 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2281 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2283 | goto unlock; |
| 2284 | ret = 1; |
| 2285 | unlock: |
| 2286 | rcu_read_unlock(); |
| 2287 | out: |
| 2288 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * A socket is closing. |
| 2293 | */ |
| 2294 | |
| 2295 | void ip_mc_drop_socket(struct sock *sk) |
| 2296 | { |
| 2297 | struct inet_sock *inet = inet_sk(sk); |
| 2298 | struct ip_mc_socklist *iml; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2299 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | |
| 2301 | if (inet->mc_list == NULL) |
| 2302 | return; |
| 2303 | |
| 2304 | rtnl_lock(); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2305 | while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | struct in_device *in_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2308 | inet->mc_list = iml->next_rcu; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2309 | in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); |
Michal Ruzicka | bb699cbc | 2006-08-15 00:20:17 -0700 | [diff] [blame] | 2310 | (void) ip_mc_leave_src(sk, iml, in_dev); |
Eric Dumazet | 18943d2 | 2010-11-08 11:15:54 +0000 | [diff] [blame] | 2311 | if (in_dev != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2313 | /* decrease mem now to avoid the memleak warning */ |
| 2314 | atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); |
Lai Jiangshan | 10d50e7 | 2011-03-18 11:45:08 +0800 | [diff] [blame] | 2315 | kfree_rcu(iml, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | } |
| 2317 | rtnl_unlock(); |
| 2318 | } |
| 2319 | |
David S. Miller | dbdd9a5 | 2011-03-10 16:34:38 -0800 | [diff] [blame] | 2320 | /* called with rcu_read_lock() */ |
| 2321 | int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | { |
| 2323 | struct ip_mc_list *im; |
| 2324 | struct ip_sf_list *psf; |
| 2325 | int rv = 0; |
| 2326 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2327 | for_each_pmc_rcu(in_dev, im) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | if (im->multiaddr == mc_addr) |
| 2329 | break; |
| 2330 | } |
| 2331 | if (im && proto == IPPROTO_IGMP) { |
| 2332 | rv = 1; |
| 2333 | } else if (im) { |
| 2334 | if (src_addr) { |
| 2335 | for (psf=im->sources; psf; psf=psf->sf_next) { |
| 2336 | if (psf->sf_inaddr == src_addr) |
| 2337 | break; |
| 2338 | } |
| 2339 | if (psf) |
| 2340 | rv = psf->sf_count[MCAST_INCLUDE] || |
| 2341 | psf->sf_count[MCAST_EXCLUDE] != |
| 2342 | im->sfcount[MCAST_EXCLUDE]; |
| 2343 | else |
| 2344 | rv = im->sfcount[MCAST_EXCLUDE] != 0; |
| 2345 | } else |
| 2346 | rv = 1; /* unspecified source; tentatively allow */ |
| 2347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | return rv; |
| 2349 | } |
| 2350 | |
| 2351 | #if defined(CONFIG_PROC_FS) |
| 2352 | struct igmp_mc_iter_state { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2353 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | struct net_device *dev; |
| 2355 | struct in_device *in_dev; |
| 2356 | }; |
| 2357 | |
| 2358 | #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) |
| 2359 | |
| 2360 | static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) |
| 2361 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2362 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | struct ip_mc_list *im = NULL; |
| 2364 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
| 2365 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 2366 | state->in_dev = NULL; |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2367 | for_each_netdev_rcu(net, state->dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | struct in_device *in_dev; |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2369 | |
| 2370 | in_dev = __in_dev_get_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | if (!in_dev) |
| 2372 | continue; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2373 | im = rcu_dereference(in_dev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | if (im) { |
| 2375 | state->in_dev = in_dev; |
| 2376 | break; |
| 2377 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | } |
| 2379 | return im; |
| 2380 | } |
| 2381 | |
| 2382 | static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) |
| 2383 | { |
| 2384 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2385 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2386 | im = rcu_dereference(im->next_rcu); |
| 2387 | while (!im) { |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2388 | state->dev = next_net_device_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | if (!state->dev) { |
| 2390 | state->in_dev = NULL; |
| 2391 | break; |
| 2392 | } |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2393 | state->in_dev = __in_dev_get_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | if (!state->in_dev) |
| 2395 | continue; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2396 | im = rcu_dereference(state->in_dev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | } |
| 2398 | return im; |
| 2399 | } |
| 2400 | |
| 2401 | static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) |
| 2402 | { |
| 2403 | struct ip_mc_list *im = igmp_mc_get_first(seq); |
| 2404 | if (im) |
| 2405 | while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) |
| 2406 | --pos; |
| 2407 | return pos ? NULL : im; |
| 2408 | } |
| 2409 | |
| 2410 | static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2411 | __acquires(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | { |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2413 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2415 | } |
| 2416 | |
| 2417 | static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2418 | { |
| 2419 | struct ip_mc_list *im; |
| 2420 | if (v == SEQ_START_TOKEN) |
| 2421 | im = igmp_mc_get_first(seq); |
| 2422 | else |
| 2423 | im = igmp_mc_get_next(seq, v); |
| 2424 | ++*pos; |
| 2425 | return im; |
| 2426 | } |
| 2427 | |
| 2428 | static void igmp_mc_seq_stop(struct seq_file *seq, void *v) |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2429 | __releases(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | { |
| 2431 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2432 | |
| 2433 | state->in_dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | state->dev = NULL; |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2435 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | static int igmp_mc_seq_show(struct seq_file *seq, void *v) |
| 2439 | { |
| 2440 | if (v == SEQ_START_TOKEN) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2441 | seq_puts(seq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); |
| 2443 | else { |
| 2444 | struct ip_mc_list *im = (struct ip_mc_list *)v; |
| 2445 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
| 2446 | char *querier; |
Eric Dumazet | a399a80 | 2012-08-08 21:13:53 +0000 | [diff] [blame] | 2447 | long delta; |
| 2448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | #ifdef CONFIG_IP_MULTICAST |
| 2450 | querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : |
| 2451 | IGMP_V2_SEEN(state->in_dev) ? "V2" : |
| 2452 | "V3"; |
| 2453 | #else |
| 2454 | querier = "NONE"; |
| 2455 | #endif |
| 2456 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2457 | if (rcu_dereference(state->in_dev->mc_list) == im) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | seq_printf(seq, "%d\t%-10s: %5d %7s\n", |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 2459 | state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | } |
| 2461 | |
Eric Dumazet | a399a80 | 2012-08-08 21:13:53 +0000 | [diff] [blame] | 2462 | delta = im->timer.expires - jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | seq_printf(seq, |
Alexey Dobriyan | 338fcf9 | 2006-06-05 21:04:39 -0700 | [diff] [blame] | 2464 | "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | im->multiaddr, im->users, |
Eric Dumazet | a399a80 | 2012-08-08 21:13:53 +0000 | [diff] [blame] | 2466 | im->tm_running, |
| 2467 | im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | im->reporter); |
| 2469 | } |
| 2470 | return 0; |
| 2471 | } |
| 2472 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 2473 | static const struct seq_operations igmp_mc_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | .start = igmp_mc_seq_start, |
| 2475 | .next = igmp_mc_seq_next, |
| 2476 | .stop = igmp_mc_seq_stop, |
| 2477 | .show = igmp_mc_seq_show, |
| 2478 | }; |
| 2479 | |
| 2480 | static int igmp_mc_seq_open(struct inode *inode, struct file *file) |
| 2481 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2482 | return seq_open_net(inode, file, &igmp_mc_seq_ops, |
Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 2483 | sizeof(struct igmp_mc_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2486 | static const struct file_operations igmp_mc_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | .owner = THIS_MODULE, |
| 2488 | .open = igmp_mc_seq_open, |
| 2489 | .read = seq_read, |
| 2490 | .llseek = seq_lseek, |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2491 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | }; |
| 2493 | |
| 2494 | struct igmp_mcf_iter_state { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2495 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | struct net_device *dev; |
| 2497 | struct in_device *idev; |
| 2498 | struct ip_mc_list *im; |
| 2499 | }; |
| 2500 | |
| 2501 | #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) |
| 2502 | |
| 2503 | static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) |
| 2504 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2505 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | struct ip_sf_list *psf = NULL; |
| 2507 | struct ip_mc_list *im = NULL; |
| 2508 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2509 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 2510 | state->idev = NULL; |
| 2511 | state->im = NULL; |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2512 | for_each_netdev_rcu(net, state->dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | struct in_device *idev; |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2514 | idev = __in_dev_get_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | if (unlikely(idev == NULL)) |
| 2516 | continue; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2517 | im = rcu_dereference(idev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | if (likely(im != NULL)) { |
| 2519 | spin_lock_bh(&im->lock); |
| 2520 | psf = im->sources; |
| 2521 | if (likely(psf != NULL)) { |
| 2522 | state->im = im; |
| 2523 | state->idev = idev; |
| 2524 | break; |
| 2525 | } |
| 2526 | spin_unlock_bh(&im->lock); |
| 2527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | } |
| 2529 | return psf; |
| 2530 | } |
| 2531 | |
| 2532 | static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) |
| 2533 | { |
| 2534 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2535 | |
| 2536 | psf = psf->sf_next; |
| 2537 | while (!psf) { |
| 2538 | spin_unlock_bh(&state->im->lock); |
| 2539 | state->im = state->im->next; |
| 2540 | while (!state->im) { |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2541 | state->dev = next_net_device_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | if (!state->dev) { |
| 2543 | state->idev = NULL; |
| 2544 | goto out; |
| 2545 | } |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2546 | state->idev = __in_dev_get_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | if (!state->idev) |
| 2548 | continue; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2549 | state->im = rcu_dereference(state->idev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | } |
| 2551 | if (!state->im) |
| 2552 | break; |
| 2553 | spin_lock_bh(&state->im->lock); |
| 2554 | psf = state->im->sources; |
| 2555 | } |
| 2556 | out: |
| 2557 | return psf; |
| 2558 | } |
| 2559 | |
| 2560 | static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) |
| 2561 | { |
| 2562 | struct ip_sf_list *psf = igmp_mcf_get_first(seq); |
| 2563 | if (psf) |
| 2564 | while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) |
| 2565 | --pos; |
| 2566 | return pos ? NULL : psf; |
| 2567 | } |
| 2568 | |
| 2569 | static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2570 | __acquires(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | { |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2572 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2574 | } |
| 2575 | |
| 2576 | static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2577 | { |
| 2578 | struct ip_sf_list *psf; |
| 2579 | if (v == SEQ_START_TOKEN) |
| 2580 | psf = igmp_mcf_get_first(seq); |
| 2581 | else |
| 2582 | psf = igmp_mcf_get_next(seq, v); |
| 2583 | ++*pos; |
| 2584 | return psf; |
| 2585 | } |
| 2586 | |
| 2587 | static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2588 | __releases(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | { |
| 2590 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2591 | if (likely(state->im != NULL)) { |
| 2592 | spin_unlock_bh(&state->im->lock); |
| 2593 | state->im = NULL; |
| 2594 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2595 | state->idev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | state->dev = NULL; |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2597 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | static int igmp_mcf_seq_show(struct seq_file *seq, void *v) |
| 2601 | { |
| 2602 | struct ip_sf_list *psf = (struct ip_sf_list *)v; |
| 2603 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2604 | |
| 2605 | if (v == SEQ_START_TOKEN) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2606 | seq_printf(seq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | "%3s %6s " |
| 2608 | "%10s %10s %6s %6s\n", "Idx", |
| 2609 | "Device", "MCA", |
| 2610 | "SRC", "INC", "EXC"); |
| 2611 | } else { |
| 2612 | seq_printf(seq, |
| 2613 | "%3d %6.6s 0x%08x " |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2614 | "0x%08x %6lu %6lu\n", |
| 2615 | state->dev->ifindex, state->dev->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | ntohl(state->im->multiaddr), |
| 2617 | ntohl(psf->sf_inaddr), |
| 2618 | psf->sf_count[MCAST_INCLUDE], |
| 2619 | psf->sf_count[MCAST_EXCLUDE]); |
| 2620 | } |
| 2621 | return 0; |
| 2622 | } |
| 2623 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 2624 | static const struct seq_operations igmp_mcf_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | .start = igmp_mcf_seq_start, |
| 2626 | .next = igmp_mcf_seq_next, |
| 2627 | .stop = igmp_mcf_seq_stop, |
| 2628 | .show = igmp_mcf_seq_show, |
| 2629 | }; |
| 2630 | |
| 2631 | static int igmp_mcf_seq_open(struct inode *inode, struct file *file) |
| 2632 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2633 | return seq_open_net(inode, file, &igmp_mcf_seq_ops, |
Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 2634 | sizeof(struct igmp_mcf_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2637 | static const struct file_operations igmp_mcf_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | .owner = THIS_MODULE, |
| 2639 | .open = igmp_mcf_seq_open, |
| 2640 | .read = seq_read, |
| 2641 | .llseek = seq_lseek, |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2642 | .release = seq_release_net, |
| 2643 | }; |
| 2644 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2645 | static int __net_init igmp_net_init(struct net *net) |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2646 | { |
| 2647 | struct proc_dir_entry *pde; |
| 2648 | |
| 2649 | pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops); |
| 2650 | if (!pde) |
| 2651 | goto out_igmp; |
| 2652 | pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops); |
| 2653 | if (!pde) |
| 2654 | goto out_mcfilter; |
| 2655 | return 0; |
| 2656 | |
| 2657 | out_mcfilter: |
| 2658 | proc_net_remove(net, "igmp"); |
| 2659 | out_igmp: |
| 2660 | return -ENOMEM; |
| 2661 | } |
| 2662 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2663 | static void __net_exit igmp_net_exit(struct net *net) |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2664 | { |
| 2665 | proc_net_remove(net, "mcfilter"); |
| 2666 | proc_net_remove(net, "igmp"); |
| 2667 | } |
| 2668 | |
| 2669 | static struct pernet_operations igmp_net_ops = { |
| 2670 | .init = igmp_net_init, |
| 2671 | .exit = igmp_net_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | }; |
| 2673 | |
| 2674 | int __init igmp_mc_proc_init(void) |
| 2675 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2676 | return register_pernet_subsys(&igmp_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | } |
| 2678 | #endif |