blob: 65e9bc1058b57c5238e3fccf9316bc8d7b6268d3 [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
Jakub Kicinski095cfcf2021-10-30 10:18:49 -070035/* State held across locks and calls for commands which have devlink fallback */
36struct ethtool_devlink_compat {
Jakub Kicinski1af0a092021-10-30 10:18:51 -070037 struct devlink *devlink;
Jakub Kicinski095cfcf2021-10-30 10:18:49 -070038 union {
39 struct ethtool_flash efl;
40 struct ethtool_drvinfo info;
41 };
42};
43
Jakub Kicinski1af0a092021-10-30 10:18:51 -070044static struct devlink *netdev_to_devlink_get(struct net_device *dev)
45{
46 struct devlink_port *devlink_port;
47
48 if (!dev->netdev_ops->ndo_get_devlink_port)
49 return NULL;
50
51 devlink_port = dev->netdev_ops->ndo_get_devlink_port(dev);
52 if (!devlink_port)
53 return NULL;
54
55 return devlink_try_get(devlink_port->devlink);
56}
57
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090058/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * Some useful ethtool_ops methods that're device independent.
60 * If we find that all drivers want to do the same thing here,
61 * we can turn these into dev_() function calls.
62 */
63
64u32 ethtool_op_get_link(struct net_device *dev)
65{
66 return netif_carrier_ok(dev) ? 1 : 0;
67}
chavey97f8aef2010-04-07 21:54:42 -070068EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Richard Cochran02eacbd2012-04-03 22:59:22 +000070int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
71{
72 info->so_timestamping =
73 SOF_TIMESTAMPING_TX_SOFTWARE |
74 SOF_TIMESTAMPING_RX_SOFTWARE |
75 SOF_TIMESTAMPING_SOFTWARE;
76 info->phc_index = -1;
77 return 0;
78}
79EXPORT_SYMBOL(ethtool_op_get_ts_info);
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* Handlers for each ethtool command */
82
Michał Mirosław5455c692011-02-15 16:59:17 +000083static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
84{
85 struct ethtool_gfeatures cmd = {
86 .cmd = ETHTOOL_GFEATURES,
87 .size = ETHTOOL_DEV_FEATURE_WORDS,
88 };
Michał Mirosław475414f2011-11-15 15:29:55 +000089 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +000090 u32 __user *sizeaddr;
91 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +000092 int i;
93
94 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +000095 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +000096
97 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +000098 features[i].available = (u32)(dev->hw_features >> (32 * i));
99 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
100 features[i].active = (u32)(dev->features >> (32 * i));
101 features[i].never_changed =
102 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000103 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000104
105 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
106 if (get_user(copy_size, sizeaddr))
107 return -EFAULT;
108
109 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
110 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
111
112 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
113 return -EFAULT;
114 useraddr += sizeof(cmd);
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -0500115 if (copy_to_user(useraddr, features,
116 array_size(copy_size, sizeof(*features))))
Michał Mirosław5455c692011-02-15 16:59:17 +0000117 return -EFAULT;
118
119 return 0;
120}
121
122static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
123{
124 struct ethtool_sfeatures cmd;
125 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000126 netdev_features_t wanted = 0, valid = 0;
127 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000128
129 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
130 return -EFAULT;
131 useraddr += sizeof(cmd);
132
133 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
134 return -EINVAL;
135
136 if (copy_from_user(features, useraddr, sizeof(features)))
137 return -EFAULT;
138
Michał Mirosław475414f2011-11-15 15:29:55 +0000139 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000140 valid |= (netdev_features_t)features[i].valid << (32 * i);
141 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000142 }
143
144 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000145 return -EINVAL;
146
Michał Mirosław475414f2011-11-15 15:29:55 +0000147 if (valid & ~dev->hw_features) {
148 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000149 ret |= ETHTOOL_F_UNSUPPORTED;
150 }
151
Michał Mirosław475414f2011-11-15 15:29:55 +0000152 dev->wanted_features &= ~valid;
153 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700154 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000155
Michał Mirosław475414f2011-11-15 15:29:55 +0000156 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000157 ret |= ETHTOOL_F_WISH;
158
159 return ret;
160}
161
Michał Mirosław340ae162011-02-15 16:59:16 +0000162static int __ethtool_get_sset_count(struct net_device *dev, int sset)
163{
Florian Fainelli17809512020-07-08 09:46:25 -0700164 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
Michał Mirosław340ae162011-02-15 16:59:16 +0000165 const struct ethtool_ops *ops = dev->ethtool_ops;
166
Michał Mirosław5455c692011-02-15 16:59:17 +0000167 if (sset == ETH_SS_FEATURES)
168 return ARRAY_SIZE(netdev_features_strings);
169
Eyal Perry892311f2014-12-02 18:12:10 +0200170 if (sset == ETH_SS_RSS_HASH_FUNCS)
171 return ARRAY_SIZE(rss_hash_func_strings);
172
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300173 if (sset == ETH_SS_TUNABLES)
174 return ARRAY_SIZE(tunable_strings);
175
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100176 if (sset == ETH_SS_PHY_TUNABLES)
177 return ARRAY_SIZE(phy_tunable_strings);
178
Florian Fainelli99943382018-04-25 12:12:48 -0700179 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
Florian Fainelli17809512020-07-08 09:46:25 -0700180 !ops->get_ethtool_phy_stats &&
181 phy_ops && phy_ops->get_sset_count)
182 return phy_ops->get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100183
Michal Kubecek428c1222019-12-11 10:58:34 +0100184 if (sset == ETH_SS_LINK_MODES)
185 return __ETHTOOL_LINK_MODE_MASK_NBITS;
186
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000187 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000188 return ops->get_sset_count(dev, sset);
189 else
190 return -EOPNOTSUPP;
191}
192
193static void __ethtool_get_strings(struct net_device *dev,
194 u32 stringset, u8 *data)
195{
Florian Fainelli17809512020-07-08 09:46:25 -0700196 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
Michał Mirosław340ae162011-02-15 16:59:16 +0000197 const struct ethtool_ops *ops = dev->ethtool_ops;
198
Michał Mirosław5455c692011-02-15 16:59:17 +0000199 if (stringset == ETH_SS_FEATURES)
200 memcpy(data, netdev_features_strings,
201 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200202 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
203 memcpy(data, rss_hash_func_strings,
204 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300205 else if (stringset == ETH_SS_TUNABLES)
206 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100207 else if (stringset == ETH_SS_PHY_TUNABLES)
208 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Florian Fainelli99943382018-04-25 12:12:48 -0700209 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
Florian Fainelli17809512020-07-08 09:46:25 -0700210 !ops->get_ethtool_phy_stats && phy_ops &&
211 phy_ops->get_strings)
212 phy_ops->get_strings(dev->phydev, data);
Michal Kubecek428c1222019-12-11 10:58:34 +0100213 else if (stringset == ETH_SS_LINK_MODES)
214 memcpy(data, link_mode_names,
215 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
Florian Fainelli99943382018-04-25 12:12:48 -0700216 else
Michał Mirosław5455c692011-02-15 16:59:17 +0000217 /* ops->get_strings is valid because checked earlier */
218 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000219}
220
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000221static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000222{
223 /* feature masks of legacy discrete ethtool ops */
224
225 switch (eth_cmd) {
226 case ETHTOOL_GTXCSUM:
227 case ETHTOOL_STXCSUM:
Vladyslav Tarasiuk9d648fb2020-03-24 13:57:08 +0200228 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
Michal Kubecekf70bb062020-03-12 21:07:43 +0100229 NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000230 case ETHTOOL_GRXCSUM:
231 case ETHTOOL_SRXCSUM:
232 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000233 case ETHTOOL_GSG:
234 case ETHTOOL_SSG:
Michal Kubecekf70bb062020-03-12 21:07:43 +0100235 return NETIF_F_SG | NETIF_F_FRAGLIST;
Michał Mirosław0a417702011-02-15 16:59:17 +0000236 case ETHTOOL_GTSO:
237 case ETHTOOL_STSO:
238 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000239 case ETHTOOL_GGSO:
240 case ETHTOOL_SGSO:
241 return NETIF_F_GSO;
242 case ETHTOOL_GGRO:
243 case ETHTOOL_SGRO:
244 return NETIF_F_GRO;
245 default:
246 BUG();
247 }
248}
249
Michał Mirosław0a417702011-02-15 16:59:17 +0000250static int ethtool_get_one_feature(struct net_device *dev,
251 char __user *useraddr, u32 ethcmd)
252{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000253 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000254 struct ethtool_value edata = {
255 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000256 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000257 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000258
Michał Mirosław0a417702011-02-15 16:59:17 +0000259 if (copy_to_user(useraddr, &edata, sizeof(edata)))
260 return -EFAULT;
261 return 0;
262}
263
Michał Mirosław0a417702011-02-15 16:59:17 +0000264static int ethtool_set_one_feature(struct net_device *dev,
265 void __user *useraddr, u32 ethcmd)
266{
267 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000268 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000269
270 if (copy_from_user(&edata, useraddr, sizeof(edata)))
271 return -EFAULT;
272
Michał Mirosław86794882011-02-15 16:59:17 +0000273 mask = ethtool_get_feature_mask(ethcmd);
274 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000275 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000276 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000277
278 if (edata.data)
279 dev->wanted_features |= mask;
280 else
281 dev->wanted_features &= ~mask;
282
283 __netdev_update_features(dev);
284
285 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000286}
287
Michał Mirosław02b3a552011-11-15 15:29:55 +0000288#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
289 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000290#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
291 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
292 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000293
294static u32 __ethtool_get_flags(struct net_device *dev)
295{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000296 u32 flags = 0;
297
Dragos Foianu8a731252013-07-13 14:43:00 +0100298 if (dev->features & NETIF_F_LRO)
299 flags |= ETH_FLAG_LRO;
300 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
301 flags |= ETH_FLAG_RXVLAN;
302 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
303 flags |= ETH_FLAG_TXVLAN;
304 if (dev->features & NETIF_F_NTUPLE)
305 flags |= ETH_FLAG_NTUPLE;
306 if (dev->features & NETIF_F_RXHASH)
307 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000308
309 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000310}
311
312static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000313{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000314 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000315
Michał Mirosław02b3a552011-11-15 15:29:55 +0000316 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000317 return -EINVAL;
318
Dragos Foianu8a731252013-07-13 14:43:00 +0100319 if (data & ETH_FLAG_LRO)
320 features |= NETIF_F_LRO;
321 if (data & ETH_FLAG_RXVLAN)
322 features |= NETIF_F_HW_VLAN_CTAG_RX;
323 if (data & ETH_FLAG_TXVLAN)
324 features |= NETIF_F_HW_VLAN_CTAG_TX;
325 if (data & ETH_FLAG_NTUPLE)
326 features |= NETIF_F_NTUPLE;
327 if (data & ETH_FLAG_RXHASH)
328 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000329
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000330 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000331 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000332 if (changed & ~dev->hw_features)
333 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
334
335 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000336 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000337
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700338 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000339
340 return 0;
341}
342
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700343/* Given two link masks, AND them together and save the result in dst. */
344void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
345 struct ethtool_link_ksettings *src)
346{
347 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
348 unsigned int idx = 0;
349
350 for (; idx < size; idx++) {
351 dst->link_modes.supported[idx] &=
352 src->link_modes.supported[idx];
353 dst->link_modes.advertising[idx] &=
354 src->link_modes.advertising[idx];
355 }
356}
357EXPORT_SYMBOL(ethtool_intersect_link_masks);
358
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200359void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
360 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800361{
Sean Anderson49730562021-10-22 18:41:04 -0400362 linkmode_zero(dst);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800363 dst[0] = legacy_u32;
364}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200365EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800366
367/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200368bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
369 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800370{
371 bool retval = true;
372
373 /* TODO: following test will soon always be true */
374 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
375 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
376
Sean Anderson49730562021-10-22 18:41:04 -0400377 linkmode_zero(ext);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800378 bitmap_fill(ext, 32);
379 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
Sean Anderson49730562021-10-22 18:41:04 -0400380 if (linkmode_intersects(ext, src)) {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800381 /* src mask goes beyond bit 31 */
382 retval = false;
383 }
384 }
385 *legacy_u32 = src[0];
386 return retval;
387}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200388EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800389
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800390/* return false if ksettings link modes had higher bits
391 * set. legacy_settings always updated (best effort)
392 */
393static bool
394convert_link_ksettings_to_legacy_settings(
395 struct ethtool_cmd *legacy_settings,
396 const struct ethtool_link_ksettings *link_ksettings)
397{
398 bool retval = true;
399
400 memset(legacy_settings, 0, sizeof(*legacy_settings));
401 /* this also clears the deprecated fields in legacy structure:
402 * __u8 transceiver;
403 * __u32 maxtxpkt;
404 * __u32 maxrxpkt;
405 */
406
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200407 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800408 &legacy_settings->supported,
409 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200410 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800411 &legacy_settings->advertising,
412 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200413 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800414 &legacy_settings->lp_advertising,
415 link_ksettings->link_modes.lp_advertising);
416 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
417 legacy_settings->duplex
418 = link_ksettings->base.duplex;
419 legacy_settings->port
420 = link_ksettings->base.port;
421 legacy_settings->phy_address
422 = link_ksettings->base.phy_address;
423 legacy_settings->autoneg
424 = link_ksettings->base.autoneg;
425 legacy_settings->mdio_support
426 = link_ksettings->base.mdio_support;
427 legacy_settings->eth_tp_mdix
428 = link_ksettings->base.eth_tp_mdix;
429 legacy_settings->eth_tp_mdix_ctrl
430 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700431 legacy_settings->transceiver
432 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800433 return retval;
434}
435
436/* number of 32-bit words to store the user's link mode bitmaps */
437#define __ETHTOOL_LINK_MODE_MASK_NU32 \
438 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
439
440/* layout of the struct passed from/to userland */
441struct ethtool_link_usettings {
442 struct ethtool_link_settings base;
443 struct {
444 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
445 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
446 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
447 } link_modes;
448};
449
Michal Kubecek9b300492018-08-28 19:56:58 +0200450/* Internal kernel helper to query a device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800451int __ethtool_get_link_ksettings(struct net_device *dev,
452 struct ethtool_link_ksettings *link_ksettings)
453{
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800454 ASSERT_RTNL();
455
Michal Kubecek9b300492018-08-28 19:56:58 +0200456 if (!dev->ethtool_ops->get_link_ksettings)
David Decotigny3237fc62016-02-24 10:58:11 -0800457 return -EOPNOTSUPP;
458
Michal Kubecek9b300492018-08-28 19:56:58 +0200459 memset(link_ksettings, 0, sizeof(*link_ksettings));
Danielle Ratsona975d7d2021-04-07 13:06:51 +0300460 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800461}
462EXPORT_SYMBOL(__ethtool_get_link_ksettings);
463
464/* convert ethtool_link_usettings in user space to a kernel internal
465 * ethtool_link_ksettings. return 0 on success, errno on error.
466 */
467static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
468 const void __user *from)
469{
470 struct ethtool_link_usettings link_usettings;
471
472 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
473 return -EFAULT;
474
475 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800476 bitmap_from_arr32(to->link_modes.supported,
477 link_usettings.link_modes.supported,
478 __ETHTOOL_LINK_MODE_MASK_NBITS);
479 bitmap_from_arr32(to->link_modes.advertising,
480 link_usettings.link_modes.advertising,
481 __ETHTOOL_LINK_MODE_MASK_NBITS);
482 bitmap_from_arr32(to->link_modes.lp_advertising,
483 link_usettings.link_modes.lp_advertising,
484 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800485
486 return 0;
487}
488
Cris Forno70ae1e12020-02-28 14:12:04 -0600489/* Check if the user is trying to change anything besides speed/duplex */
490bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
491{
492 struct ethtool_link_settings base2 = {};
493
494 base2.speed = cmd->base.speed;
495 base2.port = PORT_OTHER;
496 base2.duplex = cmd->base.duplex;
497 base2.cmd = cmd->base.cmd;
498 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
499
500 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
501 bitmap_empty(cmd->link_modes.supported,
502 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
503 bitmap_empty(cmd->link_modes.lp_advertising,
504 __ETHTOOL_LINK_MODE_MASK_NBITS);
505}
506
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800507/* convert a kernel internal ethtool_link_ksettings to
508 * ethtool_link_usettings in user space. return 0 on success, errno on
509 * error.
510 */
511static int
512store_link_ksettings_for_user(void __user *to,
513 const struct ethtool_link_ksettings *from)
514{
515 struct ethtool_link_usettings link_usettings;
516
Gustavo A. R. Silvac1d9e342021-04-16 15:15:40 -0500517 memcpy(&link_usettings, from, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800518 bitmap_to_arr32(link_usettings.link_modes.supported,
519 from->link_modes.supported,
520 __ETHTOOL_LINK_MODE_MASK_NBITS);
521 bitmap_to_arr32(link_usettings.link_modes.advertising,
522 from->link_modes.advertising,
523 __ETHTOOL_LINK_MODE_MASK_NBITS);
524 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
525 from->link_modes.lp_advertising,
526 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800527
528 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
529 return -EFAULT;
530
531 return 0;
532}
533
Michal Kubecek9b300492018-08-28 19:56:58 +0200534/* Query device for its ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800535static int ethtool_get_link_ksettings(struct net_device *dev,
536 void __user *useraddr)
537{
538 int err = 0;
539 struct ethtool_link_ksettings link_ksettings;
540
541 ASSERT_RTNL();
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800542 if (!dev->ethtool_ops->get_link_ksettings)
543 return -EOPNOTSUPP;
544
545 /* handle bitmap nbits handshake */
546 if (copy_from_user(&link_ksettings.base, useraddr,
547 sizeof(link_ksettings.base)))
548 return -EFAULT;
549
550 if (__ETHTOOL_LINK_MODE_MASK_NU32
551 != link_ksettings.base.link_mode_masks_nwords) {
552 /* wrong link mode nbits requested */
553 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000554 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800555 /* send back number of words required as negative val */
556 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
557 "need too many bits for link modes!");
558 link_ksettings.base.link_mode_masks_nwords
559 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
560
561 /* copy the base fields back to user, not the link
562 * mode bitmaps
563 */
564 if (copy_to_user(useraddr, &link_ksettings.base,
565 sizeof(link_ksettings.base)))
566 return -EFAULT;
567
568 return 0;
569 }
570
571 /* handshake successful: user/kernel agree on
572 * link_mode_masks_nwords
573 */
574
575 memset(&link_ksettings, 0, sizeof(link_ksettings));
576 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
577 if (err < 0)
578 return err;
579
580 /* make sure we tell the right values to user */
581 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
582 link_ksettings.base.link_mode_masks_nwords
583 = __ETHTOOL_LINK_MODE_MASK_NU32;
Oleksij Rempelbdbdac72020-05-05 08:35:05 +0200584 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
585 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800586
587 return store_link_ksettings_for_user(useraddr, &link_ksettings);
588}
589
Michal Kubecek9b300492018-08-28 19:56:58 +0200590/* Update device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800591static int ethtool_set_link_ksettings(struct net_device *dev,
592 void __user *useraddr)
593{
594 int err;
595 struct ethtool_link_ksettings link_ksettings;
596
597 ASSERT_RTNL();
598
599 if (!dev->ethtool_ops->set_link_ksettings)
600 return -EOPNOTSUPP;
601
602 /* make sure nbits field has expected value */
603 if (copy_from_user(&link_ksettings.base, useraddr,
604 sizeof(link_ksettings.base)))
605 return -EFAULT;
606
607 if (__ETHTOOL_LINK_MODE_MASK_NU32
608 != link_ksettings.base.link_mode_masks_nwords)
609 return -EINVAL;
610
611 /* copy the whole structure, now that we know it has expected
612 * format
613 */
614 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
615 if (err)
616 return err;
617
618 /* re-check nwords field, just in case */
619 if (__ETHTOOL_LINK_MODE_MASK_NU32
620 != link_ksettings.base.link_mode_masks_nwords)
621 return -EINVAL;
622
Oleksij Rempelbdbdac72020-05-05 08:35:05 +0200623 if (link_ksettings.base.master_slave_cfg ||
624 link_ksettings.base.master_slave_state)
625 return -EINVAL;
626
Michal Kubecek73286732019-12-27 15:56:03 +0100627 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100628 if (err >= 0) {
Michal Kubecek73286732019-12-27 15:56:03 +0100629 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100630 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
631 }
Michal Kubecek73286732019-12-27 15:56:03 +0100632 return err;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800633}
634
Cris Forno70ae1e12020-02-28 14:12:04 -0600635int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
636 const struct ethtool_link_ksettings *cmd,
637 u32 *dev_speed, u8 *dev_duplex)
638{
639 u32 speed;
640 u8 duplex;
641
642 speed = cmd->base.speed;
643 duplex = cmd->base.duplex;
644 /* don't allow custom speed and duplex */
645 if (!ethtool_validate_speed(speed) ||
646 !ethtool_validate_duplex(duplex) ||
647 !ethtool_virtdev_validate_cmd(cmd))
648 return -EINVAL;
649 *dev_speed = speed;
650 *dev_duplex = duplex;
651
652 return 0;
653}
654EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
655
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800656/* Query device for its ethtool_cmd settings.
657 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200658 * Backward compatibility note: for compatibility with legacy ethtool, this is
659 * now implemented via get_link_ksettings. When driver reports higher link mode
660 * bits, a kernel warning is logged once (with name of 1st driver/device) to
661 * recommend user to upgrade ethtool, but the command is successful (only the
662 * lower link mode bits reported back to user). Deprecated fields from
663 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800664 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000665static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
666{
Michal Kubecek9b300492018-08-28 19:56:58 +0200667 struct ethtool_link_ksettings link_ksettings;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000668 struct ethtool_cmd cmd;
Michal Kubecek9b300492018-08-28 19:56:58 +0200669 int err;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000670
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800671 ASSERT_RTNL();
Michal Kubecek9b300492018-08-28 19:56:58 +0200672 if (!dev->ethtool_ops->get_link_ksettings)
673 return -EOPNOTSUPP;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800674
Michal Kubecek9b300492018-08-28 19:56:58 +0200675 memset(&link_ksettings, 0, sizeof(link_ksettings));
676 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
677 if (err < 0)
678 return err;
679 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800680
Michal Kubecek9b300492018-08-28 19:56:58 +0200681 /* send a sensible cmd tag back to user */
682 cmd.cmd = ETHTOOL_GSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
685 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688}
689
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800690/* Update device link settings with given ethtool_cmd.
691 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200692 * Backward compatibility note: for compatibility with legacy ethtool, this is
693 * now always implemented via set_link_settings. When user's request updates
694 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
695 * warning is logged once (with name of 1st driver/device) to recommend user to
696 * upgrade ethtool, and the request is rejected.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
699{
Michal Kubecek9b300492018-08-28 19:56:58 +0200700 struct ethtool_link_ksettings link_ksettings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct ethtool_cmd cmd;
Michal Kubecek73286732019-12-27 15:56:03 +0100702 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800704 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
707 return -EFAULT;
Michal Kubecek9b300492018-08-28 19:56:58 +0200708 if (!dev->ethtool_ops->set_link_ksettings)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800709 return -EOPNOTSUPP;
710
Michal Kubecek9b300492018-08-28 19:56:58 +0200711 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
712 return -EINVAL;
713 link_ksettings.base.link_mode_masks_nwords =
714 __ETHTOOL_LINK_MODE_MASK_NU32;
Michal Kubecek73286732019-12-27 15:56:03 +0100715 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100716 if (ret >= 0) {
Michal Kubecek73286732019-12-27 15:56:03 +0100717 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100718 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
719 }
Michal Kubecek73286732019-12-27 15:56:03 +0100720 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700723static int
724ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700726 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700728 rsp->info.cmd = ETHTOOL_GDRVINFO;
729 strlcpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version));
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000730 if (ops->get_drvinfo) {
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700731 ops->get_drvinfo(dev, &rsp->info);
Ben Hutchings01414802010-08-17 02:31:15 -0700732 } else if (dev->dev.parent && dev->dev.parent->driver) {
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700733 strlcpy(rsp->info.bus_info, dev_name(dev->dev.parent),
734 sizeof(rsp->info.bus_info));
735 strlcpy(rsp->info.driver, dev->dev.parent->driver->name,
736 sizeof(rsp->info.driver));
Ben Hutchings01414802010-08-17 02:31:15 -0700737 } else {
738 return -EOPNOTSUPP;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jeff Garzik723b2f52010-03-03 22:51:50 +0000741 /*
742 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000743 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000744 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000745 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700746 int rc;
747
748 rc = ops->get_sset_count(dev, ETH_SS_TEST);
749 if (rc >= 0)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700750 rsp->info.testinfo_len = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700751 rc = ops->get_sset_count(dev, ETH_SS_STATS);
752 if (rc >= 0)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700753 rsp->info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700754 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
755 if (rc >= 0)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700756 rsp->info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700757 }
Yunsheng Linf9fc54d2018-12-26 19:51:46 +0800758 if (ops->get_regs_len) {
759 int ret = ops->get_regs_len(dev);
760
761 if (ret > 0)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700762 rsp->info.regdump_len = ret;
Yunsheng Linf9fc54d2018-12-26 19:51:46 +0800763 }
764
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000765 if (ops->get_eeprom_len)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700766 rsp->info.eedump_len = ops->get_eeprom_len(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700768 if (!rsp->info.fw_version[0])
Jakub Kicinski1af0a092021-10-30 10:18:51 -0700769 rsp->devlink = netdev_to_devlink_get(dev);
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
772}
773
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800774static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700775 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000776{
777 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000778 u64 sset_mask;
779 int i, idx = 0, n_bits = 0, ret, rc;
780 u32 *info_buf = NULL;
781
Jeff Garzik723b2f52010-03-03 22:51:50 +0000782 if (copy_from_user(&info, useraddr, sizeof(info)))
783 return -EFAULT;
784
785 /* store copy of mask, because we zero struct later on */
786 sset_mask = info.sset_mask;
787 if (!sset_mask)
788 return 0;
789
790 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000791 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000792
793 memset(&info, 0, sizeof(info));
794 info.cmd = ETHTOOL_GSSET_INFO;
795
Kees Cook6396bb22018-06-12 14:03:40 -0700796 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000797 if (!info_buf)
798 return -ENOMEM;
799
800 /*
801 * fill return buffer based on input bitmask and successful
802 * get_sset_count return
803 */
804 for (i = 0; i < 64; i++) {
805 if (!(sset_mask & (1ULL << i)))
806 continue;
807
Michał Mirosław340ae162011-02-15 16:59:16 +0000808 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000809 if (rc >= 0) {
810 info.sset_mask |= (1ULL << i);
811 info_buf[idx++] = rc;
812 }
813 }
814
815 ret = -EFAULT;
816 if (copy_to_user(useraddr, &info, sizeof(info)))
817 goto out;
818
819 useraddr += offsetof(struct ethtool_sset_info, data);
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -0500820 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
Jeff Garzik723b2f52010-03-03 22:51:50 +0000821 goto out;
822
823 ret = 0;
824
825out:
826 kfree(info_buf);
827 return ret;
828}
829
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200830static noinline_for_stack int
831ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
832 const struct compat_ethtool_rxnfc __user *useraddr,
833 size_t size)
834{
835 struct compat_ethtool_rxnfc crxnfc = {};
836
837 /* We expect there to be holes between fs.m_ext and
838 * fs.ring_cookie and at the end of fs, but nowhere else.
839 * On non-x86, no conversion should be needed.
840 */
841 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
842 sizeof(struct compat_ethtool_rxnfc) !=
843 sizeof(struct ethtool_rxnfc));
844 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
845 sizeof(useraddr->fs.m_ext) !=
846 offsetof(struct ethtool_rxnfc, fs.m_ext) +
847 sizeof(rxnfc->fs.m_ext));
848 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
849 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
850 offsetof(struct ethtool_rxnfc, fs.location) -
851 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
852
853 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
854 return -EFAULT;
855
856 *rxnfc = (struct ethtool_rxnfc) {
857 .cmd = crxnfc.cmd,
858 .flow_type = crxnfc.flow_type,
859 .data = crxnfc.data,
860 .fs = {
861 .flow_type = crxnfc.fs.flow_type,
862 .h_u = crxnfc.fs.h_u,
863 .h_ext = crxnfc.fs.h_ext,
864 .m_u = crxnfc.fs.m_u,
865 .m_ext = crxnfc.fs.m_ext,
866 .ring_cookie = crxnfc.fs.ring_cookie,
867 .location = crxnfc.fs.location,
868 },
869 .rule_cnt = crxnfc.rule_cnt,
870 };
871
872 return 0;
873}
874
875static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
876 const void __user *useraddr,
877 size_t size)
878{
879 if (compat_need_64bit_alignment_fixup())
880 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
881
882 if (copy_from_user(rxnfc, useraddr, size))
883 return -EFAULT;
884
885 return 0;
886}
887
888static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
889 const struct ethtool_rxnfc *rxnfc,
890 size_t size, const u32 *rule_buf)
891{
892 struct compat_ethtool_rxnfc crxnfc;
893
894 memset(&crxnfc, 0, sizeof(crxnfc));
895 crxnfc = (struct compat_ethtool_rxnfc) {
896 .cmd = rxnfc->cmd,
897 .flow_type = rxnfc->flow_type,
898 .data = rxnfc->data,
899 .fs = {
900 .flow_type = rxnfc->fs.flow_type,
901 .h_u = rxnfc->fs.h_u,
902 .h_ext = rxnfc->fs.h_ext,
903 .m_u = rxnfc->fs.m_u,
904 .m_ext = rxnfc->fs.m_ext,
905 .ring_cookie = rxnfc->fs.ring_cookie,
906 .location = rxnfc->fs.location,
907 },
908 .rule_cnt = rxnfc->rule_cnt,
909 };
910
911 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
912 return -EFAULT;
913
914 return 0;
915}
916
917static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
918 const struct ethtool_rxnfc *rxnfc,
919 size_t size, const u32 *rule_buf)
920{
921 int ret;
922
923 if (compat_need_64bit_alignment_fixup()) {
924 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
925 rule_buf);
926 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
927 } else {
Saeed Mahameed9b29a162021-07-26 15:15:39 -0700928 ret = copy_to_user(useraddr, rxnfc, size);
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200929 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
930 }
931
932 if (ret)
933 return -EFAULT;
934
935 if (rule_buf) {
936 if (copy_to_user(useraddr, rule_buf,
937 rxnfc->rule_cnt * sizeof(u32)))
938 return -EFAULT;
939 }
940
941 return 0;
942}
943
chavey97f8aef2010-04-07 21:54:42 -0700944static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000945 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700946{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000947 struct ethtool_rxnfc info;
948 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000949 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700950
Santwona Behera59089d82009-02-20 00:58:13 -0800951 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700952 return -EOPNOTSUPP;
953
Ben Hutchingsbf988432010-06-28 08:45:58 +0000954 /* struct ethtool_rxnfc was originally defined for
955 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
956 * members. User-space might still be using that
957 * definition. */
958 if (cmd == ETHTOOL_SRXFH)
959 info_size = (offsetof(struct ethtool_rxnfc, data) +
960 sizeof(info.data));
961
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200962 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700963 return -EFAULT;
964
Ben Hutchings55664f32012-01-03 12:04:51 +0000965 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
966 if (rc)
967 return rc;
968
969 if (cmd == ETHTOOL_SRXCLSRLINS &&
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200970 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
Ben Hutchings55664f32012-01-03 12:04:51 +0000971 return -EFAULT;
972
973 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700974}
975
chavey97f8aef2010-04-07 21:54:42 -0700976static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000977 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700978{
979 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000980 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800981 const struct ethtool_ops *ops = dev->ethtool_ops;
982 int ret;
983 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700984
Santwona Behera59089d82009-02-20 00:58:13 -0800985 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700986 return -EOPNOTSUPP;
987
Ben Hutchingsbf988432010-06-28 08:45:58 +0000988 /* struct ethtool_rxnfc was originally defined for
989 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
990 * members. User-space might still be using that
991 * definition. */
992 if (cmd == ETHTOOL_GRXFH)
993 info_size = (offsetof(struct ethtool_rxnfc, data) +
994 sizeof(info.data));
995
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200996 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700997 return -EFAULT;
998
Edward Cree84a1d9c42018-03-08 15:45:03 +0000999 /* If FLOW_RSS was requested then user-space must be using the
1000 * new definition, as FLOW_RSS is newer.
1001 */
1002 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1003 info_size = sizeof(info);
Arnd Bergmanndd98d282021-07-22 16:28:59 +02001004 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
Edward Cree84a1d9c42018-03-08 15:45:03 +00001005 return -EFAULT;
Wenwen Wangd656fe42018-04-30 12:31:13 -05001006 /* Since malicious users may modify the original data,
1007 * we need to check whether FLOW_RSS is still requested.
1008 */
1009 if (!(info.flow_type & FLOW_RSS))
1010 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001011 }
1012
Wenwen Wang2bb32072018-10-09 08:15:38 -05001013 if (info.cmd != cmd)
1014 return -EINVAL;
1015
Santwona Behera59089d82009-02-20 00:58:13 -08001016 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1017 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001018 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cook6396bb22018-06-12 14:03:40 -07001019 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001020 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001021 if (!rule_buf)
1022 return -ENOMEM;
1023 }
1024 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001025
Santwona Behera59089d82009-02-20 00:58:13 -08001026 ret = ops->get_rxnfc(dev, &info, rule_buf);
1027 if (ret < 0)
1028 goto err_out;
1029
Arnd Bergmanndd98d282021-07-22 16:28:59 +02001030 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001031err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001032 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001033
1034 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001035}
1036
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301037static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1038 struct ethtool_rxnfc *rx_rings,
1039 u32 size)
1040{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001041 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301042
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -05001043 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001044 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301045
1046 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001047 for (i = 0; i < size; i++)
1048 if (indir[i] >= rx_rings->data)
1049 return -EINVAL;
1050
1051 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301052}
1053
Kim Jonesba905f52016-02-02 03:51:16 +00001054u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001055
1056void netdev_rss_key_fill(void *buffer, size_t len)
1057{
1058 BUG_ON(len > sizeof(netdev_rss_key));
1059 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1060 memcpy(buffer, netdev_rss_key, len);
1061}
1062EXPORT_SYMBOL(netdev_rss_key_fill);
1063
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001064static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1065 void __user *useraddr)
1066{
Ben Hutchings7850f632011-12-15 13:55:01 +00001067 u32 user_size, dev_size;
1068 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001069 int ret;
1070
Ben Hutchings7850f632011-12-15 13:55:01 +00001071 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001072 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001073 return -EOPNOTSUPP;
1074 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1075 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001076 return -EOPNOTSUPP;
1077
Ben Hutchings7850f632011-12-15 13:55:01 +00001078 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001079 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001080 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001081 return -EFAULT;
1082
Ben Hutchings7850f632011-12-15 13:55:01 +00001083 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1084 &dev_size, sizeof(dev_size)))
1085 return -EFAULT;
1086
1087 /* If the user buffer size is 0, this is just a query for the
1088 * device table size. Otherwise, if it's smaller than the
1089 * device table size it's an error.
1090 */
1091 if (user_size < dev_size)
1092 return user_size == 0 ? 0 : -EINVAL;
1093
1094 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001095 if (!indir)
1096 return -ENOMEM;
1097
Eyal Perry892311f2014-12-02 18:12:10 +02001098 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001099 if (ret)
1100 goto out;
1101
Ben Hutchings7850f632011-12-15 13:55:01 +00001102 if (copy_to_user(useraddr +
1103 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1104 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001105 ret = -EFAULT;
1106
1107out:
1108 kfree(indir);
1109 return ret;
1110}
1111
1112static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1113 void __user *useraddr)
1114{
Ben Hutchings7850f632011-12-15 13:55:01 +00001115 struct ethtool_rxnfc rx_rings;
1116 u32 user_size, dev_size, i;
1117 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001118 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001119 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301120 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001121
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001122 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001123 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001124 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001125
1126 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001127 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001128 return -EOPNOTSUPP;
1129
Ben Hutchings7850f632011-12-15 13:55:01 +00001130 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001131 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001132 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001133 return -EFAULT;
1134
Ben Hutchings278bc422011-12-15 13:56:49 +00001135 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001136 return -EINVAL;
1137
1138 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001139 if (!indir)
1140 return -ENOMEM;
1141
Ben Hutchings7850f632011-12-15 13:55:01 +00001142 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001143 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001144 if (ret)
1145 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001146
1147 if (user_size == 0) {
1148 for (i = 0; i < dev_size; i++)
1149 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1150 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301151 ret = ethtool_copy_validate_indir(indir,
1152 useraddr + ringidx_offset,
1153 &rx_rings,
1154 dev_size);
1155 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001156 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001157 }
1158
Eyal Perry892311f2014-12-02 18:12:10 +02001159 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001160 if (ret)
1161 goto out;
1162
1163 /* indicate whether rxfh was set to default */
1164 if (user_size == 0)
1165 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1166 else
1167 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001168
1169out:
1170 kfree(indir);
1171 return ret;
1172}
1173
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301174static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1175 void __user *useraddr)
1176{
1177 int ret;
1178 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001179 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301180 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001181 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301182 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001183 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301184 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001185 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301186 u8 *hkey = NULL;
1187 u8 *rss_config;
1188
Eyal Perry892311f2014-12-02 18:12:10 +02001189 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301190 return -EOPNOTSUPP;
1191
1192 if (ops->get_rxfh_indir_size)
1193 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301194 if (ops->get_rxfh_key_size)
1195 dev_key_size = ops->get_rxfh_key_size(dev);
1196
Ben Hutchingsf062a382014-05-15 16:28:07 +01001197 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301198 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001199 user_indir_size = rxfh.indir_size;
1200 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301201
Ben Hutchingsf062a382014-05-15 16:28:07 +01001202 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001203 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001204 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001205 /* Most drivers don't handle rss_context, check it's 0 as well */
1206 if (rxfh.rss_context && !ops->get_rxfh_context)
1207 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001208
1209 rxfh.indir_size = dev_indir_size;
1210 rxfh.key_size = dev_key_size;
1211 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301212 return -EFAULT;
1213
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301214 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1215 (user_key_size && (user_key_size != dev_key_size)))
1216 return -EINVAL;
1217
1218 indir_bytes = user_indir_size * sizeof(indir[0]);
1219 total_size = indir_bytes + user_key_size;
1220 rss_config = kzalloc(total_size, GFP_USER);
1221 if (!rss_config)
1222 return -ENOMEM;
1223
1224 if (user_indir_size)
1225 indir = (u32 *)rss_config;
1226
1227 if (user_key_size)
1228 hkey = rss_config + indir_bytes;
1229
Edward Cree84a1d9c42018-03-08 15:45:03 +00001230 if (rxfh.rss_context)
1231 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1232 &dev_hfunc,
1233 rxfh.rss_context);
1234 else
1235 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001236 if (ret)
1237 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301238
Eyal Perry892311f2014-12-02 18:12:10 +02001239 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1240 &dev_hfunc, sizeof(rxfh.hfunc))) {
1241 ret = -EFAULT;
1242 } else if (copy_to_user(useraddr +
1243 offsetof(struct ethtool_rxfh, rss_config[0]),
1244 rss_config, total_size)) {
1245 ret = -EFAULT;
1246 }
1247out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301248 kfree(rss_config);
1249
1250 return ret;
1251}
1252
1253static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1254 void __user *useraddr)
1255{
1256 int ret;
1257 const struct ethtool_ops *ops = dev->ethtool_ops;
1258 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001259 struct ethtool_rxfh rxfh;
1260 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301261 u32 *indir = NULL, indir_bytes = 0;
1262 u8 *hkey = NULL;
1263 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301264 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001265 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301266
Eyal Perry892311f2014-12-02 18:12:10 +02001267 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301268 return -EOPNOTSUPP;
1269
1270 if (ops->get_rxfh_indir_size)
1271 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301272 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001273 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301274
Ben Hutchingsf062a382014-05-15 16:28:07 +01001275 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301276 return -EFAULT;
1277
Ben Hutchingsf062a382014-05-15 16:28:07 +01001278 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001279 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001280 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001281 /* Most drivers don't handle rss_context, check it's 0 as well */
1282 if (rxfh.rss_context && !ops->set_rxfh_context)
1283 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001284
Eyal Perry892311f2014-12-02 18:12:10 +02001285 /* If either indir, hash key or function is valid, proceed further.
1286 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301287 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001288 if ((rxfh.indir_size &&
1289 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1290 rxfh.indir_size != dev_indir_size) ||
1291 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1292 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001293 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301294 return -EINVAL;
1295
Ben Hutchingsf062a382014-05-15 16:28:07 +01001296 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301297 indir_bytes = dev_indir_size * sizeof(indir[0]);
1298
Ben Hutchingsf062a382014-05-15 16:28:07 +01001299 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301300 if (!rss_config)
1301 return -ENOMEM;
1302
1303 rx_rings.cmd = ETHTOOL_GRXRINGS;
1304 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1305 if (ret)
1306 goto out;
1307
Edward Cree84a1d9c42018-03-08 15:45:03 +00001308 /* rxfh.indir_size == 0 means reset the indir table to default (master
1309 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001310 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301311 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001312 if (rxfh.indir_size &&
1313 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301314 indir = (u32 *)rss_config;
1315 ret = ethtool_copy_validate_indir(indir,
1316 useraddr + rss_cfg_offset,
1317 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001318 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301319 if (ret)
1320 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001321 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001322 if (rxfh.rss_context == 0) {
1323 indir = (u32 *)rss_config;
1324 for (i = 0; i < dev_indir_size; i++)
1325 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1326 } else {
1327 delete = true;
1328 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301329 }
1330
Ben Hutchingsf062a382014-05-15 16:28:07 +01001331 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301332 hkey = rss_config + indir_bytes;
1333 if (copy_from_user(hkey,
1334 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001335 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301336 ret = -EFAULT;
1337 goto out;
1338 }
1339 }
1340
Edward Cree84a1d9c42018-03-08 15:45:03 +00001341 if (rxfh.rss_context)
1342 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1343 &rxfh.rss_context, delete);
1344 else
1345 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001346 if (ret)
1347 goto out;
1348
Edward Cree84a1d9c42018-03-08 15:45:03 +00001349 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1350 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1351 ret = -EFAULT;
1352
1353 if (!rxfh.rss_context) {
1354 /* indicate whether rxfh was set to default */
1355 if (rxfh.indir_size == 0)
1356 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1357 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1358 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1359 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301360
1361out:
1362 kfree(rss_config);
1363 return ret;
1364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1367{
1368 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001369 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 void *regbuf;
1371 int reglen, ret;
1372
1373 if (!ops->get_regs || !ops->get_regs_len)
1374 return -EOPNOTSUPP;
1375
1376 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1377 return -EFAULT;
1378
1379 reglen = ops->get_regs_len(dev);
Yunsheng Linf9fc54d2018-12-26 19:51:46 +08001380 if (reglen <= 0)
1381 return reglen;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (regs.len > reglen)
1384 regs.len = reglen;
1385
Dan Carpenteref76c772019-02-01 11:24:06 +03001386 regbuf = vzalloc(reglen);
1387 if (!regbuf)
1388 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Vivien Didelot0ee4e762019-06-03 16:57:13 -04001390 if (regs.len < reglen)
1391 reglen = regs.len;
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ops->get_regs(dev, &regs, regbuf);
1394
1395 ret = -EFAULT;
1396 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1397 goto out;
1398 useraddr += offsetof(struct ethtool_regs, data);
Vivien Didelot0ee4e762019-06-03 16:57:13 -04001399 if (copy_to_user(useraddr, regbuf, reglen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto out;
1401 ret = 0;
1402
1403 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001404 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return ret;
1406}
1407
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001408static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1409{
1410 struct ethtool_value reset;
1411 int ret;
1412
1413 if (!dev->ethtool_ops->reset)
1414 return -EOPNOTSUPP;
1415
1416 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1417 return -EFAULT;
1418
1419 ret = dev->ethtool_ops->reset(dev, &reset.data);
1420 if (ret)
1421 return ret;
1422
1423 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1424 return -EFAULT;
1425 return 0;
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1429{
zhanglin5ff223e82019-10-26 15:54:16 +08001430 struct ethtool_wolinfo wol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 if (!dev->ethtool_ops->get_wol)
1433 return -EOPNOTSUPP;
1434
zhanglin5ff223e82019-10-26 15:54:16 +08001435 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1436 wol.cmd = ETHTOOL_GWOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 dev->ethtool_ops->get_wol(dev, &wol);
1438
1439 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1440 return -EFAULT;
1441 return 0;
1442}
1443
1444static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1445{
1446 struct ethtool_wolinfo wol;
Heiner Kallweit61941142018-09-24 21:58:59 +02001447 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 if (!dev->ethtool_ops->set_wol)
1450 return -EOPNOTSUPP;
1451
1452 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1453 return -EFAULT;
1454
Heiner Kallweit61941142018-09-24 21:58:59 +02001455 ret = dev->ethtool_ops->set_wol(dev, &wol);
1456 if (ret)
1457 return ret;
1458
1459 dev->wol_enabled = !!wol.wolopts;
Michal Kubecek67bffa72020-01-26 23:11:19 +01001460 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
Heiner Kallweit61941142018-09-24 21:58:59 +02001461
1462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001465static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1466{
1467 struct ethtool_eee edata;
1468 int rc;
1469
1470 if (!dev->ethtool_ops->get_eee)
1471 return -EOPNOTSUPP;
1472
1473 memset(&edata, 0, sizeof(struct ethtool_eee));
1474 edata.cmd = ETHTOOL_GEEE;
1475 rc = dev->ethtool_ops->get_eee(dev, &edata);
1476
1477 if (rc)
1478 return rc;
1479
1480 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1481 return -EFAULT;
1482
1483 return 0;
1484}
1485
1486static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1487{
1488 struct ethtool_eee edata;
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +01001489 int ret;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001490
1491 if (!dev->ethtool_ops->set_eee)
1492 return -EOPNOTSUPP;
1493
1494 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1495 return -EFAULT;
1496
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +01001497 ret = dev->ethtool_ops->set_eee(dev, &edata);
1498 if (!ret)
1499 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1500 return ret;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static int ethtool_nway_reset(struct net_device *dev)
1504{
1505 if (!dev->ethtool_ops->nway_reset)
1506 return -EOPNOTSUPP;
1507
1508 return dev->ethtool_ops->nway_reset(dev);
1509}
1510
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001511static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1512{
1513 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001514 int link = __ethtool_get_link(dev);
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001515
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001516 if (link < 0)
1517 return link;
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001518
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001519 edata.data = link;
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001520 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1521 return -EFAULT;
1522 return 0;
1523}
1524
Ben Hutchings081d0942012-04-12 00:42:12 +00001525static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1526 int (*getter)(struct net_device *,
1527 struct ethtool_eeprom *, u8 *),
1528 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001531 void __user *userbuf = useraddr + sizeof(eeprom);
1532 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001534 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1537 return -EFAULT;
1538
1539 /* Check for wrap and zero */
1540 if (eeprom.offset + eeprom.len <= eeprom.offset)
1541 return -EINVAL;
1542
1543 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001544 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return -EINVAL;
1546
Austin Kim80ec82e2021-06-09 03:34:25 +01001547 data = kzalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (!data)
1549 return -ENOMEM;
1550
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001551 bytes_remaining = eeprom.len;
1552 while (bytes_remaining > 0) {
1553 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Ben Hutchings081d0942012-04-12 00:42:12 +00001555 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001556 if (ret)
1557 break;
Heiner Kallweitb9bbc4c2021-09-13 21:58:26 +02001558 if (!eeprom.len) {
1559 ret = -EIO;
1560 break;
1561 }
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001562 if (copy_to_user(userbuf, data, eeprom.len)) {
1563 ret = -EFAULT;
1564 break;
1565 }
1566 userbuf += eeprom.len;
1567 eeprom.offset += eeprom.len;
1568 bytes_remaining -= eeprom.len;
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001571 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1572 eeprom.offset -= eeprom.len;
1573 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1574 ret = -EFAULT;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 kfree(data);
1577 return ret;
1578}
1579
Ben Hutchings081d0942012-04-12 00:42:12 +00001580static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1581{
1582 const struct ethtool_ops *ops = dev->ethtool_ops;
1583
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001584 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1585 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001586 return -EOPNOTSUPP;
1587
1588 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1589 ops->get_eeprom_len(dev));
1590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1593{
1594 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001595 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001596 void __user *userbuf = useraddr + sizeof(eeprom);
1597 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001599 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001601 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1602 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return -EOPNOTSUPP;
1604
1605 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1606 return -EFAULT;
1607
1608 /* Check for wrap and zero */
1609 if (eeprom.offset + eeprom.len <= eeprom.offset)
1610 return -EINVAL;
1611
1612 /* Check for exceeding total eeprom len */
1613 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1614 return -EINVAL;
1615
Austin Kim80ec82e2021-06-09 03:34:25 +01001616 data = kzalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (!data)
1618 return -ENOMEM;
1619
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001620 bytes_remaining = eeprom.len;
1621 while (bytes_remaining > 0) {
1622 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001624 if (copy_from_user(data, userbuf, eeprom.len)) {
1625 ret = -EFAULT;
1626 break;
1627 }
1628 ret = ops->set_eeprom(dev, &eeprom, data);
1629 if (ret)
1630 break;
1631 userbuf += eeprom.len;
1632 eeprom.offset += eeprom.len;
1633 bytes_remaining -= eeprom.len;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 kfree(data);
1637 return ret;
1638}
1639
chavey97f8aef2010-04-07 21:54:42 -07001640static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1641 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Roland Dreier8e557422010-02-11 12:14:23 -08001643 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001644 struct kernel_ethtool_coalesce kernel_coalesce = {};
Heiner Kallweit31610712020-05-23 17:40:25 +02001645 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 if (!dev->ethtool_ops->get_coalesce)
1648 return -EOPNOTSUPP;
1649
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001650 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1651 NULL);
Heiner Kallweit31610712020-05-23 17:40:25 +02001652 if (ret)
1653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1656 return -EFAULT;
1657 return 0;
1658}
1659
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001660static bool
1661ethtool_set_coalesce_supported(struct net_device *dev,
1662 struct ethtool_coalesce *coalesce)
1663{
1664 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1665 u32 nonzero_params = 0;
1666
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001667 if (coalesce->rx_coalesce_usecs)
1668 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1669 if (coalesce->rx_max_coalesced_frames)
1670 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1671 if (coalesce->rx_coalesce_usecs_irq)
1672 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1673 if (coalesce->rx_max_coalesced_frames_irq)
1674 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1675 if (coalesce->tx_coalesce_usecs)
1676 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1677 if (coalesce->tx_max_coalesced_frames)
1678 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1679 if (coalesce->tx_coalesce_usecs_irq)
1680 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1681 if (coalesce->tx_max_coalesced_frames_irq)
1682 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1683 if (coalesce->stats_block_coalesce_usecs)
1684 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1685 if (coalesce->use_adaptive_rx_coalesce)
1686 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1687 if (coalesce->use_adaptive_tx_coalesce)
1688 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1689 if (coalesce->pkt_rate_low)
1690 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1691 if (coalesce->rx_coalesce_usecs_low)
1692 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1693 if (coalesce->rx_max_coalesced_frames_low)
1694 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1695 if (coalesce->tx_coalesce_usecs_low)
1696 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1697 if (coalesce->tx_max_coalesced_frames_low)
1698 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1699 if (coalesce->pkt_rate_high)
1700 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1701 if (coalesce->rx_coalesce_usecs_high)
1702 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1703 if (coalesce->rx_max_coalesced_frames_high)
1704 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1705 if (coalesce->tx_coalesce_usecs_high)
1706 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1707 if (coalesce->tx_max_coalesced_frames_high)
1708 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1709 if (coalesce->rate_sample_interval)
1710 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1711
1712 return (supported_params & nonzero_params) == nonzero_params;
1713}
1714
chavey97f8aef2010-04-07 21:54:42 -07001715static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1716 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001718 struct kernel_ethtool_coalesce kernel_coalesce = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 struct ethtool_coalesce coalesce;
Michal Kubecek0cf3eac2020-03-28 00:01:18 +01001720 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001722 if (!dev->ethtool_ops->set_coalesce && !dev->ethtool_ops->get_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return -EOPNOTSUPP;
1724
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001725 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1726 NULL);
1727 if (ret)
1728 return ret;
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1731 return -EFAULT;
1732
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001733 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1734 return -EOPNOTSUPP;
1735
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001736 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1737 NULL);
Michal Kubecek0cf3eac2020-03-28 00:01:18 +01001738 if (!ret)
1739 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1740 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1744{
Roland Dreier8e557422010-02-11 12:14:23 -08001745 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 if (!dev->ethtool_ops->get_ringparam)
1748 return -EOPNOTSUPP;
1749
1750 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1751
1752 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1753 return -EFAULT;
1754 return 0;
1755}
1756
1757static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1758{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001759 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Michal Kubecekbc9d1c92020-03-12 21:08:33 +01001760 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001762 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return -EOPNOTSUPP;
1764
1765 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1766 return -EFAULT;
1767
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001768 dev->ethtool_ops->get_ringparam(dev, &max);
1769
1770 /* ensure new ring parameters are within the maximums */
1771 if (ringparam.rx_pending > max.rx_max_pending ||
1772 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1773 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1774 ringparam.tx_pending > max.tx_max_pending)
1775 return -EINVAL;
1776
Michal Kubecekbc9d1c92020-03-12 21:08:33 +01001777 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1778 if (!ret)
1779 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1780 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
amit salecha8b5933c2011-04-07 01:58:42 +00001783static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1784 void __user *useraddr)
1785{
1786 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1787
1788 if (!dev->ethtool_ops->get_channels)
1789 return -EOPNOTSUPP;
1790
1791 dev->ethtool_ops->get_channels(dev, &channels);
1792
1793 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1794 return -EFAULT;
1795 return 0;
1796}
1797
1798static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1799 void __user *useraddr)
1800{
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001801 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
Jakub Kicinski1661d342018-10-01 14:51:36 +02001802 u16 from_channel, to_channel;
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001803 u32 max_rx_in_use = 0;
Jakub Kicinski1661d342018-10-01 14:51:36 +02001804 unsigned int i;
Michal Kubecek546379b2020-03-12 21:08:48 +01001805 int ret;
amit salecha8b5933c2011-04-07 01:58:42 +00001806
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001807 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001808 return -EOPNOTSUPP;
1809
1810 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1811 return -EFAULT;
1812
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001813 dev->ethtool_ops->get_channels(dev, &curr);
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001814
Jakub Kicinski75c36db2020-05-15 12:49:02 -07001815 if (channels.rx_count == curr.rx_count &&
1816 channels.tx_count == curr.tx_count &&
1817 channels.combined_count == curr.combined_count &&
1818 channels.other_count == curr.other_count)
1819 return 0;
1820
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001821 /* ensure new counts are within the maximums */
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001822 if (channels.rx_count > curr.max_rx ||
1823 channels.tx_count > curr.max_tx ||
1824 channels.combined_count > curr.max_combined ||
1825 channels.other_count > curr.max_other)
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001826 return -EINVAL;
1827
Jakub Kicinski7be92512020-05-15 12:49:00 -07001828 /* ensure there is at least one RX and one TX channel */
1829 if (!channels.combined_count &&
1830 (!channels.rx_count || !channels.tx_count))
1831 return -EINVAL;
1832
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001833 /* ensure the new Rx count fits within the configured Rx flow
1834 * indirection table settings */
1835 if (netif_is_rxfh_configured(dev) &&
1836 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1837 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1838 return -EINVAL;
1839
Jakub Kicinski1661d342018-10-01 14:51:36 +02001840 /* Disabling channels, query zero-copy AF_XDP sockets */
1841 from_channel = channels.combined_count +
1842 min(channels.rx_count, channels.tx_count);
1843 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1844 for (i = from_channel; i < to_channel; i++)
Magnus Karlssonc4655762020-08-28 10:26:16 +02001845 if (xsk_get_pool_from_qid(dev, i))
Jakub Kicinski1661d342018-10-01 14:51:36 +02001846 return -EINVAL;
1847
Michal Kubecek546379b2020-03-12 21:08:48 +01001848 ret = dev->ethtool_ops->set_channels(dev, &channels);
1849 if (!ret)
1850 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1851 return ret;
amit salecha8b5933c2011-04-07 01:58:42 +00001852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1855{
Li RongQinge83887f2019-02-27 20:47:57 +08001856 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 if (!dev->ethtool_ops->get_pauseparam)
1859 return -EOPNOTSUPP;
1860
1861 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1862
1863 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1864 return -EFAULT;
1865 return 0;
1866}
1867
1868static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1869{
1870 struct ethtool_pauseparam pauseparam;
Michal Kubecekbf37faa2020-03-28 00:01:33 +01001871 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Jeff Garzike1b90c42006-07-17 12:54:40 -04001873 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return -EOPNOTSUPP;
1875
1876 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1877 return -EFAULT;
1878
Michal Kubecekbf37faa2020-03-28 00:01:33 +01001879 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1880 if (!ret)
1881 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1882 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1886{
1887 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001888 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001890 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001892 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001893 return -EOPNOTSUPP;
1894
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001895 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001896 if (test_len < 0)
1897 return test_len;
1898 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 if (copy_from_user(&test, useraddr, sizeof(test)))
1901 return -EFAULT;
1902
Jeff Garzikff03d492007-08-15 16:01:08 -07001903 test.len = test_len;
Austin Kim80ec82e2021-06-09 03:34:25 +01001904 data = kcalloc(test_len, sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (!data)
1906 return -ENOMEM;
1907
Andrew Lunn77e9b2a2020-04-20 00:11:52 +02001908 netif_testing_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 ops->self_test(dev, &test, data);
Andrew Lunn77e9b2a2020-04-20 00:11:52 +02001910 netif_testing_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 ret = -EFAULT;
1913 if (copy_to_user(useraddr, &test, sizeof(test)))
1914 goto out;
1915 useraddr += sizeof(test);
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -05001916 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 goto out;
1918 ret = 0;
1919
1920 out:
1921 kfree(data);
1922 return ret;
1923}
1924
1925static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1926{
1927 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 u8 *data;
1929 int ret;
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1932 return -EFAULT;
1933
Michał Mirosław340ae162011-02-15 16:59:16 +00001934 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001935 if (ret < 0)
1936 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001937 if (ret > S32_MAX / ETH_GSTRING_LEN)
1938 return -ENOMEM;
1939 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001940
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001941 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Li RongQing3d883022019-03-29 09:18:02 +08001943 if (gstrings.len) {
1944 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1945 if (!data)
1946 return -ENOMEM;
1947
1948 __ethtool_get_strings(dev, gstrings.string_set, data);
1949 } else {
1950 data = NULL;
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 ret = -EFAULT;
1954 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1955 goto out;
1956 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001957 if (gstrings.len &&
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -05001958 copy_to_user(useraddr, data,
1959 array_size(gstrings.len, ETH_GSTRING_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto out;
1961 ret = 0;
1962
Michał Mirosław340ae162011-02-15 16:59:16 +00001963out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001964 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return ret;
1966}
1967
Alexander Duyck7888fe52021-03-16 17:30:36 -07001968__printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
1969{
1970 va_list args;
1971
1972 va_start(args, fmt);
1973 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
1974 va_end(args);
1975
1976 *data += ETH_GSTRING_LEN;
1977}
1978EXPORT_SYMBOL(ethtool_sprintf);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1981{
1982 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001983 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001984 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001985 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001987 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return -EOPNOTSUPP;
1989
Ben Hutchings68f512f2011-04-02 00:35:15 +01001990 if (busy)
1991 return -EBUSY;
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (copy_from_user(&id, useraddr, sizeof(id)))
1994 return -EFAULT;
1995
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001996 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001997 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001998 return rc;
1999
2000 /* Drop the RTNL lock while waiting, but prevent reentry or
2001 * removal of the device.
2002 */
2003 busy = true;
2004 dev_hold(dev);
2005 rtnl_unlock();
2006
2007 if (rc == 0) {
2008 /* Driver will handle this itself */
2009 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00002010 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01002011 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00002012 /* Driver expects to be called at twice the frequency in rc */
Edward Cree2adc6ed2020-09-01 18:52:32 +01002013 int n = rc * 2, interval = HZ / n;
2014 u64 count = n * id.data, i = 0;
Ben Hutchings68f512f2011-04-02 00:35:15 +01002015
Allan, Bruce Wfce55922011-04-13 13:09:10 +00002016 do {
Edward Cree2adc6ed2020-09-01 18:52:32 +01002017 rtnl_lock();
2018 rc = ops->set_phys_id(dev,
2019 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2020 rtnl_unlock();
2021 if (rc)
2022 break;
2023 schedule_timeout_interruptible(interval);
2024 } while (!signal_pending(current) && (!id.data || i < count));
Ben Hutchings68f512f2011-04-02 00:35:15 +01002025 }
2026
2027 rtnl_lock();
2028 dev_put(dev);
2029 busy = false;
2030
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002031 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01002032 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
2035static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2036{
2037 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07002038 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07002040 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00002042 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07002043 return -EOPNOTSUPP;
2044
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00002045 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07002046 if (n_stats < 0)
2047 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002048 if (n_stats > S32_MAX / sizeof(u64))
2049 return -ENOMEM;
2050 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2052 return -EFAULT;
2053
Jeff Garzikff03d492007-08-15 16:01:08 -07002054 stats.n_stats = n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Li RongQing3d883022019-03-29 09:18:02 +08002056 if (n_stats) {
2057 data = vzalloc(array_size(n_stats, sizeof(u64)));
2058 if (!data)
2059 return -ENOMEM;
2060 ops->get_ethtool_stats(dev, &stats, data);
2061 } else {
2062 data = NULL;
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 ret = -EFAULT;
2066 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2067 goto out;
2068 useraddr += sizeof(stats);
Gustavo A. R. Silva3dd14992020-06-15 18:01:07 -05002069 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 goto out;
2071 ret = 0;
2072
2073 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002074 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return ret;
2076}
2077
Andrew Lunnf3a40942015-12-30 16:28:25 +01002078static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2079{
Florian Fainelli17809512020-07-08 09:46:25 -07002080 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
Florian Fainelli99943382018-04-25 12:12:48 -07002081 const struct ethtool_ops *ops = dev->ethtool_ops;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002082 struct phy_device *phydev = dev->phydev;
Florian Fainelli99943382018-04-25 12:12:48 -07002083 struct ethtool_stats stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002084 u64 *data;
2085 int ret, n_stats;
2086
Florian Fainelli99943382018-04-25 12:12:48 -07002087 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002088 return -EOPNOTSUPP;
2089
Florian Fainelli17809512020-07-08 09:46:25 -07002090 if (dev->phydev && !ops->get_ethtool_phy_stats &&
2091 phy_ops && phy_ops->get_sset_count)
2092 n_stats = phy_ops->get_sset_count(dev->phydev);
Florian Fainelli99943382018-04-25 12:12:48 -07002093 else
2094 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002095 if (n_stats < 0)
2096 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002097 if (n_stats > S32_MAX / sizeof(u64))
2098 return -ENOMEM;
2099 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002100
2101 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2102 return -EFAULT;
2103
2104 stats.n_stats = n_stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002105
Li RongQing3d883022019-03-29 09:18:02 +08002106 if (n_stats) {
2107 data = vzalloc(array_size(n_stats, sizeof(u64)));
2108 if (!data)
2109 return -ENOMEM;
2110
Florian Fainelli17809512020-07-08 09:46:25 -07002111 if (dev->phydev && !ops->get_ethtool_phy_stats &&
2112 phy_ops && phy_ops->get_stats) {
2113 ret = phy_ops->get_stats(dev->phydev, &stats, data);
Li RongQing3d883022019-03-29 09:18:02 +08002114 if (ret < 0)
2115 goto out;
2116 } else {
2117 ops->get_ethtool_phy_stats(dev, &stats, data);
2118 }
Florian Fainelli99943382018-04-25 12:12:48 -07002119 } else {
Li RongQing3d883022019-03-29 09:18:02 +08002120 data = NULL;
Florian Fainelli99943382018-04-25 12:12:48 -07002121 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01002122
2123 ret = -EFAULT;
2124 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2125 goto out;
2126 useraddr += sizeof(stats);
Gustavo A. R. Silva3dd14992020-06-15 18:01:07 -05002127 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002128 goto out;
2129 ret = 0;
2130
2131 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002132 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002133 return ret;
2134}
2135
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002136static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002137{
2138 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002139
Matthew Wilcox313674a2007-07-31 14:00:29 -07002140 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002141 return -EFAULT;
2142
Matthew Wilcox313674a2007-07-31 14:00:29 -07002143 if (epaddr.size < dev->addr_len)
2144 return -ETOOSMALL;
2145 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002146
Jon Wetzela6f9a702005-08-20 17:15:54 -07002147 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002148 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002149 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002150 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2151 return -EFAULT;
2152 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002153}
2154
Jeff Garzik13c99b22007-08-15 16:01:56 -07002155static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2156 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002157{
Roland Dreier8e557422010-02-11 12:14:23 -08002158 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002159
Jeff Garzik13c99b22007-08-15 16:01:56 -07002160 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002161 return -EOPNOTSUPP;
2162
Jeff Garzik13c99b22007-08-15 16:01:56 -07002163 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002164
2165 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2166 return -EFAULT;
2167 return 0;
2168}
2169
Jeff Garzik13c99b22007-08-15 16:01:56 -07002170static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2171 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002172{
2173 struct ethtool_value edata;
2174
Jeff Garzik13c99b22007-08-15 16:01:56 -07002175 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002176 return -EOPNOTSUPP;
2177
2178 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2179 return -EFAULT;
2180
Jeff Garzik13c99b22007-08-15 16:01:56 -07002181 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002182 return 0;
2183}
2184
Jeff Garzik13c99b22007-08-15 16:01:56 -07002185static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2186 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002187{
2188 struct ethtool_value edata;
2189
Jeff Garzik13c99b22007-08-15 16:01:56 -07002190 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002191 return -EOPNOTSUPP;
2192
2193 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2194 return -EFAULT;
2195
Jeff Garzik13c99b22007-08-15 16:01:56 -07002196 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002197}
2198
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002199static int
2200ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002201{
Jakub Kicinski1af0a092021-10-30 10:18:51 -07002202 if (!dev->ethtool_ops->flash_device) {
2203 req->devlink = netdev_to_devlink_get(dev);
2204 return 0;
2205 }
Jakub Kicinski4eceba12019-02-14 13:40:45 -08002206
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002207 return dev->ethtool_ops->flash_device(dev, &req->efl);
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002208}
2209
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002210static int ethtool_set_dump(struct net_device *dev,
2211 void __user *useraddr)
2212{
2213 struct ethtool_dump dump;
2214
2215 if (!dev->ethtool_ops->set_dump)
2216 return -EOPNOTSUPP;
2217
2218 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2219 return -EFAULT;
2220
2221 return dev->ethtool_ops->set_dump(dev, &dump);
2222}
2223
2224static int ethtool_get_dump_flag(struct net_device *dev,
2225 void __user *useraddr)
2226{
2227 int ret;
2228 struct ethtool_dump dump;
2229 const struct ethtool_ops *ops = dev->ethtool_ops;
2230
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002231 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002232 return -EOPNOTSUPP;
2233
2234 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2235 return -EFAULT;
2236
2237 ret = ops->get_dump_flag(dev, &dump);
2238 if (ret)
2239 return ret;
2240
2241 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2242 return -EFAULT;
2243 return 0;
2244}
2245
2246static int ethtool_get_dump_data(struct net_device *dev,
2247 void __user *useraddr)
2248{
2249 int ret;
2250 __u32 len;
2251 struct ethtool_dump dump, tmp;
2252 const struct ethtool_ops *ops = dev->ethtool_ops;
2253 void *data = NULL;
2254
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002255 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002256 return -EOPNOTSUPP;
2257
2258 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2259 return -EFAULT;
2260
2261 memset(&tmp, 0, sizeof(tmp));
2262 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2263 ret = ops->get_dump_flag(dev, &tmp);
2264 if (ret)
2265 return ret;
2266
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002267 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002268 if (!len)
2269 return -EFAULT;
2270
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002271 /* Don't ever let the driver think there's more space available
2272 * than it requested with .get_dump_flag().
2273 */
2274 dump.len = len;
2275
2276 /* Always allocate enough space to hold the whole thing so that the
2277 * driver does not need to check the length and bother with partial
2278 * dumping.
2279 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002280 data = vzalloc(tmp.len);
2281 if (!data)
2282 return -ENOMEM;
2283 ret = ops->get_dump_data(dev, &dump, data);
2284 if (ret)
2285 goto out;
2286
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002287 /* There are two sane possibilities:
2288 * 1. The driver's .get_dump_data() does not touch dump.len.
2289 * 2. Or it may set dump.len to how much it really writes, which
2290 * should be tmp.len (or len if it can do a partial dump).
2291 * In any case respond to userspace with the actual length of data
2292 * it's receiving.
2293 */
2294 WARN_ON(dump.len != len && dump.len != tmp.len);
2295 dump.len = len;
2296
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002297 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2298 ret = -EFAULT;
2299 goto out;
2300 }
2301 useraddr += offsetof(struct ethtool_dump, data);
2302 if (copy_to_user(useraddr, data, len))
2303 ret = -EFAULT;
2304out:
2305 vfree(data);
2306 return ret;
2307}
2308
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002309static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2310{
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002311 struct ethtool_ts_info info;
Michal Kubecek5b071c52020-03-28 00:01:58 +01002312 int err;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002313
Michal Kubecek5b071c52020-03-28 00:01:58 +01002314 err = __ethtool_get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002315 if (err)
2316 return err;
2317
2318 if (copy_to_user(useraddr, &info, sizeof(info)))
Michal Kubecek5b071c52020-03-28 00:01:58 +01002319 return -EFAULT;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002320
Michal Kubecek5b071c52020-03-28 00:01:58 +01002321 return 0;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002322}
2323
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002324int ethtool_get_module_info_call(struct net_device *dev,
2325 struct ethtool_modinfo *modinfo)
Ed Swierk2f438362015-01-02 17:27:56 -08002326{
2327 const struct ethtool_ops *ops = dev->ethtool_ops;
2328 struct phy_device *phydev = dev->phydev;
2329
Russell Kinge679c9c2018-03-28 15:44:16 -07002330 if (dev->sfp_bus)
2331 return sfp_get_module_info(dev->sfp_bus, modinfo);
2332
Ed Swierk2f438362015-01-02 17:27:56 -08002333 if (phydev && phydev->drv && phydev->drv->module_info)
2334 return phydev->drv->module_info(phydev, modinfo);
2335
2336 if (ops->get_module_info)
2337 return ops->get_module_info(dev, modinfo);
2338
2339 return -EOPNOTSUPP;
2340}
2341
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002342static int ethtool_get_module_info(struct net_device *dev,
2343 void __user *useraddr)
2344{
2345 int ret;
2346 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002347
2348 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2349 return -EFAULT;
2350
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002351 ret = ethtool_get_module_info_call(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002352 if (ret)
2353 return ret;
2354
2355 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2356 return -EFAULT;
2357
2358 return 0;
2359}
2360
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002361int ethtool_get_module_eeprom_call(struct net_device *dev,
2362 struct ethtool_eeprom *ee, u8 *data)
Ed Swierk2f438362015-01-02 17:27:56 -08002363{
2364 const struct ethtool_ops *ops = dev->ethtool_ops;
2365 struct phy_device *phydev = dev->phydev;
2366
Russell Kinge679c9c2018-03-28 15:44:16 -07002367 if (dev->sfp_bus)
2368 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2369
Ed Swierk2f438362015-01-02 17:27:56 -08002370 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2371 return phydev->drv->module_eeprom(phydev, ee, data);
2372
2373 if (ops->get_module_eeprom)
2374 return ops->get_module_eeprom(dev, ee, data);
2375
2376 return -EOPNOTSUPP;
2377}
2378
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002379static int ethtool_get_module_eeprom(struct net_device *dev,
2380 void __user *useraddr)
2381{
2382 int ret;
2383 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002384
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002385 ret = ethtool_get_module_info_call(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002386 if (ret)
2387 return ret;
2388
Ed Swierk2f438362015-01-02 17:27:56 -08002389 return ethtool_get_any_eeprom(dev, useraddr,
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002390 ethtool_get_module_eeprom_call,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002391 modinfo.eeprom_len);
2392}
2393
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302394static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2395{
2396 switch (tuna->id) {
2397 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002398 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302399 if (tuna->len != sizeof(u32) ||
2400 tuna->type_id != ETHTOOL_TUNABLE_U32)
2401 return -EINVAL;
2402 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002403 case ETHTOOL_PFC_PREVENTION_TOUT:
2404 if (tuna->len != sizeof(u16) ||
2405 tuna->type_id != ETHTOOL_TUNABLE_U16)
2406 return -EINVAL;
2407 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302408 default:
2409 return -EINVAL;
2410 }
2411
2412 return 0;
2413}
2414
2415static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2416{
2417 int ret;
2418 struct ethtool_tunable tuna;
2419 const struct ethtool_ops *ops = dev->ethtool_ops;
2420 void *data;
2421
2422 if (!ops->get_tunable)
2423 return -EOPNOTSUPP;
2424 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2425 return -EFAULT;
2426 ret = ethtool_tunable_valid(&tuna);
2427 if (ret)
2428 return ret;
Austin Kim80ec82e2021-06-09 03:34:25 +01002429 data = kzalloc(tuna.len, GFP_USER);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302430 if (!data)
2431 return -ENOMEM;
2432 ret = ops->get_tunable(dev, &tuna, data);
2433 if (ret)
2434 goto out;
2435 useraddr += sizeof(tuna);
2436 ret = -EFAULT;
2437 if (copy_to_user(useraddr, data, tuna.len))
2438 goto out;
2439 ret = 0;
2440
2441out:
2442 kfree(data);
2443 return ret;
2444}
2445
2446static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2447{
2448 int ret;
2449 struct ethtool_tunable tuna;
2450 const struct ethtool_ops *ops = dev->ethtool_ops;
2451 void *data;
2452
2453 if (!ops->set_tunable)
2454 return -EOPNOTSUPP;
2455 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2456 return -EFAULT;
2457 ret = ethtool_tunable_valid(&tuna);
2458 if (ret)
2459 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302460 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002461 data = memdup_user(useraddr, tuna.len);
2462 if (IS_ERR(data))
2463 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302464 ret = ops->set_tunable(dev, &tuna, data);
2465
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302466 kfree(data);
2467 return ret;
2468}
2469
Arnd Bergmann3499e872019-03-07 16:58:35 +01002470static noinline_for_stack int
2471ethtool_get_per_queue_coalesce(struct net_device *dev,
2472 void __user *useraddr,
2473 struct ethtool_per_queue_op *per_queue_opt)
Kan Liang421797b2016-02-19 09:24:02 -05002474{
2475 u32 bit;
2476 int ret;
2477 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2478
2479 if (!dev->ethtool_ops->get_per_queue_coalesce)
2480 return -EOPNOTSUPP;
2481
2482 useraddr += sizeof(*per_queue_opt);
2483
Yury Norov3aa56882018-02-06 15:38:06 -08002484 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2485 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002486
2487 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2488 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2489
2490 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2491 if (ret != 0)
2492 return ret;
2493 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2494 return -EFAULT;
2495 useraddr += sizeof(coalesce);
2496 }
2497
2498 return 0;
2499}
2500
Arnd Bergmann3499e872019-03-07 16:58:35 +01002501static noinline_for_stack int
2502ethtool_set_per_queue_coalesce(struct net_device *dev,
2503 void __user *useraddr,
2504 struct ethtool_per_queue_op *per_queue_opt)
Kan Liangf38d1382016-02-19 09:24:03 -05002505{
2506 u32 bit;
2507 int i, ret = 0;
2508 int n_queue;
2509 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2510 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2511
2512 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2513 (!dev->ethtool_ops->get_per_queue_coalesce))
2514 return -EOPNOTSUPP;
2515
2516 useraddr += sizeof(*per_queue_opt);
2517
Yury Norov3aa56882018-02-06 15:38:06 -08002518 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002519 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2520 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2521 if (!backup)
2522 return -ENOMEM;
2523
2524 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2525 struct ethtool_coalesce coalesce;
2526
2527 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2528 if (ret != 0)
2529 goto roll_back;
2530
2531 tmp++;
2532
2533 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2534 ret = -EFAULT;
2535 goto roll_back;
2536 }
2537
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08002538 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2539 ret = -EOPNOTSUPP;
2540 goto roll_back;
2541 }
2542
Kan Liangf38d1382016-02-19 09:24:03 -05002543 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2544 if (ret != 0)
2545 goto roll_back;
2546
2547 useraddr += sizeof(coalesce);
2548 }
2549
2550roll_back:
2551 if (ret != 0) {
2552 tmp = backup;
2553 for_each_set_bit(i, queue_mask, bit) {
2554 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2555 tmp++;
2556 }
2557 }
2558 kfree(backup);
2559
2560 return ret;
2561}
2562
Arnd Bergmann3499e872019-03-07 16:58:35 +01002563static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002564 void __user *useraddr, u32 sub_cmd)
Kan Liangac2c7ad2016-02-19 09:24:01 -05002565{
2566 struct ethtool_per_queue_op per_queue_opt;
2567
2568 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2569 return -EFAULT;
2570
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002571 if (per_queue_opt.sub_command != sub_cmd)
2572 return -EINVAL;
2573
Kan Liangac2c7ad2016-02-19 09:24:01 -05002574 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002575 case ETHTOOL_GCOALESCE:
2576 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002577 case ETHTOOL_SCOALESCE:
2578 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002579 default:
2580 return -EOPNOTSUPP;
Tom Rix9d253c02020-11-01 07:56:01 -08002581 }
Kan Liangac2c7ad2016-02-19 09:24:01 -05002582}
2583
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002584static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2585{
2586 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002587 case ETHTOOL_PHY_DOWNSHIFT:
Heiner Kallweit3aeb0802019-03-25 19:34:58 +01002588 case ETHTOOL_PHY_FAST_LINK_DOWN:
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002589 if (tuna->len != sizeof(u8) ||
2590 tuna->type_id != ETHTOOL_TUNABLE_U8)
2591 return -EINVAL;
2592 break;
Alexandru Ardelean9f2f13f2019-09-16 10:35:25 +03002593 case ETHTOOL_PHY_EDPD:
2594 if (tuna->len != sizeof(u16) ||
2595 tuna->type_id != ETHTOOL_TUNABLE_U16)
2596 return -EINVAL;
2597 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002598 default:
2599 return -EINVAL;
2600 }
2601
2602 return 0;
2603}
2604
2605static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2606{
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002607 struct phy_device *phydev = dev->phydev;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002608 struct ethtool_tunable tuna;
2609 bool phy_drv_tunable;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002610 void *data;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002611 int ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002612
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002613 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2614 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002615 return -EOPNOTSUPP;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002616 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2617 return -EFAULT;
2618 ret = ethtool_phy_tunable_valid(&tuna);
2619 if (ret)
2620 return ret;
Austin Kim80ec82e2021-06-09 03:34:25 +01002621 data = kzalloc(tuna.len, GFP_USER);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002622 if (!data)
2623 return -ENOMEM;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002624 if (phy_drv_tunable) {
2625 mutex_lock(&phydev->lock);
2626 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2627 mutex_unlock(&phydev->lock);
2628 } else {
2629 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2630 }
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002631 if (ret)
2632 goto out;
2633 useraddr += sizeof(tuna);
2634 ret = -EFAULT;
2635 if (copy_to_user(useraddr, data, tuna.len))
2636 goto out;
2637 ret = 0;
2638
2639out:
2640 kfree(data);
2641 return ret;
2642}
2643
2644static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2645{
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002646 struct phy_device *phydev = dev->phydev;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002647 struct ethtool_tunable tuna;
2648 bool phy_drv_tunable;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002649 void *data;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002650 int ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002651
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002652 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2653 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002654 return -EOPNOTSUPP;
2655 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2656 return -EFAULT;
2657 ret = ethtool_phy_tunable_valid(&tuna);
2658 if (ret)
2659 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002660 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002661 data = memdup_user(useraddr, tuna.len);
2662 if (IS_ERR(data))
2663 return PTR_ERR(data);
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002664 if (phy_drv_tunable) {
2665 mutex_lock(&phydev->lock);
2666 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2667 mutex_unlock(&phydev->lock);
2668 } else {
2669 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2670 }
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002671
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002672 kfree(data);
2673 return ret;
2674}
2675
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002676static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2677{
Li RongQinge83887f2019-02-27 20:47:57 +08002678 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002679 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002680
2681 if (!dev->ethtool_ops->get_fecparam)
2682 return -EOPNOTSUPP;
2683
Edward Creea6d50512018-02-28 19:15:58 +00002684 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2685 if (rc)
2686 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002687
Jakub Kicinski240e11442021-03-24 18:11:57 -07002688 if (WARN_ON_ONCE(fecparam.reserved))
2689 fecparam.reserved = 0;
2690
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002691 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2692 return -EFAULT;
2693 return 0;
2694}
2695
2696static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2697{
2698 struct ethtool_fecparam fecparam;
2699
2700 if (!dev->ethtool_ops->set_fecparam)
2701 return -EOPNOTSUPP;
2702
2703 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2704 return -EFAULT;
2705
Jakub Kicinskicf2cc0b2021-03-26 13:22:22 -07002706 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
Jakub Kicinski42ce1272021-03-24 18:11:59 -07002707 return -EINVAL;
2708
Jakub Kicinskid3b37fc2021-03-24 18:11:58 -07002709 fecparam.active_fec = 0;
Jakub Kicinski240e11442021-03-24 18:11:57 -07002710 fecparam.reserved = 0;
2711
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002712 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2713}
2714
Yan Burman600fed52013-06-03 02:03:34 +00002715/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07002717static int
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002718__dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2719 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002721 struct net_device *dev;
2722 u32 sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002724 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002726 dev = __dev_get_by_name(net, ifr->ifr_name);
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002727 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return -ENODEV;
2729
Kan Liangac2c7ad2016-02-19 09:24:01 -05002730 if (ethcmd == ETHTOOL_PERQUEUE) {
2731 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2732 return -EFAULT;
2733 } else {
2734 sub_cmd = ethcmd;
2735 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002736 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002737 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002738 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002739 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002740 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002741 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002742 case ETHTOOL_GCOALESCE:
2743 case ETHTOOL_GRINGPARAM:
2744 case ETHTOOL_GPAUSEPARAM:
2745 case ETHTOOL_GRXCSUM:
2746 case ETHTOOL_GTXCSUM:
2747 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002748 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002749 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002750 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002751 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002752 case ETHTOOL_GTSO:
2753 case ETHTOOL_GPERMADDR:
Maciej Żenczykowski474ff262018-09-22 01:34:01 -07002754 case ETHTOOL_GUFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002755 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002756 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002757 case ETHTOOL_GFLAGS:
2758 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002759 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002760 case ETHTOOL_GRXRINGS:
2761 case ETHTOOL_GRXCLSRLCNT:
2762 case ETHTOOL_GRXCLSRULE:
2763 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002764 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302765 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002766 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002767 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002768 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002769 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302770 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002771 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002772 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002773 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002774 break;
2775 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002776 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002777 return -EPERM;
2778 }
2779
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002780 if (dev->dev.parent)
2781 pm_runtime_get_sync(dev->dev.parent);
2782
2783 if (!netif_device_present(dev)) {
2784 rc = -ENODEV;
2785 goto out;
2786 }
2787
chavey97f8aef2010-04-07 21:54:42 -07002788 if (dev->ethtool_ops->begin) {
2789 rc = dev->ethtool_ops->begin(dev);
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002790 if (rc < 0)
2791 goto out;
chavey97f8aef2010-04-07 21:54:42 -07002792 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002793 old_features = dev->features;
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 switch (ethcmd) {
2796 case ETHTOOL_GSET:
2797 rc = ethtool_get_settings(dev, useraddr);
2798 break;
2799 case ETHTOOL_SSET:
2800 rc = ethtool_set_settings(dev, useraddr);
2801 break;
2802 case ETHTOOL_GDRVINFO:
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002803 rc = ethtool_get_drvinfo(dev, devlink_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 break;
2805 case ETHTOOL_GREGS:
2806 rc = ethtool_get_regs(dev, useraddr);
2807 break;
2808 case ETHTOOL_GWOL:
2809 rc = ethtool_get_wol(dev, useraddr);
2810 break;
2811 case ETHTOOL_SWOL:
2812 rc = ethtool_set_wol(dev, useraddr);
2813 break;
2814 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002815 rc = ethtool_get_value(dev, useraddr, ethcmd,
2816 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 break;
2818 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002819 rc = ethtool_set_value_void(dev, useraddr,
2820 dev->ethtool_ops->set_msglevel);
Michal Kubecek0bda7af2020-01-26 23:11:10 +01002821 if (!rc)
2822 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002824 case ETHTOOL_GEEE:
2825 rc = ethtool_get_eee(dev, useraddr);
2826 break;
2827 case ETHTOOL_SEEE:
2828 rc = ethtool_set_eee(dev, useraddr);
2829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 case ETHTOOL_NWAY_RST:
2831 rc = ethtool_nway_reset(dev);
2832 break;
2833 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002834 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 break;
2836 case ETHTOOL_GEEPROM:
2837 rc = ethtool_get_eeprom(dev, useraddr);
2838 break;
2839 case ETHTOOL_SEEPROM:
2840 rc = ethtool_set_eeprom(dev, useraddr);
2841 break;
2842 case ETHTOOL_GCOALESCE:
2843 rc = ethtool_get_coalesce(dev, useraddr);
2844 break;
2845 case ETHTOOL_SCOALESCE:
2846 rc = ethtool_set_coalesce(dev, useraddr);
2847 break;
2848 case ETHTOOL_GRINGPARAM:
2849 rc = ethtool_get_ringparam(dev, useraddr);
2850 break;
2851 case ETHTOOL_SRINGPARAM:
2852 rc = ethtool_set_ringparam(dev, useraddr);
2853 break;
2854 case ETHTOOL_GPAUSEPARAM:
2855 rc = ethtool_get_pauseparam(dev, useraddr);
2856 break;
2857 case ETHTOOL_SPAUSEPARAM:
2858 rc = ethtool_set_pauseparam(dev, useraddr);
2859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 case ETHTOOL_TEST:
2861 rc = ethtool_self_test(dev, useraddr);
2862 break;
2863 case ETHTOOL_GSTRINGS:
2864 rc = ethtool_get_strings(dev, useraddr);
2865 break;
2866 case ETHTOOL_PHYS_ID:
2867 rc = ethtool_phys_id(dev, useraddr);
2868 break;
2869 case ETHTOOL_GSTATS:
2870 rc = ethtool_get_stats(dev, useraddr);
2871 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002872 case ETHTOOL_GPERMADDR:
2873 rc = ethtool_get_perm_addr(dev, useraddr);
2874 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002875 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002876 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002877 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002878 break;
2879 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002880 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002881 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002882 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002883 rc = ethtool_get_value(dev, useraddr, ethcmd,
2884 dev->ethtool_ops->get_priv_flags);
Michal Kubecek111dcba2020-03-12 21:08:18 +01002885 if (!rc)
2886 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
Jeff Garzik339bf022007-08-15 16:01:32 -07002887 break;
2888 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002889 rc = ethtool_set_value(dev, useraddr,
2890 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002891 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002892 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002893 case ETHTOOL_GRXRINGS:
2894 case ETHTOOL_GRXCLSRLCNT:
2895 case ETHTOOL_GRXCLSRULE:
2896 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002897 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002898 break;
2899 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002900 case ETHTOOL_SRXCLSRLDEL:
2901 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002902 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002903 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002904 case ETHTOOL_FLASHDEV:
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002905 rc = ethtool_flash_device(dev, devlink_state);
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002906 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002907 case ETHTOOL_RESET:
2908 rc = ethtool_reset(dev, useraddr);
2909 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002910 case ETHTOOL_GSSET_INFO:
2911 rc = ethtool_get_sset_info(dev, useraddr);
2912 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002913 case ETHTOOL_GRXFHINDIR:
2914 rc = ethtool_get_rxfh_indir(dev, useraddr);
2915 break;
2916 case ETHTOOL_SRXFHINDIR:
2917 rc = ethtool_set_rxfh_indir(dev, useraddr);
2918 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302919 case ETHTOOL_GRSSH:
2920 rc = ethtool_get_rxfh(dev, useraddr);
2921 break;
2922 case ETHTOOL_SRSSH:
2923 rc = ethtool_set_rxfh(dev, useraddr);
2924 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002925 case ETHTOOL_GFEATURES:
2926 rc = ethtool_get_features(dev, useraddr);
2927 break;
2928 case ETHTOOL_SFEATURES:
2929 rc = ethtool_set_features(dev, useraddr);
2930 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002931 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002932 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002933 case ETHTOOL_GSG:
2934 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002935 case ETHTOOL_GGSO:
2936 case ETHTOOL_GGRO:
2937 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2938 break;
2939 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002940 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002941 case ETHTOOL_SSG:
2942 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002943 case ETHTOOL_SGSO:
2944 case ETHTOOL_SGRO:
2945 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2946 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002947 case ETHTOOL_GCHANNELS:
2948 rc = ethtool_get_channels(dev, useraddr);
2949 break;
2950 case ETHTOOL_SCHANNELS:
2951 rc = ethtool_set_channels(dev, useraddr);
2952 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002953 case ETHTOOL_SET_DUMP:
2954 rc = ethtool_set_dump(dev, useraddr);
2955 break;
2956 case ETHTOOL_GET_DUMP_FLAG:
2957 rc = ethtool_get_dump_flag(dev, useraddr);
2958 break;
2959 case ETHTOOL_GET_DUMP_DATA:
2960 rc = ethtool_get_dump_data(dev, useraddr);
2961 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002962 case ETHTOOL_GET_TS_INFO:
2963 rc = ethtool_get_ts_info(dev, useraddr);
2964 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002965 case ETHTOOL_GMODULEINFO:
2966 rc = ethtool_get_module_info(dev, useraddr);
2967 break;
2968 case ETHTOOL_GMODULEEEPROM:
2969 rc = ethtool_get_module_eeprom(dev, useraddr);
2970 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302971 case ETHTOOL_GTUNABLE:
2972 rc = ethtool_get_tunable(dev, useraddr);
2973 break;
2974 case ETHTOOL_STUNABLE:
2975 rc = ethtool_set_tunable(dev, useraddr);
2976 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002977 case ETHTOOL_GPHYSTATS:
2978 rc = ethtool_get_phy_stats(dev, useraddr);
2979 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002980 case ETHTOOL_PERQUEUE:
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002981 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002982 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002983 case ETHTOOL_GLINKSETTINGS:
2984 rc = ethtool_get_link_ksettings(dev, useraddr);
2985 break;
2986 case ETHTOOL_SLINKSETTINGS:
2987 rc = ethtool_set_link_ksettings(dev, useraddr);
2988 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002989 case ETHTOOL_PHY_GTUNABLE:
2990 rc = get_phy_tunable(dev, useraddr);
2991 break;
2992 case ETHTOOL_PHY_STUNABLE:
2993 rc = set_phy_tunable(dev, useraddr);
2994 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002995 case ETHTOOL_GFECPARAM:
2996 rc = ethtool_get_fecparam(dev, useraddr);
2997 break;
2998 case ETHTOOL_SFECPARAM:
2999 rc = ethtool_set_fecparam(dev, useraddr);
3000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07003002 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003004
Stephen Hemmingere71a4782007-04-10 20:10:33 -07003005 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07003007
3008 if (old_features != dev->features)
3009 netdev_features_change(dev);
Heiner Kallweitf32a2132021-08-01 12:36:48 +02003010out:
3011 if (dev->dev.parent)
3012 pm_runtime_put(dev->dev.parent);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07003013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003016
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07003017int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3018{
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003019 struct ethtool_devlink_compat *state;
3020 u32 ethcmd;
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07003021 int rc;
3022
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003023 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3024 return -EFAULT;
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07003025
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003026 state = kzalloc(sizeof(*state), GFP_KERNEL);
3027 if (!state)
3028 return -ENOMEM;
3029
3030 switch (ethcmd) {
3031 case ETHTOOL_FLASHDEV:
3032 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3033 rc = -EFAULT;
3034 goto exit_free;
3035 }
3036 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3037 break;
3038 }
3039
3040 rtnl_lock();
3041 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3042 rtnl_unlock();
3043 if (rc)
3044 goto exit_free;
3045
3046 switch (ethcmd) {
Jakub Kicinski1af0a092021-10-30 10:18:51 -07003047 case ETHTOOL_FLASHDEV:
3048 if (state->devlink)
3049 rc = devlink_compat_flash_update(state->devlink,
3050 state->efl.data);
3051 break;
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003052 case ETHTOOL_GDRVINFO:
Jakub Kicinski1af0a092021-10-30 10:18:51 -07003053 if (state->devlink)
3054 devlink_compat_running_version(state->devlink,
3055 state->info.fw_version,
3056 sizeof(state->info.fw_version));
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003057 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3058 rc = -EFAULT;
3059 goto exit_free;
3060 }
3061 break;
3062 }
3063
3064exit_free:
Jakub Kicinski1af0a092021-10-30 10:18:51 -07003065 if (state->devlink)
3066 devlink_put(state->devlink);
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003067 kfree(state);
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07003068 return rc;
3069}
3070
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003071struct ethtool_rx_flow_key {
3072 struct flow_dissector_key_basic basic;
3073 union {
3074 struct flow_dissector_key_ipv4_addrs ipv4;
3075 struct flow_dissector_key_ipv6_addrs ipv6;
3076 };
3077 struct flow_dissector_key_ports tp;
3078 struct flow_dissector_key_ip ip;
3079 struct flow_dissector_key_vlan vlan;
3080 struct flow_dissector_key_eth_addrs eth_addrs;
3081} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3082
3083struct ethtool_rx_flow_match {
3084 struct flow_dissector dissector;
3085 struct ethtool_rx_flow_key key;
3086 struct ethtool_rx_flow_key mask;
3087};
3088
3089struct ethtool_rx_flow_rule *
3090ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3091{
3092 const struct ethtool_rx_flow_spec *fs = input->fs;
3093 static struct in6_addr zero_addr = {};
3094 struct ethtool_rx_flow_match *match;
3095 struct ethtool_rx_flow_rule *flow;
3096 struct flow_action_entry *act;
3097
3098 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3099 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3100 if (!flow)
3101 return ERR_PTR(-ENOMEM);
3102
3103 /* ethtool_rx supports only one single action per rule. */
3104 flow->rule = flow_rule_alloc(1);
3105 if (!flow->rule) {
3106 kfree(flow);
3107 return ERR_PTR(-ENOMEM);
3108 }
3109
3110 match = (struct ethtool_rx_flow_match *)flow->priv;
3111 flow->rule->match.dissector = &match->dissector;
3112 flow->rule->match.mask = &match->mask;
3113 flow->rule->match.key = &match->key;
3114
3115 match->mask.basic.n_proto = htons(0xffff);
3116
3117 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
Maxime Chevallier5b9469a2019-06-27 10:52:26 +02003118 case ETHER_FLOW: {
3119 const struct ethhdr *ether_spec, *ether_m_spec;
3120
3121 ether_spec = &fs->h_u.ether_spec;
3122 ether_m_spec = &fs->m_u.ether_spec;
3123
3124 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3125 ether_addr_copy(match->key.eth_addrs.src,
3126 ether_spec->h_source);
3127 ether_addr_copy(match->mask.eth_addrs.src,
3128 ether_m_spec->h_source);
3129 }
3130 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3131 ether_addr_copy(match->key.eth_addrs.dst,
3132 ether_spec->h_dest);
3133 ether_addr_copy(match->mask.eth_addrs.dst,
3134 ether_m_spec->h_dest);
3135 }
3136 if (ether_m_spec->h_proto) {
3137 match->key.basic.n_proto = ether_spec->h_proto;
3138 match->mask.basic.n_proto = ether_m_spec->h_proto;
3139 }
3140 }
3141 break;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003142 case TCP_V4_FLOW:
3143 case UDP_V4_FLOW: {
3144 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3145
3146 match->key.basic.n_proto = htons(ETH_P_IP);
3147
3148 v4_spec = &fs->h_u.tcp_ip4_spec;
3149 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3150
3151 if (v4_m_spec->ip4src) {
3152 match->key.ipv4.src = v4_spec->ip4src;
3153 match->mask.ipv4.src = v4_m_spec->ip4src;
3154 }
3155 if (v4_m_spec->ip4dst) {
3156 match->key.ipv4.dst = v4_spec->ip4dst;
3157 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3158 }
3159 if (v4_m_spec->ip4src ||
3160 v4_m_spec->ip4dst) {
3161 match->dissector.used_keys |=
3162 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3163 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3164 offsetof(struct ethtool_rx_flow_key, ipv4);
3165 }
3166 if (v4_m_spec->psrc) {
3167 match->key.tp.src = v4_spec->psrc;
3168 match->mask.tp.src = v4_m_spec->psrc;
3169 }
3170 if (v4_m_spec->pdst) {
3171 match->key.tp.dst = v4_spec->pdst;
3172 match->mask.tp.dst = v4_m_spec->pdst;
3173 }
3174 if (v4_m_spec->psrc ||
3175 v4_m_spec->pdst) {
3176 match->dissector.used_keys |=
3177 BIT(FLOW_DISSECTOR_KEY_PORTS);
3178 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3179 offsetof(struct ethtool_rx_flow_key, tp);
3180 }
3181 if (v4_m_spec->tos) {
3182 match->key.ip.tos = v4_spec->tos;
3183 match->mask.ip.tos = v4_m_spec->tos;
3184 match->dissector.used_keys |=
3185 BIT(FLOW_DISSECTOR_KEY_IP);
3186 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3187 offsetof(struct ethtool_rx_flow_key, ip);
3188 }
3189 }
3190 break;
3191 case TCP_V6_FLOW:
3192 case UDP_V6_FLOW: {
3193 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3194
3195 match->key.basic.n_proto = htons(ETH_P_IPV6);
3196
3197 v6_spec = &fs->h_u.tcp_ip6_spec;
3198 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3199 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
3200 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3201 sizeof(match->key.ipv6.src));
3202 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3203 sizeof(match->mask.ipv6.src));
3204 }
3205 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3206 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3207 sizeof(match->key.ipv6.dst));
3208 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3209 sizeof(match->mask.ipv6.dst));
3210 }
3211 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
Gaurav Singh21a739c2020-06-21 11:30:17 -04003212 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003213 match->dissector.used_keys |=
3214 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3215 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3216 offsetof(struct ethtool_rx_flow_key, ipv6);
3217 }
3218 if (v6_m_spec->psrc) {
3219 match->key.tp.src = v6_spec->psrc;
3220 match->mask.tp.src = v6_m_spec->psrc;
3221 }
3222 if (v6_m_spec->pdst) {
3223 match->key.tp.dst = v6_spec->pdst;
3224 match->mask.tp.dst = v6_m_spec->pdst;
3225 }
3226 if (v6_m_spec->psrc ||
3227 v6_m_spec->pdst) {
3228 match->dissector.used_keys |=
3229 BIT(FLOW_DISSECTOR_KEY_PORTS);
3230 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3231 offsetof(struct ethtool_rx_flow_key, tp);
3232 }
3233 if (v6_m_spec->tclass) {
3234 match->key.ip.tos = v6_spec->tclass;
3235 match->mask.ip.tos = v6_m_spec->tclass;
3236 match->dissector.used_keys |=
3237 BIT(FLOW_DISSECTOR_KEY_IP);
3238 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3239 offsetof(struct ethtool_rx_flow_key, ip);
3240 }
3241 }
3242 break;
3243 default:
3244 ethtool_rx_flow_rule_destroy(flow);
3245 return ERR_PTR(-EINVAL);
3246 }
3247
3248 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3249 case TCP_V4_FLOW:
3250 case TCP_V6_FLOW:
3251 match->key.basic.ip_proto = IPPROTO_TCP;
Vishal Kulkarnid0a84e12020-08-19 00:25:03 +05303252 match->mask.basic.ip_proto = 0xff;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003253 break;
3254 case UDP_V4_FLOW:
3255 case UDP_V6_FLOW:
3256 match->key.basic.ip_proto = IPPROTO_UDP;
Vishal Kulkarnid0a84e12020-08-19 00:25:03 +05303257 match->mask.basic.ip_proto = 0xff;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003258 break;
3259 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003260
3261 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3262 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3263 offsetof(struct ethtool_rx_flow_key, basic);
3264
3265 if (fs->flow_type & FLOW_EXT) {
3266 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3267 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3268
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003269 if (ext_m_spec->vlan_etype) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003270 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3271 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003272 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003273
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003274 if (ext_m_spec->vlan_tci) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003275 match->key.vlan.vlan_id =
3276 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3277 match->mask.vlan.vlan_id =
3278 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3279
Maxime Chevallierf0d2ca12019-06-12 17:18:38 +02003280 match->key.vlan.vlan_dei =
3281 !!(ext_h_spec->vlan_tci & htons(0x1000));
3282 match->mask.vlan.vlan_dei =
3283 !!(ext_m_spec->vlan_tci & htons(0x1000));
3284
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003285 match->key.vlan.vlan_priority =
3286 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3287 match->mask.vlan.vlan_priority =
3288 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003289 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003290
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003291 if (ext_m_spec->vlan_etype ||
3292 ext_m_spec->vlan_tci) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003293 match->dissector.used_keys |=
3294 BIT(FLOW_DISSECTOR_KEY_VLAN);
3295 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3296 offsetof(struct ethtool_rx_flow_key, vlan);
3297 }
3298 }
3299 if (fs->flow_type & FLOW_MAC_EXT) {
3300 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3301 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3302
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003303 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3304 ETH_ALEN);
3305 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3306 ETH_ALEN);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003307
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003308 match->dissector.used_keys |=
3309 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3310 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3311 offsetof(struct ethtool_rx_flow_key, eth_addrs);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003312 }
3313
3314 act = &flow->rule->action.entries[0];
3315 switch (fs->ring_cookie) {
3316 case RX_CLS_FLOW_DISC:
3317 act->id = FLOW_ACTION_DROP;
3318 break;
3319 case RX_CLS_FLOW_WAKE:
3320 act->id = FLOW_ACTION_WAKE;
3321 break;
3322 default:
3323 act->id = FLOW_ACTION_QUEUE;
3324 if (fs->flow_type & FLOW_RSS)
3325 act->queue.ctx = input->rss_ctx;
3326
3327 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3328 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3329 break;
3330 }
3331
3332 return flow;
3333}
3334EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3335
3336void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3337{
3338 kfree(flow->rule);
3339 kfree(flow);
3340}
3341EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);