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> |
| 76 | #include <asm/system.h> |
| 77 | #include <linux/types.h> |
| 78 | #include <linux/kernel.h> |
| 79 | #include <linux/jiffies.h> |
| 80 | #include <linux/string.h> |
| 81 | #include <linux/socket.h> |
| 82 | #include <linux/sockios.h> |
| 83 | #include <linux/in.h> |
| 84 | #include <linux/inet.h> |
| 85 | #include <linux/netdevice.h> |
| 86 | #include <linux/skbuff.h> |
| 87 | #include <linux/inetdevice.h> |
| 88 | #include <linux/igmp.h> |
| 89 | #include <linux/if_arp.h> |
| 90 | #include <linux/rtnetlink.h> |
| 91 | #include <linux/times.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 92 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 93 | #include <net/net_namespace.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 94 | #include <net/arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | #include <net/ip.h> |
| 96 | #include <net/protocol.h> |
| 97 | #include <net/route.h> |
| 98 | #include <net/sock.h> |
| 99 | #include <net/checksum.h> |
| 100 | #include <linux/netfilter_ipv4.h> |
| 101 | #ifdef CONFIG_IP_MROUTE |
| 102 | #include <linux/mroute.h> |
| 103 | #endif |
| 104 | #ifdef CONFIG_PROC_FS |
| 105 | #include <linux/proc_fs.h> |
| 106 | #include <linux/seq_file.h> |
| 107 | #endif |
| 108 | |
| 109 | #define IP_MAX_MEMBERSHIPS 20 |
| 110 | #define IP_MAX_MSF 10 |
| 111 | |
| 112 | #ifdef CONFIG_IP_MULTICAST |
| 113 | /* Parameter names and values are taken from igmp-v2-06 draft */ |
| 114 | |
| 115 | #define IGMP_V1_Router_Present_Timeout (400*HZ) |
| 116 | #define IGMP_V2_Router_Present_Timeout (400*HZ) |
| 117 | #define IGMP_Unsolicited_Report_Interval (10*HZ) |
| 118 | #define IGMP_Query_Response_Interval (10*HZ) |
| 119 | #define IGMP_Unsolicited_Report_Count 2 |
| 120 | |
| 121 | |
| 122 | #define IGMP_Initial_Report_Delay (1) |
| 123 | |
| 124 | /* IGMP_Initial_Report_Delay is not from IGMP specs! |
| 125 | * IGMP specs require to report membership immediately after |
| 126 | * joining a group, but we delay the first report by a |
| 127 | * small interval. It seems more natural and still does not |
| 128 | * contradict to specs provided this delay is small enough. |
| 129 | */ |
| 130 | |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 131 | #define IGMP_V1_SEEN(in_dev) \ |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 132 | (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 133 | IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ |
| 134 | ((in_dev)->mr_v1_seen && \ |
| 135 | time_before(jiffies, (in_dev)->mr_v1_seen))) |
| 136 | #define IGMP_V2_SEEN(in_dev) \ |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 137 | (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 138 | IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ |
| 139 | ((in_dev)->mr_v2_seen && \ |
| 140 | time_before(jiffies, (in_dev)->mr_v2_seen))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | 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] | 143 | static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | static void igmpv3_clear_delrec(struct in_device *in_dev); |
| 145 | static int sf_setstate(struct ip_mc_list *pmc); |
| 146 | static void sf_markstate(struct ip_mc_list *pmc); |
| 147 | #endif |
| 148 | static void ip_mc_clear_src(struct ip_mc_list *pmc); |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 149 | static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, |
| 150 | int sfcount, __be32 *psfsrc, int delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 152 | |
| 153 | static void ip_mc_list_reclaim(struct rcu_head *head) |
| 154 | { |
| 155 | kfree(container_of(head, struct ip_mc_list, rcu)); |
| 156 | } |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | static void ip_ma_put(struct ip_mc_list *im) |
| 159 | { |
| 160 | if (atomic_dec_and_test(&im->refcnt)) { |
| 161 | in_dev_put(im->interface); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 162 | call_rcu(&im->rcu, ip_mc_list_reclaim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
David S. Miller | d9aa938 | 2010-11-15 08:52:02 -0800 | [diff] [blame] | 166 | #define for_each_pmc_rcu(in_dev, pmc) \ |
| 167 | for (pmc = rcu_dereference(in_dev->mc_list); \ |
| 168 | pmc != NULL; \ |
| 169 | pmc = rcu_dereference(pmc->next_rcu)) |
| 170 | |
| 171 | #define for_each_pmc_rtnl(in_dev, pmc) \ |
| 172 | for (pmc = rtnl_dereference(in_dev->mc_list); \ |
| 173 | pmc != NULL; \ |
| 174 | pmc = rtnl_dereference(pmc->next_rcu)) |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | #ifdef CONFIG_IP_MULTICAST |
| 177 | |
| 178 | /* |
| 179 | * Timer management |
| 180 | */ |
| 181 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 182 | static void igmp_stop_timer(struct ip_mc_list *im) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | spin_lock_bh(&im->lock); |
| 185 | if (del_timer(&im->timer)) |
| 186 | atomic_dec(&im->refcnt); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 187 | im->tm_running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | im->reporter = 0; |
| 189 | im->unsolicit_count = 0; |
| 190 | spin_unlock_bh(&im->lock); |
| 191 | } |
| 192 | |
| 193 | /* It must be called with locked im->lock */ |
| 194 | static void igmp_start_timer(struct ip_mc_list *im, int max_delay) |
| 195 | { |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 196 | int tv = net_random() % max_delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 198 | im->tm_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (!mod_timer(&im->timer, jiffies+tv+2)) |
| 200 | atomic_inc(&im->refcnt); |
| 201 | } |
| 202 | |
| 203 | static void igmp_gq_start_timer(struct in_device *in_dev) |
| 204 | { |
| 205 | int tv = net_random() % in_dev->mr_maxdelay; |
| 206 | |
| 207 | in_dev->mr_gq_running = 1; |
| 208 | if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2)) |
| 209 | in_dev_hold(in_dev); |
| 210 | } |
| 211 | |
| 212 | static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) |
| 213 | { |
| 214 | int tv = net_random() % delay; |
| 215 | |
| 216 | if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) |
| 217 | in_dev_hold(in_dev); |
| 218 | } |
| 219 | |
| 220 | static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) |
| 221 | { |
| 222 | spin_lock_bh(&im->lock); |
| 223 | im->unsolicit_count = 0; |
| 224 | if (del_timer(&im->timer)) { |
| 225 | if ((long)(im->timer.expires-jiffies) < max_delay) { |
| 226 | add_timer(&im->timer); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 227 | im->tm_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | spin_unlock_bh(&im->lock); |
| 229 | return; |
| 230 | } |
| 231 | atomic_dec(&im->refcnt); |
| 232 | } |
| 233 | igmp_start_timer(im, max_delay); |
| 234 | spin_unlock_bh(&im->lock); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /* |
| 239 | * Send an IGMP report. |
| 240 | */ |
| 241 | |
| 242 | #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) |
| 243 | |
| 244 | |
| 245 | static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, |
| 246 | int gdeleted, int sdeleted) |
| 247 | { |
| 248 | switch (type) { |
| 249 | case IGMPV3_MODE_IS_INCLUDE: |
| 250 | case IGMPV3_MODE_IS_EXCLUDE: |
| 251 | if (gdeleted || sdeleted) |
| 252 | return 0; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 253 | if (!(pmc->gsquery && !psf->sf_gsresp)) { |
| 254 | if (pmc->sfmode == MCAST_INCLUDE) |
| 255 | return 1; |
| 256 | /* don't include if this source is excluded |
| 257 | * in all filters |
| 258 | */ |
| 259 | if (psf->sf_count[MCAST_INCLUDE]) |
| 260 | return type == IGMPV3_MODE_IS_INCLUDE; |
| 261 | return pmc->sfcount[MCAST_EXCLUDE] == |
| 262 | psf->sf_count[MCAST_EXCLUDE]; |
| 263 | } |
| 264 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | case IGMPV3_CHANGE_TO_INCLUDE: |
| 266 | if (gdeleted || sdeleted) |
| 267 | return 0; |
| 268 | return psf->sf_count[MCAST_INCLUDE] != 0; |
| 269 | case IGMPV3_CHANGE_TO_EXCLUDE: |
| 270 | if (gdeleted || sdeleted) |
| 271 | return 0; |
| 272 | if (pmc->sfcount[MCAST_EXCLUDE] == 0 || |
| 273 | psf->sf_count[MCAST_INCLUDE]) |
| 274 | return 0; |
| 275 | return pmc->sfcount[MCAST_EXCLUDE] == |
| 276 | psf->sf_count[MCAST_EXCLUDE]; |
| 277 | case IGMPV3_ALLOW_NEW_SOURCES: |
| 278 | if (gdeleted || !psf->sf_crcount) |
| 279 | return 0; |
| 280 | return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; |
| 281 | case IGMPV3_BLOCK_OLD_SOURCES: |
| 282 | if (pmc->sfmode == MCAST_INCLUDE) |
| 283 | return gdeleted || (psf->sf_crcount && sdeleted); |
| 284 | return psf->sf_crcount && !gdeleted && !sdeleted; |
| 285 | } |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int |
| 290 | igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) |
| 291 | { |
| 292 | struct ip_sf_list *psf; |
| 293 | int scount = 0; |
| 294 | |
| 295 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 296 | if (!is_in(pmc, psf, type, gdeleted, sdeleted)) |
| 297 | continue; |
| 298 | scount++; |
| 299 | } |
| 300 | return scount; |
| 301 | } |
| 302 | |
Eric Dumazet | 57e1ab6 | 2010-11-16 20:36:42 +0000 | [diff] [blame] | 303 | #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb)) |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size) |
| 306 | { |
| 307 | struct sk_buff *skb; |
| 308 | struct rtable *rt; |
| 309 | struct iphdr *pip; |
| 310 | struct igmpv3_report *pig; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 311 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Eric Dumazet | 57e1ab6 | 2010-11-16 20:36:42 +0000 | [diff] [blame] | 313 | while (1) { |
| 314 | skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), |
| 315 | GFP_ATOMIC | __GFP_NOWARN); |
| 316 | if (skb) |
| 317 | break; |
| 318 | size >>= 1; |
| 319 | if (size < 256) |
| 320 | return NULL; |
| 321 | } |
| 322 | igmp_skb_size(skb) = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame^] | 324 | rt = ip_route_output_ports(net, NULL, IGMPV3_ALL_MCR, 0, |
| 325 | 0, 0, |
| 326 | IPPROTO_IGMP, 0, dev->ifindex); |
| 327 | if (IS_ERR(rt)) { |
| 328 | kfree_skb(skb); |
| 329 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | if (rt->rt_src == 0) { |
| 332 | kfree_skb(skb); |
| 333 | ip_rt_put(rt); |
| 334 | return NULL; |
| 335 | } |
| 336 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 337 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | skb->dev = dev; |
| 339 | |
| 340 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); |
| 341 | |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 342 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 343 | pip = ip_hdr(skb); |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 344 | skb_put(skb, sizeof(struct iphdr) + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | pip->version = 4; |
| 347 | pip->ihl = (sizeof(struct iphdr)+4)>>2; |
| 348 | pip->tos = 0xc0; |
| 349 | pip->frag_off = htons(IP_DF); |
| 350 | pip->ttl = 1; |
| 351 | pip->daddr = rt->rt_dst; |
| 352 | pip->saddr = rt->rt_src; |
| 353 | pip->protocol = IPPROTO_IGMP; |
| 354 | pip->tot_len = 0; /* filled in later */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 355 | ip_select_ident(pip, &rt->dst, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | ((u8*)&pip[1])[0] = IPOPT_RA; |
| 357 | ((u8*)&pip[1])[1] = 4; |
| 358 | ((u8*)&pip[1])[2] = 0; |
| 359 | ((u8*)&pip[1])[3] = 0; |
| 360 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 361 | skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; |
Arnaldo Carvalho de Melo | d10ba34 | 2007-03-14 21:05:37 -0300 | [diff] [blame] | 362 | skb_put(skb, sizeof(*pig)); |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 363 | pig = igmpv3_report_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; |
| 365 | pig->resv1 = 0; |
| 366 | pig->csum = 0; |
| 367 | pig->resv2 = 0; |
| 368 | pig->ngrec = 0; |
| 369 | return skb; |
| 370 | } |
| 371 | |
| 372 | static int igmpv3_sendpack(struct sk_buff *skb) |
| 373 | { |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 374 | struct igmphdr *pig = igmp_hdr(skb); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 375 | const int igmplen = skb->tail - skb->transport_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 377 | pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 379 | return ip_local_out(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) |
| 383 | { |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 384 | return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, |
| 388 | int type, struct igmpv3_grec **ppgr) |
| 389 | { |
| 390 | struct net_device *dev = pmc->interface->dev; |
| 391 | struct igmpv3_report *pih; |
| 392 | struct igmpv3_grec *pgr; |
| 393 | |
| 394 | if (!skb) |
| 395 | skb = igmpv3_newpack(dev, dev->mtu); |
| 396 | if (!skb) |
| 397 | return NULL; |
| 398 | pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec)); |
| 399 | pgr->grec_type = type; |
| 400 | pgr->grec_auxwords = 0; |
| 401 | pgr->grec_nsrcs = 0; |
| 402 | pgr->grec_mca = pmc->multiaddr; |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 403 | pih = igmpv3_report_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | pih->ngrec = htons(ntohs(pih->ngrec)+1); |
| 405 | *ppgr = pgr; |
| 406 | return skb; |
| 407 | } |
| 408 | |
Eric Dumazet | 57e1ab6 | 2010-11-16 20:36:42 +0000 | [diff] [blame] | 409 | #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | skb_tailroom(skb)) : 0) |
| 411 | |
| 412 | static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, |
| 413 | int type, int gdeleted, int sdeleted) |
| 414 | { |
| 415 | struct net_device *dev = pmc->interface->dev; |
| 416 | struct igmpv3_report *pih; |
| 417 | struct igmpv3_grec *pgr = NULL; |
| 418 | struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 419 | int scount, stotal, first, isquery, truncate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | if (pmc->multiaddr == IGMP_ALL_HOSTS) |
| 422 | return skb; |
| 423 | |
| 424 | isquery = type == IGMPV3_MODE_IS_INCLUDE || |
| 425 | type == IGMPV3_MODE_IS_EXCLUDE; |
| 426 | truncate = type == IGMPV3_MODE_IS_EXCLUDE || |
| 427 | type == IGMPV3_CHANGE_TO_EXCLUDE; |
| 428 | |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 429 | stotal = scount = 0; |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | psf_list = sdeleted ? &pmc->tomb : &pmc->sources; |
| 432 | |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 433 | if (!*psf_list) |
| 434 | goto empty_source; |
| 435 | |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 436 | pih = skb ? igmpv3_report_hdr(skb) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | /* EX and TO_EX get a fresh packet, if needed */ |
| 439 | if (truncate) { |
| 440 | if (pih && pih->ngrec && |
| 441 | AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { |
| 442 | if (skb) |
| 443 | igmpv3_sendpack(skb); |
| 444 | skb = igmpv3_newpack(dev, dev->mtu); |
| 445 | } |
| 446 | } |
| 447 | first = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | psf_prev = NULL; |
| 449 | for (psf=*psf_list; psf; psf=psf_next) { |
Al Viro | ea4d9e7 | 2006-09-27 18:30:52 -0700 | [diff] [blame] | 450 | __be32 *psrc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | psf_next = psf->sf_next; |
| 453 | |
| 454 | if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { |
| 455 | psf_prev = psf; |
| 456 | continue; |
| 457 | } |
| 458 | |
| 459 | /* clear marks on query responses */ |
| 460 | if (isquery) |
| 461 | psf->sf_gsresp = 0; |
| 462 | |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 463 | if (AVAILABLE(skb) < sizeof(__be32) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | first*sizeof(struct igmpv3_grec)) { |
| 465 | if (truncate && !first) |
| 466 | break; /* truncate these */ |
| 467 | if (pgr) |
| 468 | pgr->grec_nsrcs = htons(scount); |
| 469 | if (skb) |
| 470 | igmpv3_sendpack(skb); |
| 471 | skb = igmpv3_newpack(dev, dev->mtu); |
| 472 | first = 1; |
| 473 | scount = 0; |
| 474 | } |
| 475 | if (first) { |
| 476 | skb = add_grhead(skb, pmc, type, &pgr); |
| 477 | first = 0; |
| 478 | } |
Alexey Dobriyan | cc63f70 | 2007-02-06 14:35:25 -0800 | [diff] [blame] | 479 | if (!skb) |
| 480 | return NULL; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 481 | psrc = (__be32 *)skb_put(skb, sizeof(__be32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | *psrc = psf->sf_inaddr; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 483 | scount++; stotal++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | if ((type == IGMPV3_ALLOW_NEW_SOURCES || |
| 485 | type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { |
| 486 | psf->sf_crcount--; |
| 487 | if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { |
| 488 | if (psf_prev) |
| 489 | psf_prev->sf_next = psf->sf_next; |
| 490 | else |
| 491 | *psf_list = psf->sf_next; |
| 492 | kfree(psf); |
| 493 | continue; |
| 494 | } |
| 495 | } |
| 496 | psf_prev = psf; |
| 497 | } |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 498 | |
| 499 | empty_source: |
| 500 | if (!stotal) { |
| 501 | if (type == IGMPV3_ALLOW_NEW_SOURCES || |
| 502 | type == IGMPV3_BLOCK_OLD_SOURCES) |
| 503 | return skb; |
| 504 | if (pmc->crcount || isquery) { |
| 505 | /* make sure we have room for group header */ |
| 506 | if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) { |
| 507 | igmpv3_sendpack(skb); |
| 508 | skb = NULL; /* add_grhead will get a new one */ |
| 509 | } |
| 510 | skb = add_grhead(skb, pmc, type, &pgr); |
| 511 | } |
| 512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (pgr) |
| 514 | pgr->grec_nsrcs = htons(scount); |
| 515 | |
| 516 | if (isquery) |
| 517 | pmc->gsquery = 0; /* clear query state on report */ |
| 518 | return skb; |
| 519 | } |
| 520 | |
| 521 | static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) |
| 522 | { |
| 523 | struct sk_buff *skb = NULL; |
| 524 | int type; |
| 525 | |
| 526 | if (!pmc) { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 527 | rcu_read_lock(); |
| 528 | for_each_pmc_rcu(in_dev, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (pmc->multiaddr == IGMP_ALL_HOSTS) |
| 530 | continue; |
| 531 | spin_lock_bh(&pmc->lock); |
| 532 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 533 | type = IGMPV3_MODE_IS_EXCLUDE; |
| 534 | else |
| 535 | type = IGMPV3_MODE_IS_INCLUDE; |
| 536 | skb = add_grec(skb, pmc, type, 0, 0); |
| 537 | spin_unlock_bh(&pmc->lock); |
| 538 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 539 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } else { |
| 541 | spin_lock_bh(&pmc->lock); |
| 542 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 543 | type = IGMPV3_MODE_IS_EXCLUDE; |
| 544 | else |
| 545 | type = IGMPV3_MODE_IS_INCLUDE; |
| 546 | skb = add_grec(skb, pmc, type, 0, 0); |
| 547 | spin_unlock_bh(&pmc->lock); |
| 548 | } |
| 549 | if (!skb) |
| 550 | return 0; |
| 551 | return igmpv3_sendpack(skb); |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * remove zero-count source records from a source filter list |
| 556 | */ |
| 557 | static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) |
| 558 | { |
| 559 | struct ip_sf_list *psf_prev, *psf_next, *psf; |
| 560 | |
| 561 | psf_prev = NULL; |
| 562 | for (psf=*ppsf; psf; psf = psf_next) { |
| 563 | psf_next = psf->sf_next; |
| 564 | if (psf->sf_crcount == 0) { |
| 565 | if (psf_prev) |
| 566 | psf_prev->sf_next = psf->sf_next; |
| 567 | else |
| 568 | *ppsf = psf->sf_next; |
| 569 | kfree(psf); |
| 570 | } else |
| 571 | psf_prev = psf; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | static void igmpv3_send_cr(struct in_device *in_dev) |
| 576 | { |
| 577 | struct ip_mc_list *pmc, *pmc_prev, *pmc_next; |
| 578 | struct sk_buff *skb = NULL; |
| 579 | int type, dtype; |
| 580 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 581 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 583 | |
| 584 | /* deleted MCA's */ |
| 585 | pmc_prev = NULL; |
| 586 | for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) { |
| 587 | pmc_next = pmc->next; |
| 588 | if (pmc->sfmode == MCAST_INCLUDE) { |
| 589 | type = IGMPV3_BLOCK_OLD_SOURCES; |
| 590 | dtype = IGMPV3_BLOCK_OLD_SOURCES; |
| 591 | skb = add_grec(skb, pmc, type, 1, 0); |
| 592 | skb = add_grec(skb, pmc, dtype, 1, 1); |
| 593 | } |
| 594 | if (pmc->crcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | if (pmc->sfmode == MCAST_EXCLUDE) { |
| 596 | type = IGMPV3_CHANGE_TO_INCLUDE; |
| 597 | skb = add_grec(skb, pmc, type, 1, 0); |
| 598 | } |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 599 | pmc->crcount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | if (pmc->crcount == 0) { |
| 601 | igmpv3_clear_zeros(&pmc->tomb); |
| 602 | igmpv3_clear_zeros(&pmc->sources); |
| 603 | } |
| 604 | } |
| 605 | if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { |
| 606 | if (pmc_prev) |
| 607 | pmc_prev->next = pmc_next; |
| 608 | else |
| 609 | in_dev->mc_tomb = pmc_next; |
| 610 | in_dev_put(pmc->interface); |
| 611 | kfree(pmc); |
| 612 | } else |
| 613 | pmc_prev = pmc; |
| 614 | } |
| 615 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 616 | |
| 617 | /* change recs */ |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 618 | for_each_pmc_rcu(in_dev, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | spin_lock_bh(&pmc->lock); |
| 620 | if (pmc->sfcount[MCAST_EXCLUDE]) { |
| 621 | type = IGMPV3_BLOCK_OLD_SOURCES; |
| 622 | dtype = IGMPV3_ALLOW_NEW_SOURCES; |
| 623 | } else { |
| 624 | type = IGMPV3_ALLOW_NEW_SOURCES; |
| 625 | dtype = IGMPV3_BLOCK_OLD_SOURCES; |
| 626 | } |
| 627 | skb = add_grec(skb, pmc, type, 0, 0); |
| 628 | skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ |
| 629 | |
| 630 | /* filter mode changes */ |
| 631 | if (pmc->crcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | if (pmc->sfmode == MCAST_EXCLUDE) |
| 633 | type = IGMPV3_CHANGE_TO_EXCLUDE; |
| 634 | else |
| 635 | type = IGMPV3_CHANGE_TO_INCLUDE; |
| 636 | skb = add_grec(skb, pmc, type, 0, 0); |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 637 | pmc->crcount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | spin_unlock_bh(&pmc->lock); |
| 640 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 641 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | if (!skb) |
| 644 | return; |
| 645 | (void) igmpv3_sendpack(skb); |
| 646 | } |
| 647 | |
| 648 | static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, |
| 649 | int type) |
| 650 | { |
| 651 | struct sk_buff *skb; |
| 652 | struct iphdr *iph; |
| 653 | struct igmphdr *ih; |
| 654 | struct rtable *rt; |
| 655 | struct net_device *dev = in_dev->dev; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 656 | struct net *net = dev_net(dev); |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 657 | __be32 group = pmc ? pmc->multiaddr : 0; |
| 658 | __be32 dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) |
| 661 | return igmpv3_send_report(in_dev, pmc); |
| 662 | else if (type == IGMP_HOST_LEAVE_MESSAGE) |
| 663 | dst = IGMP_ALL_ROUTER; |
| 664 | else |
| 665 | dst = group; |
| 666 | |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame^] | 667 | rt = ip_route_output_ports(net, NULL, dst, 0, |
| 668 | 0, 0, |
| 669 | IPPROTO_IGMP, 0, dev->ifindex); |
| 670 | if (IS_ERR(rt)) |
| 671 | return -1; |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (rt->rt_src == 0) { |
| 674 | ip_rt_put(rt); |
| 675 | return -1; |
| 676 | } |
| 677 | |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 678 | skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | if (skb == NULL) { |
| 680 | ip_rt_put(rt); |
| 681 | return -1; |
| 682 | } |
| 683 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 684 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
| 686 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); |
| 687 | |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 688 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 689 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | 7e28ecc | 2007-03-10 18:40:59 -0300 | [diff] [blame] | 690 | skb_put(skb, sizeof(struct iphdr) + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | iph->version = 4; |
| 693 | iph->ihl = (sizeof(struct iphdr)+4)>>2; |
| 694 | iph->tos = 0xc0; |
| 695 | iph->frag_off = htons(IP_DF); |
| 696 | iph->ttl = 1; |
| 697 | iph->daddr = dst; |
| 698 | iph->saddr = rt->rt_src; |
| 699 | iph->protocol = IPPROTO_IGMP; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 700 | ip_select_ident(iph, &rt->dst, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | ((u8*)&iph[1])[0] = IPOPT_RA; |
| 702 | ((u8*)&iph[1])[1] = 4; |
| 703 | ((u8*)&iph[1])[2] = 0; |
| 704 | ((u8*)&iph[1])[3] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 707 | ih->type = type; |
| 708 | ih->code = 0; |
| 709 | ih->csum = 0; |
| 710 | ih->group = group; |
| 711 | ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 713 | return ip_local_out(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | static void igmp_gq_timer_expire(unsigned long data) |
| 717 | { |
| 718 | struct in_device *in_dev = (struct in_device *)data; |
| 719 | |
| 720 | in_dev->mr_gq_running = 0; |
| 721 | igmpv3_send_report(in_dev, NULL); |
| 722 | __in_dev_put(in_dev); |
| 723 | } |
| 724 | |
| 725 | static void igmp_ifc_timer_expire(unsigned long data) |
| 726 | { |
| 727 | struct in_device *in_dev = (struct in_device *)data; |
| 728 | |
| 729 | igmpv3_send_cr(in_dev); |
| 730 | if (in_dev->mr_ifc_count) { |
| 731 | in_dev->mr_ifc_count--; |
| 732 | igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval); |
| 733 | } |
| 734 | __in_dev_put(in_dev); |
| 735 | } |
| 736 | |
| 737 | static void igmp_ifc_event(struct in_device *in_dev) |
| 738 | { |
| 739 | if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) |
| 740 | return; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 741 | 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] | 742 | IGMP_Unsolicited_Report_Count; |
| 743 | igmp_ifc_start_timer(in_dev, 1); |
| 744 | } |
| 745 | |
| 746 | |
| 747 | static void igmp_timer_expire(unsigned long data) |
| 748 | { |
| 749 | struct ip_mc_list *im=(struct ip_mc_list *)data; |
| 750 | struct in_device *in_dev = im->interface; |
| 751 | |
| 752 | spin_lock(&im->lock); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 753 | im->tm_running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
| 755 | if (im->unsolicit_count) { |
| 756 | im->unsolicit_count--; |
| 757 | igmp_start_timer(im, IGMP_Unsolicited_Report_Interval); |
| 758 | } |
| 759 | im->reporter = 1; |
| 760 | spin_unlock(&im->lock); |
| 761 | |
| 762 | if (IGMP_V1_SEEN(in_dev)) |
| 763 | igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); |
| 764 | else if (IGMP_V2_SEEN(in_dev)) |
| 765 | igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); |
| 766 | else |
| 767 | igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); |
| 768 | |
| 769 | ip_ma_put(im); |
| 770 | } |
| 771 | |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 772 | /* mark EXCLUDE-mode sources */ |
Al Viro | ea4d9e7 | 2006-09-27 18:30:52 -0700 | [diff] [blame] | 773 | 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] | 774 | { |
| 775 | struct ip_sf_list *psf; |
| 776 | int i, scount; |
| 777 | |
| 778 | scount = 0; |
| 779 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 780 | if (scount == nsrcs) |
| 781 | break; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 782 | for (i=0; i<nsrcs; i++) { |
| 783 | /* skip inactive filters */ |
| 784 | if (pmc->sfcount[MCAST_INCLUDE] || |
| 785 | pmc->sfcount[MCAST_EXCLUDE] != |
| 786 | psf->sf_count[MCAST_EXCLUDE]) |
| 787 | continue; |
| 788 | if (srcs[i] == psf->sf_inaddr) { |
| 789 | scount++; |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | pmc->gsquery = 0; |
| 795 | if (scount == nsrcs) /* all sources excluded */ |
| 796 | return 0; |
| 797 | return 1; |
| 798 | } |
| 799 | |
Al Viro | ea4d9e7 | 2006-09-27 18:30:52 -0700 | [diff] [blame] | 800 | 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] | 801 | { |
| 802 | struct ip_sf_list *psf; |
| 803 | int i, scount; |
| 804 | |
| 805 | if (pmc->sfmode == MCAST_EXCLUDE) |
| 806 | return igmp_xmarksources(pmc, nsrcs, srcs); |
| 807 | |
| 808 | /* mark INCLUDE-mode sources */ |
| 809 | scount = 0; |
| 810 | for (psf=pmc->sources; psf; psf=psf->sf_next) { |
| 811 | if (scount == nsrcs) |
| 812 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | for (i=0; i<nsrcs; i++) |
| 814 | if (srcs[i] == psf->sf_inaddr) { |
| 815 | psf->sf_gsresp = 1; |
| 816 | scount++; |
| 817 | break; |
| 818 | } |
| 819 | } |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 820 | if (!scount) { |
| 821 | pmc->gsquery = 0; |
| 822 | return 0; |
| 823 | } |
| 824 | pmc->gsquery = 1; |
| 825 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 828 | static void igmp_heard_report(struct in_device *in_dev, __be32 group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | { |
| 830 | struct ip_mc_list *im; |
| 831 | |
| 832 | /* Timers are only set for non-local groups */ |
| 833 | |
| 834 | if (group == IGMP_ALL_HOSTS) |
| 835 | return; |
| 836 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 837 | rcu_read_lock(); |
| 838 | for_each_pmc_rcu(in_dev, im) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | if (im->multiaddr == group) { |
| 840 | igmp_stop_timer(im); |
| 841 | break; |
| 842 | } |
| 843 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 844 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, |
| 848 | int len) |
| 849 | { |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 850 | struct igmphdr *ih = igmp_hdr(skb); |
| 851 | struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | struct ip_mc_list *im; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 853 | __be32 group = ih->group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | int max_delay; |
| 855 | int mark = 0; |
| 856 | |
| 857 | |
David Stevens | 5b7c840 | 2010-09-30 14:29:40 +0000 | [diff] [blame] | 858 | if (len == 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (ih->code == 0) { |
| 860 | /* Alas, old v1 router presents here. */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | max_delay = IGMP_Query_Response_Interval; |
| 863 | in_dev->mr_v1_seen = jiffies + |
| 864 | IGMP_V1_Router_Present_Timeout; |
| 865 | group = 0; |
| 866 | } else { |
| 867 | /* v2 router present */ |
| 868 | max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); |
| 869 | in_dev->mr_v2_seen = jiffies + |
| 870 | IGMP_V2_Router_Present_Timeout; |
| 871 | } |
| 872 | /* cancel the interface change timer */ |
| 873 | in_dev->mr_ifc_count = 0; |
| 874 | if (del_timer(&in_dev->mr_ifc_timer)) |
| 875 | __in_dev_put(in_dev); |
| 876 | /* clear deleted report items */ |
| 877 | igmpv3_clear_delrec(in_dev); |
| 878 | } else if (len < 12) { |
| 879 | return; /* ignore bogus packet; freed by caller */ |
David Stevens | 5b7c840 | 2010-09-30 14:29:40 +0000 | [diff] [blame] | 880 | } else if (IGMP_V1_SEEN(in_dev)) { |
| 881 | /* This is a v3 query with v1 queriers present */ |
| 882 | max_delay = IGMP_Query_Response_Interval; |
| 883 | group = 0; |
| 884 | } else if (IGMP_V2_SEEN(in_dev)) { |
| 885 | /* this is a v3 query with v2 queriers present; |
| 886 | * Interpretation of the max_delay code is problematic here. |
| 887 | * A real v2 host would use ih_code directly, while v3 has a |
| 888 | * different encoding. We use the v3 encoding as more likely |
| 889 | * to be intended in a v3 query. |
| 890 | */ |
| 891 | max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } else { /* v3 */ |
| 893 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) |
| 894 | return; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 895 | |
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 | if (ih3->nsrcs) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 898 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 899 | + ntohs(ih3->nsrcs)*sizeof(__be32))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | return; |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 901 | ih3 = igmpv3_query_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); |
| 905 | if (!max_delay) |
| 906 | max_delay = 1; /* can't mod w/ 0 */ |
| 907 | in_dev->mr_maxdelay = max_delay; |
| 908 | if (ih3->qrv) |
| 909 | in_dev->mr_qrv = ih3->qrv; |
| 910 | if (!group) { /* general query */ |
| 911 | if (ih3->nsrcs) |
| 912 | return; /* no sources allowed */ |
| 913 | igmp_gq_start_timer(in_dev); |
| 914 | return; |
| 915 | } |
| 916 | /* mark sources to include, if group & source-specific */ |
| 917 | mark = ih3->nsrcs != 0; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * - Start the timers in all of our membership records |
| 922 | * that the query applies to for the interface on |
| 923 | * which the query arrived excl. those that belong |
| 924 | * to a "local" group (224.0.0.X) |
| 925 | * - For timers already running check if they need to |
| 926 | * be reset. |
| 927 | * - Use the igmp->igmp_code field as the maximum |
| 928 | * delay possible |
| 929 | */ |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 930 | rcu_read_lock(); |
| 931 | for_each_pmc_rcu(in_dev, im) { |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 932 | int changed; |
| 933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | if (group && group != im->multiaddr) |
| 935 | continue; |
| 936 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 937 | continue; |
| 938 | spin_lock_bh(&im->lock); |
| 939 | if (im->tm_running) |
| 940 | im->gsquery = im->gsquery && mark; |
| 941 | else |
| 942 | im->gsquery = mark; |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 943 | changed = !im->gsquery || |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 944 | igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | spin_unlock_bh(&im->lock); |
David L Stevens | ad12583 | 2006-01-18 14:20:56 -0800 | [diff] [blame] | 946 | if (changed) |
| 947 | igmp_mod_timer(im, max_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 949 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Eric Dumazet | 9a57a9d | 2010-06-07 03:17:10 +0000 | [diff] [blame] | 952 | /* called in rcu_read_lock() section */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | int igmp_rcv(struct sk_buff *skb) |
| 954 | { |
| 955 | /* This basically follows the spec line by line -- see RFC1112 */ |
| 956 | struct igmphdr *ih; |
Eric Dumazet | 9a57a9d | 2010-06-07 03:17:10 +0000 | [diff] [blame] | 957 | struct in_device *in_dev = __in_dev_get_rcu(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | int len = skb->len; |
| 959 | |
Denis V. Lunev | cd557bc | 2008-02-09 23:22:26 -0800 | [diff] [blame] | 960 | if (in_dev == NULL) |
| 961 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 963 | if (!pskb_may_pull(skb, sizeof(struct igmphdr))) |
Eric Dumazet | 9a57a9d | 2010-06-07 03:17:10 +0000 | [diff] [blame] | 964 | goto drop; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 965 | |
| 966 | switch (skb->ip_summed) { |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 967 | case CHECKSUM_COMPLETE: |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 968 | if (!csum_fold(skb->csum)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 969 | break; |
| 970 | /* fall through */ |
| 971 | case CHECKSUM_NONE: |
| 972 | skb->csum = 0; |
| 973 | if (__skb_checksum_complete(skb)) |
Eric Dumazet | 9a57a9d | 2010-06-07 03:17:10 +0000 | [diff] [blame] | 974 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Arnaldo Carvalho de Melo | d9edf9e | 2007-03-13 14:19:23 -0300 | [diff] [blame] | 977 | ih = igmp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | switch (ih->type) { |
| 979 | case IGMP_HOST_MEMBERSHIP_QUERY: |
| 980 | igmp_heard_query(in_dev, skb, len); |
| 981 | break; |
| 982 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 983 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | /* Is it our report looped back? */ |
David S. Miller | c753796 | 2010-11-11 17:07:48 -0800 | [diff] [blame] | 985 | if (rt_is_output_route(skb_rtable(skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | break; |
David Stevens | 24c6927 | 2005-12-02 20:32:59 -0800 | [diff] [blame] | 987 | /* don't rely on MC router hearing unicast reports */ |
| 988 | if (skb->pkt_type == PACKET_MULTICAST || |
| 989 | skb->pkt_type == PACKET_BROADCAST) |
| 990 | igmp_heard_report(in_dev, ih->group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | break; |
| 992 | case IGMP_PIM: |
| 993 | #ifdef CONFIG_IP_PIMSM_V1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | return pim_rcv_v1(skb); |
| 995 | #endif |
Herbert Xu | c6b471e | 2010-02-07 17:26:30 +0000 | [diff] [blame] | 996 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | case IGMP_DVMRP: |
| 998 | case IGMP_TRACE: |
| 999 | case IGMP_HOST_LEAVE_MESSAGE: |
| 1000 | case IGMP_MTRACE: |
| 1001 | case IGMP_MTRACE_RESP: |
| 1002 | break; |
| 1003 | default: |
Linus Torvalds | dd1c185 | 2006-01-31 13:11:41 -0800 | [diff] [blame] | 1004 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | } |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 1006 | |
Denis V. Lunev | cd557bc | 2008-02-09 23:22:26 -0800 | [diff] [blame] | 1007 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | kfree_skb(skb); |
| 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 |
| 1026 | is changed. This check should be done in dev->set_multicast_list |
| 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)) |
| 1172 | goto done; |
| 1173 | if (IGMP_V2_SEEN(in_dev)) { |
| 1174 | if (reporter) |
| 1175 | igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); |
| 1176 | goto done; |
| 1177 | } |
| 1178 | /* IGMPv3 */ |
| 1179 | igmpv3_add_delrec(in_dev, im); |
| 1180 | |
| 1181 | igmp_ifc_event(in_dev); |
| 1182 | } |
| 1183 | done: |
| 1184 | #endif |
| 1185 | ip_mc_clear_src(im); |
| 1186 | } |
| 1187 | |
| 1188 | static void igmp_group_added(struct ip_mc_list *im) |
| 1189 | { |
| 1190 | struct in_device *in_dev = im->interface; |
| 1191 | |
| 1192 | if (im->loaded == 0) { |
| 1193 | im->loaded = 1; |
| 1194 | ip_mc_filter_add(in_dev, im->multiaddr); |
| 1195 | } |
| 1196 | |
| 1197 | #ifdef CONFIG_IP_MULTICAST |
| 1198 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 1199 | return; |
| 1200 | |
| 1201 | if (in_dev->dead) |
| 1202 | return; |
| 1203 | if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { |
| 1204 | spin_lock_bh(&im->lock); |
| 1205 | igmp_start_timer(im, IGMP_Initial_Report_Delay); |
| 1206 | spin_unlock_bh(&im->lock); |
| 1207 | return; |
| 1208 | } |
| 1209 | /* else, v3 */ |
| 1210 | |
| 1211 | im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : |
| 1212 | IGMP_Unsolicited_Report_Count; |
| 1213 | igmp_ifc_event(in_dev); |
| 1214 | #endif |
| 1215 | } |
| 1216 | |
| 1217 | |
| 1218 | /* |
| 1219 | * Multicast list managers |
| 1220 | */ |
| 1221 | |
| 1222 | |
| 1223 | /* |
| 1224 | * A socket has joined a multicast group on device dev. |
| 1225 | */ |
| 1226 | |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1227 | void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | { |
| 1229 | struct ip_mc_list *im; |
| 1230 | |
| 1231 | ASSERT_RTNL(); |
| 1232 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1233 | for_each_pmc_rtnl(in_dev, im) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | if (im->multiaddr == addr) { |
| 1235 | im->users++; |
| 1236 | ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0); |
| 1237 | goto out; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1241 | im = kzalloc(sizeof(*im), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | if (!im) |
| 1243 | goto out; |
| 1244 | |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1245 | im->users = 1; |
| 1246 | im->interface = in_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | in_dev_hold(in_dev); |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1248 | im->multiaddr = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | /* initial mode is (EX, empty) */ |
| 1250 | im->sfmode = MCAST_EXCLUDE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | im->sfcount[MCAST_EXCLUDE] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | atomic_set(&im->refcnt, 1); |
| 1253 | spin_lock_init(&im->lock); |
| 1254 | #ifdef CONFIG_IP_MULTICAST |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1255 | setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | im->unsolicit_count = IGMP_Unsolicited_Report_Count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | #endif |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1258 | |
| 1259 | im->next_rcu = in_dev->mc_list; |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 1260 | in_dev->mc_count++; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1261 | rcu_assign_pointer(in_dev->mc_list, im); |
| 1262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | #ifdef CONFIG_IP_MULTICAST |
| 1264 | igmpv3_del_delrec(in_dev, im->multiaddr); |
| 1265 | #endif |
| 1266 | igmp_group_added(im); |
| 1267 | if (!in_dev->dead) |
| 1268 | ip_rt_multicast_event(in_dev); |
| 1269 | out: |
| 1270 | return; |
| 1271 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 1272 | EXPORT_SYMBOL(ip_mc_inc_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1275 | * Resend IGMP JOIN report; used for bonding. |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1276 | * Called with rcu_read_lock() |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1277 | */ |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1278 | void ip_mc_rejoin_groups(struct in_device *in_dev) |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1279 | { |
Geert Uytterhoeven | 0888266 | 2007-03-12 17:02:37 -0700 | [diff] [blame] | 1280 | #ifdef CONFIG_IP_MULTICAST |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1281 | struct ip_mc_list *im; |
| 1282 | int type; |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1283 | |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1284 | for_each_pmc_rcu(in_dev, im) { |
| 1285 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 1286 | continue; |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1287 | |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1288 | /* a failover is happening and switches |
| 1289 | * must be notified immediately |
| 1290 | */ |
| 1291 | if (IGMP_V1_SEEN(in_dev)) |
| 1292 | type = IGMP_HOST_MEMBERSHIP_REPORT; |
| 1293 | else if (IGMP_V2_SEEN(in_dev)) |
| 1294 | type = IGMPV2_HOST_MEMBERSHIP_REPORT; |
| 1295 | else |
| 1296 | type = IGMPV3_HOST_MEMBERSHIP_REPORT; |
| 1297 | igmp_send_report(in_dev, im, type); |
| 1298 | } |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1299 | #endif |
| 1300 | } |
Eric Dumazet | 866f3b2 | 2010-11-18 09:33:19 -0800 | [diff] [blame] | 1301 | EXPORT_SYMBOL(ip_mc_rejoin_groups); |
Jay Vosburgh | a816c7c | 2007-02-28 17:03:37 -0800 | [diff] [blame] | 1302 | |
| 1303 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | * A socket has left a multicast group on device dev |
| 1305 | */ |
| 1306 | |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1307 | void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1309 | struct ip_mc_list *i; |
| 1310 | struct ip_mc_list __rcu **ip; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | ASSERT_RTNL(); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1313 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1314 | for (ip = &in_dev->mc_list; |
| 1315 | (i = rtnl_dereference(*ip)) != NULL; |
| 1316 | ip = &i->next_rcu) { |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1317 | if (i->multiaddr == addr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | if (--i->users == 0) { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1319 | *ip = i->next_rcu; |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 1320 | in_dev->mc_count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | igmp_group_dropped(i); |
| 1322 | |
| 1323 | if (!in_dev->dead) |
| 1324 | ip_rt_multicast_event(in_dev); |
| 1325 | |
| 1326 | ip_ma_put(i); |
| 1327 | return; |
| 1328 | } |
| 1329 | break; |
| 1330 | } |
| 1331 | } |
| 1332 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 1333 | EXPORT_SYMBOL(ip_mc_dec_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1335 | /* Device changing type */ |
| 1336 | |
| 1337 | void ip_mc_unmap(struct in_device *in_dev) |
| 1338 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1339 | struct ip_mc_list *pmc; |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1340 | |
| 1341 | ASSERT_RTNL(); |
| 1342 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1343 | for_each_pmc_rtnl(in_dev, pmc) |
| 1344 | igmp_group_dropped(pmc); |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | void ip_mc_remap(struct in_device *in_dev) |
| 1348 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1349 | struct ip_mc_list *pmc; |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1350 | |
| 1351 | ASSERT_RTNL(); |
| 1352 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1353 | for_each_pmc_rtnl(in_dev, pmc) |
| 1354 | igmp_group_added(pmc); |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | /* Device going down */ |
| 1358 | |
| 1359 | void ip_mc_down(struct in_device *in_dev) |
| 1360 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1361 | struct ip_mc_list *pmc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | ASSERT_RTNL(); |
| 1364 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1365 | for_each_pmc_rtnl(in_dev, pmc) |
| 1366 | igmp_group_dropped(pmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
| 1368 | #ifdef CONFIG_IP_MULTICAST |
| 1369 | in_dev->mr_ifc_count = 0; |
| 1370 | if (del_timer(&in_dev->mr_ifc_timer)) |
| 1371 | __in_dev_put(in_dev); |
| 1372 | in_dev->mr_gq_running = 0; |
| 1373 | if (del_timer(&in_dev->mr_gq_timer)) |
| 1374 | __in_dev_put(in_dev); |
| 1375 | igmpv3_clear_delrec(in_dev); |
| 1376 | #endif |
| 1377 | |
| 1378 | ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); |
| 1379 | } |
| 1380 | |
| 1381 | void ip_mc_init_dev(struct in_device *in_dev) |
| 1382 | { |
| 1383 | ASSERT_RTNL(); |
| 1384 | |
| 1385 | in_dev->mc_tomb = NULL; |
| 1386 | #ifdef CONFIG_IP_MULTICAST |
| 1387 | in_dev->mr_gq_running = 0; |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1388 | setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire, |
| 1389 | (unsigned long)in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | in_dev->mr_ifc_count = 0; |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 1391 | in_dev->mc_count = 0; |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1392 | setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, |
| 1393 | (unsigned long)in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | in_dev->mr_qrv = IGMP_Unsolicited_Report_Count; |
| 1395 | #endif |
| 1396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | spin_lock_init(&in_dev->mc_tomb_lock); |
| 1398 | } |
| 1399 | |
| 1400 | /* Device going up */ |
| 1401 | |
| 1402 | void ip_mc_up(struct in_device *in_dev) |
| 1403 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1404 | struct ip_mc_list *pmc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
| 1406 | ASSERT_RTNL(); |
| 1407 | |
| 1408 | ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); |
| 1409 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1410 | for_each_pmc_rtnl(in_dev, pmc) |
| 1411 | igmp_group_added(pmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * Device is about to be destroyed: clean up. |
| 1416 | */ |
| 1417 | |
| 1418 | void ip_mc_destroy_dev(struct in_device *in_dev) |
| 1419 | { |
| 1420 | struct ip_mc_list *i; |
| 1421 | |
| 1422 | ASSERT_RTNL(); |
| 1423 | |
| 1424 | /* Deactivate timers */ |
| 1425 | ip_mc_down(in_dev); |
| 1426 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1427 | while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { |
| 1428 | in_dev->mc_list = i->next_rcu; |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 1429 | in_dev->mc_count--; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | igmp_group_dropped(i); |
| 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, |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1591 | __be32 *psfsrc, int delta) |
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++) { |
| 1726 | err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta); |
| 1727 | if (err) |
| 1728 | break; |
| 1729 | } |
| 1730 | if (err) { |
| 1731 | int j; |
| 1732 | |
| 1733 | pmc->sfcount[sfmode]--; |
| 1734 | for (j=0; j<i; j++) |
| 1735 | (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); |
| 1736 | } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { |
| 1737 | #ifdef CONFIG_IP_MULTICAST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | struct ip_sf_list *psf; |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 1739 | in_dev = pmc->interface; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | #endif |
| 1741 | |
| 1742 | /* filter mode change */ |
| 1743 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 1744 | pmc->sfmode = MCAST_EXCLUDE; |
| 1745 | else if (pmc->sfcount[MCAST_INCLUDE]) |
| 1746 | pmc->sfmode = MCAST_INCLUDE; |
| 1747 | #ifdef CONFIG_IP_MULTICAST |
| 1748 | /* else no filters; keep old mode for reports */ |
| 1749 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1750 | pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | IGMP_Unsolicited_Report_Count; |
| 1752 | in_dev->mr_ifc_count = pmc->crcount; |
| 1753 | for (psf=pmc->sources; psf; psf = psf->sf_next) |
| 1754 | psf->sf_crcount = 0; |
| 1755 | igmp_ifc_event(in_dev); |
| 1756 | } else if (sf_setstate(pmc)) { |
| 1757 | igmp_ifc_event(in_dev); |
| 1758 | #endif |
| 1759 | } |
| 1760 | spin_unlock_bh(&pmc->lock); |
| 1761 | return err; |
| 1762 | } |
| 1763 | |
| 1764 | static void ip_mc_clear_src(struct ip_mc_list *pmc) |
| 1765 | { |
| 1766 | struct ip_sf_list *psf, *nextpsf; |
| 1767 | |
| 1768 | for (psf=pmc->tomb; psf; psf=nextpsf) { |
| 1769 | nextpsf = psf->sf_next; |
| 1770 | kfree(psf); |
| 1771 | } |
| 1772 | pmc->tomb = NULL; |
| 1773 | for (psf=pmc->sources; psf; psf=nextpsf) { |
| 1774 | nextpsf = psf->sf_next; |
| 1775 | kfree(psf); |
| 1776 | } |
| 1777 | pmc->sources = NULL; |
| 1778 | pmc->sfmode = MCAST_EXCLUDE; |
Denis Lukianov | de9daad | 2005-09-14 20:53:42 -0700 | [diff] [blame] | 1779 | pmc->sfcount[MCAST_INCLUDE] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | pmc->sfcount[MCAST_EXCLUDE] = 1; |
| 1781 | } |
| 1782 | |
| 1783 | |
| 1784 | /* |
| 1785 | * Join a multicast group |
| 1786 | */ |
| 1787 | int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr) |
| 1788 | { |
| 1789 | int err; |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1790 | __be32 addr = imr->imr_multiaddr.s_addr; |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1791 | struct ip_mc_socklist *iml = NULL, *i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | struct in_device *in_dev; |
| 1793 | struct inet_sock *inet = inet_sk(sk); |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1794 | struct net *net = sock_net(sk); |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1795 | int ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | int count = 0; |
| 1797 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 1798 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | return -EINVAL; |
| 1800 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 1801 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1803 | in_dev = ip_mc_find_dev(net, imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | |
| 1805 | if (!in_dev) { |
| 1806 | iml = NULL; |
| 1807 | err = -ENODEV; |
| 1808 | goto done; |
| 1809 | } |
| 1810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | err = -EADDRINUSE; |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1812 | ifindex = imr->imr_ifindex; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1813 | for_each_pmc_rtnl(inet, i) { |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1814 | if (i->multi.imr_multiaddr.s_addr == addr && |
| 1815 | i->multi.imr_ifindex == ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | count++; |
| 1818 | } |
| 1819 | err = -ENOBUFS; |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1820 | if (count >= sysctl_igmp_max_memberships) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | goto done; |
Jianjun Kong | a7e9ff7 | 2008-11-03 00:26:09 -0800 | [diff] [blame] | 1822 | iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); |
David L Stevens | ca9b907 | 2005-07-08 17:38:07 -0700 | [diff] [blame] | 1823 | if (iml == NULL) |
| 1824 | goto done; |
| 1825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | memcpy(&iml->multi, imr, sizeof(*imr)); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1827 | iml->next_rcu = inet->mc_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | iml->sflist = NULL; |
| 1829 | iml->sfmode = MCAST_EXCLUDE; |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1830 | rcu_assign_pointer(inet->mc_list, iml); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | ip_mc_inc_group(in_dev, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 1834 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | return err; |
| 1836 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 1837 | EXPORT_SYMBOL(ip_mc_join_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1839 | static void ip_sf_socklist_reclaim(struct rcu_head *rp) |
| 1840 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1841 | kfree(container_of(rp, struct ip_sf_socklist, rcu)); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1842 | /* sk_omem_alloc should have been decreased by the caller*/ |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1843 | } |
| 1844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, |
| 1846 | struct in_device *in_dev) |
| 1847 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1848 | struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | int err; |
| 1850 | |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1851 | if (psf == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | /* any-source empty exclude case */ |
| 1853 | return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, |
| 1854 | iml->sfmode, 0, NULL, 0); |
| 1855 | } |
| 1856 | 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] | 1857 | iml->sfmode, psf->sl_count, psf->sl_addr, 0); |
| 1858 | rcu_assign_pointer(iml->sflist, NULL); |
| 1859 | /* decrease mem now to avoid the memleak warning */ |
| 1860 | atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); |
| 1861 | call_rcu(&psf->rcu, ip_sf_socklist_reclaim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | return err; |
| 1863 | } |
| 1864 | |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1865 | |
| 1866 | static void ip_mc_socklist_reclaim(struct rcu_head *rp) |
| 1867 | { |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1868 | kfree(container_of(rp, struct ip_mc_socklist, rcu)); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1869 | /* sk_omem_alloc should have been decreased by the caller*/ |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | /* |
| 1874 | * Ask a socket to leave a group. |
| 1875 | */ |
| 1876 | |
| 1877 | int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) |
| 1878 | { |
| 1879 | struct inet_sock *inet = inet_sk(sk); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1880 | struct ip_mc_socklist *iml; |
| 1881 | struct ip_mc_socklist __rcu **imlp; |
David L Stevens | 84b42ba | 2005-07-08 17:48:38 -0700 | [diff] [blame] | 1882 | struct in_device *in_dev; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1883 | struct net *net = sock_net(sk); |
Al Viro | 8f935bb | 2006-09-27 18:30:07 -0700 | [diff] [blame] | 1884 | __be32 group = imr->imr_multiaddr.s_addr; |
David L Stevens | 84b42ba | 2005-07-08 17:48:38 -0700 | [diff] [blame] | 1885 | u32 ifindex; |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1886 | int ret = -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | |
| 1888 | rtnl_lock(); |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1889 | in_dev = ip_mc_find_dev(net, imr); |
David L Stevens | 84b42ba | 2005-07-08 17:48:38 -0700 | [diff] [blame] | 1890 | ifindex = imr->imr_ifindex; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1891 | for (imlp = &inet->mc_list; |
| 1892 | (iml = rtnl_dereference(*imlp)) != NULL; |
| 1893 | imlp = &iml->next_rcu) { |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1894 | if (iml->multi.imr_multiaddr.s_addr != group) |
| 1895 | continue; |
| 1896 | if (ifindex) { |
| 1897 | if (iml->multi.imr_ifindex != ifindex) |
| 1898 | continue; |
| 1899 | } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != |
| 1900 | iml->multi.imr_address.s_addr) |
| 1901 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1903 | (void) ip_mc_leave_src(sk, iml, in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1905 | *imlp = iml->next_rcu; |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1906 | |
| 1907 | if (in_dev) |
David L Stevens | 84b42ba | 2005-07-08 17:48:38 -0700 | [diff] [blame] | 1908 | ip_mc_dec_group(in_dev, group); |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1909 | rtnl_unlock(); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 1910 | /* decrease mem now to avoid the memleak warning */ |
| 1911 | atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); |
| 1912 | call_rcu(&iml->rcu, ip_mc_socklist_reclaim); |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1913 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | } |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1915 | if (!in_dev) |
| 1916 | ret = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | rtnl_unlock(); |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 1918 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | int ip_mc_source(int add, int omode, struct sock *sk, struct |
| 1922 | ip_mreq_source *mreqs, int ifindex) |
| 1923 | { |
| 1924 | int err; |
| 1925 | struct ip_mreqn imr; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 1926 | __be32 addr = mreqs->imr_multiaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | struct ip_mc_socklist *pmc; |
| 1928 | struct in_device *in_dev = NULL; |
| 1929 | struct inet_sock *inet = inet_sk(sk); |
| 1930 | struct ip_sf_socklist *psl; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1931 | struct net *net = sock_net(sk); |
David L Stevens | 8cdaaa1 | 2005-07-08 17:39:23 -0700 | [diff] [blame] | 1932 | int leavegroup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | int i, j, rv; |
| 1934 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 1935 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | return -EINVAL; |
| 1937 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 1938 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | |
| 1940 | imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; |
| 1941 | imr.imr_address.s_addr = mreqs->imr_interface; |
| 1942 | imr.imr_ifindex = ifindex; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 1943 | in_dev = ip_mc_find_dev(net, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
| 1945 | if (!in_dev) { |
| 1946 | err = -ENODEV; |
| 1947 | goto done; |
| 1948 | } |
| 1949 | err = -EADDRNOTAVAIL; |
| 1950 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1951 | for_each_pmc_rtnl(inet, pmc) { |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1952 | if ((pmc->multi.imr_multiaddr.s_addr == |
| 1953 | imr.imr_multiaddr.s_addr) && |
| 1954 | (pmc->multi.imr_ifindex == imr.imr_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | break; |
| 1956 | } |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1957 | if (!pmc) { /* must have a prior join */ |
| 1958 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | /* if a source filter was set, must be the same mode as before */ |
| 1962 | if (pmc->sflist) { |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1963 | if (pmc->sfmode != omode) { |
| 1964 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | } else if (pmc->sfmode != omode) { |
| 1968 | /* allow mode switches for empty-set filters */ |
| 1969 | 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] | 1970 | ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | NULL, 0); |
| 1972 | pmc->sfmode = omode; |
| 1973 | } |
| 1974 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 1975 | psl = rtnl_dereference(pmc->sflist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | if (!add) { |
| 1977 | if (!psl) |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1978 | goto done; /* err = -EADDRNOTAVAIL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | rv = !0; |
| 1980 | for (i=0; i<psl->sl_count; i++) { |
| 1981 | rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 1982 | sizeof(__be32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | if (rv == 0) |
| 1984 | break; |
| 1985 | } |
| 1986 | if (rv) /* source not found */ |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 1987 | goto done; /* err = -EADDRNOTAVAIL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | |
David L Stevens | 8cdaaa1 | 2005-07-08 17:39:23 -0700 | [diff] [blame] | 1989 | /* special case - (INCLUDE, empty) == LEAVE_GROUP */ |
| 1990 | if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { |
| 1991 | leavegroup = 1; |
| 1992 | goto done; |
| 1993 | } |
| 1994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | /* update the interface filter */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1996 | ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | &mreqs->imr_sourceaddr, 1); |
| 1998 | |
| 1999 | for (j=i+1; j<psl->sl_count; j++) |
| 2000 | psl->sl_addr[j-1] = psl->sl_addr[j]; |
| 2001 | psl->sl_count--; |
| 2002 | err = 0; |
| 2003 | goto done; |
| 2004 | } |
| 2005 | /* else, add a new source to the filter */ |
| 2006 | |
| 2007 | if (psl && psl->sl_count >= sysctl_igmp_max_msf) { |
| 2008 | err = -ENOBUFS; |
| 2009 | goto done; |
| 2010 | } |
| 2011 | if (!psl || psl->sl_count == psl->sl_max) { |
| 2012 | struct ip_sf_socklist *newpsl; |
| 2013 | int count = IP_SFBLOCK; |
| 2014 | |
| 2015 | if (psl) |
| 2016 | count += psl->sl_max; |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 2017 | newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | if (!newpsl) { |
| 2019 | err = -ENOBUFS; |
| 2020 | goto done; |
| 2021 | } |
| 2022 | newpsl->sl_max = count; |
| 2023 | newpsl->sl_count = count - IP_SFBLOCK; |
| 2024 | if (psl) { |
| 2025 | for (i=0; i<psl->sl_count; i++) |
| 2026 | newpsl->sl_addr[i] = psl->sl_addr[i]; |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2027 | /* decrease mem now to avoid the memleak warning */ |
| 2028 | atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); |
| 2029 | call_rcu(&psl->rcu, ip_sf_socklist_reclaim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | } |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2031 | rcu_assign_pointer(pmc->sflist, newpsl); |
| 2032 | psl = newpsl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | } |
| 2034 | rv = 1; /* > 0 for insert logic below if sl_count is 0 */ |
| 2035 | for (i=0; i<psl->sl_count; i++) { |
| 2036 | rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 2037 | sizeof(__be32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | if (rv == 0) |
| 2039 | break; |
| 2040 | } |
| 2041 | if (rv == 0) /* address already there is an error */ |
| 2042 | goto done; |
| 2043 | for (j=psl->sl_count-1; j>=i; j--) |
| 2044 | psl->sl_addr[j+1] = psl->sl_addr[j]; |
| 2045 | psl->sl_addr[i] = mreqs->imr_sourceaddr; |
| 2046 | psl->sl_count++; |
| 2047 | err = 0; |
| 2048 | /* update the interface list */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2049 | ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | &mreqs->imr_sourceaddr, 1); |
| 2051 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2052 | rtnl_unlock(); |
David L Stevens | 8cdaaa1 | 2005-07-08 17:39:23 -0700 | [diff] [blame] | 2053 | if (leavegroup) |
| 2054 | return ip_mc_leave_group(sk, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | return err; |
| 2056 | } |
| 2057 | |
| 2058 | int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) |
| 2059 | { |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 2060 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | struct ip_mreqn imr; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 2062 | __be32 addr = msf->imsf_multiaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | struct ip_mc_socklist *pmc; |
| 2064 | struct in_device *in_dev; |
| 2065 | struct inet_sock *inet = inet_sk(sk); |
| 2066 | struct ip_sf_socklist *newpsl, *psl; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2067 | struct net *net = sock_net(sk); |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 2068 | int leavegroup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 2070 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | return -EINVAL; |
| 2072 | if (msf->imsf_fmode != MCAST_INCLUDE && |
| 2073 | msf->imsf_fmode != MCAST_EXCLUDE) |
| 2074 | return -EINVAL; |
| 2075 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2076 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | |
| 2078 | imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; |
| 2079 | imr.imr_address.s_addr = msf->imsf_interface; |
| 2080 | imr.imr_ifindex = ifindex; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2081 | in_dev = ip_mc_find_dev(net, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | |
| 2083 | if (!in_dev) { |
| 2084 | err = -ENODEV; |
| 2085 | goto done; |
| 2086 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 2088 | /* special case - (INCLUDE, empty) == LEAVE_GROUP */ |
| 2089 | if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { |
| 2090 | leavegroup = 1; |
| 2091 | goto done; |
| 2092 | } |
| 2093 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2094 | for_each_pmc_rtnl(inet, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && |
| 2096 | pmc->multi.imr_ifindex == imr.imr_ifindex) |
| 2097 | break; |
| 2098 | } |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 2099 | if (!pmc) { /* must have a prior join */ |
| 2100 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 2102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | if (msf->imsf_numsrc) { |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 2104 | newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), |
| 2105 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | if (!newpsl) { |
| 2107 | err = -ENOBUFS; |
| 2108 | goto done; |
| 2109 | } |
| 2110 | newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; |
| 2111 | memcpy(newpsl->sl_addr, msf->imsf_slist, |
| 2112 | msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); |
| 2113 | err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, |
| 2114 | msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); |
| 2115 | if (err) { |
| 2116 | sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); |
| 2117 | goto done; |
| 2118 | } |
Yan Zheng | 8713dbf | 2005-10-28 08:02:08 +0800 | [diff] [blame] | 2119 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | newpsl = NULL; |
Yan Zheng | 8713dbf | 2005-10-28 08:02:08 +0800 | [diff] [blame] | 2121 | (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, |
| 2122 | msf->imsf_fmode, 0, NULL, 0); |
| 2123 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2124 | psl = rtnl_dereference(pmc->sflist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | if (psl) { |
| 2126 | (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, |
| 2127 | psl->sl_count, psl->sl_addr, 0); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2128 | /* decrease mem now to avoid the memleak warning */ |
| 2129 | atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); |
| 2130 | call_rcu(&psl->rcu, ip_sf_socklist_reclaim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | } else |
| 2132 | (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, |
| 2133 | 0, NULL, 0); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2134 | rcu_assign_pointer(pmc->sflist, newpsl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | pmc->sfmode = msf->imsf_fmode; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 2136 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2138 | rtnl_unlock(); |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 2139 | if (leavegroup) |
| 2140 | err = ip_mc_leave_group(sk, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | return err; |
| 2142 | } |
| 2143 | |
| 2144 | int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, |
| 2145 | struct ip_msfilter __user *optval, int __user *optlen) |
| 2146 | { |
| 2147 | int err, len, count, copycount; |
| 2148 | struct ip_mreqn imr; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 2149 | __be32 addr = msf->imsf_multiaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | struct ip_mc_socklist *pmc; |
| 2151 | struct in_device *in_dev; |
| 2152 | struct inet_sock *inet = inet_sk(sk); |
| 2153 | struct ip_sf_socklist *psl; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2154 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 2156 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | return -EINVAL; |
| 2158 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2159 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | |
| 2161 | imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; |
| 2162 | imr.imr_address.s_addr = msf->imsf_interface; |
| 2163 | imr.imr_ifindex = 0; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2164 | in_dev = ip_mc_find_dev(net, &imr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | |
| 2166 | if (!in_dev) { |
| 2167 | err = -ENODEV; |
| 2168 | goto done; |
| 2169 | } |
| 2170 | err = -EADDRNOTAVAIL; |
| 2171 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2172 | for_each_pmc_rtnl(inet, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && |
| 2174 | pmc->multi.imr_ifindex == imr.imr_ifindex) |
| 2175 | break; |
| 2176 | } |
| 2177 | if (!pmc) /* must have a prior join */ |
| 2178 | goto done; |
| 2179 | msf->imsf_fmode = pmc->sfmode; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2180 | psl = rtnl_dereference(pmc->sflist); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2181 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | if (!psl) { |
| 2183 | len = 0; |
| 2184 | count = 0; |
| 2185 | } else { |
| 2186 | count = psl->sl_count; |
| 2187 | } |
| 2188 | copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; |
| 2189 | len = copycount * sizeof(psl->sl_addr[0]); |
| 2190 | msf->imsf_numsrc = count; |
| 2191 | if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || |
| 2192 | copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { |
| 2193 | return -EFAULT; |
| 2194 | } |
| 2195 | if (len && |
| 2196 | copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) |
| 2197 | return -EFAULT; |
| 2198 | return 0; |
| 2199 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2200 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | return err; |
| 2202 | } |
| 2203 | |
| 2204 | int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, |
| 2205 | struct group_filter __user *optval, int __user *optlen) |
| 2206 | { |
| 2207 | int err, i, count, copycount; |
| 2208 | struct sockaddr_in *psin; |
Al Viro | 6300772 | 2006-09-27 18:31:32 -0700 | [diff] [blame] | 2209 | __be32 addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | struct ip_mc_socklist *pmc; |
| 2211 | struct inet_sock *inet = inet_sk(sk); |
| 2212 | struct ip_sf_socklist *psl; |
| 2213 | |
| 2214 | psin = (struct sockaddr_in *)&gsf->gf_group; |
| 2215 | if (psin->sin_family != AF_INET) |
| 2216 | return -EINVAL; |
| 2217 | addr = psin->sin_addr.s_addr; |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 2218 | if (!ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | return -EINVAL; |
| 2220 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2221 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | |
| 2223 | err = -EADDRNOTAVAIL; |
| 2224 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2225 | for_each_pmc_rtnl(inet, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | if (pmc->multi.imr_multiaddr.s_addr == addr && |
| 2227 | pmc->multi.imr_ifindex == gsf->gf_interface) |
| 2228 | break; |
| 2229 | } |
| 2230 | if (!pmc) /* must have a prior join */ |
| 2231 | goto done; |
| 2232 | gsf->gf_fmode = pmc->sfmode; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2233 | psl = rtnl_dereference(pmc->sflist); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2234 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | count = psl ? psl->sl_count : 0; |
| 2236 | copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; |
| 2237 | gsf->gf_numsrc = count; |
| 2238 | if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || |
| 2239 | copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { |
| 2240 | return -EFAULT; |
| 2241 | } |
| 2242 | for (i=0; i<copycount; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | struct sockaddr_storage ss; |
| 2244 | |
| 2245 | psin = (struct sockaddr_in *)&ss; |
| 2246 | memset(&ss, 0, sizeof(ss)); |
| 2247 | psin->sin_family = AF_INET; |
| 2248 | psin->sin_addr.s_addr = psl->sl_addr[i]; |
| 2249 | if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) |
| 2250 | return -EFAULT; |
| 2251 | } |
| 2252 | return 0; |
| 2253 | done: |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 2254 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | return err; |
| 2256 | } |
| 2257 | |
| 2258 | /* |
| 2259 | * check if a multicast source filter allows delivery for a given <src,dst,intf> |
| 2260 | */ |
Al Viro | c0cda06 | 2006-09-27 18:31:10 -0700 | [diff] [blame] | 2261 | 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] | 2262 | { |
| 2263 | struct inet_sock *inet = inet_sk(sk); |
| 2264 | struct ip_mc_socklist *pmc; |
| 2265 | struct ip_sf_socklist *psl; |
| 2266 | int i; |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2267 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2269 | ret = 1; |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 2270 | if (!ipv4_is_multicast(loc_addr)) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2271 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2273 | rcu_read_lock(); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2274 | for_each_pmc_rcu(inet, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | if (pmc->multi.imr_multiaddr.s_addr == loc_addr && |
| 2276 | pmc->multi.imr_ifindex == dif) |
| 2277 | break; |
| 2278 | } |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2279 | ret = inet->mc_all; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | if (!pmc) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2281 | goto unlock; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2282 | psl = rcu_dereference(pmc->sflist); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2283 | ret = (pmc->sfmode == MCAST_EXCLUDE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | if (!psl) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2285 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | |
| 2287 | for (i=0; i<psl->sl_count; i++) { |
| 2288 | if (psl->sl_addr[i] == rmt_addr) |
| 2289 | break; |
| 2290 | } |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2291 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2293 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2295 | goto unlock; |
| 2296 | ret = 1; |
| 2297 | unlock: |
| 2298 | rcu_read_unlock(); |
| 2299 | out: |
| 2300 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | /* |
| 2304 | * A socket is closing. |
| 2305 | */ |
| 2306 | |
| 2307 | void ip_mc_drop_socket(struct sock *sk) |
| 2308 | { |
| 2309 | struct inet_sock *inet = inet_sk(sk); |
| 2310 | struct ip_mc_socklist *iml; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2311 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | |
| 2313 | if (inet->mc_list == NULL) |
| 2314 | return; |
| 2315 | |
| 2316 | rtnl_lock(); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2317 | while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | struct in_device *in_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2320 | inet->mc_list = iml->next_rcu; |
Daniel Lezcano | 877aced | 2008-08-13 16:15:57 -0700 | [diff] [blame] | 2321 | in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); |
Michal Ruzicka | bb699cbc | 2006-08-15 00:20:17 -0700 | [diff] [blame] | 2322 | (void) ip_mc_leave_src(sk, iml, in_dev); |
Eric Dumazet | 18943d2 | 2010-11-08 11:15:54 +0000 | [diff] [blame] | 2323 | if (in_dev != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); |
Flavio Leitner | c85bb41 | 2010-02-02 07:32:29 -0800 | [diff] [blame] | 2325 | /* decrease mem now to avoid the memleak warning */ |
| 2326 | atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); |
| 2327 | call_rcu(&iml->rcu, ip_mc_socklist_reclaim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | } |
| 2329 | rtnl_unlock(); |
| 2330 | } |
| 2331 | |
David S. Miller | dbdd9a5 | 2011-03-10 16:34:38 -0800 | [diff] [blame] | 2332 | /* called with rcu_read_lock() */ |
| 2333 | 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] | 2334 | { |
| 2335 | struct ip_mc_list *im; |
| 2336 | struct ip_sf_list *psf; |
| 2337 | int rv = 0; |
| 2338 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2339 | for_each_pmc_rcu(in_dev, im) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | if (im->multiaddr == mc_addr) |
| 2341 | break; |
| 2342 | } |
| 2343 | if (im && proto == IPPROTO_IGMP) { |
| 2344 | rv = 1; |
| 2345 | } else if (im) { |
| 2346 | if (src_addr) { |
| 2347 | for (psf=im->sources; psf; psf=psf->sf_next) { |
| 2348 | if (psf->sf_inaddr == src_addr) |
| 2349 | break; |
| 2350 | } |
| 2351 | if (psf) |
| 2352 | rv = psf->sf_count[MCAST_INCLUDE] || |
| 2353 | psf->sf_count[MCAST_EXCLUDE] != |
| 2354 | im->sfcount[MCAST_EXCLUDE]; |
| 2355 | else |
| 2356 | rv = im->sfcount[MCAST_EXCLUDE] != 0; |
| 2357 | } else |
| 2358 | rv = 1; /* unspecified source; tentatively allow */ |
| 2359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | return rv; |
| 2361 | } |
| 2362 | |
| 2363 | #if defined(CONFIG_PROC_FS) |
| 2364 | struct igmp_mc_iter_state { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2365 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | struct net_device *dev; |
| 2367 | struct in_device *in_dev; |
| 2368 | }; |
| 2369 | |
| 2370 | #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) |
| 2371 | |
| 2372 | static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) |
| 2373 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2374 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | struct ip_mc_list *im = NULL; |
| 2376 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
| 2377 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 2378 | state->in_dev = NULL; |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2379 | for_each_netdev_rcu(net, state->dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | struct in_device *in_dev; |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2381 | |
| 2382 | in_dev = __in_dev_get_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | if (!in_dev) |
| 2384 | continue; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2385 | im = rcu_dereference(in_dev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | if (im) { |
| 2387 | state->in_dev = in_dev; |
| 2388 | break; |
| 2389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | } |
| 2391 | return im; |
| 2392 | } |
| 2393 | |
| 2394 | static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) |
| 2395 | { |
| 2396 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2397 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2398 | im = rcu_dereference(im->next_rcu); |
| 2399 | while (!im) { |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2400 | state->dev = next_net_device_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | if (!state->dev) { |
| 2402 | state->in_dev = NULL; |
| 2403 | break; |
| 2404 | } |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2405 | state->in_dev = __in_dev_get_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | if (!state->in_dev) |
| 2407 | continue; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2408 | im = rcu_dereference(state->in_dev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | } |
| 2410 | return im; |
| 2411 | } |
| 2412 | |
| 2413 | static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) |
| 2414 | { |
| 2415 | struct ip_mc_list *im = igmp_mc_get_first(seq); |
| 2416 | if (im) |
| 2417 | while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) |
| 2418 | --pos; |
| 2419 | return pos ? NULL : im; |
| 2420 | } |
| 2421 | |
| 2422 | 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] | 2423 | __acquires(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | { |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2425 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2427 | } |
| 2428 | |
| 2429 | static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2430 | { |
| 2431 | struct ip_mc_list *im; |
| 2432 | if (v == SEQ_START_TOKEN) |
| 2433 | im = igmp_mc_get_first(seq); |
| 2434 | else |
| 2435 | im = igmp_mc_get_next(seq, v); |
| 2436 | ++*pos; |
| 2437 | return im; |
| 2438 | } |
| 2439 | |
| 2440 | static void igmp_mc_seq_stop(struct seq_file *seq, void *v) |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2441 | __releases(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | { |
| 2443 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2444 | |
| 2445 | state->in_dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | state->dev = NULL; |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2447 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | static int igmp_mc_seq_show(struct seq_file *seq, void *v) |
| 2451 | { |
| 2452 | if (v == SEQ_START_TOKEN) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2453 | seq_puts(seq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); |
| 2455 | else { |
| 2456 | struct ip_mc_list *im = (struct ip_mc_list *)v; |
| 2457 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
| 2458 | char *querier; |
| 2459 | #ifdef CONFIG_IP_MULTICAST |
| 2460 | querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : |
| 2461 | IGMP_V2_SEEN(state->in_dev) ? "V2" : |
| 2462 | "V3"; |
| 2463 | #else |
| 2464 | querier = "NONE"; |
| 2465 | #endif |
| 2466 | |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2467 | if (rcu_dereference(state->in_dev->mc_list) == im) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | seq_printf(seq, "%d\t%-10s: %5d %7s\n", |
Rami Rosen | b8bae41 | 2008-10-07 15:34:37 -0700 | [diff] [blame] | 2469 | state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | seq_printf(seq, |
Alexey Dobriyan | 338fcf9 | 2006-06-05 21:04:39 -0700 | [diff] [blame] | 2473 | "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | im->multiaddr, im->users, |
| 2475 | im->tm_running, im->tm_running ? |
| 2476 | jiffies_to_clock_t(im->timer.expires-jiffies) : 0, |
| 2477 | im->reporter); |
| 2478 | } |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 2482 | static const struct seq_operations igmp_mc_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | .start = igmp_mc_seq_start, |
| 2484 | .next = igmp_mc_seq_next, |
| 2485 | .stop = igmp_mc_seq_stop, |
| 2486 | .show = igmp_mc_seq_show, |
| 2487 | }; |
| 2488 | |
| 2489 | static int igmp_mc_seq_open(struct inode *inode, struct file *file) |
| 2490 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2491 | return seq_open_net(inode, file, &igmp_mc_seq_ops, |
Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 2492 | sizeof(struct igmp_mc_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2495 | static const struct file_operations igmp_mc_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | .owner = THIS_MODULE, |
| 2497 | .open = igmp_mc_seq_open, |
| 2498 | .read = seq_read, |
| 2499 | .llseek = seq_lseek, |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2500 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | }; |
| 2502 | |
| 2503 | struct igmp_mcf_iter_state { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2504 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | struct net_device *dev; |
| 2506 | struct in_device *idev; |
| 2507 | struct ip_mc_list *im; |
| 2508 | }; |
| 2509 | |
| 2510 | #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) |
| 2511 | |
| 2512 | static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) |
| 2513 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2514 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | struct ip_sf_list *psf = NULL; |
| 2516 | struct ip_mc_list *im = NULL; |
| 2517 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2518 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 2519 | state->idev = NULL; |
| 2520 | state->im = NULL; |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2521 | for_each_netdev_rcu(net, state->dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | struct in_device *idev; |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2523 | idev = __in_dev_get_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | if (unlikely(idev == NULL)) |
| 2525 | continue; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2526 | im = rcu_dereference(idev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | if (likely(im != NULL)) { |
| 2528 | spin_lock_bh(&im->lock); |
| 2529 | psf = im->sources; |
| 2530 | if (likely(psf != NULL)) { |
| 2531 | state->im = im; |
| 2532 | state->idev = idev; |
| 2533 | break; |
| 2534 | } |
| 2535 | spin_unlock_bh(&im->lock); |
| 2536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | } |
| 2538 | return psf; |
| 2539 | } |
| 2540 | |
| 2541 | static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) |
| 2542 | { |
| 2543 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2544 | |
| 2545 | psf = psf->sf_next; |
| 2546 | while (!psf) { |
| 2547 | spin_unlock_bh(&state->im->lock); |
| 2548 | state->im = state->im->next; |
| 2549 | while (!state->im) { |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2550 | state->dev = next_net_device_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | if (!state->dev) { |
| 2552 | state->idev = NULL; |
| 2553 | goto out; |
| 2554 | } |
Eric Dumazet | 6baff15 | 2009-11-11 17:48:52 +0000 | [diff] [blame] | 2555 | state->idev = __in_dev_get_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | if (!state->idev) |
| 2557 | continue; |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2558 | state->im = rcu_dereference(state->idev->mc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | } |
| 2560 | if (!state->im) |
| 2561 | break; |
| 2562 | spin_lock_bh(&state->im->lock); |
| 2563 | psf = state->im->sources; |
| 2564 | } |
| 2565 | out: |
| 2566 | return psf; |
| 2567 | } |
| 2568 | |
| 2569 | static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) |
| 2570 | { |
| 2571 | struct ip_sf_list *psf = igmp_mcf_get_first(seq); |
| 2572 | if (psf) |
| 2573 | while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) |
| 2574 | --pos; |
| 2575 | return pos ? NULL : psf; |
| 2576 | } |
| 2577 | |
| 2578 | 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] | 2579 | __acquires(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | { |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2581 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2583 | } |
| 2584 | |
| 2585 | static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2586 | { |
| 2587 | struct ip_sf_list *psf; |
| 2588 | if (v == SEQ_START_TOKEN) |
| 2589 | psf = igmp_mcf_get_first(seq); |
| 2590 | else |
| 2591 | psf = igmp_mcf_get_next(seq, v); |
| 2592 | ++*pos; |
| 2593 | return psf; |
| 2594 | } |
| 2595 | |
| 2596 | static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2597 | __releases(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | { |
| 2599 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2600 | if (likely(state->im != NULL)) { |
| 2601 | spin_unlock_bh(&state->im->lock); |
| 2602 | state->im = NULL; |
| 2603 | } |
Eric Dumazet | 1d7138d | 2010-11-12 05:46:50 +0000 | [diff] [blame] | 2604 | state->idev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | state->dev = NULL; |
stephen hemminger | 61fbab7 | 2009-11-10 07:54:55 +0000 | [diff] [blame] | 2606 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | static int igmp_mcf_seq_show(struct seq_file *seq, void *v) |
| 2610 | { |
| 2611 | struct ip_sf_list *psf = (struct ip_sf_list *)v; |
| 2612 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2613 | |
| 2614 | if (v == SEQ_START_TOKEN) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2615 | seq_printf(seq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | "%3s %6s " |
| 2617 | "%10s %10s %6s %6s\n", "Idx", |
| 2618 | "Device", "MCA", |
| 2619 | "SRC", "INC", "EXC"); |
| 2620 | } else { |
| 2621 | seq_printf(seq, |
| 2622 | "%3d %6.6s 0x%08x " |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2623 | "0x%08x %6lu %6lu\n", |
| 2624 | state->dev->ifindex, state->dev->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | ntohl(state->im->multiaddr), |
| 2626 | ntohl(psf->sf_inaddr), |
| 2627 | psf->sf_count[MCAST_INCLUDE], |
| 2628 | psf->sf_count[MCAST_EXCLUDE]); |
| 2629 | } |
| 2630 | return 0; |
| 2631 | } |
| 2632 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 2633 | static const struct seq_operations igmp_mcf_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | .start = igmp_mcf_seq_start, |
| 2635 | .next = igmp_mcf_seq_next, |
| 2636 | .stop = igmp_mcf_seq_stop, |
| 2637 | .show = igmp_mcf_seq_show, |
| 2638 | }; |
| 2639 | |
| 2640 | static int igmp_mcf_seq_open(struct inode *inode, struct file *file) |
| 2641 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2642 | return seq_open_net(inode, file, &igmp_mcf_seq_ops, |
Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 2643 | sizeof(struct igmp_mcf_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | } |
| 2645 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2646 | static const struct file_operations igmp_mcf_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | .owner = THIS_MODULE, |
| 2648 | .open = igmp_mcf_seq_open, |
| 2649 | .read = seq_read, |
| 2650 | .llseek = seq_lseek, |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2651 | .release = seq_release_net, |
| 2652 | }; |
| 2653 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2654 | static int __net_init igmp_net_init(struct net *net) |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2655 | { |
| 2656 | struct proc_dir_entry *pde; |
| 2657 | |
| 2658 | pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops); |
| 2659 | if (!pde) |
| 2660 | goto out_igmp; |
| 2661 | pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops); |
| 2662 | if (!pde) |
| 2663 | goto out_mcfilter; |
| 2664 | return 0; |
| 2665 | |
| 2666 | out_mcfilter: |
| 2667 | proc_net_remove(net, "igmp"); |
| 2668 | out_igmp: |
| 2669 | return -ENOMEM; |
| 2670 | } |
| 2671 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2672 | static void __net_exit igmp_net_exit(struct net *net) |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2673 | { |
| 2674 | proc_net_remove(net, "mcfilter"); |
| 2675 | proc_net_remove(net, "igmp"); |
| 2676 | } |
| 2677 | |
| 2678 | static struct pernet_operations igmp_net_ops = { |
| 2679 | .init = igmp_net_init, |
| 2680 | .exit = igmp_net_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | }; |
| 2682 | |
| 2683 | int __init igmp_mc_proc_init(void) |
| 2684 | { |
Alexey Dobriyan | 7091e72 | 2008-12-25 16:42:51 -0800 | [diff] [blame] | 2685 | return register_pernet_subsys(&igmp_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | } |
| 2687 | #endif |