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