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