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