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