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