blob: 4650fd6d678cebb5b41e2d3034ad0fd14dc3f073 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4 *
5 * This file is where we call all the ethtool_ops commands to get
Matthew Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000020#include <linux/net_tstamp.h>
21#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000022#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070023#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070024#include <linux/vmalloc.h>
Russell Kinge679c9c2018-03-28 15:44:16 -070025#include <linux/sfp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010027#include <linux/rtnetlink.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010028#include <linux/sched/signal.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080029#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090031/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Some useful ethtool_ops methods that're device independent.
33 * If we find that all drivers want to do the same thing here,
34 * we can turn these into dev_() function calls.
35 */
36
37u32 ethtool_op_get_link(struct net_device *dev)
38{
39 return netif_carrier_ok(dev) ? 1 : 0;
40}
chavey97f8aef2010-04-07 21:54:42 -070041EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Richard Cochran02eacbd2012-04-03 22:59:22 +000043int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
44{
45 info->so_timestamping =
46 SOF_TIMESTAMPING_TX_SOFTWARE |
47 SOF_TIMESTAMPING_RX_SOFTWARE |
48 SOF_TIMESTAMPING_SOFTWARE;
49 info->phc_index = -1;
50 return 0;
51}
52EXPORT_SYMBOL(ethtool_op_get_ts_info);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Handlers for each ethtool command */
55
Michał Mirosław475414f2011-11-15 15:29:55 +000056#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000057
Michał Mirosław9d921542011-11-15 15:29:55 +000058static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
59 [NETIF_F_SG_BIT] = "tx-scatter-gather",
60 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000061 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
62 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
63 [NETIF_F_HIGHDMA_BIT] = "highdma",
64 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090065 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000066
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090067 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
68 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040069 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
70 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
71 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000072 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
73 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
74 [NETIF_F_LLTX_BIT] = "tx-lockless",
75 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
76 [NETIF_F_GRO_BIT] = "rx-gro",
Michael Chanfb1f5f72017-12-16 03:09:40 -050077 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
Michał Mirosław9d921542011-11-15 15:29:55 +000078 [NETIF_F_LRO_BIT] = "rx-lro",
79
80 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000081 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
82 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
Alexander Duyckcbc53e02016-04-10 21:44:51 -040083 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000084 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
85 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000086 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040087 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
Tom Herbert7e133182016-05-18 09:06:10 -070088 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
89 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000090 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040091 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
Alexander Duyck802ab552016-04-10 21:45:03 -040092 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030093 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +020094 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
Willem de Bruijn83aa0252018-04-26 13:42:21 -040095 [NETIF_F_GSO_UDP_L4_BIT] = "tx-udp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000096
97 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080098 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000099 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
100 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
101 [NETIF_F_RXHASH_BIT] = "rx-hashing",
102 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
103 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
104 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +0000105 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +0000106 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -0800107 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
John Fastabend1c78c642016-02-16 21:17:37 -0800108 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +0200109 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
110 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
Sabrina Dubrocad764a122017-07-21 12:49:28 +0200111 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
Atul Guptae0be6be2018-03-31 21:41:53 +0530112 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
Michał Mirosław9d921542011-11-15 15:29:55 +0000113};
114
Eyal Perry892311f2014-12-02 18:12:10 +0200115static const char
116rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
117 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
118 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800119 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200120};
121
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300122static const char
123tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
124 [ETHTOOL_ID_UNSPEC] = "Unspec",
125 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
126 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
Inbar Karmye1577c12017-11-20 16:14:30 +0200127 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300128};
129
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100130static const char
131phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
132 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100133 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100134};
135
Michał Mirosław5455c692011-02-15 16:59:17 +0000136static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
137{
138 struct ethtool_gfeatures cmd = {
139 .cmd = ETHTOOL_GFEATURES,
140 .size = ETHTOOL_DEV_FEATURE_WORDS,
141 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000142 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000143 u32 __user *sizeaddr;
144 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000145 int i;
146
147 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000148 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000149
150 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000151 features[i].available = (u32)(dev->hw_features >> (32 * i));
152 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
153 features[i].active = (u32)(dev->features >> (32 * i));
154 features[i].never_changed =
155 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000156 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000157
158 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
159 if (get_user(copy_size, sizeaddr))
160 return -EFAULT;
161
162 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
163 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
164
165 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
166 return -EFAULT;
167 useraddr += sizeof(cmd);
168 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
169 return -EFAULT;
170
171 return 0;
172}
173
174static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
175{
176 struct ethtool_sfeatures cmd;
177 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000178 netdev_features_t wanted = 0, valid = 0;
179 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000180
181 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
182 return -EFAULT;
183 useraddr += sizeof(cmd);
184
185 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
186 return -EINVAL;
187
188 if (copy_from_user(features, useraddr, sizeof(features)))
189 return -EFAULT;
190
Michał Mirosław475414f2011-11-15 15:29:55 +0000191 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000192 valid |= (netdev_features_t)features[i].valid << (32 * i);
193 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000194 }
195
196 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000197 return -EINVAL;
198
Michał Mirosław475414f2011-11-15 15:29:55 +0000199 if (valid & ~dev->hw_features) {
200 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000201 ret |= ETHTOOL_F_UNSUPPORTED;
202 }
203
Michał Mirosław475414f2011-11-15 15:29:55 +0000204 dev->wanted_features &= ~valid;
205 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700206 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000207
Michał Mirosław475414f2011-11-15 15:29:55 +0000208 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000209 ret |= ETHTOOL_F_WISH;
210
211 return ret;
212}
213
Andrew Lunnf3a40942015-12-30 16:28:25 +0100214static int phy_get_sset_count(struct phy_device *phydev)
215{
216 int ret;
217
218 if (phydev->drv->get_sset_count &&
219 phydev->drv->get_strings &&
220 phydev->drv->get_stats) {
221 mutex_lock(&phydev->lock);
222 ret = phydev->drv->get_sset_count(phydev);
223 mutex_unlock(&phydev->lock);
224
225 return ret;
226 }
227
228 return -EOPNOTSUPP;
229}
230
Michał Mirosław340ae162011-02-15 16:59:16 +0000231static int __ethtool_get_sset_count(struct net_device *dev, int sset)
232{
233 const struct ethtool_ops *ops = dev->ethtool_ops;
234
Michał Mirosław5455c692011-02-15 16:59:17 +0000235 if (sset == ETH_SS_FEATURES)
236 return ARRAY_SIZE(netdev_features_strings);
237
Eyal Perry892311f2014-12-02 18:12:10 +0200238 if (sset == ETH_SS_RSS_HASH_FUNCS)
239 return ARRAY_SIZE(rss_hash_func_strings);
240
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300241 if (sset == ETH_SS_TUNABLES)
242 return ARRAY_SIZE(tunable_strings);
243
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100244 if (sset == ETH_SS_PHY_TUNABLES)
245 return ARRAY_SIZE(phy_tunable_strings);
246
Andrew Lunnf3a40942015-12-30 16:28:25 +0100247 if (sset == ETH_SS_PHY_STATS) {
248 if (dev->phydev)
249 return phy_get_sset_count(dev->phydev);
250 else
251 return -EOPNOTSUPP;
252 }
253
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000254 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000255 return ops->get_sset_count(dev, sset);
256 else
257 return -EOPNOTSUPP;
258}
259
260static void __ethtool_get_strings(struct net_device *dev,
261 u32 stringset, u8 *data)
262{
263 const struct ethtool_ops *ops = dev->ethtool_ops;
264
Michał Mirosław5455c692011-02-15 16:59:17 +0000265 if (stringset == ETH_SS_FEATURES)
266 memcpy(data, netdev_features_strings,
267 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200268 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
269 memcpy(data, rss_hash_func_strings,
270 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300271 else if (stringset == ETH_SS_TUNABLES)
272 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100273 else if (stringset == ETH_SS_PHY_TUNABLES)
274 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100275 else if (stringset == ETH_SS_PHY_STATS) {
276 struct phy_device *phydev = dev->phydev;
277
278 if (phydev) {
279 mutex_lock(&phydev->lock);
280 phydev->drv->get_strings(phydev, data);
281 mutex_unlock(&phydev->lock);
282 } else {
283 return;
284 }
285 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000286 /* ops->get_strings is valid because checked earlier */
287 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000288}
289
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000290static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000291{
292 /* feature masks of legacy discrete ethtool ops */
293
294 switch (eth_cmd) {
295 case ETHTOOL_GTXCSUM:
296 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800297 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000298 case ETHTOOL_GRXCSUM:
299 case ETHTOOL_SRXCSUM:
300 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000301 case ETHTOOL_GSG:
302 case ETHTOOL_SSG:
303 return NETIF_F_SG;
304 case ETHTOOL_GTSO:
305 case ETHTOOL_STSO:
306 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000307 case ETHTOOL_GGSO:
308 case ETHTOOL_SGSO:
309 return NETIF_F_GSO;
310 case ETHTOOL_GGRO:
311 case ETHTOOL_SGRO:
312 return NETIF_F_GRO;
313 default:
314 BUG();
315 }
316}
317
Michał Mirosław0a417702011-02-15 16:59:17 +0000318static int ethtool_get_one_feature(struct net_device *dev,
319 char __user *useraddr, u32 ethcmd)
320{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000321 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000322 struct ethtool_value edata = {
323 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000324 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000325 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000326
Michał Mirosław0a417702011-02-15 16:59:17 +0000327 if (copy_to_user(useraddr, &edata, sizeof(edata)))
328 return -EFAULT;
329 return 0;
330}
331
Michał Mirosław0a417702011-02-15 16:59:17 +0000332static int ethtool_set_one_feature(struct net_device *dev,
333 void __user *useraddr, u32 ethcmd)
334{
335 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000336 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000337
338 if (copy_from_user(&edata, useraddr, sizeof(edata)))
339 return -EFAULT;
340
Michał Mirosław86794882011-02-15 16:59:17 +0000341 mask = ethtool_get_feature_mask(ethcmd);
342 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000343 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000344 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000345
346 if (edata.data)
347 dev->wanted_features |= mask;
348 else
349 dev->wanted_features &= ~mask;
350
351 __netdev_update_features(dev);
352
353 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000354}
355
Michał Mirosław02b3a552011-11-15 15:29:55 +0000356#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
357 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000358#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
359 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
360 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000361
362static u32 __ethtool_get_flags(struct net_device *dev)
363{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000364 u32 flags = 0;
365
Dragos Foianu8a731252013-07-13 14:43:00 +0100366 if (dev->features & NETIF_F_LRO)
367 flags |= ETH_FLAG_LRO;
368 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
369 flags |= ETH_FLAG_RXVLAN;
370 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
371 flags |= ETH_FLAG_TXVLAN;
372 if (dev->features & NETIF_F_NTUPLE)
373 flags |= ETH_FLAG_NTUPLE;
374 if (dev->features & NETIF_F_RXHASH)
375 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000376
377 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000378}
379
380static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000381{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000382 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000383
Michał Mirosław02b3a552011-11-15 15:29:55 +0000384 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000385 return -EINVAL;
386
Dragos Foianu8a731252013-07-13 14:43:00 +0100387 if (data & ETH_FLAG_LRO)
388 features |= NETIF_F_LRO;
389 if (data & ETH_FLAG_RXVLAN)
390 features |= NETIF_F_HW_VLAN_CTAG_RX;
391 if (data & ETH_FLAG_TXVLAN)
392 features |= NETIF_F_HW_VLAN_CTAG_TX;
393 if (data & ETH_FLAG_NTUPLE)
394 features |= NETIF_F_NTUPLE;
395 if (data & ETH_FLAG_RXHASH)
396 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000397
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000398 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000399 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000400 if (changed & ~dev->hw_features)
401 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
402
403 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000404 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000405
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700406 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000407
408 return 0;
409}
410
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700411/* Given two link masks, AND them together and save the result in dst. */
412void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
413 struct ethtool_link_ksettings *src)
414{
415 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
416 unsigned int idx = 0;
417
418 for (; idx < size; idx++) {
419 dst->link_modes.supported[idx] &=
420 src->link_modes.supported[idx];
421 dst->link_modes.advertising[idx] &=
422 src->link_modes.advertising[idx];
423 }
424}
425EXPORT_SYMBOL(ethtool_intersect_link_masks);
426
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200427void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
428 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800429{
430 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
431 dst[0] = legacy_u32;
432}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200433EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800434
435/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200436bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
437 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800438{
439 bool retval = true;
440
441 /* TODO: following test will soon always be true */
442 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
443 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
444
445 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
446 bitmap_fill(ext, 32);
447 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
448 if (bitmap_intersects(ext, src,
449 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
450 /* src mask goes beyond bit 31 */
451 retval = false;
452 }
453 }
454 *legacy_u32 = src[0];
455 return retval;
456}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200457EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800458
459/* return false if legacy contained non-0 deprecated fields
Niklas Söderlund95491e32017-10-20 01:32:08 +0200460 * maxtxpkt/maxrxpkt. rest of ksettings always updated
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800461 */
462static bool
463convert_legacy_settings_to_link_ksettings(
464 struct ethtool_link_ksettings *link_ksettings,
465 const struct ethtool_cmd *legacy_settings)
466{
467 bool retval = true;
468
469 memset(link_ksettings, 0, sizeof(*link_ksettings));
470
471 /* This is used to tell users that driver is still using these
472 * deprecated legacy fields, and they should not use
473 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
474 */
Niklas Söderlund95491e32017-10-20 01:32:08 +0200475 if (legacy_settings->maxtxpkt ||
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800476 legacy_settings->maxrxpkt)
477 retval = false;
478
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200479 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800480 link_ksettings->link_modes.supported,
481 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200482 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800483 link_ksettings->link_modes.advertising,
484 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200485 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800486 link_ksettings->link_modes.lp_advertising,
487 legacy_settings->lp_advertising);
488 link_ksettings->base.speed
489 = ethtool_cmd_speed(legacy_settings);
490 link_ksettings->base.duplex
491 = legacy_settings->duplex;
492 link_ksettings->base.port
493 = legacy_settings->port;
494 link_ksettings->base.phy_address
495 = legacy_settings->phy_address;
496 link_ksettings->base.autoneg
497 = legacy_settings->autoneg;
498 link_ksettings->base.mdio_support
499 = legacy_settings->mdio_support;
500 link_ksettings->base.eth_tp_mdix
501 = legacy_settings->eth_tp_mdix;
502 link_ksettings->base.eth_tp_mdix_ctrl
503 = legacy_settings->eth_tp_mdix_ctrl;
504 return retval;
505}
506
507/* return false if ksettings link modes had higher bits
508 * set. legacy_settings always updated (best effort)
509 */
510static bool
511convert_link_ksettings_to_legacy_settings(
512 struct ethtool_cmd *legacy_settings,
513 const struct ethtool_link_ksettings *link_ksettings)
514{
515 bool retval = true;
516
517 memset(legacy_settings, 0, sizeof(*legacy_settings));
518 /* this also clears the deprecated fields in legacy structure:
519 * __u8 transceiver;
520 * __u32 maxtxpkt;
521 * __u32 maxrxpkt;
522 */
523
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200524 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800525 &legacy_settings->supported,
526 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200527 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800528 &legacy_settings->advertising,
529 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200530 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800531 &legacy_settings->lp_advertising,
532 link_ksettings->link_modes.lp_advertising);
533 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
534 legacy_settings->duplex
535 = link_ksettings->base.duplex;
536 legacy_settings->port
537 = link_ksettings->base.port;
538 legacy_settings->phy_address
539 = link_ksettings->base.phy_address;
540 legacy_settings->autoneg
541 = link_ksettings->base.autoneg;
542 legacy_settings->mdio_support
543 = link_ksettings->base.mdio_support;
544 legacy_settings->eth_tp_mdix
545 = link_ksettings->base.eth_tp_mdix;
546 legacy_settings->eth_tp_mdix_ctrl
547 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700548 legacy_settings->transceiver
549 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800550 return retval;
551}
552
553/* number of 32-bit words to store the user's link mode bitmaps */
554#define __ETHTOOL_LINK_MODE_MASK_NU32 \
555 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
556
557/* layout of the struct passed from/to userland */
558struct ethtool_link_usettings {
559 struct ethtool_link_settings base;
560 struct {
561 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
562 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
563 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
564 } link_modes;
565};
566
567/* Internal kernel helper to query a device ethtool_link_settings.
568 *
569 * Backward compatibility note: for compatibility with legacy drivers
570 * that implement only the ethtool_cmd API, this has to work with both
571 * drivers implementing get_link_ksettings API and drivers
572 * implementing get_settings API. When drivers implement get_settings
573 * and report ethtool_cmd deprecated fields
574 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
575 * because the resulting struct ethtool_link_settings does not report them.
576 */
577int __ethtool_get_link_ksettings(struct net_device *dev,
578 struct ethtool_link_ksettings *link_ksettings)
579{
580 int err;
581 struct ethtool_cmd cmd;
582
583 ASSERT_RTNL();
584
585 if (dev->ethtool_ops->get_link_ksettings) {
586 memset(link_ksettings, 0, sizeof(*link_ksettings));
587 return dev->ethtool_ops->get_link_ksettings(dev,
588 link_ksettings);
589 }
590
591 /* driver doesn't support %ethtool_link_ksettings API. revert to
592 * legacy %ethtool_cmd API, unless it's not supported either.
593 * TODO: remove when ethtool_ops::get_settings disappears internally
594 */
David Decotigny3237fc62016-02-24 10:58:11 -0800595 if (!dev->ethtool_ops->get_settings)
596 return -EOPNOTSUPP;
597
598 memset(&cmd, 0, sizeof(cmd));
599 cmd.cmd = ETHTOOL_GSET;
600 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800601 if (err < 0)
602 return err;
603
604 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
605 */
606 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
607 return err;
608}
609EXPORT_SYMBOL(__ethtool_get_link_ksettings);
610
611/* convert ethtool_link_usettings in user space to a kernel internal
612 * ethtool_link_ksettings. return 0 on success, errno on error.
613 */
614static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
615 const void __user *from)
616{
617 struct ethtool_link_usettings link_usettings;
618
619 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
620 return -EFAULT;
621
622 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800623 bitmap_from_arr32(to->link_modes.supported,
624 link_usettings.link_modes.supported,
625 __ETHTOOL_LINK_MODE_MASK_NBITS);
626 bitmap_from_arr32(to->link_modes.advertising,
627 link_usettings.link_modes.advertising,
628 __ETHTOOL_LINK_MODE_MASK_NBITS);
629 bitmap_from_arr32(to->link_modes.lp_advertising,
630 link_usettings.link_modes.lp_advertising,
631 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800632
633 return 0;
634}
635
636/* convert a kernel internal ethtool_link_ksettings to
637 * ethtool_link_usettings in user space. return 0 on success, errno on
638 * error.
639 */
640static int
641store_link_ksettings_for_user(void __user *to,
642 const struct ethtool_link_ksettings *from)
643{
644 struct ethtool_link_usettings link_usettings;
645
646 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800647 bitmap_to_arr32(link_usettings.link_modes.supported,
648 from->link_modes.supported,
649 __ETHTOOL_LINK_MODE_MASK_NBITS);
650 bitmap_to_arr32(link_usettings.link_modes.advertising,
651 from->link_modes.advertising,
652 __ETHTOOL_LINK_MODE_MASK_NBITS);
653 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
654 from->link_modes.lp_advertising,
655 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800656
657 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
658 return -EFAULT;
659
660 return 0;
661}
662
663/* Query device for its ethtool_link_settings.
664 *
665 * Backward compatibility note: this function must fail when driver
666 * does not implement ethtool::get_link_ksettings, even if legacy
667 * ethtool_ops::get_settings is implemented. This tells new versions
668 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
669 * this driver, so that they can correctly access the ethtool_cmd
670 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
671 * implements ethtool_ops::get_settings anymore.
672 */
673static int ethtool_get_link_ksettings(struct net_device *dev,
674 void __user *useraddr)
675{
676 int err = 0;
677 struct ethtool_link_ksettings link_ksettings;
678
679 ASSERT_RTNL();
680
681 if (!dev->ethtool_ops->get_link_ksettings)
682 return -EOPNOTSUPP;
683
684 /* handle bitmap nbits handshake */
685 if (copy_from_user(&link_ksettings.base, useraddr,
686 sizeof(link_ksettings.base)))
687 return -EFAULT;
688
689 if (__ETHTOOL_LINK_MODE_MASK_NU32
690 != link_ksettings.base.link_mode_masks_nwords) {
691 /* wrong link mode nbits requested */
692 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000693 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800694 /* send back number of words required as negative val */
695 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
696 "need too many bits for link modes!");
697 link_ksettings.base.link_mode_masks_nwords
698 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
699
700 /* copy the base fields back to user, not the link
701 * mode bitmaps
702 */
703 if (copy_to_user(useraddr, &link_ksettings.base,
704 sizeof(link_ksettings.base)))
705 return -EFAULT;
706
707 return 0;
708 }
709
710 /* handshake successful: user/kernel agree on
711 * link_mode_masks_nwords
712 */
713
714 memset(&link_ksettings, 0, sizeof(link_ksettings));
715 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
716 if (err < 0)
717 return err;
718
719 /* make sure we tell the right values to user */
720 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
721 link_ksettings.base.link_mode_masks_nwords
722 = __ETHTOOL_LINK_MODE_MASK_NU32;
723
724 return store_link_ksettings_for_user(useraddr, &link_ksettings);
725}
726
727/* Update device ethtool_link_settings.
728 *
729 * Backward compatibility note: this function must fail when driver
730 * does not implement ethtool::set_link_ksettings, even if legacy
731 * ethtool_ops::set_settings is implemented. This tells new versions
732 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
733 * this driver, so that they can correctly update the ethtool_cmd
734 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
735 * implements ethtool_ops::get_settings anymore.
736 */
737static int ethtool_set_link_ksettings(struct net_device *dev,
738 void __user *useraddr)
739{
740 int err;
741 struct ethtool_link_ksettings link_ksettings;
742
743 ASSERT_RTNL();
744
745 if (!dev->ethtool_ops->set_link_ksettings)
746 return -EOPNOTSUPP;
747
748 /* make sure nbits field has expected value */
749 if (copy_from_user(&link_ksettings.base, useraddr,
750 sizeof(link_ksettings.base)))
751 return -EFAULT;
752
753 if (__ETHTOOL_LINK_MODE_MASK_NU32
754 != link_ksettings.base.link_mode_masks_nwords)
755 return -EINVAL;
756
757 /* copy the whole structure, now that we know it has expected
758 * format
759 */
760 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
761 if (err)
762 return err;
763
764 /* re-check nwords field, just in case */
765 if (__ETHTOOL_LINK_MODE_MASK_NU32
766 != link_ksettings.base.link_mode_masks_nwords)
767 return -EINVAL;
768
769 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
770}
771
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800772/* Query device for its ethtool_cmd settings.
773 *
774 * Backward compatibility note: for compatibility with legacy ethtool,
775 * this has to work with both drivers implementing get_link_ksettings
776 * API and drivers implementing get_settings API. When drivers
777 * implement get_link_ksettings and report higher link mode bits, a
778 * kernel warning is logged once (with name of 1st driver/device) to
779 * recommend user to upgrade ethtool, but the command is successful
780 * (only the lower link mode bits reported back to user).
781 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000782static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
783{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000784 struct ethtool_cmd cmd;
785
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800786 ASSERT_RTNL();
787
788 if (dev->ethtool_ops->get_link_ksettings) {
789 /* First, use link_ksettings API if it is supported */
790 int err;
791 struct ethtool_link_ksettings link_ksettings;
792
793 memset(&link_ksettings, 0, sizeof(link_ksettings));
794 err = dev->ethtool_ops->get_link_ksettings(dev,
795 &link_ksettings);
796 if (err < 0)
797 return err;
Stephen Hemminger71891e22017-12-29 10:02:52 -0800798 convert_link_ksettings_to_legacy_settings(&cmd,
799 &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800800
801 /* send a sensible cmd tag back to user */
802 cmd.cmd = ETHTOOL_GSET;
803 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800804 /* driver doesn't support %ethtool_link_ksettings
805 * API. revert to legacy %ethtool_cmd API, unless it's
806 * not supported either.
807 */
David Decotigny3237fc62016-02-24 10:58:11 -0800808 int err;
809
810 if (!dev->ethtool_ops->get_settings)
811 return -EOPNOTSUPP;
812
813 memset(&cmd, 0, sizeof(cmd));
814 cmd.cmd = ETHTOOL_GSET;
815 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800816 if (err < 0)
817 return err;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
821 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824}
825
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800826/* Update device link settings with given ethtool_cmd.
827 *
828 * Backward compatibility note: for compatibility with legacy ethtool,
829 * this has to work with both drivers implementing set_link_ksettings
830 * API and drivers implementing set_settings API. When drivers
831 * implement set_link_ksettings and user's request updates deprecated
832 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
833 * warning is logged once (with name of 1st driver/device) to
834 * recommend user to upgrade ethtool, and the request is rejected.
835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
837{
838 struct ethtool_cmd cmd;
839
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800840 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
843 return -EFAULT;
844
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800845 /* first, try new %ethtool_link_ksettings API. */
846 if (dev->ethtool_ops->set_link_ksettings) {
847 struct ethtool_link_ksettings link_ksettings;
848
849 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
850 &cmd))
851 return -EINVAL;
852
853 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
854 link_ksettings.base.link_mode_masks_nwords
855 = __ETHTOOL_LINK_MODE_MASK_NU32;
856 return dev->ethtool_ops->set_link_ksettings(dev,
857 &link_ksettings);
858 }
859
860 /* legacy %ethtool_cmd API */
861
862 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
863 * disappears internally
864 */
865
866 if (!dev->ethtool_ops->set_settings)
867 return -EOPNOTSUPP;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return dev->ethtool_ops->set_settings(dev, &cmd);
870}
871
chavey97f8aef2010-04-07 21:54:42 -0700872static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
873 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700876 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 memset(&info, 0, sizeof(info));
879 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000880 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700881 ops->get_drvinfo(dev, &info);
882 } else if (dev->dev.parent && dev->dev.parent->driver) {
883 strlcpy(info.bus_info, dev_name(dev->dev.parent),
884 sizeof(info.bus_info));
885 strlcpy(info.driver, dev->dev.parent->driver->name,
886 sizeof(info.driver));
887 } else {
888 return -EOPNOTSUPP;
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Jeff Garzik723b2f52010-03-03 22:51:50 +0000891 /*
892 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000893 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000894 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000895 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700896 int rc;
897
898 rc = ops->get_sset_count(dev, ETH_SS_TEST);
899 if (rc >= 0)
900 info.testinfo_len = rc;
901 rc = ops->get_sset_count(dev, ETH_SS_STATS);
902 if (rc >= 0)
903 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700904 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
905 if (rc >= 0)
906 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700907 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000908 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000910 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 info.eedump_len = ops->get_eeprom_len(dev);
912
913 if (copy_to_user(useraddr, &info, sizeof(info)))
914 return -EFAULT;
915 return 0;
916}
917
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800918static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700919 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000920{
921 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000922 u64 sset_mask;
923 int i, idx = 0, n_bits = 0, ret, rc;
924 u32 *info_buf = NULL;
925
Jeff Garzik723b2f52010-03-03 22:51:50 +0000926 if (copy_from_user(&info, useraddr, sizeof(info)))
927 return -EFAULT;
928
929 /* store copy of mask, because we zero struct later on */
930 sset_mask = info.sset_mask;
931 if (!sset_mask)
932 return 0;
933
934 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000935 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000936
937 memset(&info, 0, sizeof(info));
938 info.cmd = ETHTOOL_GSSET_INFO;
939
940 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
941 if (!info_buf)
942 return -ENOMEM;
943
944 /*
945 * fill return buffer based on input bitmask and successful
946 * get_sset_count return
947 */
948 for (i = 0; i < 64; i++) {
949 if (!(sset_mask & (1ULL << i)))
950 continue;
951
Michał Mirosław340ae162011-02-15 16:59:16 +0000952 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000953 if (rc >= 0) {
954 info.sset_mask |= (1ULL << i);
955 info_buf[idx++] = rc;
956 }
957 }
958
959 ret = -EFAULT;
960 if (copy_to_user(useraddr, &info, sizeof(info)))
961 goto out;
962
963 useraddr += offsetof(struct ethtool_sset_info, data);
964 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
965 goto out;
966
967 ret = 0;
968
969out:
970 kfree(info_buf);
971 return ret;
972}
973
chavey97f8aef2010-04-07 21:54:42 -0700974static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000975 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700976{
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
Santwona Behera59089d82009-02-20 00:58:13 -0800981 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700982 return -EOPNOTSUPP;
983
Ben Hutchingsbf988432010-06-28 08:45:58 +0000984 /* struct ethtool_rxnfc was originally defined for
985 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
986 * members. User-space might still be using that
987 * definition. */
988 if (cmd == ETHTOOL_SRXFH)
989 info_size = (offsetof(struct ethtool_rxnfc, data) +
990 sizeof(info.data));
991
992 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700993 return -EFAULT;
994
Ben Hutchings55664f32012-01-03 12:04:51 +0000995 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
996 if (rc)
997 return rc;
998
999 if (cmd == ETHTOOL_SRXCLSRLINS &&
1000 copy_to_user(useraddr, &info, info_size))
1001 return -EFAULT;
1002
1003 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -07001004}
1005
chavey97f8aef2010-04-07 21:54:42 -07001006static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +00001007 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -07001008{
1009 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001010 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -08001011 const struct ethtool_ops *ops = dev->ethtool_ops;
1012 int ret;
1013 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -07001014
Santwona Behera59089d82009-02-20 00:58:13 -08001015 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -07001016 return -EOPNOTSUPP;
1017
Ben Hutchingsbf988432010-06-28 08:45:58 +00001018 /* struct ethtool_rxnfc was originally defined for
1019 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1020 * members. User-space might still be using that
1021 * definition. */
1022 if (cmd == ETHTOOL_GRXFH)
1023 info_size = (offsetof(struct ethtool_rxnfc, data) +
1024 sizeof(info.data));
1025
1026 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001027 return -EFAULT;
1028
Edward Cree84a1d9c42018-03-08 15:45:03 +00001029 /* If FLOW_RSS was requested then user-space must be using the
1030 * new definition, as FLOW_RSS is newer.
1031 */
1032 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1033 info_size = sizeof(info);
1034 if (copy_from_user(&info, useraddr, info_size))
1035 return -EFAULT;
1036 }
1037
Santwona Behera59089d82009-02-20 00:58:13 -08001038 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1039 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001040 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +00001041 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001042 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001043 if (!rule_buf)
1044 return -ENOMEM;
1045 }
1046 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001047
Santwona Behera59089d82009-02-20 00:58:13 -08001048 ret = ops->get_rxnfc(dev, &info, rule_buf);
1049 if (ret < 0)
1050 goto err_out;
1051
1052 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001053 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001054 goto err_out;
1055
1056 if (rule_buf) {
1057 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1058 if (copy_to_user(useraddr, rule_buf,
1059 info.rule_cnt * sizeof(u32)))
1060 goto err_out;
1061 }
1062 ret = 0;
1063
1064err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001065 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001066
1067 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001068}
1069
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301070static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1071 struct ethtool_rxnfc *rx_rings,
1072 u32 size)
1073{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001074 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301075
1076 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001077 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301078
1079 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001080 for (i = 0; i < size; i++)
1081 if (indir[i] >= rx_rings->data)
1082 return -EINVAL;
1083
1084 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301085}
1086
Kim Jonesba905f52016-02-02 03:51:16 +00001087u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001088
1089void netdev_rss_key_fill(void *buffer, size_t len)
1090{
1091 BUG_ON(len > sizeof(netdev_rss_key));
1092 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1093 memcpy(buffer, netdev_rss_key, len);
1094}
1095EXPORT_SYMBOL(netdev_rss_key_fill);
1096
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001097static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1098{
1099 u32 dev_size, current_max = 0;
1100 u32 *indir;
1101 int ret;
1102
1103 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1104 !dev->ethtool_ops->get_rxfh)
1105 return -EOPNOTSUPP;
1106 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1107 if (dev_size == 0)
1108 return -EOPNOTSUPP;
1109
1110 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1111 if (!indir)
1112 return -ENOMEM;
1113
1114 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1115 if (ret)
1116 goto out;
1117
1118 while (dev_size--)
1119 current_max = max(current_max, indir[dev_size]);
1120
1121 *max = current_max;
1122
1123out:
1124 kfree(indir);
1125 return ret;
1126}
1127
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001128static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1129 void __user *useraddr)
1130{
Ben Hutchings7850f632011-12-15 13:55:01 +00001131 u32 user_size, dev_size;
1132 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001133 int ret;
1134
Ben Hutchings7850f632011-12-15 13:55:01 +00001135 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001136 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001137 return -EOPNOTSUPP;
1138 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1139 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001140 return -EOPNOTSUPP;
1141
Ben Hutchings7850f632011-12-15 13:55:01 +00001142 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001143 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001144 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001145 return -EFAULT;
1146
Ben Hutchings7850f632011-12-15 13:55:01 +00001147 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1148 &dev_size, sizeof(dev_size)))
1149 return -EFAULT;
1150
1151 /* If the user buffer size is 0, this is just a query for the
1152 * device table size. Otherwise, if it's smaller than the
1153 * device table size it's an error.
1154 */
1155 if (user_size < dev_size)
1156 return user_size == 0 ? 0 : -EINVAL;
1157
1158 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001159 if (!indir)
1160 return -ENOMEM;
1161
Eyal Perry892311f2014-12-02 18:12:10 +02001162 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001163 if (ret)
1164 goto out;
1165
Ben Hutchings7850f632011-12-15 13:55:01 +00001166 if (copy_to_user(useraddr +
1167 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1168 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001169 ret = -EFAULT;
1170
1171out:
1172 kfree(indir);
1173 return ret;
1174}
1175
1176static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1177 void __user *useraddr)
1178{
Ben Hutchings7850f632011-12-15 13:55:01 +00001179 struct ethtool_rxnfc rx_rings;
1180 u32 user_size, dev_size, i;
1181 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001182 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001183 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301184 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001185
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001186 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001187 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001188 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001189
1190 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001191 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001192 return -EOPNOTSUPP;
1193
Ben Hutchings7850f632011-12-15 13:55:01 +00001194 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001195 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001196 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001197 return -EFAULT;
1198
Ben Hutchings278bc422011-12-15 13:56:49 +00001199 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001200 return -EINVAL;
1201
1202 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001203 if (!indir)
1204 return -ENOMEM;
1205
Ben Hutchings7850f632011-12-15 13:55:01 +00001206 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001207 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001208 if (ret)
1209 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001210
1211 if (user_size == 0) {
1212 for (i = 0; i < dev_size; i++)
1213 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1214 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301215 ret = ethtool_copy_validate_indir(indir,
1216 useraddr + ringidx_offset,
1217 &rx_rings,
1218 dev_size);
1219 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001220 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001221 }
1222
Eyal Perry892311f2014-12-02 18:12:10 +02001223 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001224 if (ret)
1225 goto out;
1226
1227 /* indicate whether rxfh was set to default */
1228 if (user_size == 0)
1229 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1230 else
1231 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001232
1233out:
1234 kfree(indir);
1235 return ret;
1236}
1237
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301238static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1239 void __user *useraddr)
1240{
1241 int ret;
1242 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001243 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301244 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001245 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301246 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001247 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301248 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001249 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301250 u8 *hkey = NULL;
1251 u8 *rss_config;
1252
Eyal Perry892311f2014-12-02 18:12:10 +02001253 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301254 return -EOPNOTSUPP;
1255
1256 if (ops->get_rxfh_indir_size)
1257 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301258 if (ops->get_rxfh_key_size)
1259 dev_key_size = ops->get_rxfh_key_size(dev);
1260
Ben Hutchingsf062a382014-05-15 16:28:07 +01001261 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301262 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001263 user_indir_size = rxfh.indir_size;
1264 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301265
Ben Hutchingsf062a382014-05-15 16:28:07 +01001266 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001267 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001268 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001269 /* Most drivers don't handle rss_context, check it's 0 as well */
1270 if (rxfh.rss_context && !ops->get_rxfh_context)
1271 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001272
1273 rxfh.indir_size = dev_indir_size;
1274 rxfh.key_size = dev_key_size;
1275 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301276 return -EFAULT;
1277
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301278 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1279 (user_key_size && (user_key_size != dev_key_size)))
1280 return -EINVAL;
1281
1282 indir_bytes = user_indir_size * sizeof(indir[0]);
1283 total_size = indir_bytes + user_key_size;
1284 rss_config = kzalloc(total_size, GFP_USER);
1285 if (!rss_config)
1286 return -ENOMEM;
1287
1288 if (user_indir_size)
1289 indir = (u32 *)rss_config;
1290
1291 if (user_key_size)
1292 hkey = rss_config + indir_bytes;
1293
Edward Cree84a1d9c42018-03-08 15:45:03 +00001294 if (rxfh.rss_context)
1295 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1296 &dev_hfunc,
1297 rxfh.rss_context);
1298 else
1299 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001300 if (ret)
1301 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301302
Eyal Perry892311f2014-12-02 18:12:10 +02001303 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1304 &dev_hfunc, sizeof(rxfh.hfunc))) {
1305 ret = -EFAULT;
1306 } else if (copy_to_user(useraddr +
1307 offsetof(struct ethtool_rxfh, rss_config[0]),
1308 rss_config, total_size)) {
1309 ret = -EFAULT;
1310 }
1311out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301312 kfree(rss_config);
1313
1314 return ret;
1315}
1316
1317static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1318 void __user *useraddr)
1319{
1320 int ret;
1321 const struct ethtool_ops *ops = dev->ethtool_ops;
1322 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001323 struct ethtool_rxfh rxfh;
1324 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301325 u32 *indir = NULL, indir_bytes = 0;
1326 u8 *hkey = NULL;
1327 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301328 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001329 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301330
Eyal Perry892311f2014-12-02 18:12:10 +02001331 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301332 return -EOPNOTSUPP;
1333
1334 if (ops->get_rxfh_indir_size)
1335 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301336 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001337 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301338
Ben Hutchingsf062a382014-05-15 16:28:07 +01001339 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301340 return -EFAULT;
1341
Ben Hutchingsf062a382014-05-15 16:28:07 +01001342 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001343 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001344 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001345 /* Most drivers don't handle rss_context, check it's 0 as well */
1346 if (rxfh.rss_context && !ops->set_rxfh_context)
1347 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001348
Eyal Perry892311f2014-12-02 18:12:10 +02001349 /* If either indir, hash key or function is valid, proceed further.
1350 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301351 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001352 if ((rxfh.indir_size &&
1353 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1354 rxfh.indir_size != dev_indir_size) ||
1355 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1356 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001357 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301358 return -EINVAL;
1359
Ben Hutchingsf062a382014-05-15 16:28:07 +01001360 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301361 indir_bytes = dev_indir_size * sizeof(indir[0]);
1362
Ben Hutchingsf062a382014-05-15 16:28:07 +01001363 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301364 if (!rss_config)
1365 return -ENOMEM;
1366
1367 rx_rings.cmd = ETHTOOL_GRXRINGS;
1368 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1369 if (ret)
1370 goto out;
1371
Edward Cree84a1d9c42018-03-08 15:45:03 +00001372 /* rxfh.indir_size == 0 means reset the indir table to default (master
1373 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001374 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301375 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001376 if (rxfh.indir_size &&
1377 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301378 indir = (u32 *)rss_config;
1379 ret = ethtool_copy_validate_indir(indir,
1380 useraddr + rss_cfg_offset,
1381 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001382 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301383 if (ret)
1384 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001385 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001386 if (rxfh.rss_context == 0) {
1387 indir = (u32 *)rss_config;
1388 for (i = 0; i < dev_indir_size; i++)
1389 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1390 } else {
1391 delete = true;
1392 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301393 }
1394
Ben Hutchingsf062a382014-05-15 16:28:07 +01001395 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301396 hkey = rss_config + indir_bytes;
1397 if (copy_from_user(hkey,
1398 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001399 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301400 ret = -EFAULT;
1401 goto out;
1402 }
1403 }
1404
Edward Cree84a1d9c42018-03-08 15:45:03 +00001405 if (rxfh.rss_context)
1406 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1407 &rxfh.rss_context, delete);
1408 else
1409 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001410 if (ret)
1411 goto out;
1412
Edward Cree84a1d9c42018-03-08 15:45:03 +00001413 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1414 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1415 ret = -EFAULT;
1416
1417 if (!rxfh.rss_context) {
1418 /* indicate whether rxfh was set to default */
1419 if (rxfh.indir_size == 0)
1420 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1421 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1422 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1423 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301424
1425out:
1426 kfree(rss_config);
1427 return ret;
1428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1431{
1432 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001433 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 void *regbuf;
1435 int reglen, ret;
1436
1437 if (!ops->get_regs || !ops->get_regs_len)
1438 return -EOPNOTSUPP;
1439
1440 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1441 return -EFAULT;
1442
1443 reglen = ops->get_regs_len(dev);
1444 if (regs.len > reglen)
1445 regs.len = reglen;
1446
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001447 regbuf = NULL;
1448 if (reglen) {
1449 regbuf = vzalloc(reglen);
1450 if (!regbuf)
1451 return -ENOMEM;
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 ops->get_regs(dev, &regs, regbuf);
1455
1456 ret = -EFAULT;
1457 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1458 goto out;
1459 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001460 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto out;
1462 ret = 0;
1463
1464 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001465 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return ret;
1467}
1468
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001469static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1470{
1471 struct ethtool_value reset;
1472 int ret;
1473
1474 if (!dev->ethtool_ops->reset)
1475 return -EOPNOTSUPP;
1476
1477 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1478 return -EFAULT;
1479
1480 ret = dev->ethtool_ops->reset(dev, &reset.data);
1481 if (ret)
1482 return ret;
1483
1484 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1485 return -EFAULT;
1486 return 0;
1487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1490{
Roland Dreier8e557422010-02-11 12:14:23 -08001491 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 if (!dev->ethtool_ops->get_wol)
1494 return -EOPNOTSUPP;
1495
1496 dev->ethtool_ops->get_wol(dev, &wol);
1497
1498 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1499 return -EFAULT;
1500 return 0;
1501}
1502
1503static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1504{
1505 struct ethtool_wolinfo wol;
1506
1507 if (!dev->ethtool_ops->set_wol)
1508 return -EOPNOTSUPP;
1509
1510 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1511 return -EFAULT;
1512
1513 return dev->ethtool_ops->set_wol(dev, &wol);
1514}
1515
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001516static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1517{
1518 struct ethtool_eee edata;
1519 int rc;
1520
1521 if (!dev->ethtool_ops->get_eee)
1522 return -EOPNOTSUPP;
1523
1524 memset(&edata, 0, sizeof(struct ethtool_eee));
1525 edata.cmd = ETHTOOL_GEEE;
1526 rc = dev->ethtool_ops->get_eee(dev, &edata);
1527
1528 if (rc)
1529 return rc;
1530
1531 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1532 return -EFAULT;
1533
1534 return 0;
1535}
1536
1537static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1538{
1539 struct ethtool_eee edata;
1540
1541 if (!dev->ethtool_ops->set_eee)
1542 return -EOPNOTSUPP;
1543
1544 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1545 return -EFAULT;
1546
1547 return dev->ethtool_ops->set_eee(dev, &edata);
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550static int ethtool_nway_reset(struct net_device *dev)
1551{
1552 if (!dev->ethtool_ops->nway_reset)
1553 return -EOPNOTSUPP;
1554
1555 return dev->ethtool_ops->nway_reset(dev);
1556}
1557
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001558static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1559{
1560 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1561
1562 if (!dev->ethtool_ops->get_link)
1563 return -EOPNOTSUPP;
1564
1565 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1566
1567 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1568 return -EFAULT;
1569 return 0;
1570}
1571
Ben Hutchings081d0942012-04-12 00:42:12 +00001572static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1573 int (*getter)(struct net_device *,
1574 struct ethtool_eeprom *, u8 *),
1575 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001578 void __user *userbuf = useraddr + sizeof(eeprom);
1579 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001581 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1584 return -EFAULT;
1585
1586 /* Check for wrap and zero */
1587 if (eeprom.offset + eeprom.len <= eeprom.offset)
1588 return -EINVAL;
1589
1590 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001591 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return -EINVAL;
1593
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001594 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!data)
1596 return -ENOMEM;
1597
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001598 bytes_remaining = eeprom.len;
1599 while (bytes_remaining > 0) {
1600 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Ben Hutchings081d0942012-04-12 00:42:12 +00001602 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001603 if (ret)
1604 break;
1605 if (copy_to_user(userbuf, data, eeprom.len)) {
1606 ret = -EFAULT;
1607 break;
1608 }
1609 userbuf += eeprom.len;
1610 eeprom.offset += eeprom.len;
1611 bytes_remaining -= eeprom.len;
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001614 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1615 eeprom.offset -= eeprom.len;
1616 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1617 ret = -EFAULT;
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 kfree(data);
1620 return ret;
1621}
1622
Ben Hutchings081d0942012-04-12 00:42:12 +00001623static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1624{
1625 const struct ethtool_ops *ops = dev->ethtool_ops;
1626
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001627 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1628 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001629 return -EOPNOTSUPP;
1630
1631 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1632 ops->get_eeprom_len(dev));
1633}
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1636{
1637 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001638 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001639 void __user *userbuf = useraddr + sizeof(eeprom);
1640 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001642 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001644 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1645 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return -EOPNOTSUPP;
1647
1648 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1649 return -EFAULT;
1650
1651 /* Check for wrap and zero */
1652 if (eeprom.offset + eeprom.len <= eeprom.offset)
1653 return -EINVAL;
1654
1655 /* Check for exceeding total eeprom len */
1656 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1657 return -EINVAL;
1658
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001659 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (!data)
1661 return -ENOMEM;
1662
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001663 bytes_remaining = eeprom.len;
1664 while (bytes_remaining > 0) {
1665 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001667 if (copy_from_user(data, userbuf, eeprom.len)) {
1668 ret = -EFAULT;
1669 break;
1670 }
1671 ret = ops->set_eeprom(dev, &eeprom, data);
1672 if (ret)
1673 break;
1674 userbuf += eeprom.len;
1675 eeprom.offset += eeprom.len;
1676 bytes_remaining -= eeprom.len;
1677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 kfree(data);
1680 return ret;
1681}
1682
chavey97f8aef2010-04-07 21:54:42 -07001683static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1684 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Roland Dreier8e557422010-02-11 12:14:23 -08001686 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 if (!dev->ethtool_ops->get_coalesce)
1689 return -EOPNOTSUPP;
1690
1691 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1692
1693 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1694 return -EFAULT;
1695 return 0;
1696}
1697
chavey97f8aef2010-04-07 21:54:42 -07001698static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1699 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 struct ethtool_coalesce coalesce;
1702
David S. Millerfa04ae52005-06-06 15:07:19 -07001703 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -EOPNOTSUPP;
1705
1706 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1707 return -EFAULT;
1708
1709 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1710}
1711
1712static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1713{
Roland Dreier8e557422010-02-11 12:14:23 -08001714 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 if (!dev->ethtool_ops->get_ringparam)
1717 return -EOPNOTSUPP;
1718
1719 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1720
1721 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1722 return -EFAULT;
1723 return 0;
1724}
1725
1726static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1727{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001728 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001730 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -EOPNOTSUPP;
1732
1733 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1734 return -EFAULT;
1735
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001736 dev->ethtool_ops->get_ringparam(dev, &max);
1737
1738 /* ensure new ring parameters are within the maximums */
1739 if (ringparam.rx_pending > max.rx_max_pending ||
1740 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1741 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1742 ringparam.tx_pending > max.tx_max_pending)
1743 return -EINVAL;
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1746}
1747
amit salecha8b5933c2011-04-07 01:58:42 +00001748static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1749 void __user *useraddr)
1750{
1751 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1752
1753 if (!dev->ethtool_ops->get_channels)
1754 return -EOPNOTSUPP;
1755
1756 dev->ethtool_ops->get_channels(dev, &channels);
1757
1758 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1759 return -EFAULT;
1760 return 0;
1761}
1762
1763static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1764 void __user *useraddr)
1765{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001766 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001767 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001768
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001769 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001770 return -EOPNOTSUPP;
1771
1772 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1773 return -EFAULT;
1774
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001775 dev->ethtool_ops->get_channels(dev, &max);
1776
1777 /* ensure new counts are within the maximums */
1778 if ((channels.rx_count > max.max_rx) ||
1779 (channels.tx_count > max.max_tx) ||
1780 (channels.combined_count > max.max_combined) ||
1781 (channels.other_count > max.max_other))
1782 return -EINVAL;
1783
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001784 /* ensure the new Rx count fits within the configured Rx flow
1785 * indirection table settings */
1786 if (netif_is_rxfh_configured(dev) &&
1787 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1788 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1789 return -EINVAL;
1790
amit salecha8b5933c2011-04-07 01:58:42 +00001791 return dev->ethtool_ops->set_channels(dev, &channels);
1792}
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1795{
1796 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1797
1798 if (!dev->ethtool_ops->get_pauseparam)
1799 return -EOPNOTSUPP;
1800
1801 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1802
1803 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1804 return -EFAULT;
1805 return 0;
1806}
1807
1808static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1809{
1810 struct ethtool_pauseparam pauseparam;
1811
Jeff Garzike1b90c42006-07-17 12:54:40 -04001812 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return -EOPNOTSUPP;
1814
1815 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1816 return -EFAULT;
1817
1818 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1819}
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1822{
1823 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001824 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001826 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001828 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001829 return -EOPNOTSUPP;
1830
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001831 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001832 if (test_len < 0)
1833 return test_len;
1834 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 if (copy_from_user(&test, useraddr, sizeof(test)))
1837 return -EFAULT;
1838
Jeff Garzikff03d492007-08-15 16:01:08 -07001839 test.len = test_len;
1840 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (!data)
1842 return -ENOMEM;
1843
1844 ops->self_test(dev, &test, data);
1845
1846 ret = -EFAULT;
1847 if (copy_to_user(useraddr, &test, sizeof(test)))
1848 goto out;
1849 useraddr += sizeof(test);
1850 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1851 goto out;
1852 ret = 0;
1853
1854 out:
1855 kfree(data);
1856 return ret;
1857}
1858
1859static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1860{
1861 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 u8 *data;
1863 int ret;
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1866 return -EFAULT;
1867
Michał Mirosław340ae162011-02-15 16:59:16 +00001868 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001869 if (ret < 0)
1870 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001871 if (ret > S32_MAX / ETH_GSTRING_LEN)
1872 return -ENOMEM;
1873 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001874
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001875 gstrings.len = ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001876 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1877 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return -ENOMEM;
1879
Michał Mirosław340ae162011-02-15 16:59:16 +00001880 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 ret = -EFAULT;
1883 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1884 goto out;
1885 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001886 if (gstrings.len &&
1887 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto out;
1889 ret = 0;
1890
Michał Mirosław340ae162011-02-15 16:59:16 +00001891out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001892 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return ret;
1894}
1895
1896static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1897{
1898 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001899 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001900 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001901 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001903 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return -EOPNOTSUPP;
1905
Ben Hutchings68f512f2011-04-02 00:35:15 +01001906 if (busy)
1907 return -EBUSY;
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (copy_from_user(&id, useraddr, sizeof(id)))
1910 return -EFAULT;
1911
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001912 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001913 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001914 return rc;
1915
1916 /* Drop the RTNL lock while waiting, but prevent reentry or
1917 * removal of the device.
1918 */
1919 busy = true;
1920 dev_hold(dev);
1921 rtnl_unlock();
1922
1923 if (rc == 0) {
1924 /* Driver will handle this itself */
1925 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001926 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001927 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001928 /* Driver expects to be called at twice the frequency in rc */
1929 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001930
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001931 /* Count down seconds */
1932 do {
1933 /* Count down iterations per second */
1934 i = n;
1935 do {
1936 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001937 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001938 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1939 rtnl_unlock();
1940 if (rc)
1941 break;
1942 schedule_timeout_interruptible(interval);
1943 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001944 } while (!signal_pending(current) &&
1945 (id.data == 0 || --id.data != 0));
1946 }
1947
1948 rtnl_lock();
1949 dev_put(dev);
1950 busy = false;
1951
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001952 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001953 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1957{
1958 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001959 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001961 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001963 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001964 return -EOPNOTSUPP;
1965
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001966 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001967 if (n_stats < 0)
1968 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001969 if (n_stats > S32_MAX / sizeof(u64))
1970 return -ENOMEM;
1971 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1973 return -EFAULT;
1974
Jeff Garzikff03d492007-08-15 16:01:08 -07001975 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001976 data = vzalloc(n_stats * sizeof(u64));
1977 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return -ENOMEM;
1979
1980 ops->get_ethtool_stats(dev, &stats, data);
1981
1982 ret = -EFAULT;
1983 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1984 goto out;
1985 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001986 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 goto out;
1988 ret = 0;
1989
1990 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001991 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return ret;
1993}
1994
Andrew Lunnf3a40942015-12-30 16:28:25 +01001995static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1996{
1997 struct ethtool_stats stats;
1998 struct phy_device *phydev = dev->phydev;
1999 u64 *data;
2000 int ret, n_stats;
2001
2002 if (!phydev)
2003 return -EOPNOTSUPP;
2004
2005 n_stats = phy_get_sset_count(phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002006 if (n_stats < 0)
2007 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002008 if (n_stats > S32_MAX / sizeof(u64))
2009 return -ENOMEM;
2010 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002011
2012 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2013 return -EFAULT;
2014
2015 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002016 data = vzalloc(n_stats * sizeof(u64));
2017 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01002018 return -ENOMEM;
2019
2020 mutex_lock(&phydev->lock);
2021 phydev->drv->get_stats(phydev, &stats, data);
2022 mutex_unlock(&phydev->lock);
2023
2024 ret = -EFAULT;
2025 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2026 goto out;
2027 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002028 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002029 goto out;
2030 ret = 0;
2031
2032 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002033 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002034 return ret;
2035}
2036
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002037static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002038{
2039 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002040
Matthew Wilcox313674a2007-07-31 14:00:29 -07002041 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002042 return -EFAULT;
2043
Matthew Wilcox313674a2007-07-31 14:00:29 -07002044 if (epaddr.size < dev->addr_len)
2045 return -ETOOSMALL;
2046 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002047
Jon Wetzela6f9a702005-08-20 17:15:54 -07002048 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002049 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002050 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002051 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2052 return -EFAULT;
2053 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002054}
2055
Jeff Garzik13c99b22007-08-15 16:01:56 -07002056static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2057 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002058{
Roland Dreier8e557422010-02-11 12:14:23 -08002059 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002060
Jeff Garzik13c99b22007-08-15 16:01:56 -07002061 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002062 return -EOPNOTSUPP;
2063
Jeff Garzik13c99b22007-08-15 16:01:56 -07002064 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002065
2066 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2067 return -EFAULT;
2068 return 0;
2069}
2070
Jeff Garzik13c99b22007-08-15 16:01:56 -07002071static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2072 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002073{
2074 struct ethtool_value edata;
2075
Jeff Garzik13c99b22007-08-15 16:01:56 -07002076 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002077 return -EOPNOTSUPP;
2078
2079 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2080 return -EFAULT;
2081
Jeff Garzik13c99b22007-08-15 16:01:56 -07002082 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002083 return 0;
2084}
2085
Jeff Garzik13c99b22007-08-15 16:01:56 -07002086static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2087 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002088{
2089 struct ethtool_value edata;
2090
Jeff Garzik13c99b22007-08-15 16:01:56 -07002091 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002092 return -EOPNOTSUPP;
2093
2094 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2095 return -EFAULT;
2096
Jeff Garzik13c99b22007-08-15 16:01:56 -07002097 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002098}
2099
chavey97f8aef2010-04-07 21:54:42 -07002100static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2101 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002102{
2103 struct ethtool_flash efl;
2104
2105 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2106 return -EFAULT;
2107
2108 if (!dev->ethtool_ops->flash_device)
2109 return -EOPNOTSUPP;
2110
Ben Hutchings786f5282012-02-01 09:32:25 +00002111 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2112
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002113 return dev->ethtool_ops->flash_device(dev, &efl);
2114}
2115
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002116static int ethtool_set_dump(struct net_device *dev,
2117 void __user *useraddr)
2118{
2119 struct ethtool_dump dump;
2120
2121 if (!dev->ethtool_ops->set_dump)
2122 return -EOPNOTSUPP;
2123
2124 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2125 return -EFAULT;
2126
2127 return dev->ethtool_ops->set_dump(dev, &dump);
2128}
2129
2130static int ethtool_get_dump_flag(struct net_device *dev,
2131 void __user *useraddr)
2132{
2133 int ret;
2134 struct ethtool_dump dump;
2135 const struct ethtool_ops *ops = dev->ethtool_ops;
2136
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002137 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002138 return -EOPNOTSUPP;
2139
2140 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2141 return -EFAULT;
2142
2143 ret = ops->get_dump_flag(dev, &dump);
2144 if (ret)
2145 return ret;
2146
2147 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2148 return -EFAULT;
2149 return 0;
2150}
2151
2152static int ethtool_get_dump_data(struct net_device *dev,
2153 void __user *useraddr)
2154{
2155 int ret;
2156 __u32 len;
2157 struct ethtool_dump dump, tmp;
2158 const struct ethtool_ops *ops = dev->ethtool_ops;
2159 void *data = NULL;
2160
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002161 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002162 return -EOPNOTSUPP;
2163
2164 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2165 return -EFAULT;
2166
2167 memset(&tmp, 0, sizeof(tmp));
2168 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2169 ret = ops->get_dump_flag(dev, &tmp);
2170 if (ret)
2171 return ret;
2172
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002173 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002174 if (!len)
2175 return -EFAULT;
2176
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002177 /* Don't ever let the driver think there's more space available
2178 * than it requested with .get_dump_flag().
2179 */
2180 dump.len = len;
2181
2182 /* Always allocate enough space to hold the whole thing so that the
2183 * driver does not need to check the length and bother with partial
2184 * dumping.
2185 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002186 data = vzalloc(tmp.len);
2187 if (!data)
2188 return -ENOMEM;
2189 ret = ops->get_dump_data(dev, &dump, data);
2190 if (ret)
2191 goto out;
2192
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002193 /* There are two sane possibilities:
2194 * 1. The driver's .get_dump_data() does not touch dump.len.
2195 * 2. Or it may set dump.len to how much it really writes, which
2196 * should be tmp.len (or len if it can do a partial dump).
2197 * In any case respond to userspace with the actual length of data
2198 * it's receiving.
2199 */
2200 WARN_ON(dump.len != len && dump.len != tmp.len);
2201 dump.len = len;
2202
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002203 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2204 ret = -EFAULT;
2205 goto out;
2206 }
2207 useraddr += offsetof(struct ethtool_dump, data);
2208 if (copy_to_user(useraddr, data, len))
2209 ret = -EFAULT;
2210out:
2211 vfree(data);
2212 return ret;
2213}
2214
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002215static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2216{
2217 int err = 0;
2218 struct ethtool_ts_info info;
2219 const struct ethtool_ops *ops = dev->ethtool_ops;
2220 struct phy_device *phydev = dev->phydev;
2221
2222 memset(&info, 0, sizeof(info));
2223 info.cmd = ETHTOOL_GET_TS_INFO;
2224
2225 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002226 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002227 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002228 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002229 } else {
2230 info.so_timestamping =
2231 SOF_TIMESTAMPING_RX_SOFTWARE |
2232 SOF_TIMESTAMPING_SOFTWARE;
2233 info.phc_index = -1;
2234 }
2235
2236 if (err)
2237 return err;
2238
2239 if (copy_to_user(useraddr, &info, sizeof(info)))
2240 err = -EFAULT;
2241
2242 return err;
2243}
2244
Ed Swierk2f438362015-01-02 17:27:56 -08002245static int __ethtool_get_module_info(struct net_device *dev,
2246 struct ethtool_modinfo *modinfo)
2247{
2248 const struct ethtool_ops *ops = dev->ethtool_ops;
2249 struct phy_device *phydev = dev->phydev;
2250
Russell Kinge679c9c2018-03-28 15:44:16 -07002251 if (dev->sfp_bus)
2252 return sfp_get_module_info(dev->sfp_bus, modinfo);
2253
Ed Swierk2f438362015-01-02 17:27:56 -08002254 if (phydev && phydev->drv && phydev->drv->module_info)
2255 return phydev->drv->module_info(phydev, modinfo);
2256
2257 if (ops->get_module_info)
2258 return ops->get_module_info(dev, modinfo);
2259
2260 return -EOPNOTSUPP;
2261}
2262
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002263static int ethtool_get_module_info(struct net_device *dev,
2264 void __user *useraddr)
2265{
2266 int ret;
2267 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002268
2269 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2270 return -EFAULT;
2271
Ed Swierk2f438362015-01-02 17:27:56 -08002272 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002273 if (ret)
2274 return ret;
2275
2276 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2277 return -EFAULT;
2278
2279 return 0;
2280}
2281
Ed Swierk2f438362015-01-02 17:27:56 -08002282static int __ethtool_get_module_eeprom(struct net_device *dev,
2283 struct ethtool_eeprom *ee, u8 *data)
2284{
2285 const struct ethtool_ops *ops = dev->ethtool_ops;
2286 struct phy_device *phydev = dev->phydev;
2287
Russell Kinge679c9c2018-03-28 15:44:16 -07002288 if (dev->sfp_bus)
2289 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2290
Ed Swierk2f438362015-01-02 17:27:56 -08002291 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2292 return phydev->drv->module_eeprom(phydev, ee, data);
2293
2294 if (ops->get_module_eeprom)
2295 return ops->get_module_eeprom(dev, ee, data);
2296
2297 return -EOPNOTSUPP;
2298}
2299
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002300static int ethtool_get_module_eeprom(struct net_device *dev,
2301 void __user *useraddr)
2302{
2303 int ret;
2304 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002305
Ed Swierk2f438362015-01-02 17:27:56 -08002306 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002307 if (ret)
2308 return ret;
2309
Ed Swierk2f438362015-01-02 17:27:56 -08002310 return ethtool_get_any_eeprom(dev, useraddr,
2311 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002312 modinfo.eeprom_len);
2313}
2314
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302315static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2316{
2317 switch (tuna->id) {
2318 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002319 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302320 if (tuna->len != sizeof(u32) ||
2321 tuna->type_id != ETHTOOL_TUNABLE_U32)
2322 return -EINVAL;
2323 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002324 case ETHTOOL_PFC_PREVENTION_TOUT:
2325 if (tuna->len != sizeof(u16) ||
2326 tuna->type_id != ETHTOOL_TUNABLE_U16)
2327 return -EINVAL;
2328 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302329 default:
2330 return -EINVAL;
2331 }
2332
2333 return 0;
2334}
2335
2336static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2337{
2338 int ret;
2339 struct ethtool_tunable tuna;
2340 const struct ethtool_ops *ops = dev->ethtool_ops;
2341 void *data;
2342
2343 if (!ops->get_tunable)
2344 return -EOPNOTSUPP;
2345 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2346 return -EFAULT;
2347 ret = ethtool_tunable_valid(&tuna);
2348 if (ret)
2349 return ret;
2350 data = kmalloc(tuna.len, GFP_USER);
2351 if (!data)
2352 return -ENOMEM;
2353 ret = ops->get_tunable(dev, &tuna, data);
2354 if (ret)
2355 goto out;
2356 useraddr += sizeof(tuna);
2357 ret = -EFAULT;
2358 if (copy_to_user(useraddr, data, tuna.len))
2359 goto out;
2360 ret = 0;
2361
2362out:
2363 kfree(data);
2364 return ret;
2365}
2366
2367static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2368{
2369 int ret;
2370 struct ethtool_tunable tuna;
2371 const struct ethtool_ops *ops = dev->ethtool_ops;
2372 void *data;
2373
2374 if (!ops->set_tunable)
2375 return -EOPNOTSUPP;
2376 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2377 return -EFAULT;
2378 ret = ethtool_tunable_valid(&tuna);
2379 if (ret)
2380 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302381 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002382 data = memdup_user(useraddr, tuna.len);
2383 if (IS_ERR(data))
2384 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302385 ret = ops->set_tunable(dev, &tuna, data);
2386
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302387 kfree(data);
2388 return ret;
2389}
2390
Kan Liang421797b2016-02-19 09:24:02 -05002391static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2392 void __user *useraddr,
2393 struct ethtool_per_queue_op *per_queue_opt)
2394{
2395 u32 bit;
2396 int ret;
2397 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2398
2399 if (!dev->ethtool_ops->get_per_queue_coalesce)
2400 return -EOPNOTSUPP;
2401
2402 useraddr += sizeof(*per_queue_opt);
2403
Yury Norov3aa56882018-02-06 15:38:06 -08002404 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2405 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002406
2407 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2408 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2409
2410 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2411 if (ret != 0)
2412 return ret;
2413 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2414 return -EFAULT;
2415 useraddr += sizeof(coalesce);
2416 }
2417
2418 return 0;
2419}
2420
Kan Liangf38d1382016-02-19 09:24:03 -05002421static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2422 void __user *useraddr,
2423 struct ethtool_per_queue_op *per_queue_opt)
2424{
2425 u32 bit;
2426 int i, ret = 0;
2427 int n_queue;
2428 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2429 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2430
2431 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2432 (!dev->ethtool_ops->get_per_queue_coalesce))
2433 return -EOPNOTSUPP;
2434
2435 useraddr += sizeof(*per_queue_opt);
2436
Yury Norov3aa56882018-02-06 15:38:06 -08002437 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002438 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2439 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2440 if (!backup)
2441 return -ENOMEM;
2442
2443 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2444 struct ethtool_coalesce coalesce;
2445
2446 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2447 if (ret != 0)
2448 goto roll_back;
2449
2450 tmp++;
2451
2452 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2453 ret = -EFAULT;
2454 goto roll_back;
2455 }
2456
2457 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2458 if (ret != 0)
2459 goto roll_back;
2460
2461 useraddr += sizeof(coalesce);
2462 }
2463
2464roll_back:
2465 if (ret != 0) {
2466 tmp = backup;
2467 for_each_set_bit(i, queue_mask, bit) {
2468 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2469 tmp++;
2470 }
2471 }
2472 kfree(backup);
2473
2474 return ret;
2475}
2476
Kan Liangac2c7ad2016-02-19 09:24:01 -05002477static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2478{
2479 struct ethtool_per_queue_op per_queue_opt;
2480
2481 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2482 return -EFAULT;
2483
2484 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002485 case ETHTOOL_GCOALESCE:
2486 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002487 case ETHTOOL_SCOALESCE:
2488 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002489 default:
2490 return -EOPNOTSUPP;
2491 };
2492}
2493
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002494static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2495{
2496 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002497 case ETHTOOL_PHY_DOWNSHIFT:
2498 if (tuna->len != sizeof(u8) ||
2499 tuna->type_id != ETHTOOL_TUNABLE_U8)
2500 return -EINVAL;
2501 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002502 default:
2503 return -EINVAL;
2504 }
2505
2506 return 0;
2507}
2508
2509static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2510{
2511 int ret;
2512 struct ethtool_tunable tuna;
2513 struct phy_device *phydev = dev->phydev;
2514 void *data;
2515
2516 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2517 return -EOPNOTSUPP;
2518
2519 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2520 return -EFAULT;
2521 ret = ethtool_phy_tunable_valid(&tuna);
2522 if (ret)
2523 return ret;
2524 data = kmalloc(tuna.len, GFP_USER);
2525 if (!data)
2526 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002527 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002528 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002529 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002530 if (ret)
2531 goto out;
2532 useraddr += sizeof(tuna);
2533 ret = -EFAULT;
2534 if (copy_to_user(useraddr, data, tuna.len))
2535 goto out;
2536 ret = 0;
2537
2538out:
2539 kfree(data);
2540 return ret;
2541}
2542
2543static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2544{
2545 int ret;
2546 struct ethtool_tunable tuna;
2547 struct phy_device *phydev = dev->phydev;
2548 void *data;
2549
2550 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2551 return -EOPNOTSUPP;
2552 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2553 return -EFAULT;
2554 ret = ethtool_phy_tunable_valid(&tuna);
2555 if (ret)
2556 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002557 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002558 data = memdup_user(useraddr, tuna.len);
2559 if (IS_ERR(data))
2560 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002561 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002562 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002563 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002564
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002565 kfree(data);
2566 return ret;
2567}
2568
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002569static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2570{
2571 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002572 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002573
2574 if (!dev->ethtool_ops->get_fecparam)
2575 return -EOPNOTSUPP;
2576
Edward Creea6d50512018-02-28 19:15:58 +00002577 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2578 if (rc)
2579 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002580
2581 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2582 return -EFAULT;
2583 return 0;
2584}
2585
2586static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2587{
2588 struct ethtool_fecparam fecparam;
2589
2590 if (!dev->ethtool_ops->set_fecparam)
2591 return -EOPNOTSUPP;
2592
2593 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2594 return -EFAULT;
2595
2596 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2597}
2598
Yan Burman600fed52013-06-03 02:03:34 +00002599/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Eric W. Biederman881d9662007-09-17 11:56:21 -07002601int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002603 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002605 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002607 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (!dev || !netif_device_present(dev))
2610 return -ENODEV;
2611
chavey97f8aef2010-04-07 21:54:42 -07002612 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return -EFAULT;
2614
Kan Liangac2c7ad2016-02-19 09:24:01 -05002615 if (ethcmd == ETHTOOL_PERQUEUE) {
2616 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2617 return -EFAULT;
2618 } else {
2619 sub_cmd = ethcmd;
2620 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002621 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002622 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002623 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002624 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002625 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002626 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002627 case ETHTOOL_GCOALESCE:
2628 case ETHTOOL_GRINGPARAM:
2629 case ETHTOOL_GPAUSEPARAM:
2630 case ETHTOOL_GRXCSUM:
2631 case ETHTOOL_GTXCSUM:
2632 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002633 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002634 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002635 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002636 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002637 case ETHTOOL_GTSO:
2638 case ETHTOOL_GPERMADDR:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002639 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002640 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002641 case ETHTOOL_GFLAGS:
2642 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002643 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002644 case ETHTOOL_GRXRINGS:
2645 case ETHTOOL_GRXCLSRLCNT:
2646 case ETHTOOL_GRXCLSRULE:
2647 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002648 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302649 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002650 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002651 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002652 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002653 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302654 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002655 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002656 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002657 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002658 break;
2659 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002660 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002661 return -EPERM;
2662 }
2663
chavey97f8aef2010-04-07 21:54:42 -07002664 if (dev->ethtool_ops->begin) {
2665 rc = dev->ethtool_ops->begin(dev);
2666 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002668 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002669 old_features = dev->features;
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 switch (ethcmd) {
2672 case ETHTOOL_GSET:
2673 rc = ethtool_get_settings(dev, useraddr);
2674 break;
2675 case ETHTOOL_SSET:
2676 rc = ethtool_set_settings(dev, useraddr);
2677 break;
2678 case ETHTOOL_GDRVINFO:
2679 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 break;
2681 case ETHTOOL_GREGS:
2682 rc = ethtool_get_regs(dev, useraddr);
2683 break;
2684 case ETHTOOL_GWOL:
2685 rc = ethtool_get_wol(dev, useraddr);
2686 break;
2687 case ETHTOOL_SWOL:
2688 rc = ethtool_set_wol(dev, useraddr);
2689 break;
2690 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002691 rc = ethtool_get_value(dev, useraddr, ethcmd,
2692 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 break;
2694 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002695 rc = ethtool_set_value_void(dev, useraddr,
2696 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002698 case ETHTOOL_GEEE:
2699 rc = ethtool_get_eee(dev, useraddr);
2700 break;
2701 case ETHTOOL_SEEE:
2702 rc = ethtool_set_eee(dev, useraddr);
2703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 case ETHTOOL_NWAY_RST:
2705 rc = ethtool_nway_reset(dev);
2706 break;
2707 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002708 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 break;
2710 case ETHTOOL_GEEPROM:
2711 rc = ethtool_get_eeprom(dev, useraddr);
2712 break;
2713 case ETHTOOL_SEEPROM:
2714 rc = ethtool_set_eeprom(dev, useraddr);
2715 break;
2716 case ETHTOOL_GCOALESCE:
2717 rc = ethtool_get_coalesce(dev, useraddr);
2718 break;
2719 case ETHTOOL_SCOALESCE:
2720 rc = ethtool_set_coalesce(dev, useraddr);
2721 break;
2722 case ETHTOOL_GRINGPARAM:
2723 rc = ethtool_get_ringparam(dev, useraddr);
2724 break;
2725 case ETHTOOL_SRINGPARAM:
2726 rc = ethtool_set_ringparam(dev, useraddr);
2727 break;
2728 case ETHTOOL_GPAUSEPARAM:
2729 rc = ethtool_get_pauseparam(dev, useraddr);
2730 break;
2731 case ETHTOOL_SPAUSEPARAM:
2732 rc = ethtool_set_pauseparam(dev, useraddr);
2733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 case ETHTOOL_TEST:
2735 rc = ethtool_self_test(dev, useraddr);
2736 break;
2737 case ETHTOOL_GSTRINGS:
2738 rc = ethtool_get_strings(dev, useraddr);
2739 break;
2740 case ETHTOOL_PHYS_ID:
2741 rc = ethtool_phys_id(dev, useraddr);
2742 break;
2743 case ETHTOOL_GSTATS:
2744 rc = ethtool_get_stats(dev, useraddr);
2745 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002746 case ETHTOOL_GPERMADDR:
2747 rc = ethtool_get_perm_addr(dev, useraddr);
2748 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002749 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002750 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002751 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002752 break;
2753 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002754 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002755 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002756 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002757 rc = ethtool_get_value(dev, useraddr, ethcmd,
2758 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002759 break;
2760 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002761 rc = ethtool_set_value(dev, useraddr,
2762 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002763 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002764 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002765 case ETHTOOL_GRXRINGS:
2766 case ETHTOOL_GRXCLSRLCNT:
2767 case ETHTOOL_GRXCLSRULE:
2768 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002769 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002770 break;
2771 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002772 case ETHTOOL_SRXCLSRLDEL:
2773 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002774 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002775 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002776 case ETHTOOL_FLASHDEV:
2777 rc = ethtool_flash_device(dev, useraddr);
2778 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002779 case ETHTOOL_RESET:
2780 rc = ethtool_reset(dev, useraddr);
2781 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002782 case ETHTOOL_GSSET_INFO:
2783 rc = ethtool_get_sset_info(dev, useraddr);
2784 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002785 case ETHTOOL_GRXFHINDIR:
2786 rc = ethtool_get_rxfh_indir(dev, useraddr);
2787 break;
2788 case ETHTOOL_SRXFHINDIR:
2789 rc = ethtool_set_rxfh_indir(dev, useraddr);
2790 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302791 case ETHTOOL_GRSSH:
2792 rc = ethtool_get_rxfh(dev, useraddr);
2793 break;
2794 case ETHTOOL_SRSSH:
2795 rc = ethtool_set_rxfh(dev, useraddr);
2796 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002797 case ETHTOOL_GFEATURES:
2798 rc = ethtool_get_features(dev, useraddr);
2799 break;
2800 case ETHTOOL_SFEATURES:
2801 rc = ethtool_set_features(dev, useraddr);
2802 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002803 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002804 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002805 case ETHTOOL_GSG:
2806 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002807 case ETHTOOL_GGSO:
2808 case ETHTOOL_GGRO:
2809 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2810 break;
2811 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002812 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002813 case ETHTOOL_SSG:
2814 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002815 case ETHTOOL_SGSO:
2816 case ETHTOOL_SGRO:
2817 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2818 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002819 case ETHTOOL_GCHANNELS:
2820 rc = ethtool_get_channels(dev, useraddr);
2821 break;
2822 case ETHTOOL_SCHANNELS:
2823 rc = ethtool_set_channels(dev, useraddr);
2824 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002825 case ETHTOOL_SET_DUMP:
2826 rc = ethtool_set_dump(dev, useraddr);
2827 break;
2828 case ETHTOOL_GET_DUMP_FLAG:
2829 rc = ethtool_get_dump_flag(dev, useraddr);
2830 break;
2831 case ETHTOOL_GET_DUMP_DATA:
2832 rc = ethtool_get_dump_data(dev, useraddr);
2833 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002834 case ETHTOOL_GET_TS_INFO:
2835 rc = ethtool_get_ts_info(dev, useraddr);
2836 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002837 case ETHTOOL_GMODULEINFO:
2838 rc = ethtool_get_module_info(dev, useraddr);
2839 break;
2840 case ETHTOOL_GMODULEEEPROM:
2841 rc = ethtool_get_module_eeprom(dev, useraddr);
2842 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302843 case ETHTOOL_GTUNABLE:
2844 rc = ethtool_get_tunable(dev, useraddr);
2845 break;
2846 case ETHTOOL_STUNABLE:
2847 rc = ethtool_set_tunable(dev, useraddr);
2848 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002849 case ETHTOOL_GPHYSTATS:
2850 rc = ethtool_get_phy_stats(dev, useraddr);
2851 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002852 case ETHTOOL_PERQUEUE:
2853 rc = ethtool_set_per_queue(dev, useraddr);
2854 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002855 case ETHTOOL_GLINKSETTINGS:
2856 rc = ethtool_get_link_ksettings(dev, useraddr);
2857 break;
2858 case ETHTOOL_SLINKSETTINGS:
2859 rc = ethtool_set_link_ksettings(dev, useraddr);
2860 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002861 case ETHTOOL_PHY_GTUNABLE:
2862 rc = get_phy_tunable(dev, useraddr);
2863 break;
2864 case ETHTOOL_PHY_STUNABLE:
2865 rc = set_phy_tunable(dev, useraddr);
2866 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002867 case ETHTOOL_GFECPARAM:
2868 rc = ethtool_get_fecparam(dev, useraddr);
2869 break;
2870 case ETHTOOL_SFECPARAM:
2871 rc = ethtool_set_fecparam(dev, useraddr);
2872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002874 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002876
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002877 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002879
2880 if (old_features != dev->features)
2881 netdev_features_change(dev);
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}