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