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