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