blob: e645d751a5e8998064ef5fa239d465f66c044e6f [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>
Jakub Kicinskib6459412021-12-28 16:49:13 -080011#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080014#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/ethtool.h>
17#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000018#include <linux/net_tstamp.h>
19#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000020#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070021#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070022#include <linux/vmalloc.h>
Russell Kinge679c9c2018-03-28 15:44:16 -070023#include <linux/sfp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010025#include <linux/rtnetlink.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080027#include <linux/net.h>
Heiner Kallweitf32a2132021-08-01 12:36:48 +020028#include <linux/pm_runtime.h>
Jann Hornd2efeb52024-02-20 20:42:44 +010029#include <linux/utsname.h>
Jakub Kicinskiddb6e992019-01-31 10:50:47 -080030#include <net/devlink.h>
Kuniyuki Iwashima8cdc3222023-03-27 16:54:54 -070031#include <net/ipv6.h>
Magnus Karlssona71506a2020-05-20 21:20:51 +020032#include <net/xdp_sock_drv.h>
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +010033#include <net/flow_offload.h>
Michal Kubecek73286732019-12-27 15:56:03 +010034#include <linux/ethtool_netlink.h>
Michal Kubecekd44e1312019-12-11 10:58:29 +010035#include "common.h"
36
Jakub Kicinski095cfcf2021-10-30 10:18:49 -070037/* State held across locks and calls for commands which have devlink fallback */
38struct ethtool_devlink_compat {
Jakub Kicinski1af0a092021-10-30 10:18:51 -070039 struct devlink *devlink;
Jakub Kicinski095cfcf2021-10-30 10:18:49 -070040 union {
41 struct ethtool_flash efl;
42 struct ethtool_drvinfo info;
43 };
44};
45
Jakub Kicinski1af0a092021-10-30 10:18:51 -070046static struct devlink *netdev_to_devlink_get(struct net_device *dev)
47{
Jiri Pirko8eba37f2022-11-02 17:02:09 +010048 if (!dev->devlink_port)
Jakub Kicinski1af0a092021-10-30 10:18:51 -070049 return NULL;
Jiri Pirko8eba37f2022-11-02 17:02:09 +010050 return devlink_try_get(dev->devlink_port->devlink);
Jakub Kicinski1af0a092021-10-30 10:18:51 -070051}
52
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Some useful ethtool_ops methods that're device independent.
55 * If we find that all drivers want to do the same thing here,
56 * we can turn these into dev_() function calls.
57 */
58
59u32 ethtool_op_get_link(struct net_device *dev)
60{
Johannes Bergfacd15d2023-12-04 21:47:07 +010061 /* Synchronize carrier state with link watch, see also rtnl_getlink() */
62 linkwatch_sync_dev(dev);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return netif_carrier_ok(dev) ? 1 : 0;
65}
chavey97f8aef2010-04-07 21:54:42 -070066EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Richard Cochran02eacbd2012-04-03 22:59:22 +000068int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
69{
70 info->so_timestamping =
71 SOF_TIMESTAMPING_TX_SOFTWARE |
72 SOF_TIMESTAMPING_RX_SOFTWARE |
73 SOF_TIMESTAMPING_SOFTWARE;
74 info->phc_index = -1;
75 return 0;
76}
77EXPORT_SYMBOL(ethtool_op_get_ts_info);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Handlers for each ethtool command */
80
Michał Mirosław5455c692011-02-15 16:59:17 +000081static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
82{
83 struct ethtool_gfeatures cmd = {
84 .cmd = ETHTOOL_GFEATURES,
85 .size = ETHTOOL_DEV_FEATURE_WORDS,
86 };
Michał Mirosław475414f2011-11-15 15:29:55 +000087 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +000088 u32 __user *sizeaddr;
89 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +000090 int i;
91
92 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +000093 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +000094
95 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +000096 features[i].available = (u32)(dev->hw_features >> (32 * i));
97 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
98 features[i].active = (u32)(dev->features >> (32 * i));
99 features[i].never_changed =
100 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000101 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000102
103 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
104 if (get_user(copy_size, sizeaddr))
105 return -EFAULT;
106
107 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
108 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
109
110 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
111 return -EFAULT;
112 useraddr += sizeof(cmd);
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -0500113 if (copy_to_user(useraddr, features,
114 array_size(copy_size, sizeof(*features))))
Michał Mirosław5455c692011-02-15 16:59:17 +0000115 return -EFAULT;
116
117 return 0;
118}
119
120static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
121{
122 struct ethtool_sfeatures cmd;
123 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000124 netdev_features_t wanted = 0, valid = 0;
125 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000126
127 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
128 return -EFAULT;
129 useraddr += sizeof(cmd);
130
131 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
132 return -EINVAL;
133
134 if (copy_from_user(features, useraddr, sizeof(features)))
135 return -EFAULT;
136
Michał Mirosław475414f2011-11-15 15:29:55 +0000137 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000138 valid |= (netdev_features_t)features[i].valid << (32 * i);
139 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000140 }
141
142 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000143 return -EINVAL;
144
Michał Mirosław475414f2011-11-15 15:29:55 +0000145 if (valid & ~dev->hw_features) {
146 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000147 ret |= ETHTOOL_F_UNSUPPORTED;
148 }
149
Michał Mirosław475414f2011-11-15 15:29:55 +0000150 dev->wanted_features &= ~valid;
151 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700152 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000153
Michał Mirosław475414f2011-11-15 15:29:55 +0000154 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000155 ret |= ETHTOOL_F_WISH;
156
157 return ret;
158}
159
Michał Mirosław340ae162011-02-15 16:59:16 +0000160static int __ethtool_get_sset_count(struct net_device *dev, int sset)
161{
Florian Fainelli17809512020-07-08 09:46:25 -0700162 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
Michał Mirosław340ae162011-02-15 16:59:16 +0000163 const struct ethtool_ops *ops = dev->ethtool_ops;
164
Michał Mirosław5455c692011-02-15 16:59:17 +0000165 if (sset == ETH_SS_FEATURES)
166 return ARRAY_SIZE(netdev_features_strings);
167
Eyal Perry892311f2014-12-02 18:12:10 +0200168 if (sset == ETH_SS_RSS_HASH_FUNCS)
169 return ARRAY_SIZE(rss_hash_func_strings);
170
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300171 if (sset == ETH_SS_TUNABLES)
172 return ARRAY_SIZE(tunable_strings);
173
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100174 if (sset == ETH_SS_PHY_TUNABLES)
175 return ARRAY_SIZE(phy_tunable_strings);
176
Florian Fainelli99943382018-04-25 12:12:48 -0700177 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
Florian Fainelli17809512020-07-08 09:46:25 -0700178 !ops->get_ethtool_phy_stats &&
179 phy_ops && phy_ops->get_sset_count)
180 return phy_ops->get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100181
Michal Kubecek428c1222019-12-11 10:58:34 +0100182 if (sset == ETH_SS_LINK_MODES)
183 return __ETHTOOL_LINK_MODE_MASK_NBITS;
184
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000185 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000186 return ops->get_sset_count(dev, sset);
187 else
188 return -EOPNOTSUPP;
189}
190
191static void __ethtool_get_strings(struct net_device *dev,
192 u32 stringset, u8 *data)
193{
Florian Fainelli17809512020-07-08 09:46:25 -0700194 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
Michał Mirosław340ae162011-02-15 16:59:16 +0000195 const struct ethtool_ops *ops = dev->ethtool_ops;
196
Michał Mirosław5455c692011-02-15 16:59:17 +0000197 if (stringset == ETH_SS_FEATURES)
198 memcpy(data, netdev_features_strings,
199 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200200 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
201 memcpy(data, rss_hash_func_strings,
202 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300203 else if (stringset == ETH_SS_TUNABLES)
204 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100205 else if (stringset == ETH_SS_PHY_TUNABLES)
206 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Florian Fainelli99943382018-04-25 12:12:48 -0700207 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
Florian Fainelli17809512020-07-08 09:46:25 -0700208 !ops->get_ethtool_phy_stats && phy_ops &&
209 phy_ops->get_strings)
210 phy_ops->get_strings(dev->phydev, data);
Michal Kubecek428c1222019-12-11 10:58:34 +0100211 else if (stringset == ETH_SS_LINK_MODES)
212 memcpy(data, link_mode_names,
213 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
Florian Fainelli99943382018-04-25 12:12:48 -0700214 else
Michał Mirosław5455c692011-02-15 16:59:17 +0000215 /* ops->get_strings is valid because checked earlier */
216 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000217}
218
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000219static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000220{
221 /* feature masks of legacy discrete ethtool ops */
222
223 switch (eth_cmd) {
224 case ETHTOOL_GTXCSUM:
225 case ETHTOOL_STXCSUM:
Vladyslav Tarasiuk9d648fb2020-03-24 13:57:08 +0200226 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
Michal Kubecekf70bb062020-03-12 21:07:43 +0100227 NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000228 case ETHTOOL_GRXCSUM:
229 case ETHTOOL_SRXCSUM:
230 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000231 case ETHTOOL_GSG:
232 case ETHTOOL_SSG:
Michal Kubecekf70bb062020-03-12 21:07:43 +0100233 return NETIF_F_SG | NETIF_F_FRAGLIST;
Michał Mirosław0a417702011-02-15 16:59:17 +0000234 case ETHTOOL_GTSO:
235 case ETHTOOL_STSO:
236 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000237 case ETHTOOL_GGSO:
238 case ETHTOOL_SGSO:
239 return NETIF_F_GSO;
240 case ETHTOOL_GGRO:
241 case ETHTOOL_SGRO:
242 return NETIF_F_GRO;
243 default:
244 BUG();
245 }
246}
247
Michał Mirosław0a417702011-02-15 16:59:17 +0000248static int ethtool_get_one_feature(struct net_device *dev,
249 char __user *useraddr, u32 ethcmd)
250{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000251 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000252 struct ethtool_value edata = {
253 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000254 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000255 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000256
Michał Mirosław0a417702011-02-15 16:59:17 +0000257 if (copy_to_user(useraddr, &edata, sizeof(edata)))
258 return -EFAULT;
259 return 0;
260}
261
Michał Mirosław0a417702011-02-15 16:59:17 +0000262static int ethtool_set_one_feature(struct net_device *dev,
263 void __user *useraddr, u32 ethcmd)
264{
265 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000266 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000267
268 if (copy_from_user(&edata, useraddr, sizeof(edata)))
269 return -EFAULT;
270
Michał Mirosław86794882011-02-15 16:59:17 +0000271 mask = ethtool_get_feature_mask(ethcmd);
272 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000273 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000274 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000275
276 if (edata.data)
277 dev->wanted_features |= mask;
278 else
279 dev->wanted_features &= ~mask;
280
281 __netdev_update_features(dev);
282
283 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000284}
285
Michał Mirosław02b3a552011-11-15 15:29:55 +0000286#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
287 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000288#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
289 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
290 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000291
292static u32 __ethtool_get_flags(struct net_device *dev)
293{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000294 u32 flags = 0;
295
Dragos Foianu8a731252013-07-13 14:43:00 +0100296 if (dev->features & NETIF_F_LRO)
297 flags |= ETH_FLAG_LRO;
298 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
299 flags |= ETH_FLAG_RXVLAN;
300 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
301 flags |= ETH_FLAG_TXVLAN;
302 if (dev->features & NETIF_F_NTUPLE)
303 flags |= ETH_FLAG_NTUPLE;
304 if (dev->features & NETIF_F_RXHASH)
305 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000306
307 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000308}
309
310static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000311{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000312 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000313
Michał Mirosław02b3a552011-11-15 15:29:55 +0000314 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000315 return -EINVAL;
316
Dragos Foianu8a731252013-07-13 14:43:00 +0100317 if (data & ETH_FLAG_LRO)
318 features |= NETIF_F_LRO;
319 if (data & ETH_FLAG_RXVLAN)
320 features |= NETIF_F_HW_VLAN_CTAG_RX;
321 if (data & ETH_FLAG_TXVLAN)
322 features |= NETIF_F_HW_VLAN_CTAG_TX;
323 if (data & ETH_FLAG_NTUPLE)
324 features |= NETIF_F_NTUPLE;
325 if (data & ETH_FLAG_RXHASH)
326 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000327
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000328 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000329 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000330 if (changed & ~dev->hw_features)
331 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
332
333 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000334 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000335
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700336 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000337
338 return 0;
339}
340
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700341/* Given two link masks, AND them together and save the result in dst. */
342void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
343 struct ethtool_link_ksettings *src)
344{
345 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
346 unsigned int idx = 0;
347
348 for (; idx < size; idx++) {
349 dst->link_modes.supported[idx] &=
350 src->link_modes.supported[idx];
351 dst->link_modes.advertising[idx] &=
352 src->link_modes.advertising[idx];
353 }
354}
355EXPORT_SYMBOL(ethtool_intersect_link_masks);
356
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200357void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
358 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800359{
Sean Anderson49730562021-10-22 18:41:04 -0400360 linkmode_zero(dst);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800361 dst[0] = legacy_u32;
362}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200363EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800364
365/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200366bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
367 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800368{
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800369 *legacy_u32 = src[0];
Marco Bonelli19d62f52022-06-09 15:49:01 +0200370 return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
371 __ETHTOOL_LINK_MODE_MASK_NBITS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800372}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200373EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800374
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800375/* return false if ksettings link modes had higher bits
376 * set. legacy_settings always updated (best effort)
377 */
378static bool
379convert_link_ksettings_to_legacy_settings(
380 struct ethtool_cmd *legacy_settings,
381 const struct ethtool_link_ksettings *link_ksettings)
382{
383 bool retval = true;
384
385 memset(legacy_settings, 0, sizeof(*legacy_settings));
386 /* this also clears the deprecated fields in legacy structure:
387 * __u8 transceiver;
388 * __u32 maxtxpkt;
389 * __u32 maxrxpkt;
390 */
391
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200392 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800393 &legacy_settings->supported,
394 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200395 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800396 &legacy_settings->advertising,
397 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200398 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800399 &legacy_settings->lp_advertising,
400 link_ksettings->link_modes.lp_advertising);
401 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
402 legacy_settings->duplex
403 = link_ksettings->base.duplex;
404 legacy_settings->port
405 = link_ksettings->base.port;
406 legacy_settings->phy_address
407 = link_ksettings->base.phy_address;
408 legacy_settings->autoneg
409 = link_ksettings->base.autoneg;
410 legacy_settings->mdio_support
411 = link_ksettings->base.mdio_support;
412 legacy_settings->eth_tp_mdix
413 = link_ksettings->base.eth_tp_mdix;
414 legacy_settings->eth_tp_mdix_ctrl
415 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700416 legacy_settings->transceiver
417 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800418 return retval;
419}
420
421/* number of 32-bit words to store the user's link mode bitmaps */
422#define __ETHTOOL_LINK_MODE_MASK_NU32 \
423 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
424
425/* layout of the struct passed from/to userland */
426struct ethtool_link_usettings {
427 struct ethtool_link_settings base;
428 struct {
429 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
430 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
431 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
432 } link_modes;
433};
434
Michal Kubecek9b300492018-08-28 19:56:58 +0200435/* Internal kernel helper to query a device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800436int __ethtool_get_link_ksettings(struct net_device *dev,
437 struct ethtool_link_ksettings *link_ksettings)
438{
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800439 ASSERT_RTNL();
440
Michal Kubecek9b300492018-08-28 19:56:58 +0200441 if (!dev->ethtool_ops->get_link_ksettings)
David Decotigny3237fc62016-02-24 10:58:11 -0800442 return -EOPNOTSUPP;
443
Michal Kubecek9b300492018-08-28 19:56:58 +0200444 memset(link_ksettings, 0, sizeof(*link_ksettings));
Danielle Ratsona975d7d2021-04-07 13:06:51 +0300445 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800446}
447EXPORT_SYMBOL(__ethtool_get_link_ksettings);
448
449/* convert ethtool_link_usettings in user space to a kernel internal
450 * ethtool_link_ksettings. return 0 on success, errno on error.
451 */
452static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
453 const void __user *from)
454{
455 struct ethtool_link_usettings link_usettings;
456
457 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
458 return -EFAULT;
459
460 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800461 bitmap_from_arr32(to->link_modes.supported,
462 link_usettings.link_modes.supported,
463 __ETHTOOL_LINK_MODE_MASK_NBITS);
464 bitmap_from_arr32(to->link_modes.advertising,
465 link_usettings.link_modes.advertising,
466 __ETHTOOL_LINK_MODE_MASK_NBITS);
467 bitmap_from_arr32(to->link_modes.lp_advertising,
468 link_usettings.link_modes.lp_advertising,
469 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800470
471 return 0;
472}
473
Cris Forno70ae1e12020-02-28 14:12:04 -0600474/* Check if the user is trying to change anything besides speed/duplex */
475bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
476{
477 struct ethtool_link_settings base2 = {};
478
479 base2.speed = cmd->base.speed;
480 base2.port = PORT_OTHER;
481 base2.duplex = cmd->base.duplex;
482 base2.cmd = cmd->base.cmd;
483 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
484
485 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
486 bitmap_empty(cmd->link_modes.supported,
487 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
488 bitmap_empty(cmd->link_modes.lp_advertising,
489 __ETHTOOL_LINK_MODE_MASK_NBITS);
490}
491
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800492/* convert a kernel internal ethtool_link_ksettings to
493 * ethtool_link_usettings in user space. return 0 on success, errno on
494 * error.
495 */
496static int
497store_link_ksettings_for_user(void __user *to,
498 const struct ethtool_link_ksettings *from)
499{
500 struct ethtool_link_usettings link_usettings;
501
Gustavo A. R. Silvac1d9e342021-04-16 15:15:40 -0500502 memcpy(&link_usettings, from, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800503 bitmap_to_arr32(link_usettings.link_modes.supported,
504 from->link_modes.supported,
505 __ETHTOOL_LINK_MODE_MASK_NBITS);
506 bitmap_to_arr32(link_usettings.link_modes.advertising,
507 from->link_modes.advertising,
508 __ETHTOOL_LINK_MODE_MASK_NBITS);
509 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
510 from->link_modes.lp_advertising,
511 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800512
513 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
514 return -EFAULT;
515
516 return 0;
517}
518
Michal Kubecek9b300492018-08-28 19:56:58 +0200519/* Query device for its ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800520static int ethtool_get_link_ksettings(struct net_device *dev,
521 void __user *useraddr)
522{
523 int err = 0;
524 struct ethtool_link_ksettings link_ksettings;
525
526 ASSERT_RTNL();
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800527 if (!dev->ethtool_ops->get_link_ksettings)
528 return -EOPNOTSUPP;
529
530 /* handle bitmap nbits handshake */
531 if (copy_from_user(&link_ksettings.base, useraddr,
532 sizeof(link_ksettings.base)))
533 return -EFAULT;
534
535 if (__ETHTOOL_LINK_MODE_MASK_NU32
536 != link_ksettings.base.link_mode_masks_nwords) {
537 /* wrong link mode nbits requested */
538 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000539 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800540 /* send back number of words required as negative val */
541 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
542 "need too many bits for link modes!");
543 link_ksettings.base.link_mode_masks_nwords
544 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
545
546 /* copy the base fields back to user, not the link
547 * mode bitmaps
548 */
549 if (copy_to_user(useraddr, &link_ksettings.base,
550 sizeof(link_ksettings.base)))
551 return -EFAULT;
552
553 return 0;
554 }
555
556 /* handshake successful: user/kernel agree on
557 * link_mode_masks_nwords
558 */
559
560 memset(&link_ksettings, 0, sizeof(link_ksettings));
561 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
562 if (err < 0)
563 return err;
564
565 /* make sure we tell the right values to user */
566 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
567 link_ksettings.base.link_mode_masks_nwords
568 = __ETHTOOL_LINK_MODE_MASK_NU32;
Oleksij Rempelbdbdac72020-05-05 08:35:05 +0200569 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
570 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
Sean Anderson0c3e10c2022-09-20 18:12:31 -0400571 link_ksettings.base.rate_matching = RATE_MATCH_NONE;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800572
573 return store_link_ksettings_for_user(useraddr, &link_ksettings);
574}
575
Michal Kubecek9b300492018-08-28 19:56:58 +0200576/* Update device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800577static int ethtool_set_link_ksettings(struct net_device *dev,
578 void __user *useraddr)
579{
Ido Schimmel9ad685d2023-05-02 15:20:50 +0300580 struct ethtool_link_ksettings link_ksettings = {};
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800581 int err;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800582
583 ASSERT_RTNL();
584
585 if (!dev->ethtool_ops->set_link_ksettings)
586 return -EOPNOTSUPP;
587
588 /* make sure nbits field has expected value */
589 if (copy_from_user(&link_ksettings.base, useraddr,
590 sizeof(link_ksettings.base)))
591 return -EFAULT;
592
593 if (__ETHTOOL_LINK_MODE_MASK_NU32
594 != link_ksettings.base.link_mode_masks_nwords)
595 return -EINVAL;
596
597 /* copy the whole structure, now that we know it has expected
598 * format
599 */
600 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
601 if (err)
602 return err;
603
604 /* re-check nwords field, just in case */
605 if (__ETHTOOL_LINK_MODE_MASK_NU32
606 != link_ksettings.base.link_mode_masks_nwords)
607 return -EINVAL;
608
Oleksij Rempelbdbdac72020-05-05 08:35:05 +0200609 if (link_ksettings.base.master_slave_cfg ||
610 link_ksettings.base.master_slave_state)
611 return -EINVAL;
612
Michal Kubecek73286732019-12-27 15:56:03 +0100613 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100614 if (err >= 0) {
Michal Kubecek73286732019-12-27 15:56:03 +0100615 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100616 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
617 }
Michal Kubecek73286732019-12-27 15:56:03 +0100618 return err;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800619}
620
Cris Forno70ae1e12020-02-28 14:12:04 -0600621int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
622 const struct ethtool_link_ksettings *cmd,
623 u32 *dev_speed, u8 *dev_duplex)
624{
625 u32 speed;
626 u8 duplex;
627
628 speed = cmd->base.speed;
629 duplex = cmd->base.duplex;
630 /* don't allow custom speed and duplex */
631 if (!ethtool_validate_speed(speed) ||
632 !ethtool_validate_duplex(duplex) ||
633 !ethtool_virtdev_validate_cmd(cmd))
634 return -EINVAL;
635 *dev_speed = speed;
636 *dev_duplex = duplex;
637
638 return 0;
639}
640EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
641
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800642/* Query device for its ethtool_cmd settings.
643 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200644 * Backward compatibility note: for compatibility with legacy ethtool, this is
645 * now implemented via get_link_ksettings. When driver reports higher link mode
646 * bits, a kernel warning is logged once (with name of 1st driver/device) to
647 * recommend user to upgrade ethtool, but the command is successful (only the
648 * lower link mode bits reported back to user). Deprecated fields from
649 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800650 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000651static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
652{
Michal Kubecek9b300492018-08-28 19:56:58 +0200653 struct ethtool_link_ksettings link_ksettings;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000654 struct ethtool_cmd cmd;
Michal Kubecek9b300492018-08-28 19:56:58 +0200655 int err;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000656
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800657 ASSERT_RTNL();
Michal Kubecek9b300492018-08-28 19:56:58 +0200658 if (!dev->ethtool_ops->get_link_ksettings)
659 return -EOPNOTSUPP;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800660
Michal Kubecek9b300492018-08-28 19:56:58 +0200661 memset(&link_ksettings, 0, sizeof(link_ksettings));
662 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
663 if (err < 0)
664 return err;
665 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800666
Michal Kubecek9b300492018-08-28 19:56:58 +0200667 /* send a sensible cmd tag back to user */
668 cmd.cmd = ETHTOOL_GSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
671 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
674}
675
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800676/* Update device link settings with given ethtool_cmd.
677 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200678 * Backward compatibility note: for compatibility with legacy ethtool, this is
679 * now always implemented via set_link_settings. When user's request updates
680 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
681 * warning is logged once (with name of 1st driver/device) to recommend user to
682 * upgrade ethtool, and the request is rejected.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
685{
Michal Kubecek9b300492018-08-28 19:56:58 +0200686 struct ethtool_link_ksettings link_ksettings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct ethtool_cmd cmd;
Michal Kubecek73286732019-12-27 15:56:03 +0100688 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800690 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
693 return -EFAULT;
Michal Kubecek9b300492018-08-28 19:56:58 +0200694 if (!dev->ethtool_ops->set_link_ksettings)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800695 return -EOPNOTSUPP;
696
Michal Kubecek9b300492018-08-28 19:56:58 +0200697 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
698 return -EINVAL;
699 link_ksettings.base.link_mode_masks_nwords =
700 __ETHTOOL_LINK_MODE_MASK_NU32;
Michal Kubecek73286732019-12-27 15:56:03 +0100701 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100702 if (ret >= 0) {
Michal Kubecek73286732019-12-27 15:56:03 +0100703 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100704 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
705 }
Michal Kubecek73286732019-12-27 15:56:03 +0100706 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700709static int
710ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700712 const struct ethtool_ops *ops = dev->ethtool_ops;
Vincent Mailholedaf5df2022-11-08 12:57:54 +0900713 struct device *parent = dev->dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700715 rsp->info.cmd = ETHTOOL_GDRVINFO;
Jann Hornd2efeb52024-02-20 20:42:44 +0100716 strscpy(rsp->info.version, init_uts_ns.name.release,
717 sizeof(rsp->info.version));
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000718 if (ops->get_drvinfo) {
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700719 ops->get_drvinfo(dev, &rsp->info);
Vincent Mailholedaf5df2022-11-08 12:57:54 +0900720 if (!rsp->info.bus_info[0] && parent)
721 strscpy(rsp->info.bus_info, dev_name(parent),
722 sizeof(rsp->info.bus_info));
723 if (!rsp->info.driver[0] && parent && parent->driver)
724 strscpy(rsp->info.driver, parent->driver->name,
725 sizeof(rsp->info.driver));
726 } else if (parent && parent->driver) {
727 strscpy(rsp->info.bus_info, dev_name(parent),
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700728 sizeof(rsp->info.bus_info));
Vincent Mailholedaf5df2022-11-08 12:57:54 +0900729 strscpy(rsp->info.driver, parent->driver->name,
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700730 sizeof(rsp->info.driver));
Tonghao Zhangbde3b0f2021-11-26 00:30:49 +0800731 } else if (dev->rtnl_link_ops) {
Wolfram Sanga71af892022-08-18 23:02:18 +0200732 strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
Tonghao Zhangbde3b0f2021-11-26 00:30:49 +0800733 sizeof(rsp->info.driver));
Ben Hutchings01414802010-08-17 02:31:15 -0700734 } else {
735 return -EOPNOTSUPP;
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Jeff Garzik723b2f52010-03-03 22:51:50 +0000738 /*
739 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000740 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000741 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000742 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700743 int rc;
744
745 rc = ops->get_sset_count(dev, ETH_SS_TEST);
746 if (rc >= 0)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700747 rsp->info.testinfo_len = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700748 rc = ops->get_sset_count(dev, ETH_SS_STATS);
749 if (rc >= 0)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700750 rsp->info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700751 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
752 if (rc >= 0)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700753 rsp->info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700754 }
Yunsheng Linf9fc54d2018-12-26 19:51:46 +0800755 if (ops->get_regs_len) {
756 int ret = ops->get_regs_len(dev);
757
758 if (ret > 0)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700759 rsp->info.regdump_len = ret;
Yunsheng Linf9fc54d2018-12-26 19:51:46 +0800760 }
761
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000762 if (ops->get_eeprom_len)
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700763 rsp->info.eedump_len = ops->get_eeprom_len(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Jakub Kicinski095cfcf2021-10-30 10:18:49 -0700765 if (!rsp->info.fw_version[0])
Jakub Kicinski1af0a092021-10-30 10:18:51 -0700766 rsp->devlink = netdev_to_devlink_get(dev);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769}
770
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800771static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700772 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000773{
774 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000775 u64 sset_mask;
776 int i, idx = 0, n_bits = 0, ret, rc;
777 u32 *info_buf = NULL;
778
Jeff Garzik723b2f52010-03-03 22:51:50 +0000779 if (copy_from_user(&info, useraddr, sizeof(info)))
780 return -EFAULT;
781
782 /* store copy of mask, because we zero struct later on */
783 sset_mask = info.sset_mask;
784 if (!sset_mask)
785 return 0;
786
787 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000788 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000789
790 memset(&info, 0, sizeof(info));
791 info.cmd = ETHTOOL_GSSET_INFO;
792
Kees Cook6396bb22018-06-12 14:03:40 -0700793 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000794 if (!info_buf)
795 return -ENOMEM;
796
797 /*
798 * fill return buffer based on input bitmask and successful
799 * get_sset_count return
800 */
801 for (i = 0; i < 64; i++) {
802 if (!(sset_mask & (1ULL << i)))
803 continue;
804
Michał Mirosław340ae162011-02-15 16:59:16 +0000805 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000806 if (rc >= 0) {
807 info.sset_mask |= (1ULL << i);
808 info_buf[idx++] = rc;
809 }
810 }
811
812 ret = -EFAULT;
813 if (copy_to_user(useraddr, &info, sizeof(info)))
814 goto out;
815
816 useraddr += offsetof(struct ethtool_sset_info, data);
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -0500817 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
Jeff Garzik723b2f52010-03-03 22:51:50 +0000818 goto out;
819
820 ret = 0;
821
822out:
823 kfree(info_buf);
824 return ret;
825}
826
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200827static noinline_for_stack int
828ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
829 const struct compat_ethtool_rxnfc __user *useraddr,
830 size_t size)
831{
832 struct compat_ethtool_rxnfc crxnfc = {};
833
834 /* We expect there to be holes between fs.m_ext and
835 * fs.ring_cookie and at the end of fs, but nowhere else.
836 * On non-x86, no conversion should be needed.
837 */
838 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
839 sizeof(struct compat_ethtool_rxnfc) !=
840 sizeof(struct ethtool_rxnfc));
841 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
842 sizeof(useraddr->fs.m_ext) !=
843 offsetof(struct ethtool_rxnfc, fs.m_ext) +
844 sizeof(rxnfc->fs.m_ext));
845 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
846 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
847 offsetof(struct ethtool_rxnfc, fs.location) -
848 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
849
850 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
851 return -EFAULT;
852
853 *rxnfc = (struct ethtool_rxnfc) {
854 .cmd = crxnfc.cmd,
855 .flow_type = crxnfc.flow_type,
856 .data = crxnfc.data,
857 .fs = {
858 .flow_type = crxnfc.fs.flow_type,
859 .h_u = crxnfc.fs.h_u,
860 .h_ext = crxnfc.fs.h_ext,
861 .m_u = crxnfc.fs.m_u,
862 .m_ext = crxnfc.fs.m_ext,
863 .ring_cookie = crxnfc.fs.ring_cookie,
864 .location = crxnfc.fs.location,
865 },
866 .rule_cnt = crxnfc.rule_cnt,
867 };
868
869 return 0;
870}
871
872static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
873 const void __user *useraddr,
874 size_t size)
875{
876 if (compat_need_64bit_alignment_fixup())
877 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
878
879 if (copy_from_user(rxnfc, useraddr, size))
880 return -EFAULT;
881
882 return 0;
883}
884
885static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
886 const struct ethtool_rxnfc *rxnfc,
887 size_t size, const u32 *rule_buf)
888{
889 struct compat_ethtool_rxnfc crxnfc;
890
891 memset(&crxnfc, 0, sizeof(crxnfc));
892 crxnfc = (struct compat_ethtool_rxnfc) {
893 .cmd = rxnfc->cmd,
894 .flow_type = rxnfc->flow_type,
895 .data = rxnfc->data,
896 .fs = {
897 .flow_type = rxnfc->fs.flow_type,
898 .h_u = rxnfc->fs.h_u,
899 .h_ext = rxnfc->fs.h_ext,
900 .m_u = rxnfc->fs.m_u,
901 .m_ext = rxnfc->fs.m_ext,
902 .ring_cookie = rxnfc->fs.ring_cookie,
903 .location = rxnfc->fs.location,
904 },
905 .rule_cnt = rxnfc->rule_cnt,
906 };
907
908 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
909 return -EFAULT;
910
911 return 0;
912}
913
Joe Damato801b27e2023-07-25 20:56:54 +0000914static int ethtool_rxnfc_copy_struct(u32 cmd, struct ethtool_rxnfc *info,
915 size_t *info_size, void __user *useraddr)
916{
917 /* struct ethtool_rxnfc was originally defined for
918 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
919 * members. User-space might still be using that
920 * definition.
921 */
922 if (cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH)
923 *info_size = (offsetof(struct ethtool_rxnfc, data) +
924 sizeof(info->data));
925
926 if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
927 return -EFAULT;
928
929 if ((cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH) && info->flow_type & FLOW_RSS) {
930 *info_size = sizeof(*info);
931 if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
932 return -EFAULT;
933 /* Since malicious users may modify the original data,
934 * we need to check whether FLOW_RSS is still requested.
935 */
936 if (!(info->flow_type & FLOW_RSS))
937 return -EINVAL;
938 }
939
940 if (info->cmd != cmd)
941 return -EINVAL;
942
943 return 0;
944}
945
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200946static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
947 const struct ethtool_rxnfc *rxnfc,
948 size_t size, const u32 *rule_buf)
949{
950 int ret;
951
952 if (compat_need_64bit_alignment_fixup()) {
953 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
954 rule_buf);
955 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
956 } else {
Saeed Mahameed9b29a162021-07-26 15:15:39 -0700957 ret = copy_to_user(useraddr, rxnfc, size);
Arnd Bergmanndd98d282021-07-22 16:28:59 +0200958 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
959 }
960
961 if (ret)
962 return -EFAULT;
963
964 if (rule_buf) {
965 if (copy_to_user(useraddr, rule_buf,
966 rxnfc->rule_cnt * sizeof(u32)))
967 return -EFAULT;
968 }
969
970 return 0;
971}
972
chavey97f8aef2010-04-07 21:54:42 -0700973static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000974 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700975{
Ahmed Zaki13e59342023-12-12 17:33:16 -0700976 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000977 struct ethtool_rxnfc info;
978 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000979 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700980
Gerhard Engleder501869f2023-12-26 21:55:36 +0100981 if (!ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700982 return -EOPNOTSUPP;
983
Joe Damato801b27e2023-07-25 20:56:54 +0000984 rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
985 if (rc)
986 return rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700987
Gerhard Engleder501869f2023-12-26 21:55:36 +0100988 if (ops->get_rxfh) {
989 struct ethtool_rxfh_param rxfh = {};
Ahmed Zaki13e59342023-12-12 17:33:16 -0700990
Gerhard Engleder501869f2023-12-26 21:55:36 +0100991 rc = ops->get_rxfh(dev, &rxfh);
992 if (rc)
993 return rc;
994
995 /* Sanity check: if symmetric-xor is set, then:
996 * 1 - no other fields besides IP src/dst and/or L4 src/dst
997 * 2 - If src is set, dst must also be set
998 */
999 if ((rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
1000 ((info.data & ~(RXH_IP_SRC | RXH_IP_DST |
1001 RXH_L4_B_0_1 | RXH_L4_B_2_3)) ||
1002 (!!(info.data & RXH_IP_SRC) ^ !!(info.data & RXH_IP_DST)) ||
1003 (!!(info.data & RXH_L4_B_0_1) ^ !!(info.data & RXH_L4_B_2_3))))
1004 return -EINVAL;
1005 }
Ahmed Zaki13e59342023-12-12 17:33:16 -07001006
1007 rc = ops->set_rxnfc(dev, &info);
Ben Hutchings55664f32012-01-03 12:04:51 +00001008 if (rc)
1009 return rc;
1010
1011 if (cmd == ETHTOOL_SRXCLSRLINS &&
Arnd Bergmanndd98d282021-07-22 16:28:59 +02001012 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
Ben Hutchings55664f32012-01-03 12:04:51 +00001013 return -EFAULT;
1014
1015 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -07001016}
1017
chavey97f8aef2010-04-07 21:54:42 -07001018static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +00001019 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -07001020{
1021 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001022 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -08001023 const struct ethtool_ops *ops = dev->ethtool_ops;
1024 int ret;
1025 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -07001026
Santwona Behera59089d82009-02-20 00:58:13 -08001027 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -07001028 return -EOPNOTSUPP;
1029
Joe Damato801b27e2023-07-25 20:56:54 +00001030 ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
1031 if (ret)
1032 return ret;
Wenwen Wang2bb32072018-10-09 08:15:38 -05001033
Santwona Behera59089d82009-02-20 00:58:13 -08001034 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1035 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001036 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cook6396bb22018-06-12 14:03:40 -07001037 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001038 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001039 if (!rule_buf)
1040 return -ENOMEM;
1041 }
1042 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001043
Santwona Behera59089d82009-02-20 00:58:13 -08001044 ret = ops->get_rxnfc(dev, &info, rule_buf);
1045 if (ret < 0)
1046 goto err_out;
1047
Arnd Bergmanndd98d282021-07-22 16:28:59 +02001048 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001049err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001050 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001051
1052 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001053}
1054
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301055static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1056 struct ethtool_rxnfc *rx_rings,
1057 u32 size)
1058{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001059 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301060
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -05001061 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001062 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301063
1064 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001065 for (i = 0; i < size; i++)
1066 if (indir[i] >= rx_rings->data)
1067 return -EINVAL;
1068
1069 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301070}
1071
Kim Jonesba905f52016-02-02 03:51:16 +00001072u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001073
1074void netdev_rss_key_fill(void *buffer, size_t len)
1075{
1076 BUG_ON(len > sizeof(netdev_rss_key));
1077 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1078 memcpy(buffer, netdev_rss_key, len);
1079}
1080EXPORT_SYMBOL(netdev_rss_key_fill);
1081
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001082static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1083 void __user *useraddr)
1084{
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001085 struct ethtool_rxfh_param rxfh = {};
1086 u32 user_size;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001087 int ret;
1088
Ben Hutchings7850f632011-12-15 13:55:01 +00001089 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001090 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001091 return -EOPNOTSUPP;
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001092 rxfh.indir_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1093 if (rxfh.indir_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001094 return -EOPNOTSUPP;
1095
Ben Hutchings7850f632011-12-15 13:55:01 +00001096 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001097 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001098 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001099 return -EFAULT;
1100
Ben Hutchings7850f632011-12-15 13:55:01 +00001101 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001102 &rxfh.indir_size, sizeof(rxfh.indir_size)))
Ben Hutchings7850f632011-12-15 13:55:01 +00001103 return -EFAULT;
1104
1105 /* If the user buffer size is 0, this is just a query for the
1106 * device table size. Otherwise, if it's smaller than the
1107 * device table size it's an error.
1108 */
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001109 if (user_size < rxfh.indir_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001110 return user_size == 0 ? 0 : -EINVAL;
1111
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001112 rxfh.indir = kcalloc(rxfh.indir_size, sizeof(rxfh.indir[0]), GFP_USER);
1113 if (!rxfh.indir)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001114 return -ENOMEM;
1115
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001116 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001117 if (ret)
1118 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001119 if (copy_to_user(useraddr +
1120 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001121 rxfh.indir, rxfh.indir_size * sizeof(*rxfh.indir)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001122 ret = -EFAULT;
1123
1124out:
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001125 kfree(rxfh.indir);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001126 return ret;
1127}
1128
1129static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1130 void __user *useraddr)
1131{
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001132 const struct ethtool_ops *ops = dev->ethtool_ops;
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001133 struct ethtool_rxfh_param rxfh_dev = {};
1134 struct netlink_ext_ack *extack = NULL;
1135 struct ethtool_rxnfc rx_rings;
1136 u32 user_size, i;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001137 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301138 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001139
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001140 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001141 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001142 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001143
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001144 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1145 if (rxfh_dev.indir_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001146 return -EOPNOTSUPP;
1147
Ben Hutchings7850f632011-12-15 13:55:01 +00001148 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001149 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001150 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001151 return -EFAULT;
1152
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001153 if (user_size != 0 && user_size != rxfh_dev.indir_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001154 return -EINVAL;
1155
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001156 rxfh_dev.indir = kcalloc(rxfh_dev.indir_size,
1157 sizeof(rxfh_dev.indir[0]), GFP_USER);
1158 if (!rxfh_dev.indir)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001159 return -ENOMEM;
1160
Ben Hutchings7850f632011-12-15 13:55:01 +00001161 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001162 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001163 if (ret)
1164 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001165
1166 if (user_size == 0) {
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001167 u32 *indir = rxfh_dev.indir;
1168
1169 for (i = 0; i < rxfh_dev.indir_size; i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001170 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1171 } else {
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001172 ret = ethtool_copy_validate_indir(rxfh_dev.indir,
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301173 useraddr + ringidx_offset,
1174 &rx_rings,
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001175 rxfh_dev.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301176 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001177 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001178 }
1179
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001180 rxfh_dev.hfunc = ETH_RSS_HASH_NO_CHANGE;
1181 ret = ops->set_rxfh(dev, &rxfh_dev, extack);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001182 if (ret)
1183 goto out;
1184
1185 /* indicate whether rxfh was set to default */
1186 if (user_size == 0)
1187 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1188 else
1189 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001190
1191out:
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001192 kfree(rxfh_dev.indir);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001193 return ret;
1194}
1195
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301196static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1197 void __user *useraddr)
1198{
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301199 const struct ethtool_ops *ops = dev->ethtool_ops;
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001200 struct ethtool_rxfh_param rxfh_dev = {};
Ben Hutchingsf062a382014-05-15 16:28:07 +01001201 u32 user_indir_size, user_key_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001202 struct ethtool_rxfh rxfh;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001203 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301204 u8 *rss_config;
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001205 u32 total_size;
1206 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301207
Eyal Perry892311f2014-12-02 18:12:10 +02001208 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301209 return -EOPNOTSUPP;
1210
1211 if (ops->get_rxfh_indir_size)
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001212 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301213 if (ops->get_rxfh_key_size)
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001214 rxfh_dev.key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301215
Ben Hutchingsf062a382014-05-15 16:28:07 +01001216 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301217 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001218 user_indir_size = rxfh.indir_size;
1219 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301220
Ben Hutchingsf062a382014-05-15 16:28:07 +01001221 /* Check that reserved fields are 0 for now */
Ahmed Zaki13e59342023-12-12 17:33:16 -07001222 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001223 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001224 /* Most drivers don't handle rss_context, check it's 0 as well */
Ahmed Zakidcd8dbf2023-12-12 17:33:15 -07001225 if (rxfh.rss_context && !ops->cap_rss_ctx_supported)
Edward Cree84a1d9c42018-03-08 15:45:03 +00001226 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001227
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001228 rxfh.indir_size = rxfh_dev.indir_size;
1229 rxfh.key_size = rxfh_dev.key_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001230 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301231 return -EFAULT;
1232
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001233 if ((user_indir_size && user_indir_size != rxfh_dev.indir_size) ||
1234 (user_key_size && user_key_size != rxfh_dev.key_size))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301235 return -EINVAL;
1236
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001237 indir_bytes = user_indir_size * sizeof(rxfh_dev.indir[0]);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301238 total_size = indir_bytes + user_key_size;
1239 rss_config = kzalloc(total_size, GFP_USER);
1240 if (!rss_config)
1241 return -ENOMEM;
1242
1243 if (user_indir_size)
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001244 rxfh_dev.indir = (u32 *)rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301245
1246 if (user_key_size)
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001247 rxfh_dev.key = rss_config + indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301248
Ahmed Zakidcd8dbf2023-12-12 17:33:15 -07001249 rxfh_dev.rss_context = rxfh.rss_context;
1250
1251 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh_dev);
Eyal Perry892311f2014-12-02 18:12:10 +02001252 if (ret)
1253 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301254
Eyal Perry892311f2014-12-02 18:12:10 +02001255 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001256 &rxfh_dev.hfunc, sizeof(rxfh.hfunc))) {
Eyal Perry892311f2014-12-02 18:12:10 +02001257 ret = -EFAULT;
1258 } else if (copy_to_user(useraddr +
Ahmed Zaki7c402f72023-12-21 11:42:34 -07001259 offsetof(struct ethtool_rxfh, input_xfrm),
1260 &rxfh_dev.input_xfrm,
1261 sizeof(rxfh.input_xfrm))) {
1262 ret = -EFAULT;
1263 } else if (copy_to_user(useraddr +
Eyal Perry892311f2014-12-02 18:12:10 +02001264 offsetof(struct ethtool_rxfh, rss_config[0]),
1265 rss_config, total_size)) {
1266 ret = -EFAULT;
1267 }
1268out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301269 kfree(rss_config);
1270
1271 return ret;
1272}
1273
1274static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1275 void __user *useraddr)
1276{
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001277 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301278 const struct ethtool_ops *ops = dev->ethtool_ops;
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001279 u32 dev_indir_size = 0, dev_key_size = 0, i;
1280 struct ethtool_rxfh_param rxfh_dev = {};
1281 struct netlink_ext_ack *extack = NULL;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301282 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001283 struct ethtool_rxfh rxfh;
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001284 u32 indir_bytes = 0;
1285 u8 *rss_config;
1286 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301287
Eyal Perry892311f2014-12-02 18:12:10 +02001288 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301289 return -EOPNOTSUPP;
1290
1291 if (ops->get_rxfh_indir_size)
1292 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301293 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001294 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301295
Ben Hutchingsf062a382014-05-15 16:28:07 +01001296 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301297 return -EFAULT;
1298
Ben Hutchingsf062a382014-05-15 16:28:07 +01001299 /* Check that reserved fields are 0 for now */
Ahmed Zaki13e59342023-12-12 17:33:16 -07001300 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001301 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001302 /* Most drivers don't handle rss_context, check it's 0 as well */
Ahmed Zakidcd8dbf2023-12-12 17:33:15 -07001303 if (rxfh.rss_context && !ops->cap_rss_ctx_supported)
Edward Cree84a1d9c42018-03-08 15:45:03 +00001304 return -EOPNOTSUPP;
Ahmed Zaki13e59342023-12-12 17:33:16 -07001305 /* Check input data transformation capabilities */
Ahmed Zaki948f97f2024-01-04 14:26:53 -07001306 if (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_SYM_XOR &&
1307 rxfh.input_xfrm != RXH_XFRM_NO_CHANGE)
1308 return -EINVAL;
Ahmed Zaki13e59342023-12-12 17:33:16 -07001309 if ((rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
1310 !ops->cap_rss_sym_xor_supported)
1311 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001312
Eyal Perry892311f2014-12-02 18:12:10 +02001313 /* If either indir, hash key or function is valid, proceed further.
Ahmed Zaki0dd415d2023-12-21 11:42:35 -07001314 * Must request at least one change: indir size, hash key, function
1315 * or input transformation.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301316 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001317 if ((rxfh.indir_size &&
1318 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1319 rxfh.indir_size != dev_indir_size) ||
1320 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1321 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Ahmed Zaki0dd415d2023-12-21 11:42:35 -07001322 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE &&
1323 rxfh.input_xfrm == RXH_XFRM_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301324 return -EINVAL;
1325
Ben Hutchingsf062a382014-05-15 16:28:07 +01001326 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001327 indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301328
Ben Hutchingsf062a382014-05-15 16:28:07 +01001329 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301330 if (!rss_config)
1331 return -ENOMEM;
1332
1333 rx_rings.cmd = ETHTOOL_GRXRINGS;
1334 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1335 if (ret)
1336 goto out;
1337
Edward Cree84a1d9c42018-03-08 15:45:03 +00001338 /* rxfh.indir_size == 0 means reset the indir table to default (master
1339 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001340 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301341 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001342 if (rxfh.indir_size &&
1343 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001344 rxfh_dev.indir = (u32 *)rss_config;
1345 rxfh_dev.indir_size = dev_indir_size;
1346 ret = ethtool_copy_validate_indir(rxfh_dev.indir,
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301347 useraddr + rss_cfg_offset,
1348 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001349 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301350 if (ret)
1351 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001352 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001353 if (rxfh.rss_context == 0) {
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001354 u32 *indir;
1355
1356 rxfh_dev.indir = (u32 *)rss_config;
1357 rxfh_dev.indir_size = dev_indir_size;
1358 indir = rxfh_dev.indir;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001359 for (i = 0; i < dev_indir_size; i++)
1360 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1361 } else {
Ahmed Zakidcd8dbf2023-12-12 17:33:15 -07001362 rxfh_dev.rss_delete = true;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001363 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301364 }
1365
Ben Hutchingsf062a382014-05-15 16:28:07 +01001366 if (rxfh.key_size) {
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001367 rxfh_dev.key_size = dev_key_size;
1368 rxfh_dev.key = rss_config + indir_bytes;
1369 if (copy_from_user(rxfh_dev.key,
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301370 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001371 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301372 ret = -EFAULT;
1373 goto out;
1374 }
1375 }
1376
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001377 rxfh_dev.hfunc = rxfh.hfunc;
Ahmed Zakidcd8dbf2023-12-12 17:33:15 -07001378 rxfh_dev.rss_context = rxfh.rss_context;
Ahmed Zaki13e59342023-12-12 17:33:16 -07001379 rxfh_dev.input_xfrm = rxfh.input_xfrm;
Ahmed Zakifb6e30a2023-12-12 17:33:14 -07001380
Ahmed Zakidcd8dbf2023-12-12 17:33:15 -07001381 ret = ops->set_rxfh(dev, &rxfh_dev, extack);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001382 if (ret)
1383 goto out;
1384
Edward Cree84a1d9c42018-03-08 15:45:03 +00001385 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
Ahmed Zakidcd8dbf2023-12-12 17:33:15 -07001386 &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context)))
Edward Cree84a1d9c42018-03-08 15:45:03 +00001387 ret = -EFAULT;
1388
Ahmed Zakidcd8dbf2023-12-12 17:33:15 -07001389 if (!rxfh_dev.rss_context) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001390 /* indicate whether rxfh was set to default */
1391 if (rxfh.indir_size == 0)
1392 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1393 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1394 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1395 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301396
1397out:
1398 kfree(rss_config);
1399 return ret;
1400}
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1403{
1404 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001405 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 void *regbuf;
1407 int reglen, ret;
1408
1409 if (!ops->get_regs || !ops->get_regs_len)
1410 return -EOPNOTSUPP;
1411
1412 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1413 return -EFAULT;
1414
1415 reglen = ops->get_regs_len(dev);
Yunsheng Linf9fc54d2018-12-26 19:51:46 +08001416 if (reglen <= 0)
1417 return reglen;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (regs.len > reglen)
1420 regs.len = reglen;
1421
Dan Carpenteref76c772019-02-01 11:24:06 +03001422 regbuf = vzalloc(reglen);
1423 if (!regbuf)
1424 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Vivien Didelot0ee4e762019-06-03 16:57:13 -04001426 if (regs.len < reglen)
1427 reglen = regs.len;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 ops->get_regs(dev, &regs, regbuf);
1430
1431 ret = -EFAULT;
1432 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1433 goto out;
1434 useraddr += offsetof(struct ethtool_regs, data);
Vivien Didelot0ee4e762019-06-03 16:57:13 -04001435 if (copy_to_user(useraddr, regbuf, reglen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 goto out;
1437 ret = 0;
1438
1439 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001440 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return ret;
1442}
1443
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001444static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1445{
1446 struct ethtool_value reset;
1447 int ret;
1448
1449 if (!dev->ethtool_ops->reset)
1450 return -EOPNOTSUPP;
1451
1452 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1453 return -EFAULT;
1454
1455 ret = dev->ethtool_ops->reset(dev, &reset.data);
1456 if (ret)
1457 return ret;
1458
1459 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1460 return -EFAULT;
1461 return 0;
1462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1465{
zhanglin5ff223e82019-10-26 15:54:16 +08001466 struct ethtool_wolinfo wol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 if (!dev->ethtool_ops->get_wol)
1469 return -EOPNOTSUPP;
1470
zhanglin5ff223e82019-10-26 15:54:16 +08001471 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1472 wol.cmd = ETHTOOL_GWOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 dev->ethtool_ops->get_wol(dev, &wol);
1474
1475 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1476 return -EFAULT;
1477 return 0;
1478}
1479
1480static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1481{
Justin Chen55b243342023-06-07 16:14:11 -07001482 struct ethtool_wolinfo wol, cur_wol;
Heiner Kallweit61941142018-09-24 21:58:59 +02001483 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Justin Chen55b243342023-06-07 16:14:11 -07001485 if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -EOPNOTSUPP;
1487
Justin Chen55b243342023-06-07 16:14:11 -07001488 memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo));
1489 cur_wol.cmd = ETHTOOL_GWOL;
1490 dev->ethtool_ops->get_wol(dev, &cur_wol);
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1493 return -EFAULT;
1494
Justin Chen55b243342023-06-07 16:14:11 -07001495 if (wol.wolopts & ~cur_wol.supported)
1496 return -EINVAL;
1497
Justin Chen2bddad92023-06-12 14:37:00 -07001498 if (wol.wolopts == cur_wol.wolopts &&
1499 !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass)))
Justin Chen55b243342023-06-07 16:14:11 -07001500 return 0;
1501
Heiner Kallweit61941142018-09-24 21:58:59 +02001502 ret = dev->ethtool_ops->set_wol(dev, &wol);
1503 if (ret)
1504 return ret;
1505
1506 dev->wol_enabled = !!wol.wolopts;
Michal Kubecek67bffa72020-01-26 23:11:19 +01001507 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
Heiner Kallweit61941142018-09-24 21:58:59 +02001508
1509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001512static void eee_to_keee(struct ethtool_keee *keee,
1513 const struct ethtool_eee *eee)
1514{
1515 memset(keee, 0, sizeof(*keee));
1516
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001517 keee->eee_enabled = eee->eee_enabled;
1518 keee->tx_lpi_enabled = eee->tx_lpi_enabled;
1519 keee->tx_lpi_timer = eee->tx_lpi_timer;
Heiner Kallweit1f069de2024-01-27 14:29:33 +01001520
Heiner Kallweit1f069de2024-01-27 14:29:33 +01001521 ethtool_convert_legacy_u32_to_link_mode(keee->advertised,
1522 eee->advertised);
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001523}
1524
1525static void keee_to_eee(struct ethtool_eee *eee,
1526 const struct ethtool_keee *keee)
1527{
Andrew Lunn292fac42024-02-26 19:29:15 -06001528 bool overflow;
1529
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001530 memset(eee, 0, sizeof(*eee));
1531
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001532 eee->eee_active = keee->eee_active;
1533 eee->eee_enabled = keee->eee_enabled;
1534 eee->tx_lpi_enabled = keee->tx_lpi_enabled;
1535 eee->tx_lpi_timer = keee->tx_lpi_timer;
Heiner Kallweit1f069de2024-01-27 14:29:33 +01001536
Andrew Lunn292fac42024-02-26 19:29:15 -06001537 overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported,
1538 keee->supported);
1539 ethtool_convert_link_mode_to_legacy_u32(&eee->advertised,
1540 keee->advertised);
1541 ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised,
1542 keee->lp_advertised);
1543 if (overflow)
1544 pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n");
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001545}
1546
Yuval Mintz80f12ecc2012-06-06 17:13:06 +00001547static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1548{
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001549 struct ethtool_keee keee;
1550 struct ethtool_eee eee;
Yuval Mintz80f12ecc2012-06-06 17:13:06 +00001551 int rc;
1552
1553 if (!dev->ethtool_ops->get_eee)
1554 return -EOPNOTSUPP;
1555
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001556 memset(&keee, 0, sizeof(keee));
1557 rc = dev->ethtool_ops->get_eee(dev, &keee);
Yuval Mintz80f12ecc2012-06-06 17:13:06 +00001558 if (rc)
1559 return rc;
1560
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001561 keee_to_eee(&eee, &keee);
1562 if (copy_to_user(useraddr, &eee, sizeof(eee)))
Yuval Mintz80f12ecc2012-06-06 17:13:06 +00001563 return -EFAULT;
1564
1565 return 0;
1566}
1567
1568static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1569{
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001570 struct ethtool_keee keee;
1571 struct ethtool_eee eee;
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +01001572 int ret;
Yuval Mintz80f12ecc2012-06-06 17:13:06 +00001573
1574 if (!dev->ethtool_ops->set_eee)
1575 return -EOPNOTSUPP;
1576
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001577 if (copy_from_user(&eee, useraddr, sizeof(eee)))
Yuval Mintz80f12ecc2012-06-06 17:13:06 +00001578 return -EFAULT;
1579
Heiner Kallweit0b3100b2024-01-27 14:26:06 +01001580 eee_to_keee(&keee, &eee);
1581 ret = dev->ethtool_ops->set_eee(dev, &keee);
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +01001582 if (!ret)
1583 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1584 return ret;
Yuval Mintz80f12ecc2012-06-06 17:13:06 +00001585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587static int ethtool_nway_reset(struct net_device *dev)
1588{
1589 if (!dev->ethtool_ops->nway_reset)
1590 return -EOPNOTSUPP;
1591
1592 return dev->ethtool_ops->nway_reset(dev);
1593}
1594
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001595static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1596{
1597 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001598 int link = __ethtool_get_link(dev);
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001599
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001600 if (link < 0)
1601 return link;
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001602
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001603 edata.data = link;
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001604 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1605 return -EFAULT;
1606 return 0;
1607}
1608
Ben Hutchings081d0942012-04-12 00:42:12 +00001609static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1610 int (*getter)(struct net_device *,
1611 struct ethtool_eeprom *, u8 *),
1612 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001615 void __user *userbuf = useraddr + sizeof(eeprom);
1616 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001618 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1621 return -EFAULT;
1622
1623 /* Check for wrap and zero */
1624 if (eeprom.offset + eeprom.len <= eeprom.offset)
1625 return -EINVAL;
1626
1627 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001628 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return -EINVAL;
1630
Austin Kim80ec82e2021-06-09 03:34:25 +01001631 data = kzalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (!data)
1633 return -ENOMEM;
1634
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001635 bytes_remaining = eeprom.len;
1636 while (bytes_remaining > 0) {
1637 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Ben Hutchings081d0942012-04-12 00:42:12 +00001639 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001640 if (ret)
1641 break;
Heiner Kallweitb9bbc4c2021-09-13 21:58:26 +02001642 if (!eeprom.len) {
1643 ret = -EIO;
1644 break;
1645 }
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001646 if (copy_to_user(userbuf, data, eeprom.len)) {
1647 ret = -EFAULT;
1648 break;
1649 }
1650 userbuf += eeprom.len;
1651 eeprom.offset += eeprom.len;
1652 bytes_remaining -= eeprom.len;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001655 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1656 eeprom.offset -= eeprom.len;
1657 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1658 ret = -EFAULT;
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 kfree(data);
1661 return ret;
1662}
1663
Ben Hutchings081d0942012-04-12 00:42:12 +00001664static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1665{
1666 const struct ethtool_ops *ops = dev->ethtool_ops;
1667
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001668 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1669 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001670 return -EOPNOTSUPP;
1671
1672 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1673 ops->get_eeprom_len(dev));
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1677{
1678 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001679 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001680 void __user *userbuf = useraddr + sizeof(eeprom);
1681 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001683 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001685 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1686 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return -EOPNOTSUPP;
1688
1689 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1690 return -EFAULT;
1691
1692 /* Check for wrap and zero */
1693 if (eeprom.offset + eeprom.len <= eeprom.offset)
1694 return -EINVAL;
1695
1696 /* Check for exceeding total eeprom len */
1697 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1698 return -EINVAL;
1699
Austin Kim80ec82e2021-06-09 03:34:25 +01001700 data = kzalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (!data)
1702 return -ENOMEM;
1703
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001704 bytes_remaining = eeprom.len;
1705 while (bytes_remaining > 0) {
1706 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001708 if (copy_from_user(data, userbuf, eeprom.len)) {
1709 ret = -EFAULT;
1710 break;
1711 }
1712 ret = ops->set_eeprom(dev, &eeprom, data);
1713 if (ret)
1714 break;
1715 userbuf += eeprom.len;
1716 eeprom.offset += eeprom.len;
1717 bytes_remaining -= eeprom.len;
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 kfree(data);
1721 return ret;
1722}
1723
chavey97f8aef2010-04-07 21:54:42 -07001724static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1725 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Roland Dreier8e557422010-02-11 12:14:23 -08001727 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001728 struct kernel_ethtool_coalesce kernel_coalesce = {};
Heiner Kallweit31610712020-05-23 17:40:25 +02001729 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 if (!dev->ethtool_ops->get_coalesce)
1732 return -EOPNOTSUPP;
1733
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001734 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1735 NULL);
Heiner Kallweit31610712020-05-23 17:40:25 +02001736 if (ret)
1737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1740 return -EFAULT;
1741 return 0;
1742}
1743
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001744static bool
1745ethtool_set_coalesce_supported(struct net_device *dev,
1746 struct ethtool_coalesce *coalesce)
1747{
1748 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1749 u32 nonzero_params = 0;
1750
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001751 if (coalesce->rx_coalesce_usecs)
1752 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1753 if (coalesce->rx_max_coalesced_frames)
1754 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1755 if (coalesce->rx_coalesce_usecs_irq)
1756 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1757 if (coalesce->rx_max_coalesced_frames_irq)
1758 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1759 if (coalesce->tx_coalesce_usecs)
1760 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1761 if (coalesce->tx_max_coalesced_frames)
1762 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1763 if (coalesce->tx_coalesce_usecs_irq)
1764 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1765 if (coalesce->tx_max_coalesced_frames_irq)
1766 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1767 if (coalesce->stats_block_coalesce_usecs)
1768 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1769 if (coalesce->use_adaptive_rx_coalesce)
1770 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1771 if (coalesce->use_adaptive_tx_coalesce)
1772 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1773 if (coalesce->pkt_rate_low)
1774 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1775 if (coalesce->rx_coalesce_usecs_low)
1776 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1777 if (coalesce->rx_max_coalesced_frames_low)
1778 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1779 if (coalesce->tx_coalesce_usecs_low)
1780 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1781 if (coalesce->tx_max_coalesced_frames_low)
1782 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1783 if (coalesce->pkt_rate_high)
1784 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1785 if (coalesce->rx_coalesce_usecs_high)
1786 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1787 if (coalesce->rx_max_coalesced_frames_high)
1788 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1789 if (coalesce->tx_coalesce_usecs_high)
1790 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1791 if (coalesce->tx_max_coalesced_frames_high)
1792 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1793 if (coalesce->rate_sample_interval)
1794 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1795
1796 return (supported_params & nonzero_params) == nonzero_params;
1797}
1798
chavey97f8aef2010-04-07 21:54:42 -07001799static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1800 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001802 struct kernel_ethtool_coalesce kernel_coalesce = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 struct ethtool_coalesce coalesce;
Michal Kubecek0cf3eac2020-03-28 00:01:18 +01001804 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Julian Wiedmann0276af22021-11-26 18:55:43 +01001806 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return -EOPNOTSUPP;
1808
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001809 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1810 NULL);
1811 if (ret)
1812 return ret;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1815 return -EFAULT;
1816
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001817 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1818 return -EOPNOTSUPP;
1819
Yufeng Mof3ccfda12021-08-20 15:35:18 +08001820 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1821 NULL);
Michal Kubecek0cf3eac2020-03-28 00:01:18 +01001822 if (!ret)
1823 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1824 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1828{
Roland Dreier8e557422010-02-11 12:14:23 -08001829 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Hao Chen74624942021-11-18 20:12:43 +08001830 struct kernel_ethtool_ringparam kernel_ringparam = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 if (!dev->ethtool_ops->get_ringparam)
1833 return -EOPNOTSUPP;
1834
Hao Chen74624942021-11-18 20:12:43 +08001835 dev->ethtool_ops->get_ringparam(dev, &ringparam,
1836 &kernel_ringparam, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1839 return -EFAULT;
1840 return 0;
1841}
1842
1843static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1844{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001845 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Hao Chen74624942021-11-18 20:12:43 +08001846 struct kernel_ethtool_ringparam kernel_ringparam;
Michal Kubecekbc9d1c92020-03-12 21:08:33 +01001847 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001849 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return -EOPNOTSUPP;
1851
1852 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1853 return -EFAULT;
1854
Hao Chen74624942021-11-18 20:12:43 +08001855 dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001856
1857 /* ensure new ring parameters are within the maximums */
1858 if (ringparam.rx_pending > max.rx_max_pending ||
1859 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1860 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1861 ringparam.tx_pending > max.tx_max_pending)
1862 return -EINVAL;
1863
Hao Chen74624942021-11-18 20:12:43 +08001864 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
1865 &kernel_ringparam, NULL);
Michal Kubecekbc9d1c92020-03-12 21:08:33 +01001866 if (!ret)
1867 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1868 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
amit salecha8b5933c2011-04-07 01:58:42 +00001871static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1872 void __user *useraddr)
1873{
1874 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1875
1876 if (!dev->ethtool_ops->get_channels)
1877 return -EOPNOTSUPP;
1878
1879 dev->ethtool_ops->get_channels(dev, &channels);
1880
1881 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1882 return -EFAULT;
1883 return 0;
1884}
1885
1886static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1887 void __user *useraddr)
1888{
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001889 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
Jakub Kicinski1661d342018-10-01 14:51:36 +02001890 u16 from_channel, to_channel;
Gal Pressman47f3ecf2022-11-06 14:31:27 +02001891 u64 max_rxnfc_in_use;
1892 u32 max_rxfh_in_use;
Jakub Kicinski1661d342018-10-01 14:51:36 +02001893 unsigned int i;
Michal Kubecek546379b2020-03-12 21:08:48 +01001894 int ret;
amit salecha8b5933c2011-04-07 01:58:42 +00001895
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001896 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001897 return -EOPNOTSUPP;
1898
1899 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1900 return -EFAULT;
1901
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001902 dev->ethtool_ops->get_channels(dev, &curr);
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001903
Jakub Kicinski75c36db2020-05-15 12:49:02 -07001904 if (channels.rx_count == curr.rx_count &&
1905 channels.tx_count == curr.tx_count &&
1906 channels.combined_count == curr.combined_count &&
1907 channels.other_count == curr.other_count)
1908 return 0;
1909
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001910 /* ensure new counts are within the maximums */
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001911 if (channels.rx_count > curr.max_rx ||
1912 channels.tx_count > curr.max_tx ||
1913 channels.combined_count > curr.max_combined ||
1914 channels.other_count > curr.max_other)
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001915 return -EINVAL;
1916
Jakub Kicinski7be92512020-05-15 12:49:00 -07001917 /* ensure there is at least one RX and one TX channel */
1918 if (!channels.combined_count &&
1919 (!channels.rx_count || !channels.tx_count))
1920 return -EINVAL;
1921
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001922 /* ensure the new Rx count fits within the configured Rx flow
Gal Pressman47f3ecf2022-11-06 14:31:27 +02001923 * indirection table/rxnfc settings */
1924 if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use))
1925 max_rxnfc_in_use = 0;
1926 if (!netif_is_rxfh_configured(dev) ||
1927 ethtool_get_max_rxfh_channel(dev, &max_rxfh_in_use))
1928 max_rxfh_in_use = 0;
1929 if (channels.combined_count + channels.rx_count <=
1930 max_t(u64, max_rxnfc_in_use, max_rxfh_in_use))
1931 return -EINVAL;
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001932
Jakub Kicinski1661d342018-10-01 14:51:36 +02001933 /* Disabling channels, query zero-copy AF_XDP sockets */
1934 from_channel = channels.combined_count +
1935 min(channels.rx_count, channels.tx_count);
1936 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1937 for (i = from_channel; i < to_channel; i++)
Magnus Karlssonc4655762020-08-28 10:26:16 +02001938 if (xsk_get_pool_from_qid(dev, i))
Jakub Kicinski1661d342018-10-01 14:51:36 +02001939 return -EINVAL;
1940
Michal Kubecek546379b2020-03-12 21:08:48 +01001941 ret = dev->ethtool_ops->set_channels(dev, &channels);
1942 if (!ret)
1943 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1944 return ret;
amit salecha8b5933c2011-04-07 01:58:42 +00001945}
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1948{
Li RongQinge83887f2019-02-27 20:47:57 +08001949 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 if (!dev->ethtool_ops->get_pauseparam)
1952 return -EOPNOTSUPP;
1953
1954 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1955
1956 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1957 return -EFAULT;
1958 return 0;
1959}
1960
1961static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1962{
1963 struct ethtool_pauseparam pauseparam;
Michal Kubecekbf37faa2020-03-28 00:01:33 +01001964 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Jeff Garzike1b90c42006-07-17 12:54:40 -04001966 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return -EOPNOTSUPP;
1968
1969 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1970 return -EFAULT;
1971
Michal Kubecekbf37faa2020-03-28 00:01:33 +01001972 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1973 if (!ret)
1974 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1975 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1979{
1980 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001981 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001983 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001985 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001986 return -EOPNOTSUPP;
1987
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001988 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001989 if (test_len < 0)
1990 return test_len;
1991 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 if (copy_from_user(&test, useraddr, sizeof(test)))
1994 return -EFAULT;
1995
Jeff Garzikff03d492007-08-15 16:01:08 -07001996 test.len = test_len;
Austin Kim80ec82e2021-06-09 03:34:25 +01001997 data = kcalloc(test_len, sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (!data)
1999 return -ENOMEM;
2000
Andrew Lunn77e9b2a2020-04-20 00:11:52 +02002001 netif_testing_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 ops->self_test(dev, &test, data);
Andrew Lunn77e9b2a2020-04-20 00:11:52 +02002003 netif_testing_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 ret = -EFAULT;
2006 if (copy_to_user(useraddr, &test, sizeof(test)))
2007 goto out;
2008 useraddr += sizeof(test);
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -05002009 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 goto out;
2011 ret = 0;
2012
2013 out:
2014 kfree(data);
2015 return ret;
2016}
2017
2018static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
2019{
2020 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 u8 *data;
2022 int ret;
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
2025 return -EFAULT;
2026
Michał Mirosław340ae162011-02-15 16:59:16 +00002027 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00002028 if (ret < 0)
2029 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002030 if (ret > S32_MAX / ETH_GSTRING_LEN)
2031 return -ENOMEM;
2032 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07002033
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00002034 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Li RongQing3d883022019-03-29 09:18:02 +08002036 if (gstrings.len) {
2037 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
2038 if (!data)
2039 return -ENOMEM;
2040
2041 __ethtool_get_strings(dev, gstrings.string_set, data);
2042 } else {
2043 data = NULL;
2044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 ret = -EFAULT;
2047 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
2048 goto out;
2049 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002050 if (gstrings.len &&
Gustavo A. R. Silvaed717612021-09-28 14:57:35 -05002051 copy_to_user(useraddr, data,
2052 array_size(gstrings.len, ETH_GSTRING_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 goto out;
2054 ret = 0;
2055
Michał Mirosław340ae162011-02-15 16:59:16 +00002056out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002057 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return ret;
2059}
2060
Alexander Duyck7888fe52021-03-16 17:30:36 -07002061__printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
2062{
2063 va_list args;
2064
2065 va_start(args, fmt);
2066 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
2067 va_end(args);
2068
2069 *data += ETH_GSTRING_LEN;
2070}
2071EXPORT_SYMBOL(ethtool_sprintf);
2072
justinstitt@google.com2a48c632023-12-06 23:16:10 +00002073void ethtool_puts(u8 **data, const char *str)
2074{
2075 strscpy(*data, str, ETH_GSTRING_LEN);
2076 *data += ETH_GSTRING_LEN;
2077}
2078EXPORT_SYMBOL(ethtool_puts);
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
2081{
2082 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01002083 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002084 const struct ethtool_ops *ops = dev->ethtool_ops;
Eric Dumazet5ae21952021-12-04 20:22:00 -08002085 netdevice_tracker dev_tracker;
Ben Hutchings68f512f2011-04-02 00:35:15 +01002086 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002088 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return -EOPNOTSUPP;
2090
Ben Hutchings68f512f2011-04-02 00:35:15 +01002091 if (busy)
2092 return -EBUSY;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (copy_from_user(&id, useraddr, sizeof(id)))
2095 return -EFAULT;
2096
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002097 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00002098 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01002099 return rc;
2100
2101 /* Drop the RTNL lock while waiting, but prevent reentry or
2102 * removal of the device.
2103 */
2104 busy = true;
Jakub Kicinskid62607c2022-06-07 21:39:55 -07002105 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
Ben Hutchings68f512f2011-04-02 00:35:15 +01002106 rtnl_unlock();
2107
2108 if (rc == 0) {
2109 /* Driver will handle this itself */
2110 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00002111 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01002112 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00002113 /* Driver expects to be called at twice the frequency in rc */
Edward Cree2adc6ed2020-09-01 18:52:32 +01002114 int n = rc * 2, interval = HZ / n;
Maxim Korotkov64a8f8f2022-11-22 15:29:01 +03002115 u64 count = mul_u32_u32(n, id.data);
2116 u64 i = 0;
Ben Hutchings68f512f2011-04-02 00:35:15 +01002117
Allan, Bruce Wfce55922011-04-13 13:09:10 +00002118 do {
Edward Cree2adc6ed2020-09-01 18:52:32 +01002119 rtnl_lock();
2120 rc = ops->set_phys_id(dev,
2121 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2122 rtnl_unlock();
2123 if (rc)
2124 break;
2125 schedule_timeout_interruptible(interval);
2126 } while (!signal_pending(current) && (!id.data || i < count));
Ben Hutchings68f512f2011-04-02 00:35:15 +01002127 }
2128
2129 rtnl_lock();
Jakub Kicinskid62607c2022-06-07 21:39:55 -07002130 netdev_put(dev, &dev_tracker);
Ben Hutchings68f512f2011-04-02 00:35:15 +01002131 busy = false;
2132
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002133 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01002134 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2138{
2139 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07002140 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07002142 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00002144 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07002145 return -EOPNOTSUPP;
2146
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00002147 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07002148 if (n_stats < 0)
2149 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002150 if (n_stats > S32_MAX / sizeof(u64))
2151 return -ENOMEM;
2152 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2154 return -EFAULT;
2155
Jeff Garzikff03d492007-08-15 16:01:08 -07002156 stats.n_stats = n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Li RongQing3d883022019-03-29 09:18:02 +08002158 if (n_stats) {
2159 data = vzalloc(array_size(n_stats, sizeof(u64)));
2160 if (!data)
2161 return -ENOMEM;
2162 ops->get_ethtool_stats(dev, &stats, data);
2163 } else {
2164 data = NULL;
2165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 ret = -EFAULT;
2168 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2169 goto out;
2170 useraddr += sizeof(stats);
Gustavo A. R. Silva3dd14992020-06-15 18:01:07 -05002171 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto out;
2173 ret = 0;
2174
2175 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002176 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return ret;
2178}
2179
Daniil Tatianin201ed312022-12-26 14:48:25 +03002180static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01002181{
Andrew Lunnf3a40942015-12-30 16:28:25 +01002182 if (n_stats < 0)
2183 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002184 if (n_stats > S32_MAX / sizeof(u64))
2185 return -ENOMEM;
Daniil Tatianin9deb1e92022-12-26 14:48:23 +03002186 if (WARN_ON_ONCE(!n_stats))
2187 return -EOPNOTSUPP;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002188
Daniil Tatianin201ed312022-12-26 14:48:25 +03002189 *data = vzalloc(array_size(n_stats, sizeof(u64)));
2190 if (!*data)
2191 return -ENOMEM;
2192
2193 return 0;
2194}
2195
2196static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2197 struct ethtool_stats *stats,
2198 u64 **data)
2199 {
2200 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2201 int n_stats, ret;
2202
2203 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2204 return -EOPNOTSUPP;
2205
2206 n_stats = phy_ops->get_sset_count(phydev);
2207
2208 ret = ethtool_vzalloc_stats_array(n_stats, data);
2209 if (ret)
2210 return ret;
2211
2212 stats->n_stats = n_stats;
2213 return phy_ops->get_stats(phydev, stats, *data);
2214}
2215
2216static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2217 struct ethtool_stats *stats,
2218 u64 **data)
2219{
2220 const struct ethtool_ops *ops = dev->ethtool_ops;
2221 int n_stats, ret;
2222
Su Hui0dcc53a2024-06-05 11:47:43 +08002223 if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats)
Daniil Tatianin201ed312022-12-26 14:48:25 +03002224 return -EOPNOTSUPP;
2225
2226 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2227
2228 ret = ethtool_vzalloc_stats_array(n_stats, data);
2229 if (ret)
2230 return ret;
2231
2232 stats->n_stats = n_stats;
2233 ops->get_ethtool_phy_stats(dev, stats, *data);
2234
2235 return 0;
2236}
2237
2238static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2239{
2240 struct phy_device *phydev = dev->phydev;
2241 struct ethtool_stats stats;
2242 u64 *data = NULL;
2243 int ret = -EOPNOTSUPP;
2244
Andrew Lunnf3a40942015-12-30 16:28:25 +01002245 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2246 return -EFAULT;
2247
Daniil Tatianin201ed312022-12-26 14:48:25 +03002248 if (phydev)
2249 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002250
Daniil Tatianin201ed312022-12-26 14:48:25 +03002251 if (ret == -EOPNOTSUPP)
2252 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
Li RongQing3d883022019-03-29 09:18:02 +08002253
Daniil Tatianin201ed312022-12-26 14:48:25 +03002254 if (ret)
2255 goto out;
2256
2257 if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2258 ret = -EFAULT;
2259 goto out;
Florian Fainelli99943382018-04-25 12:12:48 -07002260 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01002261
Andrew Lunnf3a40942015-12-30 16:28:25 +01002262 useraddr += sizeof(stats);
Daniil Tatianin201ed312022-12-26 14:48:25 +03002263 if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2264 ret = -EFAULT;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002265
2266 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002267 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002268 return ret;
2269}
2270
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002271static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002272{
2273 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002274
Matthew Wilcox313674a2007-07-31 14:00:29 -07002275 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002276 return -EFAULT;
2277
Matthew Wilcox313674a2007-07-31 14:00:29 -07002278 if (epaddr.size < dev->addr_len)
2279 return -ETOOSMALL;
2280 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002281
Jon Wetzela6f9a702005-08-20 17:15:54 -07002282 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002283 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002284 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002285 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2286 return -EFAULT;
2287 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002288}
2289
Jeff Garzik13c99b22007-08-15 16:01:56 -07002290static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2291 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002292{
Roland Dreier8e557422010-02-11 12:14:23 -08002293 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002294
Jeff Garzik13c99b22007-08-15 16:01:56 -07002295 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002296 return -EOPNOTSUPP;
2297
Jeff Garzik13c99b22007-08-15 16:01:56 -07002298 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002299
2300 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2301 return -EFAULT;
2302 return 0;
2303}
2304
Jeff Garzik13c99b22007-08-15 16:01:56 -07002305static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2306 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002307{
2308 struct ethtool_value edata;
2309
Jeff Garzik13c99b22007-08-15 16:01:56 -07002310 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002311 return -EOPNOTSUPP;
2312
2313 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2314 return -EFAULT;
2315
Jeff Garzik13c99b22007-08-15 16:01:56 -07002316 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002317 return 0;
2318}
2319
Jeff Garzik13c99b22007-08-15 16:01:56 -07002320static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2321 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002322{
2323 struct ethtool_value edata;
2324
Jeff Garzik13c99b22007-08-15 16:01:56 -07002325 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002326 return -EOPNOTSUPP;
2327
2328 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2329 return -EFAULT;
2330
Jeff Garzik13c99b22007-08-15 16:01:56 -07002331 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002332}
2333
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002334static int
2335ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002336{
Jakub Kicinski1af0a092021-10-30 10:18:51 -07002337 if (!dev->ethtool_ops->flash_device) {
2338 req->devlink = netdev_to_devlink_get(dev);
2339 return 0;
2340 }
Jakub Kicinski4eceba12019-02-14 13:40:45 -08002341
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002342 return dev->ethtool_ops->flash_device(dev, &req->efl);
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002343}
2344
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002345static int ethtool_set_dump(struct net_device *dev,
2346 void __user *useraddr)
2347{
2348 struct ethtool_dump dump;
2349
2350 if (!dev->ethtool_ops->set_dump)
2351 return -EOPNOTSUPP;
2352
2353 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2354 return -EFAULT;
2355
2356 return dev->ethtool_ops->set_dump(dev, &dump);
2357}
2358
2359static int ethtool_get_dump_flag(struct net_device *dev,
2360 void __user *useraddr)
2361{
2362 int ret;
2363 struct ethtool_dump dump;
2364 const struct ethtool_ops *ops = dev->ethtool_ops;
2365
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002366 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002367 return -EOPNOTSUPP;
2368
2369 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2370 return -EFAULT;
2371
2372 ret = ops->get_dump_flag(dev, &dump);
2373 if (ret)
2374 return ret;
2375
2376 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2377 return -EFAULT;
2378 return 0;
2379}
2380
2381static int ethtool_get_dump_data(struct net_device *dev,
2382 void __user *useraddr)
2383{
2384 int ret;
2385 __u32 len;
2386 struct ethtool_dump dump, tmp;
2387 const struct ethtool_ops *ops = dev->ethtool_ops;
2388 void *data = NULL;
2389
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002390 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002391 return -EOPNOTSUPP;
2392
2393 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2394 return -EFAULT;
2395
2396 memset(&tmp, 0, sizeof(tmp));
2397 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2398 ret = ops->get_dump_flag(dev, &tmp);
2399 if (ret)
2400 return ret;
2401
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002402 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002403 if (!len)
2404 return -EFAULT;
2405
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002406 /* Don't ever let the driver think there's more space available
2407 * than it requested with .get_dump_flag().
2408 */
2409 dump.len = len;
2410
2411 /* Always allocate enough space to hold the whole thing so that the
2412 * driver does not need to check the length and bother with partial
2413 * dumping.
2414 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002415 data = vzalloc(tmp.len);
2416 if (!data)
2417 return -ENOMEM;
2418 ret = ops->get_dump_data(dev, &dump, data);
2419 if (ret)
2420 goto out;
2421
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002422 /* There are two sane possibilities:
2423 * 1. The driver's .get_dump_data() does not touch dump.len.
2424 * 2. Or it may set dump.len to how much it really writes, which
2425 * should be tmp.len (or len if it can do a partial dump).
2426 * In any case respond to userspace with the actual length of data
2427 * it's receiving.
2428 */
2429 WARN_ON(dump.len != len && dump.len != tmp.len);
2430 dump.len = len;
2431
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002432 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2433 ret = -EFAULT;
2434 goto out;
2435 }
2436 useraddr += offsetof(struct ethtool_dump, data);
2437 if (copy_to_user(useraddr, data, len))
2438 ret = -EFAULT;
2439out:
2440 vfree(data);
2441 return ret;
2442}
2443
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002444static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2445{
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002446 struct ethtool_ts_info info;
Michal Kubecek5b071c52020-03-28 00:01:58 +01002447 int err;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002448
Michal Kubecek5b071c52020-03-28 00:01:58 +01002449 err = __ethtool_get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002450 if (err)
2451 return err;
2452
2453 if (copy_to_user(useraddr, &info, sizeof(info)))
Michal Kubecek5b071c52020-03-28 00:01:58 +01002454 return -EFAULT;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002455
Michal Kubecek5b071c52020-03-28 00:01:58 +01002456 return 0;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002457}
2458
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002459int ethtool_get_module_info_call(struct net_device *dev,
2460 struct ethtool_modinfo *modinfo)
Ed Swierk2f438362015-01-02 17:27:56 -08002461{
2462 const struct ethtool_ops *ops = dev->ethtool_ops;
2463 struct phy_device *phydev = dev->phydev;
2464
Russell Kinge679c9c2018-03-28 15:44:16 -07002465 if (dev->sfp_bus)
2466 return sfp_get_module_info(dev->sfp_bus, modinfo);
2467
Ed Swierk2f438362015-01-02 17:27:56 -08002468 if (phydev && phydev->drv && phydev->drv->module_info)
2469 return phydev->drv->module_info(phydev, modinfo);
2470
2471 if (ops->get_module_info)
2472 return ops->get_module_info(dev, modinfo);
2473
2474 return -EOPNOTSUPP;
2475}
2476
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002477static int ethtool_get_module_info(struct net_device *dev,
2478 void __user *useraddr)
2479{
2480 int ret;
2481 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002482
2483 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2484 return -EFAULT;
2485
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002486 ret = ethtool_get_module_info_call(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002487 if (ret)
2488 return ret;
2489
2490 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2491 return -EFAULT;
2492
2493 return 0;
2494}
2495
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002496int ethtool_get_module_eeprom_call(struct net_device *dev,
2497 struct ethtool_eeprom *ee, u8 *data)
Ed Swierk2f438362015-01-02 17:27:56 -08002498{
2499 const struct ethtool_ops *ops = dev->ethtool_ops;
2500 struct phy_device *phydev = dev->phydev;
2501
Russell Kinge679c9c2018-03-28 15:44:16 -07002502 if (dev->sfp_bus)
2503 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2504
Ed Swierk2f438362015-01-02 17:27:56 -08002505 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2506 return phydev->drv->module_eeprom(phydev, ee, data);
2507
2508 if (ops->get_module_eeprom)
2509 return ops->get_module_eeprom(dev, ee, data);
2510
2511 return -EOPNOTSUPP;
2512}
2513
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002514static int ethtool_get_module_eeprom(struct net_device *dev,
2515 void __user *useraddr)
2516{
2517 int ret;
2518 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002519
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002520 ret = ethtool_get_module_info_call(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002521 if (ret)
2522 return ret;
2523
Ed Swierk2f438362015-01-02 17:27:56 -08002524 return ethtool_get_any_eeprom(dev, useraddr,
Andrew Lunn95dfc7e2021-04-09 11:06:38 +03002525 ethtool_get_module_eeprom_call,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002526 modinfo.eeprom_len);
2527}
2528
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302529static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2530{
2531 switch (tuna->id) {
2532 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002533 case ETHTOOL_TX_COPYBREAK:
Hao Chen448f4132021-11-18 20:12:40 +08002534 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302535 if (tuna->len != sizeof(u32) ||
2536 tuna->type_id != ETHTOOL_TUNABLE_U32)
2537 return -EINVAL;
2538 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002539 case ETHTOOL_PFC_PREVENTION_TOUT:
2540 if (tuna->len != sizeof(u16) ||
2541 tuna->type_id != ETHTOOL_TUNABLE_U16)
2542 return -EINVAL;
2543 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302544 default:
2545 return -EINVAL;
2546 }
2547
2548 return 0;
2549}
2550
2551static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2552{
2553 int ret;
2554 struct ethtool_tunable tuna;
2555 const struct ethtool_ops *ops = dev->ethtool_ops;
2556 void *data;
2557
2558 if (!ops->get_tunable)
2559 return -EOPNOTSUPP;
2560 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2561 return -EFAULT;
2562 ret = ethtool_tunable_valid(&tuna);
2563 if (ret)
2564 return ret;
Austin Kim80ec82e2021-06-09 03:34:25 +01002565 data = kzalloc(tuna.len, GFP_USER);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302566 if (!data)
2567 return -ENOMEM;
2568 ret = ops->get_tunable(dev, &tuna, data);
2569 if (ret)
2570 goto out;
2571 useraddr += sizeof(tuna);
2572 ret = -EFAULT;
2573 if (copy_to_user(useraddr, data, tuna.len))
2574 goto out;
2575 ret = 0;
2576
2577out:
2578 kfree(data);
2579 return ret;
2580}
2581
2582static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2583{
2584 int ret;
2585 struct ethtool_tunable tuna;
2586 const struct ethtool_ops *ops = dev->ethtool_ops;
2587 void *data;
2588
2589 if (!ops->set_tunable)
2590 return -EOPNOTSUPP;
2591 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2592 return -EFAULT;
2593 ret = ethtool_tunable_valid(&tuna);
2594 if (ret)
2595 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302596 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002597 data = memdup_user(useraddr, tuna.len);
2598 if (IS_ERR(data))
2599 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302600 ret = ops->set_tunable(dev, &tuna, data);
2601
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302602 kfree(data);
2603 return ret;
2604}
2605
Arnd Bergmann3499e872019-03-07 16:58:35 +01002606static noinline_for_stack int
2607ethtool_get_per_queue_coalesce(struct net_device *dev,
2608 void __user *useraddr,
2609 struct ethtool_per_queue_op *per_queue_opt)
Kan Liang421797b2016-02-19 09:24:02 -05002610{
2611 u32 bit;
2612 int ret;
2613 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2614
2615 if (!dev->ethtool_ops->get_per_queue_coalesce)
2616 return -EOPNOTSUPP;
2617
2618 useraddr += sizeof(*per_queue_opt);
2619
Yury Norov3aa56882018-02-06 15:38:06 -08002620 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2621 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002622
2623 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2624 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2625
2626 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2627 if (ret != 0)
2628 return ret;
2629 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2630 return -EFAULT;
2631 useraddr += sizeof(coalesce);
2632 }
2633
2634 return 0;
2635}
2636
Arnd Bergmann3499e872019-03-07 16:58:35 +01002637static noinline_for_stack int
2638ethtool_set_per_queue_coalesce(struct net_device *dev,
2639 void __user *useraddr,
2640 struct ethtool_per_queue_op *per_queue_opt)
Kan Liangf38d1382016-02-19 09:24:03 -05002641{
2642 u32 bit;
2643 int i, ret = 0;
2644 int n_queue;
2645 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2646 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2647
2648 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2649 (!dev->ethtool_ops->get_per_queue_coalesce))
2650 return -EOPNOTSUPP;
2651
2652 useraddr += sizeof(*per_queue_opt);
2653
Yury Norov3aa56882018-02-06 15:38:06 -08002654 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002655 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2656 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2657 if (!backup)
2658 return -ENOMEM;
2659
2660 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2661 struct ethtool_coalesce coalesce;
2662
2663 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2664 if (ret != 0)
2665 goto roll_back;
2666
2667 tmp++;
2668
2669 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2670 ret = -EFAULT;
2671 goto roll_back;
2672 }
2673
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08002674 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2675 ret = -EOPNOTSUPP;
2676 goto roll_back;
2677 }
2678
Kan Liangf38d1382016-02-19 09:24:03 -05002679 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2680 if (ret != 0)
2681 goto roll_back;
2682
2683 useraddr += sizeof(coalesce);
2684 }
2685
2686roll_back:
2687 if (ret != 0) {
2688 tmp = backup;
2689 for_each_set_bit(i, queue_mask, bit) {
2690 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2691 tmp++;
2692 }
2693 }
2694 kfree(backup);
2695
2696 return ret;
2697}
2698
Arnd Bergmann3499e872019-03-07 16:58:35 +01002699static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002700 void __user *useraddr, u32 sub_cmd)
Kan Liangac2c7ad2016-02-19 09:24:01 -05002701{
2702 struct ethtool_per_queue_op per_queue_opt;
2703
2704 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2705 return -EFAULT;
2706
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002707 if (per_queue_opt.sub_command != sub_cmd)
2708 return -EINVAL;
2709
Kan Liangac2c7ad2016-02-19 09:24:01 -05002710 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002711 case ETHTOOL_GCOALESCE:
2712 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002713 case ETHTOOL_SCOALESCE:
2714 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002715 default:
2716 return -EOPNOTSUPP;
Tom Rix9d253c02020-11-01 07:56:01 -08002717 }
Kan Liangac2c7ad2016-02-19 09:24:01 -05002718}
2719
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002720static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2721{
2722 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002723 case ETHTOOL_PHY_DOWNSHIFT:
Heiner Kallweit3aeb0802019-03-25 19:34:58 +01002724 case ETHTOOL_PHY_FAST_LINK_DOWN:
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002725 if (tuna->len != sizeof(u8) ||
2726 tuna->type_id != ETHTOOL_TUNABLE_U8)
2727 return -EINVAL;
2728 break;
Alexandru Ardelean9f2f13f2019-09-16 10:35:25 +03002729 case ETHTOOL_PHY_EDPD:
2730 if (tuna->len != sizeof(u16) ||
2731 tuna->type_id != ETHTOOL_TUNABLE_U16)
2732 return -EINVAL;
2733 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002734 default:
2735 return -EINVAL;
2736 }
2737
2738 return 0;
2739}
2740
2741static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2742{
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002743 struct phy_device *phydev = dev->phydev;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002744 struct ethtool_tunable tuna;
2745 bool phy_drv_tunable;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002746 void *data;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002747 int ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002748
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002749 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2750 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002751 return -EOPNOTSUPP;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002752 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2753 return -EFAULT;
2754 ret = ethtool_phy_tunable_valid(&tuna);
2755 if (ret)
2756 return ret;
Austin Kim80ec82e2021-06-09 03:34:25 +01002757 data = kzalloc(tuna.len, GFP_USER);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002758 if (!data)
2759 return -ENOMEM;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002760 if (phy_drv_tunable) {
2761 mutex_lock(&phydev->lock);
2762 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2763 mutex_unlock(&phydev->lock);
2764 } else {
2765 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2766 }
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002767 if (ret)
2768 goto out;
2769 useraddr += sizeof(tuna);
2770 ret = -EFAULT;
2771 if (copy_to_user(useraddr, data, tuna.len))
2772 goto out;
2773 ret = 0;
2774
2775out:
2776 kfree(data);
2777 return ret;
2778}
2779
2780static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2781{
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002782 struct phy_device *phydev = dev->phydev;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002783 struct ethtool_tunable tuna;
2784 bool phy_drv_tunable;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002785 void *data;
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002786 int ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002787
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002788 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2789 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002790 return -EOPNOTSUPP;
2791 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2792 return -EFAULT;
2793 ret = ethtool_phy_tunable_valid(&tuna);
2794 if (ret)
2795 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002796 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002797 data = memdup_user(useraddr, tuna.len);
2798 if (IS_ERR(data))
2799 return PTR_ERR(data);
Igor Russkikhc6db31f2020-10-05 18:39:37 +03002800 if (phy_drv_tunable) {
2801 mutex_lock(&phydev->lock);
2802 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2803 mutex_unlock(&phydev->lock);
2804 } else {
2805 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2806 }
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002807
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002808 kfree(data);
2809 return ret;
2810}
2811
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002812static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2813{
Li RongQinge83887f2019-02-27 20:47:57 +08002814 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002815 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002816
2817 if (!dev->ethtool_ops->get_fecparam)
2818 return -EOPNOTSUPP;
2819
Edward Creea6d50512018-02-28 19:15:58 +00002820 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2821 if (rc)
2822 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002823
Jakub Kicinski240e11442021-03-24 18:11:57 -07002824 if (WARN_ON_ONCE(fecparam.reserved))
2825 fecparam.reserved = 0;
2826
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002827 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2828 return -EFAULT;
2829 return 0;
2830}
2831
2832static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2833{
2834 struct ethtool_fecparam fecparam;
2835
2836 if (!dev->ethtool_ops->set_fecparam)
2837 return -EOPNOTSUPP;
2838
2839 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2840 return -EFAULT;
2841
Jakub Kicinskicf2cc0b2021-03-26 13:22:22 -07002842 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
Jakub Kicinski42ce1272021-03-24 18:11:59 -07002843 return -EINVAL;
2844
Jakub Kicinskid3b37fc2021-03-24 18:11:58 -07002845 fecparam.active_fec = 0;
Jakub Kicinski240e11442021-03-24 18:11:57 -07002846 fecparam.reserved = 0;
2847
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002848 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2849}
2850
Yan Burman600fed52013-06-03 02:03:34 +00002851/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07002853static int
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002854__dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2855 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002857 struct net_device *dev;
2858 u32 sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002860 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002862 dev = __dev_get_by_name(net, ifr->ifr_name);
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002863 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return -ENODEV;
2865
Kan Liangac2c7ad2016-02-19 09:24:01 -05002866 if (ethcmd == ETHTOOL_PERQUEUE) {
2867 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2868 return -EFAULT;
2869 } else {
2870 sub_cmd = ethcmd;
2871 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002872 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002873 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002874 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002875 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002876 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db22012-06-12 13:05:41 +00002877 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002878 case ETHTOOL_GCOALESCE:
2879 case ETHTOOL_GRINGPARAM:
2880 case ETHTOOL_GPAUSEPARAM:
2881 case ETHTOOL_GRXCSUM:
2882 case ETHTOOL_GTXCSUM:
2883 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002884 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002885 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db22012-06-12 13:05:41 +00002886 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002887 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002888 case ETHTOOL_GTSO:
2889 case ETHTOOL_GPERMADDR:
Maciej Żenczykowski474ff262018-09-22 01:34:01 -07002890 case ETHTOOL_GUFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002891 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002892 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002893 case ETHTOOL_GFLAGS:
2894 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002895 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002896 case ETHTOOL_GRXRINGS:
2897 case ETHTOOL_GRXCLSRLCNT:
2898 case ETHTOOL_GRXCLSRULE:
2899 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db22012-06-12 13:05:41 +00002900 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302901 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002902 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db22012-06-12 13:05:41 +00002903 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002904 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db22012-06-12 13:05:41 +00002905 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302906 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002907 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002908 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002909 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002910 break;
2911 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002912 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002913 return -EPERM;
2914 }
2915
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002916 if (dev->dev.parent)
2917 pm_runtime_get_sync(dev->dev.parent);
2918
2919 if (!netif_device_present(dev)) {
2920 rc = -ENODEV;
2921 goto out;
2922 }
2923
chavey97f8aef2010-04-07 21:54:42 -07002924 if (dev->ethtool_ops->begin) {
2925 rc = dev->ethtool_ops->begin(dev);
Heiner Kallweitf32a2132021-08-01 12:36:48 +02002926 if (rc < 0)
2927 goto out;
chavey97f8aef2010-04-07 21:54:42 -07002928 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002929 old_features = dev->features;
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 switch (ethcmd) {
2932 case ETHTOOL_GSET:
2933 rc = ethtool_get_settings(dev, useraddr);
2934 break;
2935 case ETHTOOL_SSET:
2936 rc = ethtool_set_settings(dev, useraddr);
2937 break;
2938 case ETHTOOL_GDRVINFO:
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07002939 rc = ethtool_get_drvinfo(dev, devlink_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 break;
2941 case ETHTOOL_GREGS:
2942 rc = ethtool_get_regs(dev, useraddr);
2943 break;
2944 case ETHTOOL_GWOL:
2945 rc = ethtool_get_wol(dev, useraddr);
2946 break;
2947 case ETHTOOL_SWOL:
2948 rc = ethtool_set_wol(dev, useraddr);
2949 break;
2950 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002951 rc = ethtool_get_value(dev, useraddr, ethcmd,
2952 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 break;
2954 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002955 rc = ethtool_set_value_void(dev, useraddr,
2956 dev->ethtool_ops->set_msglevel);
Michal Kubecek0bda7af2020-01-26 23:11:10 +01002957 if (!rc)
2958 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 break;
Yuval Mintz80f12ecc2012-06-06 17:13:06 +00002960 case ETHTOOL_GEEE:
2961 rc = ethtool_get_eee(dev, useraddr);
2962 break;
2963 case ETHTOOL_SEEE:
2964 rc = ethtool_set_eee(dev, useraddr);
2965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 case ETHTOOL_NWAY_RST:
2967 rc = ethtool_nway_reset(dev);
2968 break;
2969 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002970 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 break;
2972 case ETHTOOL_GEEPROM:
2973 rc = ethtool_get_eeprom(dev, useraddr);
2974 break;
2975 case ETHTOOL_SEEPROM:
2976 rc = ethtool_set_eeprom(dev, useraddr);
2977 break;
2978 case ETHTOOL_GCOALESCE:
2979 rc = ethtool_get_coalesce(dev, useraddr);
2980 break;
2981 case ETHTOOL_SCOALESCE:
2982 rc = ethtool_set_coalesce(dev, useraddr);
2983 break;
2984 case ETHTOOL_GRINGPARAM:
2985 rc = ethtool_get_ringparam(dev, useraddr);
2986 break;
2987 case ETHTOOL_SRINGPARAM:
2988 rc = ethtool_set_ringparam(dev, useraddr);
2989 break;
2990 case ETHTOOL_GPAUSEPARAM:
2991 rc = ethtool_get_pauseparam(dev, useraddr);
2992 break;
2993 case ETHTOOL_SPAUSEPARAM:
2994 rc = ethtool_set_pauseparam(dev, useraddr);
2995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 case ETHTOOL_TEST:
2997 rc = ethtool_self_test(dev, useraddr);
2998 break;
2999 case ETHTOOL_GSTRINGS:
3000 rc = ethtool_get_strings(dev, useraddr);
3001 break;
3002 case ETHTOOL_PHYS_ID:
3003 rc = ethtool_phys_id(dev, useraddr);
3004 break;
3005 case ETHTOOL_GSTATS:
3006 rc = ethtool_get_stats(dev, useraddr);
3007 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07003008 case ETHTOOL_GPERMADDR:
3009 rc = ethtool_get_perm_addr(dev, useraddr);
3010 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07003011 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07003012 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00003013 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07003014 break;
3015 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00003016 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07003017 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07003018 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07003019 rc = ethtool_get_value(dev, useraddr, ethcmd,
3020 dev->ethtool_ops->get_priv_flags);
Michal Kubecek111dcba2020-03-12 21:08:18 +01003021 if (!rc)
3022 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
Jeff Garzik339bf022007-08-15 16:01:32 -07003023 break;
3024 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07003025 rc = ethtool_set_value(dev, useraddr,
3026 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07003027 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07003028 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08003029 case ETHTOOL_GRXRINGS:
3030 case ETHTOOL_GRXCLSRLCNT:
3031 case ETHTOOL_GRXCLSRULE:
3032 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00003033 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07003034 break;
3035 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08003036 case ETHTOOL_SRXCLSRLDEL:
3037 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00003038 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07003039 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00003040 case ETHTOOL_FLASHDEV:
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003041 rc = ethtool_flash_device(dev, devlink_state);
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00003042 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00003043 case ETHTOOL_RESET:
3044 rc = ethtool_reset(dev, useraddr);
3045 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00003046 case ETHTOOL_GSSET_INFO:
3047 rc = ethtool_get_sset_info(dev, useraddr);
3048 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00003049 case ETHTOOL_GRXFHINDIR:
3050 rc = ethtool_get_rxfh_indir(dev, useraddr);
3051 break;
3052 case ETHTOOL_SRXFHINDIR:
3053 rc = ethtool_set_rxfh_indir(dev, useraddr);
3054 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05303055 case ETHTOOL_GRSSH:
3056 rc = ethtool_get_rxfh(dev, useraddr);
3057 break;
3058 case ETHTOOL_SRSSH:
3059 rc = ethtool_set_rxfh(dev, useraddr);
3060 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00003061 case ETHTOOL_GFEATURES:
3062 rc = ethtool_get_features(dev, useraddr);
3063 break;
3064 case ETHTOOL_SFEATURES:
3065 rc = ethtool_set_features(dev, useraddr);
3066 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00003067 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00003068 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00003069 case ETHTOOL_GSG:
3070 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00003071 case ETHTOOL_GGSO:
3072 case ETHTOOL_GGRO:
3073 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
3074 break;
3075 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00003076 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00003077 case ETHTOOL_SSG:
3078 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00003079 case ETHTOOL_SGSO:
3080 case ETHTOOL_SGRO:
3081 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
3082 break;
amit salecha8b5933c2011-04-07 01:58:42 +00003083 case ETHTOOL_GCHANNELS:
3084 rc = ethtool_get_channels(dev, useraddr);
3085 break;
3086 case ETHTOOL_SCHANNELS:
3087 rc = ethtool_set_channels(dev, useraddr);
3088 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00003089 case ETHTOOL_SET_DUMP:
3090 rc = ethtool_set_dump(dev, useraddr);
3091 break;
3092 case ETHTOOL_GET_DUMP_FLAG:
3093 rc = ethtool_get_dump_flag(dev, useraddr);
3094 break;
3095 case ETHTOOL_GET_DUMP_DATA:
3096 rc = ethtool_get_dump_data(dev, useraddr);
3097 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00003098 case ETHTOOL_GET_TS_INFO:
3099 rc = ethtool_get_ts_info(dev, useraddr);
3100 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01003101 case ETHTOOL_GMODULEINFO:
3102 rc = ethtool_get_module_info(dev, useraddr);
3103 break;
3104 case ETHTOOL_GMODULEEEPROM:
3105 rc = ethtool_get_module_eeprom(dev, useraddr);
3106 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05303107 case ETHTOOL_GTUNABLE:
3108 rc = ethtool_get_tunable(dev, useraddr);
3109 break;
3110 case ETHTOOL_STUNABLE:
3111 rc = ethtool_set_tunable(dev, useraddr);
3112 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01003113 case ETHTOOL_GPHYSTATS:
3114 rc = ethtool_get_phy_stats(dev, useraddr);
3115 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05003116 case ETHTOOL_PERQUEUE:
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05003117 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
Kan Liangac2c7ad2016-02-19 09:24:01 -05003118 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08003119 case ETHTOOL_GLINKSETTINGS:
3120 rc = ethtool_get_link_ksettings(dev, useraddr);
3121 break;
3122 case ETHTOOL_SLINKSETTINGS:
3123 rc = ethtool_set_link_ksettings(dev, useraddr);
3124 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01003125 case ETHTOOL_PHY_GTUNABLE:
3126 rc = get_phy_tunable(dev, useraddr);
3127 break;
3128 case ETHTOOL_PHY_STUNABLE:
3129 rc = set_phy_tunable(dev, useraddr);
3130 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07003131 case ETHTOOL_GFECPARAM:
3132 rc = ethtool_get_fecparam(dev, useraddr);
3133 break;
3134 case ETHTOOL_SFECPARAM:
3135 rc = ethtool_set_fecparam(dev, useraddr);
3136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07003138 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003140
Stephen Hemmingere71a4782007-04-10 20:10:33 -07003141 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07003143
3144 if (old_features != dev->features)
3145 netdev_features_change(dev);
Heiner Kallweitf32a2132021-08-01 12:36:48 +02003146out:
3147 if (dev->dev.parent)
3148 pm_runtime_put(dev->dev.parent);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07003149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003152
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07003153int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3154{
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003155 struct ethtool_devlink_compat *state;
3156 u32 ethcmd;
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07003157 int rc;
3158
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003159 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3160 return -EFAULT;
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07003161
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003162 state = kzalloc(sizeof(*state), GFP_KERNEL);
3163 if (!state)
3164 return -ENOMEM;
3165
3166 switch (ethcmd) {
3167 case ETHTOOL_FLASHDEV:
3168 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3169 rc = -EFAULT;
3170 goto exit_free;
3171 }
3172 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3173 break;
3174 }
3175
3176 rtnl_lock();
3177 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3178 rtnl_unlock();
3179 if (rc)
3180 goto exit_free;
3181
3182 switch (ethcmd) {
Jakub Kicinski1af0a092021-10-30 10:18:51 -07003183 case ETHTOOL_FLASHDEV:
3184 if (state->devlink)
3185 rc = devlink_compat_flash_update(state->devlink,
3186 state->efl.data);
3187 break;
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003188 case ETHTOOL_GDRVINFO:
Jakub Kicinski1af0a092021-10-30 10:18:51 -07003189 if (state->devlink)
3190 devlink_compat_running_version(state->devlink,
3191 state->info.fw_version,
3192 sizeof(state->info.fw_version));
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003193 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3194 rc = -EFAULT;
3195 goto exit_free;
3196 }
3197 break;
3198 }
3199
3200exit_free:
Jakub Kicinski1af0a092021-10-30 10:18:51 -07003201 if (state->devlink)
3202 devlink_put(state->devlink);
Jakub Kicinski095cfcf2021-10-30 10:18:49 -07003203 kfree(state);
Jakub Kicinskif49deaa2021-10-30 10:18:48 -07003204 return rc;
3205}
3206
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003207struct ethtool_rx_flow_key {
3208 struct flow_dissector_key_basic basic;
3209 union {
3210 struct flow_dissector_key_ipv4_addrs ipv4;
3211 struct flow_dissector_key_ipv6_addrs ipv6;
3212 };
3213 struct flow_dissector_key_ports tp;
3214 struct flow_dissector_key_ip ip;
3215 struct flow_dissector_key_vlan vlan;
3216 struct flow_dissector_key_eth_addrs eth_addrs;
3217} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3218
3219struct ethtool_rx_flow_match {
3220 struct flow_dissector dissector;
3221 struct ethtool_rx_flow_key key;
3222 struct ethtool_rx_flow_key mask;
3223};
3224
3225struct ethtool_rx_flow_rule *
3226ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3227{
3228 const struct ethtool_rx_flow_spec *fs = input->fs;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003229 struct ethtool_rx_flow_match *match;
3230 struct ethtool_rx_flow_rule *flow;
3231 struct flow_action_entry *act;
3232
3233 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3234 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3235 if (!flow)
3236 return ERR_PTR(-ENOMEM);
3237
3238 /* ethtool_rx supports only one single action per rule. */
3239 flow->rule = flow_rule_alloc(1);
3240 if (!flow->rule) {
3241 kfree(flow);
3242 return ERR_PTR(-ENOMEM);
3243 }
3244
3245 match = (struct ethtool_rx_flow_match *)flow->priv;
3246 flow->rule->match.dissector = &match->dissector;
3247 flow->rule->match.mask = &match->mask;
3248 flow->rule->match.key = &match->key;
3249
3250 match->mask.basic.n_proto = htons(0xffff);
3251
3252 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
Maxime Chevallier5b9469a2019-06-27 10:52:26 +02003253 case ETHER_FLOW: {
3254 const struct ethhdr *ether_spec, *ether_m_spec;
3255
3256 ether_spec = &fs->h_u.ether_spec;
3257 ether_m_spec = &fs->m_u.ether_spec;
3258
3259 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3260 ether_addr_copy(match->key.eth_addrs.src,
3261 ether_spec->h_source);
3262 ether_addr_copy(match->mask.eth_addrs.src,
3263 ether_m_spec->h_source);
3264 }
3265 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3266 ether_addr_copy(match->key.eth_addrs.dst,
3267 ether_spec->h_dest);
3268 ether_addr_copy(match->mask.eth_addrs.dst,
3269 ether_m_spec->h_dest);
3270 }
3271 if (ether_m_spec->h_proto) {
3272 match->key.basic.n_proto = ether_spec->h_proto;
3273 match->mask.basic.n_proto = ether_m_spec->h_proto;
3274 }
3275 }
3276 break;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003277 case TCP_V4_FLOW:
3278 case UDP_V4_FLOW: {
3279 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3280
3281 match->key.basic.n_proto = htons(ETH_P_IP);
3282
3283 v4_spec = &fs->h_u.tcp_ip4_spec;
3284 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3285
3286 if (v4_m_spec->ip4src) {
3287 match->key.ipv4.src = v4_spec->ip4src;
3288 match->mask.ipv4.src = v4_m_spec->ip4src;
3289 }
3290 if (v4_m_spec->ip4dst) {
3291 match->key.ipv4.dst = v4_spec->ip4dst;
3292 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3293 }
3294 if (v4_m_spec->ip4src ||
3295 v4_m_spec->ip4dst) {
3296 match->dissector.used_keys |=
Ratheesh Kannoth2b3082c2023-07-29 04:52:15 +05303297 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003298 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3299 offsetof(struct ethtool_rx_flow_key, ipv4);
3300 }
3301 if (v4_m_spec->psrc) {
3302 match->key.tp.src = v4_spec->psrc;
3303 match->mask.tp.src = v4_m_spec->psrc;
3304 }
3305 if (v4_m_spec->pdst) {
3306 match->key.tp.dst = v4_spec->pdst;
3307 match->mask.tp.dst = v4_m_spec->pdst;
3308 }
3309 if (v4_m_spec->psrc ||
3310 v4_m_spec->pdst) {
3311 match->dissector.used_keys |=
Ratheesh Kannoth2b3082c2023-07-29 04:52:15 +05303312 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003313 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3314 offsetof(struct ethtool_rx_flow_key, tp);
3315 }
3316 if (v4_m_spec->tos) {
3317 match->key.ip.tos = v4_spec->tos;
3318 match->mask.ip.tos = v4_m_spec->tos;
3319 match->dissector.used_keys |=
3320 BIT(FLOW_DISSECTOR_KEY_IP);
3321 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3322 offsetof(struct ethtool_rx_flow_key, ip);
3323 }
3324 }
3325 break;
3326 case TCP_V6_FLOW:
3327 case UDP_V6_FLOW: {
3328 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3329
3330 match->key.basic.n_proto = htons(ETH_P_IPV6);
3331
3332 v6_spec = &fs->h_u.tcp_ip6_spec;
3333 v6_m_spec = &fs->m_u.tcp_ip6_spec;
Kuniyuki Iwashima8cdc3222023-03-27 16:54:54 -07003334 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003335 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3336 sizeof(match->key.ipv6.src));
3337 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3338 sizeof(match->mask.ipv6.src));
3339 }
Kuniyuki Iwashima8cdc3222023-03-27 16:54:54 -07003340 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003341 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3342 sizeof(match->key.ipv6.dst));
3343 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3344 sizeof(match->mask.ipv6.dst));
3345 }
Kuniyuki Iwashima8cdc3222023-03-27 16:54:54 -07003346 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
3347 !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003348 match->dissector.used_keys |=
Ratheesh Kannoth2b3082c2023-07-29 04:52:15 +05303349 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003350 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3351 offsetof(struct ethtool_rx_flow_key, ipv6);
3352 }
3353 if (v6_m_spec->psrc) {
3354 match->key.tp.src = v6_spec->psrc;
3355 match->mask.tp.src = v6_m_spec->psrc;
3356 }
3357 if (v6_m_spec->pdst) {
3358 match->key.tp.dst = v6_spec->pdst;
3359 match->mask.tp.dst = v6_m_spec->pdst;
3360 }
3361 if (v6_m_spec->psrc ||
3362 v6_m_spec->pdst) {
3363 match->dissector.used_keys |=
Ratheesh Kannoth2b3082c2023-07-29 04:52:15 +05303364 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003365 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3366 offsetof(struct ethtool_rx_flow_key, tp);
3367 }
3368 if (v6_m_spec->tclass) {
3369 match->key.ip.tos = v6_spec->tclass;
3370 match->mask.ip.tos = v6_m_spec->tclass;
3371 match->dissector.used_keys |=
Ratheesh Kannoth2b3082c2023-07-29 04:52:15 +05303372 BIT_ULL(FLOW_DISSECTOR_KEY_IP);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003373 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3374 offsetof(struct ethtool_rx_flow_key, ip);
3375 }
3376 }
3377 break;
3378 default:
3379 ethtool_rx_flow_rule_destroy(flow);
3380 return ERR_PTR(-EINVAL);
3381 }
3382
3383 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3384 case TCP_V4_FLOW:
3385 case TCP_V6_FLOW:
3386 match->key.basic.ip_proto = IPPROTO_TCP;
Vishal Kulkarnid0a84e12020-08-19 00:25:03 +05303387 match->mask.basic.ip_proto = 0xff;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003388 break;
3389 case UDP_V4_FLOW:
3390 case UDP_V6_FLOW:
3391 match->key.basic.ip_proto = IPPROTO_UDP;
Vishal Kulkarnid0a84e12020-08-19 00:25:03 +05303392 match->mask.basic.ip_proto = 0xff;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003393 break;
3394 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003395
Ratheesh Kannoth2b3082c2023-07-29 04:52:15 +05303396 match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003397 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3398 offsetof(struct ethtool_rx_flow_key, basic);
3399
3400 if (fs->flow_type & FLOW_EXT) {
3401 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3402 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3403
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003404 if (ext_m_spec->vlan_etype) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003405 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3406 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003407 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003408
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003409 if (ext_m_spec->vlan_tci) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003410 match->key.vlan.vlan_id =
3411 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3412 match->mask.vlan.vlan_id =
3413 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3414
Maxime Chevallierf0d2ca12019-06-12 17:18:38 +02003415 match->key.vlan.vlan_dei =
3416 !!(ext_h_spec->vlan_tci & htons(0x1000));
3417 match->mask.vlan.vlan_dei =
3418 !!(ext_m_spec->vlan_tci & htons(0x1000));
3419
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003420 match->key.vlan.vlan_priority =
3421 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3422 match->mask.vlan.vlan_priority =
3423 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003424 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003425
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003426 if (ext_m_spec->vlan_etype ||
3427 ext_m_spec->vlan_tci) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003428 match->dissector.used_keys |=
Ratheesh Kannoth2b3082c2023-07-29 04:52:15 +05303429 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003430 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3431 offsetof(struct ethtool_rx_flow_key, vlan);
3432 }
3433 }
3434 if (fs->flow_type & FLOW_MAC_EXT) {
3435 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3436 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3437
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003438 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3439 ETH_ALEN);
3440 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3441 ETH_ALEN);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003442
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003443 match->dissector.used_keys |=
Ratheesh Kannoth2b3082c2023-07-29 04:52:15 +05303444 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS);
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003445 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3446 offsetof(struct ethtool_rx_flow_key, eth_addrs);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003447 }
3448
3449 act = &flow->rule->action.entries[0];
3450 switch (fs->ring_cookie) {
3451 case RX_CLS_FLOW_DISC:
3452 act->id = FLOW_ACTION_DROP;
3453 break;
3454 case RX_CLS_FLOW_WAKE:
3455 act->id = FLOW_ACTION_WAKE;
3456 break;
3457 default:
3458 act->id = FLOW_ACTION_QUEUE;
3459 if (fs->flow_type & FLOW_RSS)
3460 act->queue.ctx = input->rss_ctx;
3461
3462 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3463 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3464 break;
3465 }
3466
3467 return flow;
3468}
3469EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3470
3471void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3472{
3473 kfree(flow->rule);
3474 kfree(flow);
3475}
3476EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);