blob: f2abc315288839fdb7a401d1d50e9c8d3e9cf991 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/core/ethtool.c - Ethtool ioctl handler
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 *
6 * This file is where we call all the ethtool_ops commands to get
Matthew Wilcox61a44b92007-07-31 14:00:02 -07007 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Arnd Bergmanndd98d282021-07-22 16:28:59 +020010#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/ethtool.h>
16#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000017#include <linux/net_tstamp.h>
18#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000019#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070020#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070021#include <linux/vmalloc.h>
Russell Kinge679c9c2018-03-28 15:44:16 -070022#include <linux/sfp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010024#include <linux/rtnetlink.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080026#include <linux/net.h>
Heiner Kallweitf32a2132021-08-01 12:36:48 +020027#include <linux/pm_runtime.h>
Jakub Kicinskiddb6e992019-01-31 10:50:47 -080028#include <net/devlink.h>
Magnus Karlssona71506a2020-05-20 21:20:51 +020029#include <net/xdp_sock_drv.h>
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +010030#include <net/flow_offload.h>
Michal Kubecek73286732019-12-27 15:56:03 +010031#include <linux/ethtool_netlink.h>
Leon Romanovsky1c790312020-04-19 17:18:47 +030032#include <generated/utsrelease.h>
Michal Kubecekd44e1312019-12-11 10:58:29 +010033#include "common.h"
34
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090035/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * Some useful ethtool_ops methods that're device independent.
37 * If we find that all drivers want to do the same thing here,
38 * we can turn these into dev_() function calls.
39 */
40
41u32 ethtool_op_get_link(struct net_device *dev)
42{
43 return netif_carrier_ok(dev) ? 1 : 0;
44}
chavey97f8aef2010-04-07 21:54:42 -070045EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Richard Cochran02eacbd2012-04-03 22:59:22 +000047int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
48{
49 info->so_timestamping =
50 SOF_TIMESTAMPING_TX_SOFTWARE |
51 SOF_TIMESTAMPING_RX_SOFTWARE |
52 SOF_TIMESTAMPING_SOFTWARE;
53 info->phc_index = -1;
54 return 0;
55}
56EXPORT_SYMBOL(ethtool_op_get_ts_info);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* Handlers for each ethtool command */
59
Michał Mirosław5455c692011-02-15 16:59:17 +000060static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
61{
62 struct ethtool_gfeatures cmd = {
63 .cmd = ETHTOOL_GFEATURES,
64 .size = ETHTOOL_DEV_FEATURE_WORDS,
65 };
Michał Mirosław475414f2011-11-15 15:29:55 +000066 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +000067 u32 __user *sizeaddr;
68 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +000069 int i;
70
71 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +000072 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +000073
74 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +000075 features[i].available = (u32)(dev->hw_features >> (32 * i));
76 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
77 features[i].active = (u32)(dev->features >> (32 * i));
78 features[i].never_changed =
79 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +000080 }
Michał Mirosław5455c692011-02-15 16:59:17 +000081
82 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
83 if (get_user(copy_size, sizeaddr))
84 return -EFAULT;
85
86 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
87 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
88
89 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
90 return -EFAULT;
91 useraddr += sizeof(cmd);
92 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
93 return -EFAULT;
94
95 return 0;
96}
97
98static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
99{
100 struct ethtool_sfeatures cmd;
101 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000102 netdev_features_t wanted = 0, valid = 0;
103 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000104
105 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
106 return -EFAULT;
107 useraddr += sizeof(cmd);
108
109 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
110 return -EINVAL;
111
112 if (copy_from_user(features, useraddr, sizeof(features)))
113 return -EFAULT;
114
Michał Mirosław475414f2011-11-15 15:29:55 +0000115 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000116 valid |= (netdev_features_t)features[i].valid << (32 * i);
117 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000118 }
119
120 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000121 return -EINVAL;
122
Michał Mirosław475414f2011-11-15 15:29:55 +0000123 if (valid & ~dev->hw_features) {
124 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000125 ret |= ETHTOOL_F_UNSUPPORTED;
126 }
127
Michał Mirosław475414f2011-11-15 15:29:55 +0000128 dev->wanted_features &= ~valid;
129 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700130 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000131
Michał Mirosław475414f2011-11-15 15:29:55 +0000132 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000133 ret |= ETHTOOL_F_WISH;
134
135 return ret;
136}
137
Michał Mirosław340ae162011-02-15 16:59:16 +0000138static int __ethtool_get_sset_count(struct net_device *dev, int sset)
139{
Florian Fainelli17809512020-07-08 09:46:25 -0700140 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
Michał Mirosław340ae162011-02-15 16:59:16 +0000141 const struct ethtool_ops *ops = dev->ethtool_ops;
142
Michał Mirosław5455c692011-02-15 16:59:17 +0000143 if (sset == ETH_SS_FEATURES)
144 return ARRAY_SIZE(netdev_features_strings);
145
Eyal Perry892311f2014-12-02 18:12:10 +0200146 if (sset == ETH_SS_RSS_HASH_FUNCS)
147 return ARRAY_SIZE(rss_hash_func_strings);
148
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300149 if (sset == ETH_SS_TUNABLES)
150 return ARRAY_SIZE(tunable_strings);
151
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100152 if (sset == ETH_SS_PHY_TUNABLES)
153 return ARRAY_SIZE(phy_tunable_strings);
154
Florian Fainelli99943382018-04-25 12:12:48 -0700155 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
Florian Fainelli17809512020-07-08 09:46:25 -0700156 !ops->get_ethtool_phy_stats &&
157 phy_ops && phy_ops->get_sset_count)
158 return phy_ops->get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100159
Michal Kubecek428c1222019-12-11 10:58:34 +0100160 if (sset == ETH_SS_LINK_MODES)
161 return __ETHTOOL_LINK_MODE_MASK_NBITS;
162
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000163 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000164 return ops->get_sset_count(dev, sset);
165 else
166 return -EOPNOTSUPP;
167}
168
169static void __ethtool_get_strings(struct net_device *dev,
170 u32 stringset, u8 *data)
171{
Florian Fainelli17809512020-07-08 09:46:25 -0700172 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
Michał Mirosław340ae162011-02-15 16:59:16 +0000173 const struct ethtool_ops *ops = dev->ethtool_ops;
174
Michał Mirosław5455c692011-02-15 16:59:17 +0000175 if (stringset == ETH_SS_FEATURES)
176 memcpy(data, netdev_features_strings,
177 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200178 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
179 memcpy(data, rss_hash_func_strings,
180 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300181 else if (stringset == ETH_SS_TUNABLES)
182 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100183 else if (stringset == ETH_SS_PHY_TUNABLES)
184 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Florian Fainelli99943382018-04-25 12:12:48 -0700185 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
Florian Fainelli17809512020-07-08 09:46:25 -0700186 !ops->get_ethtool_phy_stats && phy_ops &&
187 phy_ops->get_strings)
188 phy_ops->get_strings(dev->phydev, data);
Michal Kubecek428c1222019-12-11 10:58:34 +0100189 else if (stringset == ETH_SS_LINK_MODES)
190 memcpy(data, link_mode_names,
191 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
Florian Fainelli99943382018-04-25 12:12:48 -0700192 else
Michał Mirosław5455c692011-02-15 16:59:17 +0000193 /* ops->get_strings is valid because checked earlier */
194 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000195}
196
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000197static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000198{
199 /* feature masks of legacy discrete ethtool ops */
200
201 switch (eth_cmd) {
202 case ETHTOOL_GTXCSUM:
203 case ETHTOOL_STXCSUM:
Vladyslav Tarasiuk9d648fb2020-03-24 13:57:08 +0200204 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
Michal Kubecekf70bb062020-03-12 21:07:43 +0100205 NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000206 case ETHTOOL_GRXCSUM:
207 case ETHTOOL_SRXCSUM:
208 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000209 case ETHTOOL_GSG:
210 case ETHTOOL_SSG:
Michal Kubecekf70bb062020-03-12 21:07:43 +0100211 return NETIF_F_SG | NETIF_F_FRAGLIST;
Michał Mirosław0a417702011-02-15 16:59:17 +0000212 case ETHTOOL_GTSO:
213 case ETHTOOL_STSO:
214 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000215 case ETHTOOL_GGSO:
216 case ETHTOOL_SGSO:
217 return NETIF_F_GSO;
218 case ETHTOOL_GGRO:
219 case ETHTOOL_SGRO:
220 return NETIF_F_GRO;
221 default:
222 BUG();
223 }
224}
225
Michał Mirosław0a417702011-02-15 16:59:17 +0000226static int ethtool_get_one_feature(struct net_device *dev,
227 char __user *useraddr, u32 ethcmd)
228{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000229 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000230 struct ethtool_value edata = {
231 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000232 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000233 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000234
Michał Mirosław0a417702011-02-15 16:59:17 +0000235 if (copy_to_user(useraddr, &edata, sizeof(edata)))
236 return -EFAULT;
237 return 0;
238}
239
Michał Mirosław0a417702011-02-15 16:59:17 +0000240static int ethtool_set_one_feature(struct net_device *dev,
241 void __user *useraddr, u32 ethcmd)
242{
243 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000244 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000245
246 if (copy_from_user(&edata, useraddr, sizeof(edata)))
247 return -EFAULT;
248
Michał Mirosław86794882011-02-15 16:59:17 +0000249 mask = ethtool_get_feature_mask(ethcmd);
250 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000251 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000252 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000253
254 if (edata.data)
255 dev->wanted_features |= mask;
256 else
257 dev->wanted_features &= ~mask;
258
259 __netdev_update_features(dev);
260
261 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000262}
263
Michał Mirosław02b3a552011-11-15 15:29:55 +0000264#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
265 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000266#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
267 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
268 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000269
270static u32 __ethtool_get_flags(struct net_device *dev)
271{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000272 u32 flags = 0;
273
Dragos Foianu8a731252013-07-13 14:43:00 +0100274 if (dev->features & NETIF_F_LRO)
275 flags |= ETH_FLAG_LRO;
276 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
277 flags |= ETH_FLAG_RXVLAN;
278 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
279 flags |= ETH_FLAG_TXVLAN;
280 if (dev->features & NETIF_F_NTUPLE)
281 flags |= ETH_FLAG_NTUPLE;
282 if (dev->features & NETIF_F_RXHASH)
283 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000284
285 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000286}
287
288static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000289{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000290 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000291
Michał Mirosław02b3a552011-11-15 15:29:55 +0000292 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000293 return -EINVAL;
294
Dragos Foianu8a731252013-07-13 14:43:00 +0100295 if (data & ETH_FLAG_LRO)
296 features |= NETIF_F_LRO;
297 if (data & ETH_FLAG_RXVLAN)
298 features |= NETIF_F_HW_VLAN_CTAG_RX;
299 if (data & ETH_FLAG_TXVLAN)
300 features |= NETIF_F_HW_VLAN_CTAG_TX;
301 if (data & ETH_FLAG_NTUPLE)
302 features |= NETIF_F_NTUPLE;
303 if (data & ETH_FLAG_RXHASH)
304 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000305
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000306 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000307 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000308 if (changed & ~dev->hw_features)
309 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
310
311 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000312 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000313
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700314 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000315
316 return 0;
317}
318
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700319/* Given two link masks, AND them together and save the result in dst. */
320void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
321 struct ethtool_link_ksettings *src)
322{
323 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
324 unsigned int idx = 0;
325
326 for (; idx < size; idx++) {
327 dst->link_modes.supported[idx] &=
328 src->link_modes.supported[idx];
329 dst->link_modes.advertising[idx] &=
330 src->link_modes.advertising[idx];
331 }
332}
333EXPORT_SYMBOL(ethtool_intersect_link_masks);
334
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200335void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
336 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800337{
338 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
339 dst[0] = legacy_u32;
340}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200341EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800342
343/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200344bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
345 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800346{
347 bool retval = true;
348
349 /* TODO: following test will soon always be true */
350 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
351 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
352
353 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
354 bitmap_fill(ext, 32);
355 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
356 if (bitmap_intersects(ext, src,
357 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
358 /* src mask goes beyond bit 31 */
359 retval = false;
360 }
361 }
362 *legacy_u32 = src[0];
363 return retval;
364}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200365EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800366
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800367/* return false if ksettings link modes had higher bits
368 * set. legacy_settings always updated (best effort)
369 */
370static bool
371convert_link_ksettings_to_legacy_settings(
372 struct ethtool_cmd *legacy_settings,
373 const struct ethtool_link_ksettings *link_ksettings)
374{
375 bool retval = true;
376
377 memset(legacy_settings, 0, sizeof(*legacy_settings));
378 /* this also clears the deprecated fields in legacy structure:
379 * __u8 transceiver;
380 * __u32 maxtxpkt;
381 * __u32 maxrxpkt;
382 */
383
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200384 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800385 &legacy_settings->supported,
386 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200387 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800388 &legacy_settings->advertising,
389 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200390 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800391 &legacy_settings->lp_advertising,
392 link_ksettings->link_modes.lp_advertising);
393 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
394 legacy_settings->duplex
395 = link_ksettings->base.duplex;
396 legacy_settings->port
397 = link_ksettings->base.port;
398 legacy_settings->phy_address
399 = link_ksettings->base.phy_address;
400 legacy_settings->autoneg
401 = link_ksettings->base.autoneg;
402 legacy_settings->mdio_support
403 = link_ksettings->base.mdio_support;
404 legacy_settings->eth_tp_mdix
405 = link_ksettings->base.eth_tp_mdix;
406 legacy_settings->eth_tp_mdix_ctrl
407 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700408 legacy_settings->transceiver
409 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800410 return retval;
411}
412
413/* number of 32-bit words to store the user's link mode bitmaps */
414#define __ETHTOOL_LINK_MODE_MASK_NU32 \
415 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
416
417/* layout of the struct passed from/to userland */
418struct ethtool_link_usettings {
419 struct ethtool_link_settings base;
420 struct {
421 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
422 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
423 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
424 } link_modes;
425};
426
Michal Kubecek9b300492018-08-28 19:56:58 +0200427/* Internal kernel helper to query a device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800428int __ethtool_get_link_ksettings(struct net_device *dev,
429 struct ethtool_link_ksettings *link_ksettings)
430{
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800431 ASSERT_RTNL();
432
Michal Kubecek9b300492018-08-28 19:56:58 +0200433 if (!dev->ethtool_ops->get_link_ksettings)
David Decotigny3237fc62016-02-24 10:58:11 -0800434 return -EOPNOTSUPP;
435
Michal Kubecek9b300492018-08-28 19:56:58 +0200436 memset(link_ksettings, 0, sizeof(*link_ksettings));
Danielle Ratsona975d7d2021-04-07 13:06:51 +0300437 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800438}
439EXPORT_SYMBOL(__ethtool_get_link_ksettings);
440
441/* convert ethtool_link_usettings in user space to a kernel internal
442 * ethtool_link_ksettings. return 0 on success, errno on error.
443 */
444static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
445 const void __user *from)
446{
447 struct ethtool_link_usettings link_usettings;
448
449 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
450 return -EFAULT;
451
452 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800453 bitmap_from_arr32(to->link_modes.supported,
454 link_usettings.link_modes.supported,
455 __ETHTOOL_LINK_MODE_MASK_NBITS);
456 bitmap_from_arr32(to->link_modes.advertising,
457 link_usettings.link_modes.advertising,
458 __ETHTOOL_LINK_MODE_MASK_NBITS);
459 bitmap_from_arr32(to->link_modes.lp_advertising,
460 link_usettings.link_modes.lp_advertising,
461 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800462
463 return 0;
464}
465
Cris Forno70ae1e12020-02-28 14:12:04 -0600466/* Check if the user is trying to change anything besides speed/duplex */
467bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
468{
469 struct ethtool_link_settings base2 = {};
470
471 base2.speed = cmd->base.speed;
472 base2.port = PORT_OTHER;
473 base2.duplex = cmd->base.duplex;
474 base2.cmd = cmd->base.cmd;
475 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
476
477 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
478 bitmap_empty(cmd->link_modes.supported,
479 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
480 bitmap_empty(cmd->link_modes.lp_advertising,
481 __ETHTOOL_LINK_MODE_MASK_NBITS);
482}
483
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800484/* convert a kernel internal ethtool_link_ksettings to
485 * ethtool_link_usettings in user space. return 0 on success, errno on
486 * error.
487 */
488static int
489store_link_ksettings_for_user(void __user *to,
490 const struct ethtool_link_ksettings *from)
491{
492 struct ethtool_link_usettings link_usettings;
493
Gustavo A. R. Silvac1d9e342021-04-16 15:15:40 -0500494 memcpy(&link_usettings, from, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800495 bitmap_to_arr32(link_usettings.link_modes.supported,
496 from->link_modes.supported,
497 __ETHTOOL_LINK_MODE_MASK_NBITS);
498 bitmap_to_arr32(link_usettings.link_modes.advertising,
499 from->link_modes.advertising,
500 __ETHTOOL_LINK_MODE_MASK_NBITS);
501 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
502 from->link_modes.lp_advertising,
503 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800504
505 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
506 return -EFAULT;
507
508 return 0;
509}
510
Michal Kubecek9b300492018-08-28 19:56:58 +0200511/* Query device for its ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800512static int ethtool_get_link_ksettings(struct net_device *dev,
513 void __user *useraddr)
514{
515 int err = 0;
516 struct ethtool_link_ksettings link_ksettings;
517
518 ASSERT_RTNL();
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800519 if (!dev->ethtool_ops->get_link_ksettings)
520 return -EOPNOTSUPP;
521
522 /* handle bitmap nbits handshake */
523 if (copy_from_user(&link_ksettings.base, useraddr,
524 sizeof(link_ksettings.base)))
525 return -EFAULT;
526
527 if (__ETHTOOL_LINK_MODE_MASK_NU32
528 != link_ksettings.base.link_mode_masks_nwords) {
529 /* wrong link mode nbits requested */
530 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000531 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800532 /* send back number of words required as negative val */
533 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
534 "need too many bits for link modes!");
535 link_ksettings.base.link_mode_masks_nwords
536 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
537
538 /* copy the base fields back to user, not the link
539 * mode bitmaps
540 */
541 if (copy_to_user(useraddr, &link_ksettings.base,
542 sizeof(link_ksettings.base)))
543 return -EFAULT;
544
545 return 0;
546 }
547
548 /* handshake successful: user/kernel agree on
549 * link_mode_masks_nwords
550 */
551
552 memset(&link_ksettings, 0, sizeof(link_ksettings));
553 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
554 if (err < 0)
555 return err;
556
557 /* make sure we tell the right values to user */
558 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
559 link_ksettings.base.link_mode_masks_nwords
560 = __ETHTOOL_LINK_MODE_MASK_NU32;
Oleksij Rempelbdbdac72020-05-05 08:35:05 +0200561 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
562 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800563
564 return store_link_ksettings_for_user(useraddr, &link_ksettings);
565}
566
Michal Kubecek9b300492018-08-28 19:56:58 +0200567/* Update device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800568static int ethtool_set_link_ksettings(struct net_device *dev,
569 void __user *useraddr)
570{
571 int err;
572 struct ethtool_link_ksettings link_ksettings;
573
574 ASSERT_RTNL();
575
576 if (!dev->ethtool_ops->set_link_ksettings)
577 return -EOPNOTSUPP;
578
579 /* make sure nbits field has expected value */
580 if (copy_from_user(&link_ksettings.base, useraddr,
581 sizeof(link_ksettings.base)))
582 return -EFAULT;
583
584 if (__ETHTOOL_LINK_MODE_MASK_NU32
585 != link_ksettings.base.link_mode_masks_nwords)
586 return -EINVAL;
587
588 /* copy the whole structure, now that we know it has expected
589 * format
590 */
591 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
592 if (err)
593 return err;
594
595 /* re-check nwords field, just in case */
596 if (__ETHTOOL_LINK_MODE_MASK_NU32
597 != link_ksettings.base.link_mode_masks_nwords)
598 return -EINVAL;
599
Oleksij Rempelbdbdac72020-05-05 08:35:05 +0200600 if (link_ksettings.base.master_slave_cfg ||
601 link_ksettings.base.master_slave_state)
602 return -EINVAL;
603
Michal Kubecek73286732019-12-27 15:56:03 +0100604 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100605 if (err >= 0) {
Michal Kubecek73286732019-12-27 15:56:03 +0100606 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100607 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
608 }
Michal Kubecek73286732019-12-27 15:56:03 +0100609 return err;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800610}
611
Cris Forno70ae1e12020-02-28 14:12:04 -0600612int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
613 const struct ethtool_link_ksettings *cmd,
614 u32 *dev_speed, u8 *dev_duplex)
615{
616 u32 speed;
617 u8 duplex;
618
619 speed = cmd->base.speed;
620 duplex = cmd->base.duplex;
621 /* don't allow custom speed and duplex */
622 if (!ethtool_validate_speed(speed) ||
623 !ethtool_validate_duplex(duplex) ||
624 !ethtool_virtdev_validate_cmd(cmd))
625 return -EINVAL;
626 *dev_speed = speed;
627 *dev_duplex = duplex;
628
629 return 0;
630}
631EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
632
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800633/* Query device for its ethtool_cmd settings.
634 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200635 * Backward compatibility note: for compatibility with legacy ethtool, this is
636 * now implemented via get_link_ksettings. When driver reports higher link mode
637 * bits, a kernel warning is logged once (with name of 1st driver/device) to
638 * recommend user to upgrade ethtool, but the command is successful (only the
639 * lower link mode bits reported back to user). Deprecated fields from
640 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800641 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000642static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
643{
Michal Kubecek9b300492018-08-28 19:56:58 +0200644 struct ethtool_link_ksettings link_ksettings;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000645 struct ethtool_cmd cmd;
Michal Kubecek9b300492018-08-28 19:56:58 +0200646 int err;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000647
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800648 ASSERT_RTNL();
Michal Kubecek9b300492018-08-28 19:56:58 +0200649 if (!dev->ethtool_ops->get_link_ksettings)
650 return -EOPNOTSUPP;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800651
Michal Kubecek9b300492018-08-28 19:56:58 +0200652 memset(&link_ksettings, 0, sizeof(link_ksettings));
653 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
654 if (err < 0)
655 return err;
656 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800657
Michal Kubecek9b300492018-08-28 19:56:58 +0200658 /* send a sensible cmd tag back to user */
659 cmd.cmd = ETHTOOL_GSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
662 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665}
666
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800667/* Update device link settings with given ethtool_cmd.
668 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200669 * Backward compatibility note: for compatibility with legacy ethtool, this is
670 * now always implemented via set_link_settings. When user's request updates
671 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
672 * warning is logged once (with name of 1st driver/device) to recommend user to
673 * upgrade ethtool, and the request is rejected.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800674 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
676{
Michal Kubecek9b300492018-08-28 19:56:58 +0200677 struct ethtool_link_ksettings link_ksettings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct ethtool_cmd cmd;
Michal Kubecek73286732019-12-27 15:56:03 +0100679 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800681 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
684 return -EFAULT;
Michal Kubecek9b300492018-08-28 19:56:58 +0200685 if (!dev->ethtool_ops->set_link_ksettings)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800686 return -EOPNOTSUPP;
687
Michal Kubecek9b300492018-08-28 19:56:58 +0200688 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
689 return -EINVAL;
690 link_ksettings.base.link_mode_masks_nwords =
691 __ETHTOOL_LINK_MODE_MASK_NU32;
Michal Kubecek73286732019-12-27 15:56:03 +0100692 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100693 if (ret >= 0) {
Michal Kubecek73286732019-12-27 15:56:03 +0100694 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100695 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
696 }
Michal Kubecek73286732019-12-27 15:56:03 +0100697 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
chavey97f8aef2010-04-07 21:54:42 -0700700static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
701 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700704 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 memset(&info, 0, sizeof(info));
707 info.cmd = ETHTOOL_GDRVINFO;
Leon Romanovsky6a7e25c2020-01-27 09:20:28 +0200708 strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000709 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700710 ops->get_drvinfo(dev, &info);
711 } else if (dev->dev.parent && dev->dev.parent->driver) {
712 strlcpy(info.bus_info, dev_name(dev->dev.parent),
713 sizeof(info.bus_info));
714 strlcpy(info.driver, dev->dev.parent->driver->name,
715 sizeof(info.driver));
716 } else {
717 return -EOPNOTSUPP;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jeff Garzik723b2f52010-03-03 22:51:50 +0000720 /*
721 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000722 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000723 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000724 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700725 int rc;
726
727 rc = ops->get_sset_count(dev, ETH_SS_TEST);
728 if (rc >= 0)
729 info.testinfo_len = rc;
730 rc = ops->get_sset_count(dev, ETH_SS_STATS);
731 if (rc >= 0)
732 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700733 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
734 if (rc >= 0)
735 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700736 }
Yunsheng Linf9fc54d2018-12-26 19:51:46 +0800737 if (ops->get_regs_len) {
738 int ret = ops->get_regs_len(dev);
739
740 if (ret > 0)
741 info.regdump_len = ret;
742 }
743
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000744 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 info.eedump_len = ops->get_eeprom_len(dev);
746
Jakub Kicinskiddb6e992019-01-31 10:50:47 -0800747 if (!info.fw_version[0])
748 devlink_compat_running_version(dev, info.fw_version,
749 sizeof(info.fw_version));
Jakub Kicinskiddb6e992019-01-31 10:50:47 -0800750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (copy_to_user(useraddr, &info, sizeof(info)))
752 return -EFAULT;
753 return 0;
754}
755
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800756static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700757 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000758{
759 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000760 u64 sset_mask;
761 int i, idx = 0, n_bits = 0, ret, rc;
762 u32 *info_buf = NULL;
763
Jeff Garzik723b2f52010-03-03 22:51:50 +0000764 if (copy_from_user(&info, useraddr, sizeof(info)))
765 return -EFAULT;
766
767 /* store copy of mask, because we zero struct later on */
768 sset_mask = info.sset_mask;
769 if (!sset_mask)
770 return 0;
771
772 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000773 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000774
775 memset(&info, 0, sizeof(info));
776 info.cmd = ETHTOOL_GSSET_INFO;
777
Kees Cook6396bb22018-06-12 14:03:40 -0700778 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000779 if (!info_buf)
780 return -ENOMEM;
781
782 /*
783 * fill return buffer based on input bitmask and successful
784 * get_sset_count return
785 */
786 for (i = 0; i < 64; i++) {
787 if (!(sset_mask & (1ULL << i)))
788 continue;
789
Michał Mirosław340ae162011-02-15 16:59:16 +0000790 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000791 if (rc >= 0) {
792 info.sset_mask |= (1ULL << i);
793 info_buf[idx++] = rc;
794 }
795 }
796
797 ret = -EFAULT;
798 if (copy_to_user(useraddr, &info, sizeof(info)))
799 goto out;
800
801 useraddr += offsetof(struct ethtool_sset_info, data);
802 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
803 goto out;
804
805 ret = 0;
806
807out:
808 kfree(info_buf);
809 return ret;
810}
811
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200812static noinline_for_stack int
813ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
814 const struct compat_ethtool_rxnfc __user *useraddr,
815 size_t size)
816{
817 struct compat_ethtool_rxnfc crxnfc = {};
818
819 /* We expect there to be holes between fs.m_ext and
820 * fs.ring_cookie and at the end of fs, but nowhere else.
821 * On non-x86, no conversion should be needed.
822 */
823 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
824 sizeof(struct compat_ethtool_rxnfc) !=
825 sizeof(struct ethtool_rxnfc));
826 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
827 sizeof(useraddr->fs.m_ext) !=
828 offsetof(struct ethtool_rxnfc, fs.m_ext) +
829 sizeof(rxnfc->fs.m_ext));
830 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
831 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
832 offsetof(struct ethtool_rxnfc, fs.location) -
833 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
834
835 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
836 return -EFAULT;
837
838 *rxnfc = (struct ethtool_rxnfc) {
839 .cmd = crxnfc.cmd,
840 .flow_type = crxnfc.flow_type,
841 .data = crxnfc.data,
842 .fs = {
843 .flow_type = crxnfc.fs.flow_type,
844 .h_u = crxnfc.fs.h_u,
845 .h_ext = crxnfc.fs.h_ext,
846 .m_u = crxnfc.fs.m_u,
847 .m_ext = crxnfc.fs.m_ext,
848 .ring_cookie = crxnfc.fs.ring_cookie,
849 .location = crxnfc.fs.location,
850 },
851 .rule_cnt = crxnfc.rule_cnt,
852 };
853
854 return 0;
855}
856
857static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
858 const void __user *useraddr,
859 size_t size)
860{
861 if (compat_need_64bit_alignment_fixup())
862 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
863
864 if (copy_from_user(rxnfc, useraddr, size))
865 return -EFAULT;
866
867 return 0;
868}
869
870static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
871 const struct ethtool_rxnfc *rxnfc,
872 size_t size, const u32 *rule_buf)
873{
874 struct compat_ethtool_rxnfc crxnfc;
875
876 memset(&crxnfc, 0, sizeof(crxnfc));
877 crxnfc = (struct compat_ethtool_rxnfc) {
878 .cmd = rxnfc->cmd,
879 .flow_type = rxnfc->flow_type,
880 .data = rxnfc->data,
881 .fs = {
882 .flow_type = rxnfc->fs.flow_type,
883 .h_u = rxnfc->fs.h_u,
884 .h_ext = rxnfc->fs.h_ext,
885 .m_u = rxnfc->fs.m_u,
886 .m_ext = rxnfc->fs.m_ext,
887 .ring_cookie = rxnfc->fs.ring_cookie,
888 .location = rxnfc->fs.location,
889 },
890 .rule_cnt = rxnfc->rule_cnt,
891 };
892
893 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
894 return -EFAULT;
895
896 return 0;
897}
898
899static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
900 const struct ethtool_rxnfc *rxnfc,
901 size_t size, const u32 *rule_buf)
902{
903 int ret;
904
905 if (compat_need_64bit_alignment_fixup()) {
906 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
907 rule_buf);
908 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
909 } else {
Saeed Mahameed9b29a162021-07-26 15:15:39 -0700910 ret = copy_to_user(useraddr, rxnfc, size);
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200911 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
912 }
913
914 if (ret)
915 return -EFAULT;
916
917 if (rule_buf) {
918 if (copy_to_user(useraddr, rule_buf,
919 rxnfc->rule_cnt * sizeof(u32)))
920 return -EFAULT;
921 }
922
923 return 0;
924}
925
chavey97f8aef2010-04-07 21:54:42 -0700926static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000927 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700928{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000929 struct ethtool_rxnfc info;
930 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000931 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700932
Santwona Behera59089d82009-02-20 00:58:13 -0800933 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700934 return -EOPNOTSUPP;
935
Ben Hutchingsbf988432010-06-28 08:45:58 +0000936 /* struct ethtool_rxnfc was originally defined for
937 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
938 * members. User-space might still be using that
939 * definition. */
940 if (cmd == ETHTOOL_SRXFH)
941 info_size = (offsetof(struct ethtool_rxnfc, data) +
942 sizeof(info.data));
943
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200944 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700945 return -EFAULT;
946
Ben Hutchings55664f32012-01-03 12:04:51 +0000947 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
948 if (rc)
949 return rc;
950
951 if (cmd == ETHTOOL_SRXCLSRLINS &&
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200952 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
Ben Hutchings55664f32012-01-03 12:04:51 +0000953 return -EFAULT;
954
955 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700956}
957
chavey97f8aef2010-04-07 21:54:42 -0700958static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000959 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700960{
961 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000962 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800963 const struct ethtool_ops *ops = dev->ethtool_ops;
964 int ret;
965 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700966
Santwona Behera59089d82009-02-20 00:58:13 -0800967 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700968 return -EOPNOTSUPP;
969
Ben Hutchingsbf988432010-06-28 08:45:58 +0000970 /* struct ethtool_rxnfc was originally defined for
971 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
972 * members. User-space might still be using that
973 * definition. */
974 if (cmd == ETHTOOL_GRXFH)
975 info_size = (offsetof(struct ethtool_rxnfc, data) +
976 sizeof(info.data));
977
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200978 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700979 return -EFAULT;
980
Edward Cree84a1d9c42018-03-08 15:45:03 +0000981 /* If FLOW_RSS was requested then user-space must be using the
982 * new definition, as FLOW_RSS is newer.
983 */
984 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
985 info_size = sizeof(info);
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200986 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
Edward Cree84a1d9c42018-03-08 15:45:03 +0000987 return -EFAULT;
Wenwen Wangd656fe42018-04-30 12:31:13 -0500988 /* Since malicious users may modify the original data,
989 * we need to check whether FLOW_RSS is still requested.
990 */
991 if (!(info.flow_type & FLOW_RSS))
992 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +0000993 }
994
Wenwen Wang2bb32072018-10-09 08:15:38 -0500995 if (info.cmd != cmd)
996 return -EINVAL;
997
Santwona Behera59089d82009-02-20 00:58:13 -0800998 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
999 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001000 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cook6396bb22018-06-12 14:03:40 -07001001 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001002 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001003 if (!rule_buf)
1004 return -ENOMEM;
1005 }
1006 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001007
Santwona Behera59089d82009-02-20 00:58:13 -08001008 ret = ops->get_rxnfc(dev, &info, rule_buf);
1009 if (ret < 0)
1010 goto err_out;
1011
Arnd Bergmanndd98d282021-07-22 16:28:59 +02001012 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001013err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001014 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001015
1016 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001017}
1018
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301019static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1020 struct ethtool_rxnfc *rx_rings,
1021 u32 size)
1022{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001023 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301024
1025 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001026 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301027
1028 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001029 for (i = 0; i < size; i++)
1030 if (indir[i] >= rx_rings->data)
1031 return -EINVAL;
1032
1033 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301034}
1035
Kim Jonesba905f52016-02-02 03:51:16 +00001036u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001037
1038void netdev_rss_key_fill(void *buffer, size_t len)
1039{
1040 BUG_ON(len > sizeof(netdev_rss_key));
1041 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1042 memcpy(buffer, netdev_rss_key, len);
1043}
1044EXPORT_SYMBOL(netdev_rss_key_fill);
1045
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001046static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1047 void __user *useraddr)
1048{
Ben Hutchings7850f632011-12-15 13:55:01 +00001049 u32 user_size, dev_size;
1050 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001051 int ret;
1052
Ben Hutchings7850f632011-12-15 13:55:01 +00001053 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001054 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001055 return -EOPNOTSUPP;
1056 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1057 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001058 return -EOPNOTSUPP;
1059
Ben Hutchings7850f632011-12-15 13:55:01 +00001060 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001061 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001062 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001063 return -EFAULT;
1064
Ben Hutchings7850f632011-12-15 13:55:01 +00001065 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1066 &dev_size, sizeof(dev_size)))
1067 return -EFAULT;
1068
1069 /* If the user buffer size is 0, this is just a query for the
1070 * device table size. Otherwise, if it's smaller than the
1071 * device table size it's an error.
1072 */
1073 if (user_size < dev_size)
1074 return user_size == 0 ? 0 : -EINVAL;
1075
1076 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001077 if (!indir)
1078 return -ENOMEM;
1079
Eyal Perry892311f2014-12-02 18:12:10 +02001080 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001081 if (ret)
1082 goto out;
1083
Ben Hutchings7850f632011-12-15 13:55:01 +00001084 if (copy_to_user(useraddr +
1085 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1086 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001087 ret = -EFAULT;
1088
1089out:
1090 kfree(indir);
1091 return ret;
1092}
1093
1094static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1095 void __user *useraddr)
1096{
Ben Hutchings7850f632011-12-15 13:55:01 +00001097 struct ethtool_rxnfc rx_rings;
1098 u32 user_size, dev_size, i;
1099 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001100 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001101 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301102 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001103
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001104 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001105 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001106 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001107
1108 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001109 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001110 return -EOPNOTSUPP;
1111
Ben Hutchings7850f632011-12-15 13:55:01 +00001112 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001113 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001114 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001115 return -EFAULT;
1116
Ben Hutchings278bc422011-12-15 13:56:49 +00001117 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001118 return -EINVAL;
1119
1120 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001121 if (!indir)
1122 return -ENOMEM;
1123
Ben Hutchings7850f632011-12-15 13:55:01 +00001124 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001125 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001126 if (ret)
1127 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001128
1129 if (user_size == 0) {
1130 for (i = 0; i < dev_size; i++)
1131 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1132 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301133 ret = ethtool_copy_validate_indir(indir,
1134 useraddr + ringidx_offset,
1135 &rx_rings,
1136 dev_size);
1137 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001138 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001139 }
1140
Eyal Perry892311f2014-12-02 18:12:10 +02001141 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001142 if (ret)
1143 goto out;
1144
1145 /* indicate whether rxfh was set to default */
1146 if (user_size == 0)
1147 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1148 else
1149 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001150
1151out:
1152 kfree(indir);
1153 return ret;
1154}
1155
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301156static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1157 void __user *useraddr)
1158{
1159 int ret;
1160 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001161 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301162 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001163 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301164 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001165 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301166 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001167 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301168 u8 *hkey = NULL;
1169 u8 *rss_config;
1170
Eyal Perry892311f2014-12-02 18:12:10 +02001171 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301172 return -EOPNOTSUPP;
1173
1174 if (ops->get_rxfh_indir_size)
1175 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301176 if (ops->get_rxfh_key_size)
1177 dev_key_size = ops->get_rxfh_key_size(dev);
1178
Ben Hutchingsf062a382014-05-15 16:28:07 +01001179 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301180 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001181 user_indir_size = rxfh.indir_size;
1182 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301183
Ben Hutchingsf062a382014-05-15 16:28:07 +01001184 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001185 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001186 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001187 /* Most drivers don't handle rss_context, check it's 0 as well */
1188 if (rxfh.rss_context && !ops->get_rxfh_context)
1189 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001190
1191 rxfh.indir_size = dev_indir_size;
1192 rxfh.key_size = dev_key_size;
1193 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301194 return -EFAULT;
1195
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301196 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1197 (user_key_size && (user_key_size != dev_key_size)))
1198 return -EINVAL;
1199
1200 indir_bytes = user_indir_size * sizeof(indir[0]);
1201 total_size = indir_bytes + user_key_size;
1202 rss_config = kzalloc(total_size, GFP_USER);
1203 if (!rss_config)
1204 return -ENOMEM;
1205
1206 if (user_indir_size)
1207 indir = (u32 *)rss_config;
1208
1209 if (user_key_size)
1210 hkey = rss_config + indir_bytes;
1211
Edward Cree84a1d9c42018-03-08 15:45:03 +00001212 if (rxfh.rss_context)
1213 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1214 &dev_hfunc,
1215 rxfh.rss_context);
1216 else
1217 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001218 if (ret)
1219 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301220
Eyal Perry892311f2014-12-02 18:12:10 +02001221 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1222 &dev_hfunc, sizeof(rxfh.hfunc))) {
1223 ret = -EFAULT;
1224 } else if (copy_to_user(useraddr +
1225 offsetof(struct ethtool_rxfh, rss_config[0]),
1226 rss_config, total_size)) {
1227 ret = -EFAULT;
1228 }
1229out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301230 kfree(rss_config);
1231
1232 return ret;
1233}
1234
1235static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1236 void __user *useraddr)
1237{
1238 int ret;
1239 const struct ethtool_ops *ops = dev->ethtool_ops;
1240 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001241 struct ethtool_rxfh rxfh;
1242 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301243 u32 *indir = NULL, indir_bytes = 0;
1244 u8 *hkey = NULL;
1245 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301246 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001247 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301248
Eyal Perry892311f2014-12-02 18:12:10 +02001249 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301250 return -EOPNOTSUPP;
1251
1252 if (ops->get_rxfh_indir_size)
1253 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301254 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001255 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301256
Ben Hutchingsf062a382014-05-15 16:28:07 +01001257 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301258 return -EFAULT;
1259
Ben Hutchingsf062a382014-05-15 16:28:07 +01001260 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001261 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001262 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001263 /* Most drivers don't handle rss_context, check it's 0 as well */
1264 if (rxfh.rss_context && !ops->set_rxfh_context)
1265 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001266
Eyal Perry892311f2014-12-02 18:12:10 +02001267 /* If either indir, hash key or function is valid, proceed further.
1268 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301269 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001270 if ((rxfh.indir_size &&
1271 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1272 rxfh.indir_size != dev_indir_size) ||
1273 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1274 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001275 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301276 return -EINVAL;
1277
Ben Hutchingsf062a382014-05-15 16:28:07 +01001278 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301279 indir_bytes = dev_indir_size * sizeof(indir[0]);
1280
Ben Hutchingsf062a382014-05-15 16:28:07 +01001281 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301282 if (!rss_config)
1283 return -ENOMEM;
1284
1285 rx_rings.cmd = ETHTOOL_GRXRINGS;
1286 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1287 if (ret)
1288 goto out;
1289
Edward Cree84a1d9c42018-03-08 15:45:03 +00001290 /* rxfh.indir_size == 0 means reset the indir table to default (master
1291 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001292 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301293 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001294 if (rxfh.indir_size &&
1295 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301296 indir = (u32 *)rss_config;
1297 ret = ethtool_copy_validate_indir(indir,
1298 useraddr + rss_cfg_offset,
1299 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001300 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301301 if (ret)
1302 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001303 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001304 if (rxfh.rss_context == 0) {
1305 indir = (u32 *)rss_config;
1306 for (i = 0; i < dev_indir_size; i++)
1307 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1308 } else {
1309 delete = true;
1310 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301311 }
1312
Ben Hutchingsf062a382014-05-15 16:28:07 +01001313 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301314 hkey = rss_config + indir_bytes;
1315 if (copy_from_user(hkey,
1316 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001317 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301318 ret = -EFAULT;
1319 goto out;
1320 }
1321 }
1322
Edward Cree84a1d9c42018-03-08 15:45:03 +00001323 if (rxfh.rss_context)
1324 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1325 &rxfh.rss_context, delete);
1326 else
1327 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001328 if (ret)
1329 goto out;
1330
Edward Cree84a1d9c42018-03-08 15:45:03 +00001331 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1332 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1333 ret = -EFAULT;
1334
1335 if (!rxfh.rss_context) {
1336 /* indicate whether rxfh was set to default */
1337 if (rxfh.indir_size == 0)
1338 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1339 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1340 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1341 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301342
1343out:
1344 kfree(rss_config);
1345 return ret;
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1349{
1350 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001351 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 void *regbuf;
1353 int reglen, ret;
1354
1355 if (!ops->get_regs || !ops->get_regs_len)
1356 return -EOPNOTSUPP;
1357
1358 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1359 return -EFAULT;
1360
1361 reglen = ops->get_regs_len(dev);
Yunsheng Linf9fc54d2018-12-26 19:51:46 +08001362 if (reglen <= 0)
1363 return reglen;
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (regs.len > reglen)
1366 regs.len = reglen;
1367
Dan Carpenteref76c772019-02-01 11:24:06 +03001368 regbuf = vzalloc(reglen);
1369 if (!regbuf)
1370 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Vivien Didelot0ee4e762019-06-03 16:57:13 -04001372 if (regs.len < reglen)
1373 reglen = regs.len;
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ops->get_regs(dev, &regs, regbuf);
1376
1377 ret = -EFAULT;
1378 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1379 goto out;
1380 useraddr += offsetof(struct ethtool_regs, data);
Vivien Didelot0ee4e762019-06-03 16:57:13 -04001381 if (copy_to_user(useraddr, regbuf, reglen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 goto out;
1383 ret = 0;
1384
1385 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001386 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return ret;
1388}
1389
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001390static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1391{
1392 struct ethtool_value reset;
1393 int ret;
1394
1395 if (!dev->ethtool_ops->reset)
1396 return -EOPNOTSUPP;
1397
1398 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1399 return -EFAULT;
1400
1401 ret = dev->ethtool_ops->reset(dev, &reset.data);
1402 if (ret)
1403 return ret;
1404
1405 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1406 return -EFAULT;
1407 return 0;
1408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1411{
zhanglin5ff223e82019-10-26 15:54:16 +08001412 struct ethtool_wolinfo wol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (!dev->ethtool_ops->get_wol)
1415 return -EOPNOTSUPP;
1416
zhanglin5ff223e82019-10-26 15:54:16 +08001417 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1418 wol.cmd = ETHTOOL_GWOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 dev->ethtool_ops->get_wol(dev, &wol);
1420
1421 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1422 return -EFAULT;
1423 return 0;
1424}
1425
1426static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1427{
1428 struct ethtool_wolinfo wol;
Heiner Kallweit61941142018-09-24 21:58:59 +02001429 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 if (!dev->ethtool_ops->set_wol)
1432 return -EOPNOTSUPP;
1433
1434 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1435 return -EFAULT;
1436
Heiner Kallweit61941142018-09-24 21:58:59 +02001437 ret = dev->ethtool_ops->set_wol(dev, &wol);
1438 if (ret)
1439 return ret;
1440
1441 dev->wol_enabled = !!wol.wolopts;
Michal Kubecek67bffa72020-01-26 23:11:19 +01001442 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
Heiner Kallweit61941142018-09-24 21:58:59 +02001443
1444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001447static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1448{
1449 struct ethtool_eee edata;
1450 int rc;
1451
1452 if (!dev->ethtool_ops->get_eee)
1453 return -EOPNOTSUPP;
1454
1455 memset(&edata, 0, sizeof(struct ethtool_eee));
1456 edata.cmd = ETHTOOL_GEEE;
1457 rc = dev->ethtool_ops->get_eee(dev, &edata);
1458
1459 if (rc)
1460 return rc;
1461
1462 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1463 return -EFAULT;
1464
1465 return 0;
1466}
1467
1468static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1469{
1470 struct ethtool_eee edata;
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +01001471 int ret;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001472
1473 if (!dev->ethtool_ops->set_eee)
1474 return -EOPNOTSUPP;
1475
1476 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1477 return -EFAULT;
1478
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +01001479 ret = dev->ethtool_ops->set_eee(dev, &edata);
1480 if (!ret)
1481 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1482 return ret;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485static int ethtool_nway_reset(struct net_device *dev)
1486{
1487 if (!dev->ethtool_ops->nway_reset)
1488 return -EOPNOTSUPP;
1489
1490 return dev->ethtool_ops->nway_reset(dev);
1491}
1492
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001493static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1494{
1495 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001496 int link = __ethtool_get_link(dev);
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001497
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001498 if (link < 0)
1499 return link;
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001500
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001501 edata.data = link;
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001502 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1503 return -EFAULT;
1504 return 0;
1505}
1506
Ben Hutchings081d0942012-04-12 00:42:12 +00001507static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1508 int (*getter)(struct net_device *,
1509 struct ethtool_eeprom *, u8 *),
1510 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001513 void __user *userbuf = useraddr + sizeof(eeprom);
1514 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001516 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1519 return -EFAULT;
1520
1521 /* Check for wrap and zero */
1522 if (eeprom.offset + eeprom.len <= eeprom.offset)
1523 return -EINVAL;
1524
1525 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001526 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return -EINVAL;
1528
Austin Kim80ec82e2021-06-09 03:34:25 +01001529 data = kzalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (!data)
1531 return -ENOMEM;
1532
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001533 bytes_remaining = eeprom.len;
1534 while (bytes_remaining > 0) {
1535 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Ben Hutchings081d0942012-04-12 00:42:12 +00001537 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001538 if (ret)
1539 break;
1540 if (copy_to_user(userbuf, data, eeprom.len)) {
1541 ret = -EFAULT;
1542 break;
1543 }
1544 userbuf += eeprom.len;
1545 eeprom.offset += eeprom.len;
1546 bytes_remaining -= eeprom.len;
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001549 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1550 eeprom.offset -= eeprom.len;
1551 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1552 ret = -EFAULT;
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 kfree(data);
1555 return ret;
1556}
1557
Ben Hutchings081d0942012-04-12 00:42:12 +00001558static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1559{
1560 const struct ethtool_ops *ops = dev->ethtool_ops;
1561
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001562 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1563 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001564 return -EOPNOTSUPP;
1565
1566 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1567 ops->get_eeprom_len(dev));
1568}
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1571{
1572 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001573 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001574 void __user *userbuf = useraddr + sizeof(eeprom);
1575 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001577 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001579 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1580 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return -EOPNOTSUPP;
1582
1583 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1584 return -EFAULT;
1585
1586 /* Check for wrap and zero */
1587 if (eeprom.offset + eeprom.len <= eeprom.offset)
1588 return -EINVAL;
1589
1590 /* Check for exceeding total eeprom len */
1591 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1592 return -EINVAL;
1593
Austin Kim80ec82e2021-06-09 03:34:25 +01001594 data = kzalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!data)
1596 return -ENOMEM;
1597
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001598 bytes_remaining = eeprom.len;
1599 while (bytes_remaining > 0) {
1600 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001602 if (copy_from_user(data, userbuf, eeprom.len)) {
1603 ret = -EFAULT;
1604 break;
1605 }
1606 ret = ops->set_eeprom(dev, &eeprom, data);
1607 if (ret)
1608 break;
1609 userbuf += eeprom.len;
1610 eeprom.offset += eeprom.len;
1611 bytes_remaining -= eeprom.len;
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 kfree(data);
1615 return ret;
1616}
1617
chavey97f8aef2010-04-07 21:54:42 -07001618static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1619 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Roland Dreier8e557422010-02-11 12:14:23 -08001621 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001622 struct kernel_ethtool_coalesce kernel_coalesce = {};
Heiner Kallweit31610712020-05-23 17:40:25 +02001623 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 if (!dev->ethtool_ops->get_coalesce)
1626 return -EOPNOTSUPP;
1627
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001628 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1629 NULL);
Heiner Kallweit31610712020-05-23 17:40:25 +02001630 if (ret)
1631 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1634 return -EFAULT;
1635 return 0;
1636}
1637
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001638static bool
1639ethtool_set_coalesce_supported(struct net_device *dev,
1640 struct ethtool_coalesce *coalesce)
1641{
1642 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1643 u32 nonzero_params = 0;
1644
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001645 if (coalesce->rx_coalesce_usecs)
1646 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1647 if (coalesce->rx_max_coalesced_frames)
1648 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1649 if (coalesce->rx_coalesce_usecs_irq)
1650 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1651 if (coalesce->rx_max_coalesced_frames_irq)
1652 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1653 if (coalesce->tx_coalesce_usecs)
1654 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1655 if (coalesce->tx_max_coalesced_frames)
1656 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1657 if (coalesce->tx_coalesce_usecs_irq)
1658 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1659 if (coalesce->tx_max_coalesced_frames_irq)
1660 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1661 if (coalesce->stats_block_coalesce_usecs)
1662 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1663 if (coalesce->use_adaptive_rx_coalesce)
1664 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1665 if (coalesce->use_adaptive_tx_coalesce)
1666 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1667 if (coalesce->pkt_rate_low)
1668 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1669 if (coalesce->rx_coalesce_usecs_low)
1670 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1671 if (coalesce->rx_max_coalesced_frames_low)
1672 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1673 if (coalesce->tx_coalesce_usecs_low)
1674 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1675 if (coalesce->tx_max_coalesced_frames_low)
1676 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1677 if (coalesce->pkt_rate_high)
1678 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1679 if (coalesce->rx_coalesce_usecs_high)
1680 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1681 if (coalesce->rx_max_coalesced_frames_high)
1682 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1683 if (coalesce->tx_coalesce_usecs_high)
1684 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1685 if (coalesce->tx_max_coalesced_frames_high)
1686 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1687 if (coalesce->rate_sample_interval)
1688 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1689
1690 return (supported_params & nonzero_params) == nonzero_params;
1691}
1692
chavey97f8aef2010-04-07 21:54:42 -07001693static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1694 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001696 struct kernel_ethtool_coalesce kernel_coalesce = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct ethtool_coalesce coalesce;
Michal Kubecek0cf3eac2020-03-28 00:01:18 +01001698 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001700 if (!dev->ethtool_ops->set_coalesce && !dev->ethtool_ops->get_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return -EOPNOTSUPP;
1702
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001703 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1704 NULL);
1705 if (ret)
1706 return ret;
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1709 return -EFAULT;
1710
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001711 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1712 return -EOPNOTSUPP;
1713
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001714 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1715 NULL);
Michal Kubecek0cf3eac2020-03-28 00:01:18 +01001716 if (!ret)
1717 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1718 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
1721static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1722{
Roland Dreier8e557422010-02-11 12:14:23 -08001723 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 if (!dev->ethtool_ops->get_ringparam)
1726 return -EOPNOTSUPP;
1727
1728 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1729
1730 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1731 return -EFAULT;
1732 return 0;
1733}
1734
1735static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1736{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001737 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Michal Kubecekbc9d1c92020-03-12 21:08:33 +01001738 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001740 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return -EOPNOTSUPP;
1742
1743 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1744 return -EFAULT;
1745
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001746 dev->ethtool_ops->get_ringparam(dev, &max);
1747
1748 /* ensure new ring parameters are within the maximums */
1749 if (ringparam.rx_pending > max.rx_max_pending ||
1750 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1751 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1752 ringparam.tx_pending > max.tx_max_pending)
1753 return -EINVAL;
1754
Michal Kubecekbc9d1c92020-03-12 21:08:33 +01001755 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1756 if (!ret)
1757 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1758 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
amit salecha8b5933c2011-04-07 01:58:42 +00001761static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1762 void __user *useraddr)
1763{
1764 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1765
1766 if (!dev->ethtool_ops->get_channels)
1767 return -EOPNOTSUPP;
1768
1769 dev->ethtool_ops->get_channels(dev, &channels);
1770
1771 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1772 return -EFAULT;
1773 return 0;
1774}
1775
1776static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1777 void __user *useraddr)
1778{
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001779 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
Jakub Kicinski1661d342018-10-01 14:51:36 +02001780 u16 from_channel, to_channel;
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001781 u32 max_rx_in_use = 0;
Jakub Kicinski1661d342018-10-01 14:51:36 +02001782 unsigned int i;
Michal Kubecek546379b2020-03-12 21:08:48 +01001783 int ret;
amit salecha8b5933c2011-04-07 01:58:42 +00001784
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001785 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001786 return -EOPNOTSUPP;
1787
1788 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1789 return -EFAULT;
1790
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001791 dev->ethtool_ops->get_channels(dev, &curr);
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001792
Jakub Kicinski75c36db2020-05-15 12:49:02 -07001793 if (channels.rx_count == curr.rx_count &&
1794 channels.tx_count == curr.tx_count &&
1795 channels.combined_count == curr.combined_count &&
1796 channels.other_count == curr.other_count)
1797 return 0;
1798
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001799 /* ensure new counts are within the maximums */
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001800 if (channels.rx_count > curr.max_rx ||
1801 channels.tx_count > curr.max_tx ||
1802 channels.combined_count > curr.max_combined ||
1803 channels.other_count > curr.max_other)
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001804 return -EINVAL;
1805
Jakub Kicinski7be92512020-05-15 12:49:00 -07001806 /* ensure there is at least one RX and one TX channel */
1807 if (!channels.combined_count &&
1808 (!channels.rx_count || !channels.tx_count))
1809 return -EINVAL;
1810
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001811 /* ensure the new Rx count fits within the configured Rx flow
1812 * indirection table settings */
1813 if (netif_is_rxfh_configured(dev) &&
1814 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1815 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1816 return -EINVAL;
1817
Jakub Kicinski1661d342018-10-01 14:51:36 +02001818 /* Disabling channels, query zero-copy AF_XDP sockets */
1819 from_channel = channels.combined_count +
1820 min(channels.rx_count, channels.tx_count);
1821 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1822 for (i = from_channel; i < to_channel; i++)
Magnus Karlssonc4655762020-08-28 10:26:16 +02001823 if (xsk_get_pool_from_qid(dev, i))
Jakub Kicinski1661d342018-10-01 14:51:36 +02001824 return -EINVAL;
1825
Michal Kubecek546379b2020-03-12 21:08:48 +01001826 ret = dev->ethtool_ops->set_channels(dev, &channels);
1827 if (!ret)
1828 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1829 return ret;
amit salecha8b5933c2011-04-07 01:58:42 +00001830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1833{
Li RongQinge83887f2019-02-27 20:47:57 +08001834 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 if (!dev->ethtool_ops->get_pauseparam)
1837 return -EOPNOTSUPP;
1838
1839 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1840
1841 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1842 return -EFAULT;
1843 return 0;
1844}
1845
1846static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1847{
1848 struct ethtool_pauseparam pauseparam;
Michal Kubecekbf37faa2020-03-28 00:01:33 +01001849 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Jeff Garzike1b90c42006-07-17 12:54:40 -04001851 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return -EOPNOTSUPP;
1853
1854 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1855 return -EFAULT;
1856
Michal Kubecekbf37faa2020-03-28 00:01:33 +01001857 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1858 if (!ret)
1859 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1860 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1864{
1865 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001866 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001868 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001870 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001871 return -EOPNOTSUPP;
1872
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001873 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001874 if (test_len < 0)
1875 return test_len;
1876 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 if (copy_from_user(&test, useraddr, sizeof(test)))
1879 return -EFAULT;
1880
Jeff Garzikff03d492007-08-15 16:01:08 -07001881 test.len = test_len;
Austin Kim80ec82e2021-06-09 03:34:25 +01001882 data = kcalloc(test_len, sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (!data)
1884 return -ENOMEM;
1885
Andrew Lunn77e9b2a2020-04-20 00:11:52 +02001886 netif_testing_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 ops->self_test(dev, &test, data);
Andrew Lunn77e9b2a2020-04-20 00:11:52 +02001888 netif_testing_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 ret = -EFAULT;
1891 if (copy_to_user(useraddr, &test, sizeof(test)))
1892 goto out;
1893 useraddr += sizeof(test);
1894 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1895 goto out;
1896 ret = 0;
1897
1898 out:
1899 kfree(data);
1900 return ret;
1901}
1902
1903static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1904{
1905 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 u8 *data;
1907 int ret;
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1910 return -EFAULT;
1911
Michał Mirosław340ae162011-02-15 16:59:16 +00001912 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001913 if (ret < 0)
1914 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001915 if (ret > S32_MAX / ETH_GSTRING_LEN)
1916 return -ENOMEM;
1917 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001918
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001919 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Li RongQing3d883022019-03-29 09:18:02 +08001921 if (gstrings.len) {
1922 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1923 if (!data)
1924 return -ENOMEM;
1925
1926 __ethtool_get_strings(dev, gstrings.string_set, data);
1927 } else {
1928 data = NULL;
1929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 ret = -EFAULT;
1932 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1933 goto out;
1934 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001935 if (gstrings.len &&
1936 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 goto out;
1938 ret = 0;
1939
Michał Mirosław340ae162011-02-15 16:59:16 +00001940out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001941 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return ret;
1943}
1944
Alexander Duyck7888fe52021-03-16 17:30:36 -07001945__printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
1946{
1947 va_list args;
1948
1949 va_start(args, fmt);
1950 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
1951 va_end(args);
1952
1953 *data += ETH_GSTRING_LEN;
1954}
1955EXPORT_SYMBOL(ethtool_sprintf);
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1958{
1959 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001960 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001961 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001962 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001964 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return -EOPNOTSUPP;
1966
Ben Hutchings68f512f2011-04-02 00:35:15 +01001967 if (busy)
1968 return -EBUSY;
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (copy_from_user(&id, useraddr, sizeof(id)))
1971 return -EFAULT;
1972
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001973 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001974 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001975 return rc;
1976
1977 /* Drop the RTNL lock while waiting, but prevent reentry or
1978 * removal of the device.
1979 */
1980 busy = true;
1981 dev_hold(dev);
1982 rtnl_unlock();
1983
1984 if (rc == 0) {
1985 /* Driver will handle this itself */
1986 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001987 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001988 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001989 /* Driver expects to be called at twice the frequency in rc */
Edward Cree2adc6ed2020-09-01 18:52:32 +01001990 int n = rc * 2, interval = HZ / n;
1991 u64 count = n * id.data, i = 0;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001992
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001993 do {
Edward Cree2adc6ed2020-09-01 18:52:32 +01001994 rtnl_lock();
1995 rc = ops->set_phys_id(dev,
1996 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1997 rtnl_unlock();
1998 if (rc)
1999 break;
2000 schedule_timeout_interruptible(interval);
2001 } while (!signal_pending(current) && (!id.data || i < count));
Ben Hutchings68f512f2011-04-02 00:35:15 +01002002 }
2003
2004 rtnl_lock();
2005 dev_put(dev);
2006 busy = false;
2007
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002008 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01002009 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
2012static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2013{
2014 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07002015 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07002017 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00002019 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07002020 return -EOPNOTSUPP;
2021
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00002022 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07002023 if (n_stats < 0)
2024 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002025 if (n_stats > S32_MAX / sizeof(u64))
2026 return -ENOMEM;
2027 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2029 return -EFAULT;
2030
Jeff Garzikff03d492007-08-15 16:01:08 -07002031 stats.n_stats = n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Li RongQing3d883022019-03-29 09:18:02 +08002033 if (n_stats) {
2034 data = vzalloc(array_size(n_stats, sizeof(u64)));
2035 if (!data)
2036 return -ENOMEM;
2037 ops->get_ethtool_stats(dev, &stats, data);
2038 } else {
2039 data = NULL;
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 ret = -EFAULT;
2043 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2044 goto out;
2045 useraddr += sizeof(stats);
Gustavo A. R. Silva3dd14992020-06-15 18:01:07 -05002046 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 goto out;
2048 ret = 0;
2049
2050 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002051 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return ret;
2053}
2054
Andrew Lunnf3a40942015-12-30 16:28:25 +01002055static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2056{
Florian Fainelli17809512020-07-08 09:46:25 -07002057 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
Florian Fainelli99943382018-04-25 12:12:48 -07002058 const struct ethtool_ops *ops = dev->ethtool_ops;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002059 struct phy_device *phydev = dev->phydev;
Florian Fainelli99943382018-04-25 12:12:48 -07002060 struct ethtool_stats stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002061 u64 *data;
2062 int ret, n_stats;
2063
Florian Fainelli99943382018-04-25 12:12:48 -07002064 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002065 return -EOPNOTSUPP;
2066
Florian Fainelli17809512020-07-08 09:46:25 -07002067 if (dev->phydev && !ops->get_ethtool_phy_stats &&
2068 phy_ops && phy_ops->get_sset_count)
2069 n_stats = phy_ops->get_sset_count(dev->phydev);
Florian Fainelli99943382018-04-25 12:12:48 -07002070 else
2071 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002072 if (n_stats < 0)
2073 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002074 if (n_stats > S32_MAX / sizeof(u64))
2075 return -ENOMEM;
2076 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002077
2078 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2079 return -EFAULT;
2080
2081 stats.n_stats = n_stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002082
Li RongQing3d883022019-03-29 09:18:02 +08002083 if (n_stats) {
2084 data = vzalloc(array_size(n_stats, sizeof(u64)));
2085 if (!data)
2086 return -ENOMEM;
2087
Florian Fainelli17809512020-07-08 09:46:25 -07002088 if (dev->phydev && !ops->get_ethtool_phy_stats &&
2089 phy_ops && phy_ops->get_stats) {
2090 ret = phy_ops->get_stats(dev->phydev, &stats, data);
Li RongQing3d883022019-03-29 09:18:02 +08002091 if (ret < 0)
2092 goto out;
2093 } else {
2094 ops->get_ethtool_phy_stats(dev, &stats, data);
2095 }
Florian Fainelli99943382018-04-25 12:12:48 -07002096 } else {
Li RongQing3d883022019-03-29 09:18:02 +08002097 data = NULL;
Florian Fainelli99943382018-04-25 12:12:48 -07002098 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01002099
2100 ret = -EFAULT;
2101 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2102 goto out;
2103 useraddr += sizeof(stats);
Gustavo A. R. Silva3dd14992020-06-15 18:01:07 -05002104 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002105 goto out;
2106 ret = 0;
2107
2108 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002109 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002110 return ret;
2111}
2112
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002113static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002114{
2115 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002116
Matthew Wilcox313674a2007-07-31 14:00:29 -07002117 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002118 return -EFAULT;
2119
Matthew Wilcox313674a2007-07-31 14:00:29 -07002120 if (epaddr.size < dev->addr_len)
2121 return -ETOOSMALL;
2122 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002123
Jon Wetzela6f9a702005-08-20 17:15:54 -07002124 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002125 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002126 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002127 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2128 return -EFAULT;
2129 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002130}
2131
Jeff Garzik13c99b22007-08-15 16:01:56 -07002132static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2133 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002134{
Roland Dreier8e557422010-02-11 12:14:23 -08002135 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002136
Jeff Garzik13c99b22007-08-15 16:01:56 -07002137 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002138 return -EOPNOTSUPP;
2139
Jeff Garzik13c99b22007-08-15 16:01:56 -07002140 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002141
2142 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2143 return -EFAULT;
2144 return 0;
2145}
2146
Jeff Garzik13c99b22007-08-15 16:01:56 -07002147static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2148 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002149{
2150 struct ethtool_value edata;
2151
Jeff Garzik13c99b22007-08-15 16:01:56 -07002152 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002153 return -EOPNOTSUPP;
2154
2155 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2156 return -EFAULT;
2157
Jeff Garzik13c99b22007-08-15 16:01:56 -07002158 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002159 return 0;
2160}
2161
Jeff Garzik13c99b22007-08-15 16:01:56 -07002162static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2163 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002164{
2165 struct ethtool_value edata;
2166
Jeff Garzik13c99b22007-08-15 16:01:56 -07002167 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002168 return -EOPNOTSUPP;
2169
2170 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2171 return -EFAULT;
2172
Jeff Garzik13c99b22007-08-15 16:01:56 -07002173 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002174}
2175
chavey97f8aef2010-04-07 21:54:42 -07002176static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2177 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002178{
2179 struct ethtool_flash efl;
2180
2181 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2182 return -EFAULT;
Ben Hutchings786f5282012-02-01 09:32:25 +00002183 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2184
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08002185 if (!dev->ethtool_ops->flash_device)
2186 return devlink_compat_flash_update(dev, efl.data);
Jakub Kicinski4eceba12019-02-14 13:40:45 -08002187
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002188 return dev->ethtool_ops->flash_device(dev, &efl);
2189}
2190
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002191static int ethtool_set_dump(struct net_device *dev,
2192 void __user *useraddr)
2193{
2194 struct ethtool_dump dump;
2195
2196 if (!dev->ethtool_ops->set_dump)
2197 return -EOPNOTSUPP;
2198
2199 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2200 return -EFAULT;
2201
2202 return dev->ethtool_ops->set_dump(dev, &dump);
2203}
2204
2205static int ethtool_get_dump_flag(struct net_device *dev,
2206 void __user *useraddr)
2207{
2208 int ret;
2209 struct ethtool_dump dump;
2210 const struct ethtool_ops *ops = dev->ethtool_ops;
2211
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002212 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002213 return -EOPNOTSUPP;
2214
2215 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2216 return -EFAULT;
2217
2218 ret = ops->get_dump_flag(dev, &dump);
2219 if (ret)
2220 return ret;
2221
2222 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2223 return -EFAULT;
2224 return 0;
2225}
2226
2227static int ethtool_get_dump_data(struct net_device *dev,
2228 void __user *useraddr)
2229{
2230 int ret;
2231 __u32 len;
2232 struct ethtool_dump dump, tmp;
2233 const struct ethtool_ops *ops = dev->ethtool_ops;
2234 void *data = NULL;
2235
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002236 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002237 return -EOPNOTSUPP;
2238
2239 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2240 return -EFAULT;
2241
2242 memset(&tmp, 0, sizeof(tmp));
2243 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2244 ret = ops->get_dump_flag(dev, &tmp);
2245 if (ret)
2246 return ret;
2247
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002248 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002249 if (!len)
2250 return -EFAULT;
2251
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002252 /* Don't ever let the driver think there's more space available
2253 * than it requested with .get_dump_flag().
2254 */
2255 dump.len = len;
2256
2257 /* Always allocate enough space to hold the whole thing so that the
2258 * driver does not need to check the length and bother with partial
2259 * dumping.
2260 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002261 data = vzalloc(tmp.len);
2262 if (!data)
2263 return -ENOMEM;
2264 ret = ops->get_dump_data(dev, &dump, data);
2265 if (ret)
2266 goto out;
2267
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002268 /* There are two sane possibilities:
2269 * 1. The driver's .get_dump_data() does not touch dump.len.
2270 * 2. Or it may set dump.len to how much it really writes, which
2271 * should be tmp.len (or len if it can do a partial dump).
2272 * In any case respond to userspace with the actual length of data
2273 * it's receiving.
2274 */
2275 WARN_ON(dump.len != len && dump.len != tmp.len);
2276 dump.len = len;
2277
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002278 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2279 ret = -EFAULT;
2280 goto out;
2281 }
2282 useraddr += offsetof(struct ethtool_dump, data);
2283 if (copy_to_user(useraddr, data, len))
2284 ret = -EFAULT;
2285out:
2286 vfree(data);
2287 return ret;
2288}
2289
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002290static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2291{
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002292 struct ethtool_ts_info info;
Michal Kubecek5b071c52020-03-28 00:01:58 +01002293 int err;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002294
Michal Kubecek5b071c52020-03-28 00:01:58 +01002295 err = __ethtool_get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002296 if (err)
2297 return err;
2298
2299 if (copy_to_user(useraddr, &info, sizeof(info)))
Michal Kubecek5b071c52020-03-28 00:01:58 +01002300 return -EFAULT;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002301
Michal Kubecek5b071c52020-03-28 00:01:58 +01002302 return 0;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002303}
2304
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002305int ethtool_get_module_info_call(struct net_device *dev,
2306 struct ethtool_modinfo *modinfo)
Ed Swierk2f438362015-01-02 17:27:56 -08002307{
2308 const struct ethtool_ops *ops = dev->ethtool_ops;
2309 struct phy_device *phydev = dev->phydev;
2310
Russell Kinge679c9c2018-03-28 15:44:16 -07002311 if (dev->sfp_bus)
2312 return sfp_get_module_info(dev->sfp_bus, modinfo);
2313
Ed Swierk2f438362015-01-02 17:27:56 -08002314 if (phydev && phydev->drv && phydev->drv->module_info)
2315 return phydev->drv->module_info(phydev, modinfo);
2316
2317 if (ops->get_module_info)
2318 return ops->get_module_info(dev, modinfo);
2319
2320 return -EOPNOTSUPP;
2321}
2322
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002323static int ethtool_get_module_info(struct net_device *dev,
2324 void __user *useraddr)
2325{
2326 int ret;
2327 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002328
2329 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2330 return -EFAULT;
2331
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002332 ret = ethtool_get_module_info_call(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002333 if (ret)
2334 return ret;
2335
2336 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2337 return -EFAULT;
2338
2339 return 0;
2340}
2341
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002342int ethtool_get_module_eeprom_call(struct net_device *dev,
2343 struct ethtool_eeprom *ee, u8 *data)
Ed Swierk2f438362015-01-02 17:27:56 -08002344{
2345 const struct ethtool_ops *ops = dev->ethtool_ops;
2346 struct phy_device *phydev = dev->phydev;
2347
Russell Kinge679c9c2018-03-28 15:44:16 -07002348 if (dev->sfp_bus)
2349 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2350
Ed Swierk2f438362015-01-02 17:27:56 -08002351 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2352 return phydev->drv->module_eeprom(phydev, ee, data);
2353
2354 if (ops->get_module_eeprom)
2355 return ops->get_module_eeprom(dev, ee, data);
2356
2357 return -EOPNOTSUPP;
2358}
2359
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002360static int ethtool_get_module_eeprom(struct net_device *dev,
2361 void __user *useraddr)
2362{
2363 int ret;
2364 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002365
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002366 ret = ethtool_get_module_info_call(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002367 if (ret)
2368 return ret;
2369
Ed Swierk2f438362015-01-02 17:27:56 -08002370 return ethtool_get_any_eeprom(dev, useraddr,
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002371 ethtool_get_module_eeprom_call,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002372 modinfo.eeprom_len);
2373}
2374
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302375static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2376{
2377 switch (tuna->id) {
2378 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002379 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302380 if (tuna->len != sizeof(u32) ||
2381 tuna->type_id != ETHTOOL_TUNABLE_U32)
2382 return -EINVAL;
2383 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002384 case ETHTOOL_PFC_PREVENTION_TOUT:
2385 if (tuna->len != sizeof(u16) ||
2386 tuna->type_id != ETHTOOL_TUNABLE_U16)
2387 return -EINVAL;
2388 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302389 default:
2390 return -EINVAL;
2391 }
2392
2393 return 0;
2394}
2395
2396static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2397{
2398 int ret;
2399 struct ethtool_tunable tuna;
2400 const struct ethtool_ops *ops = dev->ethtool_ops;
2401 void *data;
2402
2403 if (!ops->get_tunable)
2404 return -EOPNOTSUPP;
2405 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2406 return -EFAULT;
2407 ret = ethtool_tunable_valid(&tuna);
2408 if (ret)
2409 return ret;
Austin Kim80ec82e2021-06-09 03:34:25 +01002410 data = kzalloc(tuna.len, GFP_USER);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302411 if (!data)
2412 return -ENOMEM;
2413 ret = ops->get_tunable(dev, &tuna, data);
2414 if (ret)
2415 goto out;
2416 useraddr += sizeof(tuna);
2417 ret = -EFAULT;
2418 if (copy_to_user(useraddr, data, tuna.len))
2419 goto out;
2420 ret = 0;
2421
2422out:
2423 kfree(data);
2424 return ret;
2425}
2426
2427static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2428{
2429 int ret;
2430 struct ethtool_tunable tuna;
2431 const struct ethtool_ops *ops = dev->ethtool_ops;
2432 void *data;
2433
2434 if (!ops->set_tunable)
2435 return -EOPNOTSUPP;
2436 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2437 return -EFAULT;
2438 ret = ethtool_tunable_valid(&tuna);
2439 if (ret)
2440 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302441 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002442 data = memdup_user(useraddr, tuna.len);
2443 if (IS_ERR(data))
2444 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302445 ret = ops->set_tunable(dev, &tuna, data);
2446
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302447 kfree(data);
2448 return ret;
2449}
2450
Arnd Bergmann3499e872019-03-07 16:58:35 +01002451static noinline_for_stack int
2452ethtool_get_per_queue_coalesce(struct net_device *dev,
2453 void __user *useraddr,
2454 struct ethtool_per_queue_op *per_queue_opt)
Kan Liang421797b2016-02-19 09:24:02 -05002455{
2456 u32 bit;
2457 int ret;
2458 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2459
2460 if (!dev->ethtool_ops->get_per_queue_coalesce)
2461 return -EOPNOTSUPP;
2462
2463 useraddr += sizeof(*per_queue_opt);
2464
Yury Norov3aa56882018-02-06 15:38:06 -08002465 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2466 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002467
2468 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2469 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2470
2471 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2472 if (ret != 0)
2473 return ret;
2474 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2475 return -EFAULT;
2476 useraddr += sizeof(coalesce);
2477 }
2478
2479 return 0;
2480}
2481
Arnd Bergmann3499e872019-03-07 16:58:35 +01002482static noinline_for_stack int
2483ethtool_set_per_queue_coalesce(struct net_device *dev,
2484 void __user *useraddr,
2485 struct ethtool_per_queue_op *per_queue_opt)
Kan Liangf38d1382016-02-19 09:24:03 -05002486{
2487 u32 bit;
2488 int i, ret = 0;
2489 int n_queue;
2490 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2491 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2492
2493 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2494 (!dev->ethtool_ops->get_per_queue_coalesce))
2495 return -EOPNOTSUPP;
2496
2497 useraddr += sizeof(*per_queue_opt);
2498
Yury Norov3aa56882018-02-06 15:38:06 -08002499 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002500 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2501 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2502 if (!backup)
2503 return -ENOMEM;
2504
2505 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2506 struct ethtool_coalesce coalesce;
2507
2508 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2509 if (ret != 0)
2510 goto roll_back;
2511
2512 tmp++;
2513
2514 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2515 ret = -EFAULT;
2516 goto roll_back;
2517 }
2518
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08002519 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2520 ret = -EOPNOTSUPP;
2521 goto roll_back;
2522 }
2523
Kan Liangf38d1382016-02-19 09:24:03 -05002524 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2525 if (ret != 0)
2526 goto roll_back;
2527
2528 useraddr += sizeof(coalesce);
2529 }
2530
2531roll_back:
2532 if (ret != 0) {
2533 tmp = backup;
2534 for_each_set_bit(i, queue_mask, bit) {
2535 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2536 tmp++;
2537 }
2538 }
2539 kfree(backup);
2540
2541 return ret;
2542}
2543
Arnd Bergmann3499e872019-03-07 16:58:35 +01002544static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002545 void __user *useraddr, u32 sub_cmd)
Kan Liangac2c7ad2016-02-19 09:24:01 -05002546{
2547 struct ethtool_per_queue_op per_queue_opt;
2548
2549 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2550 return -EFAULT;
2551
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002552 if (per_queue_opt.sub_command != sub_cmd)
2553 return -EINVAL;
2554
Kan Liangac2c7ad2016-02-19 09:24:01 -05002555 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002556 case ETHTOOL_GCOALESCE:
2557 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002558 case ETHTOOL_SCOALESCE:
2559 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002560 default:
2561 return -EOPNOTSUPP;
Tom Rix9d253c02020-11-01 07:56:01 -08002562 }
Kan Liangac2c7ad2016-02-19 09:24:01 -05002563}
2564
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002565static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2566{
2567 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002568 case ETHTOOL_PHY_DOWNSHIFT:
Heiner Kallweit3aeb0802019-03-25 19:34:58 +01002569 case ETHTOOL_PHY_FAST_LINK_DOWN:
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002570 if (tuna->len != sizeof(u8) ||
2571 tuna->type_id != ETHTOOL_TUNABLE_U8)
2572 return -EINVAL;
2573 break;
Alexandru Ardelean9f2f13f2019-09-16 10:35:25 +03002574 case ETHTOOL_PHY_EDPD:
2575 if (tuna->len != sizeof(u16) ||
2576 tuna->type_id != ETHTOOL_TUNABLE_U16)
2577 return -EINVAL;
2578 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002579 default:
2580 return -EINVAL;
2581 }
2582
2583 return 0;
2584}
2585
2586static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2587{
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002588 struct phy_device *phydev = dev->phydev;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002589 struct ethtool_tunable tuna;
2590 bool phy_drv_tunable;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002591 void *data;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002592 int ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002593
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002594 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2595 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002596 return -EOPNOTSUPP;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002597 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2598 return -EFAULT;
2599 ret = ethtool_phy_tunable_valid(&tuna);
2600 if (ret)
2601 return ret;
Austin Kim80ec82e2021-06-09 03:34:25 +01002602 data = kzalloc(tuna.len, GFP_USER);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002603 if (!data)
2604 return -ENOMEM;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002605 if (phy_drv_tunable) {
2606 mutex_lock(&phydev->lock);
2607 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2608 mutex_unlock(&phydev->lock);
2609 } else {
2610 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2611 }
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002612 if (ret)
2613 goto out;
2614 useraddr += sizeof(tuna);
2615 ret = -EFAULT;
2616 if (copy_to_user(useraddr, data, tuna.len))
2617 goto out;
2618 ret = 0;
2619
2620out:
2621 kfree(data);
2622 return ret;
2623}
2624
2625static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2626{
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002627 struct phy_device *phydev = dev->phydev;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002628 struct ethtool_tunable tuna;
2629 bool phy_drv_tunable;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002630 void *data;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002631 int ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002632
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002633 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2634 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002635 return -EOPNOTSUPP;
2636 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2637 return -EFAULT;
2638 ret = ethtool_phy_tunable_valid(&tuna);
2639 if (ret)
2640 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002641 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002642 data = memdup_user(useraddr, tuna.len);
2643 if (IS_ERR(data))
2644 return PTR_ERR(data);
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002645 if (phy_drv_tunable) {
2646 mutex_lock(&phydev->lock);
2647 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2648 mutex_unlock(&phydev->lock);
2649 } else {
2650 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2651 }
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002652
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002653 kfree(data);
2654 return ret;
2655}
2656
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002657static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2658{
Li RongQinge83887f2019-02-27 20:47:57 +08002659 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002660 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002661
2662 if (!dev->ethtool_ops->get_fecparam)
2663 return -EOPNOTSUPP;
2664
Edward Creea6d50512018-02-28 19:15:58 +00002665 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2666 if (rc)
2667 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002668
Jakub Kicinski240e11442021-03-24 18:11:57 -07002669 if (WARN_ON_ONCE(fecparam.reserved))
2670 fecparam.reserved = 0;
2671
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002672 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2673 return -EFAULT;
2674 return 0;
2675}
2676
2677static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2678{
2679 struct ethtool_fecparam fecparam;
2680
2681 if (!dev->ethtool_ops->set_fecparam)
2682 return -EOPNOTSUPP;
2683
2684 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2685 return -EFAULT;
2686
Jakub Kicinskicf2cc0b2021-03-26 13:22:22 -07002687 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
Jakub Kicinski42ce1272021-03-24 18:11:59 -07002688 return -EINVAL;
2689
Jakub Kicinskid3b37fc2021-03-24 18:11:58 -07002690 fecparam.active_fec = 0;
Jakub Kicinski240e11442021-03-24 18:11:57 -07002691 fecparam.reserved = 0;
2692
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002693 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2694}
2695
Yan Burman600fed52013-06-03 02:03:34 +00002696/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Arnd Bergmanna554bf92021-07-27 15:45:12 +02002698int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002700 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002701 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002703 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002705 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return -ENODEV;
2707
chavey97f8aef2010-04-07 21:54:42 -07002708 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return -EFAULT;
2710
Kan Liangac2c7ad2016-02-19 09:24:01 -05002711 if (ethcmd == ETHTOOL_PERQUEUE) {
2712 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2713 return -EFAULT;
2714 } else {
2715 sub_cmd = ethcmd;
2716 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002717 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002718 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002719 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002720 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002721 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002722 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002723 case ETHTOOL_GCOALESCE:
2724 case ETHTOOL_GRINGPARAM:
2725 case ETHTOOL_GPAUSEPARAM:
2726 case ETHTOOL_GRXCSUM:
2727 case ETHTOOL_GTXCSUM:
2728 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002729 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002730 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002731 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002732 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002733 case ETHTOOL_GTSO:
2734 case ETHTOOL_GPERMADDR:
Maciej Żenczykowski474ff262018-09-22 01:34:01 -07002735 case ETHTOOL_GUFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002736 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002737 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002738 case ETHTOOL_GFLAGS:
2739 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002740 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002741 case ETHTOOL_GRXRINGS:
2742 case ETHTOOL_GRXCLSRLCNT:
2743 case ETHTOOL_GRXCLSRULE:
2744 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002745 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302746 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002747 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002748 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002749 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002750 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302751 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002752 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002753 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002754 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002755 break;
2756 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002757 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002758 return -EPERM;
2759 }
2760
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002761 if (dev->dev.parent)
2762 pm_runtime_get_sync(dev->dev.parent);
2763
2764 if (!netif_device_present(dev)) {
2765 rc = -ENODEV;
2766 goto out;
2767 }
2768
chavey97f8aef2010-04-07 21:54:42 -07002769 if (dev->ethtool_ops->begin) {
2770 rc = dev->ethtool_ops->begin(dev);
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002771 if (rc < 0)
2772 goto out;
chavey97f8aef2010-04-07 21:54:42 -07002773 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002774 old_features = dev->features;
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 switch (ethcmd) {
2777 case ETHTOOL_GSET:
2778 rc = ethtool_get_settings(dev, useraddr);
2779 break;
2780 case ETHTOOL_SSET:
2781 rc = ethtool_set_settings(dev, useraddr);
2782 break;
2783 case ETHTOOL_GDRVINFO:
2784 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 break;
2786 case ETHTOOL_GREGS:
2787 rc = ethtool_get_regs(dev, useraddr);
2788 break;
2789 case ETHTOOL_GWOL:
2790 rc = ethtool_get_wol(dev, useraddr);
2791 break;
2792 case ETHTOOL_SWOL:
2793 rc = ethtool_set_wol(dev, useraddr);
2794 break;
2795 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002796 rc = ethtool_get_value(dev, useraddr, ethcmd,
2797 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 break;
2799 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002800 rc = ethtool_set_value_void(dev, useraddr,
2801 dev->ethtool_ops->set_msglevel);
Michal Kubecek0bda7af2020-01-26 23:11:10 +01002802 if (!rc)
2803 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002805 case ETHTOOL_GEEE:
2806 rc = ethtool_get_eee(dev, useraddr);
2807 break;
2808 case ETHTOOL_SEEE:
2809 rc = ethtool_set_eee(dev, useraddr);
2810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 case ETHTOOL_NWAY_RST:
2812 rc = ethtool_nway_reset(dev);
2813 break;
2814 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002815 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 break;
2817 case ETHTOOL_GEEPROM:
2818 rc = ethtool_get_eeprom(dev, useraddr);
2819 break;
2820 case ETHTOOL_SEEPROM:
2821 rc = ethtool_set_eeprom(dev, useraddr);
2822 break;
2823 case ETHTOOL_GCOALESCE:
2824 rc = ethtool_get_coalesce(dev, useraddr);
2825 break;
2826 case ETHTOOL_SCOALESCE:
2827 rc = ethtool_set_coalesce(dev, useraddr);
2828 break;
2829 case ETHTOOL_GRINGPARAM:
2830 rc = ethtool_get_ringparam(dev, useraddr);
2831 break;
2832 case ETHTOOL_SRINGPARAM:
2833 rc = ethtool_set_ringparam(dev, useraddr);
2834 break;
2835 case ETHTOOL_GPAUSEPARAM:
2836 rc = ethtool_get_pauseparam(dev, useraddr);
2837 break;
2838 case ETHTOOL_SPAUSEPARAM:
2839 rc = ethtool_set_pauseparam(dev, useraddr);
2840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 case ETHTOOL_TEST:
2842 rc = ethtool_self_test(dev, useraddr);
2843 break;
2844 case ETHTOOL_GSTRINGS:
2845 rc = ethtool_get_strings(dev, useraddr);
2846 break;
2847 case ETHTOOL_PHYS_ID:
2848 rc = ethtool_phys_id(dev, useraddr);
2849 break;
2850 case ETHTOOL_GSTATS:
2851 rc = ethtool_get_stats(dev, useraddr);
2852 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002853 case ETHTOOL_GPERMADDR:
2854 rc = ethtool_get_perm_addr(dev, useraddr);
2855 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002856 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002857 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002858 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002859 break;
2860 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002861 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002862 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002863 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002864 rc = ethtool_get_value(dev, useraddr, ethcmd,
2865 dev->ethtool_ops->get_priv_flags);
Michal Kubecek111dcba2020-03-12 21:08:18 +01002866 if (!rc)
2867 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
Jeff Garzik339bf022007-08-15 16:01:32 -07002868 break;
2869 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002870 rc = ethtool_set_value(dev, useraddr,
2871 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002872 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002873 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002874 case ETHTOOL_GRXRINGS:
2875 case ETHTOOL_GRXCLSRLCNT:
2876 case ETHTOOL_GRXCLSRULE:
2877 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002878 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002879 break;
2880 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002881 case ETHTOOL_SRXCLSRLDEL:
2882 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002883 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002884 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002885 case ETHTOOL_FLASHDEV:
2886 rc = ethtool_flash_device(dev, useraddr);
2887 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002888 case ETHTOOL_RESET:
2889 rc = ethtool_reset(dev, useraddr);
2890 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002891 case ETHTOOL_GSSET_INFO:
2892 rc = ethtool_get_sset_info(dev, useraddr);
2893 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002894 case ETHTOOL_GRXFHINDIR:
2895 rc = ethtool_get_rxfh_indir(dev, useraddr);
2896 break;
2897 case ETHTOOL_SRXFHINDIR:
2898 rc = ethtool_set_rxfh_indir(dev, useraddr);
2899 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302900 case ETHTOOL_GRSSH:
2901 rc = ethtool_get_rxfh(dev, useraddr);
2902 break;
2903 case ETHTOOL_SRSSH:
2904 rc = ethtool_set_rxfh(dev, useraddr);
2905 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002906 case ETHTOOL_GFEATURES:
2907 rc = ethtool_get_features(dev, useraddr);
2908 break;
2909 case ETHTOOL_SFEATURES:
2910 rc = ethtool_set_features(dev, useraddr);
2911 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002912 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002913 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002914 case ETHTOOL_GSG:
2915 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002916 case ETHTOOL_GGSO:
2917 case ETHTOOL_GGRO:
2918 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2919 break;
2920 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002921 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002922 case ETHTOOL_SSG:
2923 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002924 case ETHTOOL_SGSO:
2925 case ETHTOOL_SGRO:
2926 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2927 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002928 case ETHTOOL_GCHANNELS:
2929 rc = ethtool_get_channels(dev, useraddr);
2930 break;
2931 case ETHTOOL_SCHANNELS:
2932 rc = ethtool_set_channels(dev, useraddr);
2933 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002934 case ETHTOOL_SET_DUMP:
2935 rc = ethtool_set_dump(dev, useraddr);
2936 break;
2937 case ETHTOOL_GET_DUMP_FLAG:
2938 rc = ethtool_get_dump_flag(dev, useraddr);
2939 break;
2940 case ETHTOOL_GET_DUMP_DATA:
2941 rc = ethtool_get_dump_data(dev, useraddr);
2942 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002943 case ETHTOOL_GET_TS_INFO:
2944 rc = ethtool_get_ts_info(dev, useraddr);
2945 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002946 case ETHTOOL_GMODULEINFO:
2947 rc = ethtool_get_module_info(dev, useraddr);
2948 break;
2949 case ETHTOOL_GMODULEEEPROM:
2950 rc = ethtool_get_module_eeprom(dev, useraddr);
2951 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302952 case ETHTOOL_GTUNABLE:
2953 rc = ethtool_get_tunable(dev, useraddr);
2954 break;
2955 case ETHTOOL_STUNABLE:
2956 rc = ethtool_set_tunable(dev, useraddr);
2957 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002958 case ETHTOOL_GPHYSTATS:
2959 rc = ethtool_get_phy_stats(dev, useraddr);
2960 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002961 case ETHTOOL_PERQUEUE:
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002962 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002963 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002964 case ETHTOOL_GLINKSETTINGS:
2965 rc = ethtool_get_link_ksettings(dev, useraddr);
2966 break;
2967 case ETHTOOL_SLINKSETTINGS:
2968 rc = ethtool_set_link_ksettings(dev, useraddr);
2969 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002970 case ETHTOOL_PHY_GTUNABLE:
2971 rc = get_phy_tunable(dev, useraddr);
2972 break;
2973 case ETHTOOL_PHY_STUNABLE:
2974 rc = set_phy_tunable(dev, useraddr);
2975 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002976 case ETHTOOL_GFECPARAM:
2977 rc = ethtool_get_fecparam(dev, useraddr);
2978 break;
2979 case ETHTOOL_SFECPARAM:
2980 rc = ethtool_set_fecparam(dev, useraddr);
2981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002983 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002985
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002986 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002988
2989 if (old_features != dev->features)
2990 netdev_features_change(dev);
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002991out:
2992 if (dev->dev.parent)
2993 pm_runtime_put(dev->dev.parent);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01002997
2998struct ethtool_rx_flow_key {
2999 struct flow_dissector_key_basic basic;
3000 union {
3001 struct flow_dissector_key_ipv4_addrs ipv4;
3002 struct flow_dissector_key_ipv6_addrs ipv6;
3003 };
3004 struct flow_dissector_key_ports tp;
3005 struct flow_dissector_key_ip ip;
3006 struct flow_dissector_key_vlan vlan;
3007 struct flow_dissector_key_eth_addrs eth_addrs;
3008} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3009
3010struct ethtool_rx_flow_match {
3011 struct flow_dissector dissector;
3012 struct ethtool_rx_flow_key key;
3013 struct ethtool_rx_flow_key mask;
3014};
3015
3016struct ethtool_rx_flow_rule *
3017ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3018{
3019 const struct ethtool_rx_flow_spec *fs = input->fs;
3020 static struct in6_addr zero_addr = {};
3021 struct ethtool_rx_flow_match *match;
3022 struct ethtool_rx_flow_rule *flow;
3023 struct flow_action_entry *act;
3024
3025 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3026 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3027 if (!flow)
3028 return ERR_PTR(-ENOMEM);
3029
3030 /* ethtool_rx supports only one single action per rule. */
3031 flow->rule = flow_rule_alloc(1);
3032 if (!flow->rule) {
3033 kfree(flow);
3034 return ERR_PTR(-ENOMEM);
3035 }
3036
3037 match = (struct ethtool_rx_flow_match *)flow->priv;
3038 flow->rule->match.dissector = &match->dissector;
3039 flow->rule->match.mask = &match->mask;
3040 flow->rule->match.key = &match->key;
3041
3042 match->mask.basic.n_proto = htons(0xffff);
3043
3044 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
Maxime Chevallier5b9469a2019-06-27 10:52:26 +02003045 case ETHER_FLOW: {
3046 const struct ethhdr *ether_spec, *ether_m_spec;
3047
3048 ether_spec = &fs->h_u.ether_spec;
3049 ether_m_spec = &fs->m_u.ether_spec;
3050
3051 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3052 ether_addr_copy(match->key.eth_addrs.src,
3053 ether_spec->h_source);
3054 ether_addr_copy(match->mask.eth_addrs.src,
3055 ether_m_spec->h_source);
3056 }
3057 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3058 ether_addr_copy(match->key.eth_addrs.dst,
3059 ether_spec->h_dest);
3060 ether_addr_copy(match->mask.eth_addrs.dst,
3061 ether_m_spec->h_dest);
3062 }
3063 if (ether_m_spec->h_proto) {
3064 match->key.basic.n_proto = ether_spec->h_proto;
3065 match->mask.basic.n_proto = ether_m_spec->h_proto;
3066 }
3067 }
3068 break;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003069 case TCP_V4_FLOW:
3070 case UDP_V4_FLOW: {
3071 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3072
3073 match->key.basic.n_proto = htons(ETH_P_IP);
3074
3075 v4_spec = &fs->h_u.tcp_ip4_spec;
3076 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3077
3078 if (v4_m_spec->ip4src) {
3079 match->key.ipv4.src = v4_spec->ip4src;
3080 match->mask.ipv4.src = v4_m_spec->ip4src;
3081 }
3082 if (v4_m_spec->ip4dst) {
3083 match->key.ipv4.dst = v4_spec->ip4dst;
3084 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3085 }
3086 if (v4_m_spec->ip4src ||
3087 v4_m_spec->ip4dst) {
3088 match->dissector.used_keys |=
3089 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3090 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3091 offsetof(struct ethtool_rx_flow_key, ipv4);
3092 }
3093 if (v4_m_spec->psrc) {
3094 match->key.tp.src = v4_spec->psrc;
3095 match->mask.tp.src = v4_m_spec->psrc;
3096 }
3097 if (v4_m_spec->pdst) {
3098 match->key.tp.dst = v4_spec->pdst;
3099 match->mask.tp.dst = v4_m_spec->pdst;
3100 }
3101 if (v4_m_spec->psrc ||
3102 v4_m_spec->pdst) {
3103 match->dissector.used_keys |=
3104 BIT(FLOW_DISSECTOR_KEY_PORTS);
3105 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3106 offsetof(struct ethtool_rx_flow_key, tp);
3107 }
3108 if (v4_m_spec->tos) {
3109 match->key.ip.tos = v4_spec->tos;
3110 match->mask.ip.tos = v4_m_spec->tos;
3111 match->dissector.used_keys |=
3112 BIT(FLOW_DISSECTOR_KEY_IP);
3113 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3114 offsetof(struct ethtool_rx_flow_key, ip);
3115 }
3116 }
3117 break;
3118 case TCP_V6_FLOW:
3119 case UDP_V6_FLOW: {
3120 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3121
3122 match->key.basic.n_proto = htons(ETH_P_IPV6);
3123
3124 v6_spec = &fs->h_u.tcp_ip6_spec;
3125 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3126 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
3127 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3128 sizeof(match->key.ipv6.src));
3129 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3130 sizeof(match->mask.ipv6.src));
3131 }
3132 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3133 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3134 sizeof(match->key.ipv6.dst));
3135 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3136 sizeof(match->mask.ipv6.dst));
3137 }
3138 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
Gaurav Singh21a739c2020-06-21 11:30:17 -04003139 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003140 match->dissector.used_keys |=
3141 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3142 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3143 offsetof(struct ethtool_rx_flow_key, ipv6);
3144 }
3145 if (v6_m_spec->psrc) {
3146 match->key.tp.src = v6_spec->psrc;
3147 match->mask.tp.src = v6_m_spec->psrc;
3148 }
3149 if (v6_m_spec->pdst) {
3150 match->key.tp.dst = v6_spec->pdst;
3151 match->mask.tp.dst = v6_m_spec->pdst;
3152 }
3153 if (v6_m_spec->psrc ||
3154 v6_m_spec->pdst) {
3155 match->dissector.used_keys |=
3156 BIT(FLOW_DISSECTOR_KEY_PORTS);
3157 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3158 offsetof(struct ethtool_rx_flow_key, tp);
3159 }
3160 if (v6_m_spec->tclass) {
3161 match->key.ip.tos = v6_spec->tclass;
3162 match->mask.ip.tos = v6_m_spec->tclass;
3163 match->dissector.used_keys |=
3164 BIT(FLOW_DISSECTOR_KEY_IP);
3165 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3166 offsetof(struct ethtool_rx_flow_key, ip);
3167 }
3168 }
3169 break;
3170 default:
3171 ethtool_rx_flow_rule_destroy(flow);
3172 return ERR_PTR(-EINVAL);
3173 }
3174
3175 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3176 case TCP_V4_FLOW:
3177 case TCP_V6_FLOW:
3178 match->key.basic.ip_proto = IPPROTO_TCP;
Vishal Kulkarnid0a84e12020-08-19 00:25:03 +05303179 match->mask.basic.ip_proto = 0xff;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003180 break;
3181 case UDP_V4_FLOW:
3182 case UDP_V6_FLOW:
3183 match->key.basic.ip_proto = IPPROTO_UDP;
Vishal Kulkarnid0a84e12020-08-19 00:25:03 +05303184 match->mask.basic.ip_proto = 0xff;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003185 break;
3186 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003187
3188 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3189 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3190 offsetof(struct ethtool_rx_flow_key, basic);
3191
3192 if (fs->flow_type & FLOW_EXT) {
3193 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3194 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3195
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003196 if (ext_m_spec->vlan_etype) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003197 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3198 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003199 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003200
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003201 if (ext_m_spec->vlan_tci) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003202 match->key.vlan.vlan_id =
3203 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3204 match->mask.vlan.vlan_id =
3205 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3206
Maxime Chevallierf0d2ca12019-06-12 17:18:38 +02003207 match->key.vlan.vlan_dei =
3208 !!(ext_h_spec->vlan_tci & htons(0x1000));
3209 match->mask.vlan.vlan_dei =
3210 !!(ext_m_spec->vlan_tci & htons(0x1000));
3211
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003212 match->key.vlan.vlan_priority =
3213 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3214 match->mask.vlan.vlan_priority =
3215 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003216 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003217
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003218 if (ext_m_spec->vlan_etype ||
3219 ext_m_spec->vlan_tci) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003220 match->dissector.used_keys |=
3221 BIT(FLOW_DISSECTOR_KEY_VLAN);
3222 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3223 offsetof(struct ethtool_rx_flow_key, vlan);
3224 }
3225 }
3226 if (fs->flow_type & FLOW_MAC_EXT) {
3227 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3228 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3229
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003230 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3231 ETH_ALEN);
3232 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3233 ETH_ALEN);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003234
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003235 match->dissector.used_keys |=
3236 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3237 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3238 offsetof(struct ethtool_rx_flow_key, eth_addrs);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003239 }
3240
3241 act = &flow->rule->action.entries[0];
3242 switch (fs->ring_cookie) {
3243 case RX_CLS_FLOW_DISC:
3244 act->id = FLOW_ACTION_DROP;
3245 break;
3246 case RX_CLS_FLOW_WAKE:
3247 act->id = FLOW_ACTION_WAKE;
3248 break;
3249 default:
3250 act->id = FLOW_ACTION_QUEUE;
3251 if (fs->flow_type & FLOW_RSS)
3252 act->queue.ctx = input->rss_ctx;
3253
3254 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3255 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3256 break;
3257 }
3258
3259 return flow;
3260}
3261EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3262
3263void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3264{
3265 kfree(flow->rule);
3266 kfree(flow);
3267}
3268EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);