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