blob: eb55252ca1fbbf873b974575b33708078ae26230 [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",
Michał Mirosław9d921542011-11-15 15:29:55 +000095
96 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080097 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000098 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
99 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
100 [NETIF_F_RXHASH_BIT] = "rx-hashing",
101 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
102 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
103 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +0000104 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +0000105 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -0800106 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
John Fastabend1c78c642016-02-16 21:17:37 -0800107 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +0200108 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
109 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
Sabrina Dubrocad764a122017-07-21 12:49:28 +0200110 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
Michał Mirosław9d921542011-11-15 15:29:55 +0000111};
112
Eyal Perry892311f2014-12-02 18:12:10 +0200113static const char
114rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
115 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
116 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800117 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200118};
119
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300120static const char
121tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
122 [ETHTOOL_ID_UNSPEC] = "Unspec",
123 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
124 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
Inbar Karmye1577c12017-11-20 16:14:30 +0200125 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300126};
127
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100128static const char
129phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
130 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100131 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100132};
133
Michał Mirosław5455c692011-02-15 16:59:17 +0000134static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
135{
136 struct ethtool_gfeatures cmd = {
137 .cmd = ETHTOOL_GFEATURES,
138 .size = ETHTOOL_DEV_FEATURE_WORDS,
139 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000140 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000141 u32 __user *sizeaddr;
142 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000143 int i;
144
145 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000146 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000147
148 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000149 features[i].available = (u32)(dev->hw_features >> (32 * i));
150 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
151 features[i].active = (u32)(dev->features >> (32 * i));
152 features[i].never_changed =
153 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000154 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000155
156 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
157 if (get_user(copy_size, sizeaddr))
158 return -EFAULT;
159
160 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
161 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
162
163 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
164 return -EFAULT;
165 useraddr += sizeof(cmd);
166 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
167 return -EFAULT;
168
169 return 0;
170}
171
172static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
173{
174 struct ethtool_sfeatures cmd;
175 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000176 netdev_features_t wanted = 0, valid = 0;
177 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000178
179 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
180 return -EFAULT;
181 useraddr += sizeof(cmd);
182
183 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
184 return -EINVAL;
185
186 if (copy_from_user(features, useraddr, sizeof(features)))
187 return -EFAULT;
188
Michał Mirosław475414f2011-11-15 15:29:55 +0000189 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000190 valid |= (netdev_features_t)features[i].valid << (32 * i);
191 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000192 }
193
194 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000195 return -EINVAL;
196
Michał Mirosław475414f2011-11-15 15:29:55 +0000197 if (valid & ~dev->hw_features) {
198 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000199 ret |= ETHTOOL_F_UNSUPPORTED;
200 }
201
Michał Mirosław475414f2011-11-15 15:29:55 +0000202 dev->wanted_features &= ~valid;
203 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700204 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000205
Michał Mirosław475414f2011-11-15 15:29:55 +0000206 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000207 ret |= ETHTOOL_F_WISH;
208
209 return ret;
210}
211
Andrew Lunnf3a40942015-12-30 16:28:25 +0100212static int phy_get_sset_count(struct phy_device *phydev)
213{
214 int ret;
215
216 if (phydev->drv->get_sset_count &&
217 phydev->drv->get_strings &&
218 phydev->drv->get_stats) {
219 mutex_lock(&phydev->lock);
220 ret = phydev->drv->get_sset_count(phydev);
221 mutex_unlock(&phydev->lock);
222
223 return ret;
224 }
225
226 return -EOPNOTSUPP;
227}
228
Michał Mirosław340ae162011-02-15 16:59:16 +0000229static int __ethtool_get_sset_count(struct net_device *dev, int sset)
230{
231 const struct ethtool_ops *ops = dev->ethtool_ops;
232
Michał Mirosław5455c692011-02-15 16:59:17 +0000233 if (sset == ETH_SS_FEATURES)
234 return ARRAY_SIZE(netdev_features_strings);
235
Eyal Perry892311f2014-12-02 18:12:10 +0200236 if (sset == ETH_SS_RSS_HASH_FUNCS)
237 return ARRAY_SIZE(rss_hash_func_strings);
238
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300239 if (sset == ETH_SS_TUNABLES)
240 return ARRAY_SIZE(tunable_strings);
241
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100242 if (sset == ETH_SS_PHY_TUNABLES)
243 return ARRAY_SIZE(phy_tunable_strings);
244
Andrew Lunnf3a40942015-12-30 16:28:25 +0100245 if (sset == ETH_SS_PHY_STATS) {
246 if (dev->phydev)
247 return phy_get_sset_count(dev->phydev);
248 else
249 return -EOPNOTSUPP;
250 }
251
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000252 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000253 return ops->get_sset_count(dev, sset);
254 else
255 return -EOPNOTSUPP;
256}
257
258static void __ethtool_get_strings(struct net_device *dev,
259 u32 stringset, u8 *data)
260{
261 const struct ethtool_ops *ops = dev->ethtool_ops;
262
Michał Mirosław5455c692011-02-15 16:59:17 +0000263 if (stringset == ETH_SS_FEATURES)
264 memcpy(data, netdev_features_strings,
265 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200266 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
267 memcpy(data, rss_hash_func_strings,
268 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300269 else if (stringset == ETH_SS_TUNABLES)
270 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100271 else if (stringset == ETH_SS_PHY_TUNABLES)
272 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100273 else if (stringset == ETH_SS_PHY_STATS) {
274 struct phy_device *phydev = dev->phydev;
275
276 if (phydev) {
277 mutex_lock(&phydev->lock);
278 phydev->drv->get_strings(phydev, data);
279 mutex_unlock(&phydev->lock);
280 } else {
281 return;
282 }
283 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000284 /* ops->get_strings is valid because checked earlier */
285 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000286}
287
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000288static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000289{
290 /* feature masks of legacy discrete ethtool ops */
291
292 switch (eth_cmd) {
293 case ETHTOOL_GTXCSUM:
294 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800295 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000296 case ETHTOOL_GRXCSUM:
297 case ETHTOOL_SRXCSUM:
298 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000299 case ETHTOOL_GSG:
300 case ETHTOOL_SSG:
301 return NETIF_F_SG;
302 case ETHTOOL_GTSO:
303 case ETHTOOL_STSO:
304 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000305 case ETHTOOL_GGSO:
306 case ETHTOOL_SGSO:
307 return NETIF_F_GSO;
308 case ETHTOOL_GGRO:
309 case ETHTOOL_SGRO:
310 return NETIF_F_GRO;
311 default:
312 BUG();
313 }
314}
315
Michał Mirosław0a417702011-02-15 16:59:17 +0000316static int ethtool_get_one_feature(struct net_device *dev,
317 char __user *useraddr, u32 ethcmd)
318{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000319 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000320 struct ethtool_value edata = {
321 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000322 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000323 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000324
Michał Mirosław0a417702011-02-15 16:59:17 +0000325 if (copy_to_user(useraddr, &edata, sizeof(edata)))
326 return -EFAULT;
327 return 0;
328}
329
Michał Mirosław0a417702011-02-15 16:59:17 +0000330static int ethtool_set_one_feature(struct net_device *dev,
331 void __user *useraddr, u32 ethcmd)
332{
333 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000334 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000335
336 if (copy_from_user(&edata, useraddr, sizeof(edata)))
337 return -EFAULT;
338
Michał Mirosław86794882011-02-15 16:59:17 +0000339 mask = ethtool_get_feature_mask(ethcmd);
340 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000341 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000342 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000343
344 if (edata.data)
345 dev->wanted_features |= mask;
346 else
347 dev->wanted_features &= ~mask;
348
349 __netdev_update_features(dev);
350
351 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000352}
353
Michał Mirosław02b3a552011-11-15 15:29:55 +0000354#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
355 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000356#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
357 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
358 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000359
360static u32 __ethtool_get_flags(struct net_device *dev)
361{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000362 u32 flags = 0;
363
Dragos Foianu8a731252013-07-13 14:43:00 +0100364 if (dev->features & NETIF_F_LRO)
365 flags |= ETH_FLAG_LRO;
366 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
367 flags |= ETH_FLAG_RXVLAN;
368 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
369 flags |= ETH_FLAG_TXVLAN;
370 if (dev->features & NETIF_F_NTUPLE)
371 flags |= ETH_FLAG_NTUPLE;
372 if (dev->features & NETIF_F_RXHASH)
373 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000374
375 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000376}
377
378static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000379{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000380 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000381
Michał Mirosław02b3a552011-11-15 15:29:55 +0000382 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000383 return -EINVAL;
384
Dragos Foianu8a731252013-07-13 14:43:00 +0100385 if (data & ETH_FLAG_LRO)
386 features |= NETIF_F_LRO;
387 if (data & ETH_FLAG_RXVLAN)
388 features |= NETIF_F_HW_VLAN_CTAG_RX;
389 if (data & ETH_FLAG_TXVLAN)
390 features |= NETIF_F_HW_VLAN_CTAG_TX;
391 if (data & ETH_FLAG_NTUPLE)
392 features |= NETIF_F_NTUPLE;
393 if (data & ETH_FLAG_RXHASH)
394 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000395
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000396 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000397 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000398 if (changed & ~dev->hw_features)
399 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
400
401 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000402 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000403
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700404 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000405
406 return 0;
407}
408
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700409/* Given two link masks, AND them together and save the result in dst. */
410void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
411 struct ethtool_link_ksettings *src)
412{
413 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
414 unsigned int idx = 0;
415
416 for (; idx < size; idx++) {
417 dst->link_modes.supported[idx] &=
418 src->link_modes.supported[idx];
419 dst->link_modes.advertising[idx] &=
420 src->link_modes.advertising[idx];
421 }
422}
423EXPORT_SYMBOL(ethtool_intersect_link_masks);
424
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200425void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
426 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800427{
428 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
429 dst[0] = legacy_u32;
430}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200431EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800432
433/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200434bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
435 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800436{
437 bool retval = true;
438
439 /* TODO: following test will soon always be true */
440 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
441 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
442
443 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
444 bitmap_fill(ext, 32);
445 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
446 if (bitmap_intersects(ext, src,
447 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
448 /* src mask goes beyond bit 31 */
449 retval = false;
450 }
451 }
452 *legacy_u32 = src[0];
453 return retval;
454}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200455EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800456
457/* return false if legacy contained non-0 deprecated fields
Niklas Söderlund95491e32017-10-20 01:32:08 +0200458 * maxtxpkt/maxrxpkt. rest of ksettings always updated
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800459 */
460static bool
461convert_legacy_settings_to_link_ksettings(
462 struct ethtool_link_ksettings *link_ksettings,
463 const struct ethtool_cmd *legacy_settings)
464{
465 bool retval = true;
466
467 memset(link_ksettings, 0, sizeof(*link_ksettings));
468
469 /* This is used to tell users that driver is still using these
470 * deprecated legacy fields, and they should not use
471 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
472 */
Niklas Söderlund95491e32017-10-20 01:32:08 +0200473 if (legacy_settings->maxtxpkt ||
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800474 legacy_settings->maxrxpkt)
475 retval = false;
476
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200477 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800478 link_ksettings->link_modes.supported,
479 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200480 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800481 link_ksettings->link_modes.advertising,
482 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200483 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800484 link_ksettings->link_modes.lp_advertising,
485 legacy_settings->lp_advertising);
486 link_ksettings->base.speed
487 = ethtool_cmd_speed(legacy_settings);
488 link_ksettings->base.duplex
489 = legacy_settings->duplex;
490 link_ksettings->base.port
491 = legacy_settings->port;
492 link_ksettings->base.phy_address
493 = legacy_settings->phy_address;
494 link_ksettings->base.autoneg
495 = legacy_settings->autoneg;
496 link_ksettings->base.mdio_support
497 = legacy_settings->mdio_support;
498 link_ksettings->base.eth_tp_mdix
499 = legacy_settings->eth_tp_mdix;
500 link_ksettings->base.eth_tp_mdix_ctrl
501 = legacy_settings->eth_tp_mdix_ctrl;
502 return retval;
503}
504
505/* return false if ksettings link modes had higher bits
506 * set. legacy_settings always updated (best effort)
507 */
508static bool
509convert_link_ksettings_to_legacy_settings(
510 struct ethtool_cmd *legacy_settings,
511 const struct ethtool_link_ksettings *link_ksettings)
512{
513 bool retval = true;
514
515 memset(legacy_settings, 0, sizeof(*legacy_settings));
516 /* this also clears the deprecated fields in legacy structure:
517 * __u8 transceiver;
518 * __u32 maxtxpkt;
519 * __u32 maxrxpkt;
520 */
521
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200522 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800523 &legacy_settings->supported,
524 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200525 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800526 &legacy_settings->advertising,
527 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200528 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800529 &legacy_settings->lp_advertising,
530 link_ksettings->link_modes.lp_advertising);
531 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
532 legacy_settings->duplex
533 = link_ksettings->base.duplex;
534 legacy_settings->port
535 = link_ksettings->base.port;
536 legacy_settings->phy_address
537 = link_ksettings->base.phy_address;
538 legacy_settings->autoneg
539 = link_ksettings->base.autoneg;
540 legacy_settings->mdio_support
541 = link_ksettings->base.mdio_support;
542 legacy_settings->eth_tp_mdix
543 = link_ksettings->base.eth_tp_mdix;
544 legacy_settings->eth_tp_mdix_ctrl
545 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700546 legacy_settings->transceiver
547 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800548 return retval;
549}
550
551/* number of 32-bit words to store the user's link mode bitmaps */
552#define __ETHTOOL_LINK_MODE_MASK_NU32 \
553 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
554
555/* layout of the struct passed from/to userland */
556struct ethtool_link_usettings {
557 struct ethtool_link_settings base;
558 struct {
559 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
560 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
561 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
562 } link_modes;
563};
564
565/* Internal kernel helper to query a device ethtool_link_settings.
566 *
567 * Backward compatibility note: for compatibility with legacy drivers
568 * that implement only the ethtool_cmd API, this has to work with both
569 * drivers implementing get_link_ksettings API and drivers
570 * implementing get_settings API. When drivers implement get_settings
571 * and report ethtool_cmd deprecated fields
572 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
573 * because the resulting struct ethtool_link_settings does not report them.
574 */
575int __ethtool_get_link_ksettings(struct net_device *dev,
576 struct ethtool_link_ksettings *link_ksettings)
577{
578 int err;
579 struct ethtool_cmd cmd;
580
581 ASSERT_RTNL();
582
583 if (dev->ethtool_ops->get_link_ksettings) {
584 memset(link_ksettings, 0, sizeof(*link_ksettings));
585 return dev->ethtool_ops->get_link_ksettings(dev,
586 link_ksettings);
587 }
588
589 /* driver doesn't support %ethtool_link_ksettings API. revert to
590 * legacy %ethtool_cmd API, unless it's not supported either.
591 * TODO: remove when ethtool_ops::get_settings disappears internally
592 */
David Decotigny3237fc62016-02-24 10:58:11 -0800593 if (!dev->ethtool_ops->get_settings)
594 return -EOPNOTSUPP;
595
596 memset(&cmd, 0, sizeof(cmd));
597 cmd.cmd = ETHTOOL_GSET;
598 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800599 if (err < 0)
600 return err;
601
602 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
603 */
604 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
605 return err;
606}
607EXPORT_SYMBOL(__ethtool_get_link_ksettings);
608
609/* convert ethtool_link_usettings in user space to a kernel internal
610 * ethtool_link_ksettings. return 0 on success, errno on error.
611 */
612static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
613 const void __user *from)
614{
615 struct ethtool_link_usettings link_usettings;
616
617 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
618 return -EFAULT;
619
620 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800621 bitmap_from_arr32(to->link_modes.supported,
622 link_usettings.link_modes.supported,
623 __ETHTOOL_LINK_MODE_MASK_NBITS);
624 bitmap_from_arr32(to->link_modes.advertising,
625 link_usettings.link_modes.advertising,
626 __ETHTOOL_LINK_MODE_MASK_NBITS);
627 bitmap_from_arr32(to->link_modes.lp_advertising,
628 link_usettings.link_modes.lp_advertising,
629 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800630
631 return 0;
632}
633
634/* convert a kernel internal ethtool_link_ksettings to
635 * ethtool_link_usettings in user space. return 0 on success, errno on
636 * error.
637 */
638static int
639store_link_ksettings_for_user(void __user *to,
640 const struct ethtool_link_ksettings *from)
641{
642 struct ethtool_link_usettings link_usettings;
643
644 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800645 bitmap_to_arr32(link_usettings.link_modes.supported,
646 from->link_modes.supported,
647 __ETHTOOL_LINK_MODE_MASK_NBITS);
648 bitmap_to_arr32(link_usettings.link_modes.advertising,
649 from->link_modes.advertising,
650 __ETHTOOL_LINK_MODE_MASK_NBITS);
651 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
652 from->link_modes.lp_advertising,
653 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800654
655 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
656 return -EFAULT;
657
658 return 0;
659}
660
661/* Query device for its ethtool_link_settings.
662 *
663 * Backward compatibility note: this function must fail when driver
664 * does not implement ethtool::get_link_ksettings, even if legacy
665 * ethtool_ops::get_settings is implemented. This tells new versions
666 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
667 * this driver, so that they can correctly access the ethtool_cmd
668 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
669 * implements ethtool_ops::get_settings anymore.
670 */
671static int ethtool_get_link_ksettings(struct net_device *dev,
672 void __user *useraddr)
673{
674 int err = 0;
675 struct ethtool_link_ksettings link_ksettings;
676
677 ASSERT_RTNL();
678
679 if (!dev->ethtool_ops->get_link_ksettings)
680 return -EOPNOTSUPP;
681
682 /* handle bitmap nbits handshake */
683 if (copy_from_user(&link_ksettings.base, useraddr,
684 sizeof(link_ksettings.base)))
685 return -EFAULT;
686
687 if (__ETHTOOL_LINK_MODE_MASK_NU32
688 != link_ksettings.base.link_mode_masks_nwords) {
689 /* wrong link mode nbits requested */
690 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000691 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800692 /* send back number of words required as negative val */
693 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
694 "need too many bits for link modes!");
695 link_ksettings.base.link_mode_masks_nwords
696 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
697
698 /* copy the base fields back to user, not the link
699 * mode bitmaps
700 */
701 if (copy_to_user(useraddr, &link_ksettings.base,
702 sizeof(link_ksettings.base)))
703 return -EFAULT;
704
705 return 0;
706 }
707
708 /* handshake successful: user/kernel agree on
709 * link_mode_masks_nwords
710 */
711
712 memset(&link_ksettings, 0, sizeof(link_ksettings));
713 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
714 if (err < 0)
715 return err;
716
717 /* make sure we tell the right values to user */
718 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
719 link_ksettings.base.link_mode_masks_nwords
720 = __ETHTOOL_LINK_MODE_MASK_NU32;
721
722 return store_link_ksettings_for_user(useraddr, &link_ksettings);
723}
724
725/* Update device ethtool_link_settings.
726 *
727 * Backward compatibility note: this function must fail when driver
728 * does not implement ethtool::set_link_ksettings, even if legacy
729 * ethtool_ops::set_settings is implemented. This tells new versions
730 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
731 * this driver, so that they can correctly update the ethtool_cmd
732 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
733 * implements ethtool_ops::get_settings anymore.
734 */
735static int ethtool_set_link_ksettings(struct net_device *dev,
736 void __user *useraddr)
737{
738 int err;
739 struct ethtool_link_ksettings link_ksettings;
740
741 ASSERT_RTNL();
742
743 if (!dev->ethtool_ops->set_link_ksettings)
744 return -EOPNOTSUPP;
745
746 /* make sure nbits field has expected value */
747 if (copy_from_user(&link_ksettings.base, useraddr,
748 sizeof(link_ksettings.base)))
749 return -EFAULT;
750
751 if (__ETHTOOL_LINK_MODE_MASK_NU32
752 != link_ksettings.base.link_mode_masks_nwords)
753 return -EINVAL;
754
755 /* copy the whole structure, now that we know it has expected
756 * format
757 */
758 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
759 if (err)
760 return err;
761
762 /* re-check nwords field, just in case */
763 if (__ETHTOOL_LINK_MODE_MASK_NU32
764 != link_ksettings.base.link_mode_masks_nwords)
765 return -EINVAL;
766
767 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
768}
769
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800770/* Query device for its ethtool_cmd settings.
771 *
772 * Backward compatibility note: for compatibility with legacy ethtool,
773 * this has to work with both drivers implementing get_link_ksettings
774 * API and drivers implementing get_settings API. When drivers
775 * implement get_link_ksettings and report higher link mode bits, a
776 * kernel warning is logged once (with name of 1st driver/device) to
777 * recommend user to upgrade ethtool, but the command is successful
778 * (only the lower link mode bits reported back to user).
779 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000780static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
781{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000782 struct ethtool_cmd cmd;
783
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800784 ASSERT_RTNL();
785
786 if (dev->ethtool_ops->get_link_ksettings) {
787 /* First, use link_ksettings API if it is supported */
788 int err;
789 struct ethtool_link_ksettings link_ksettings;
790
791 memset(&link_ksettings, 0, sizeof(link_ksettings));
792 err = dev->ethtool_ops->get_link_ksettings(dev,
793 &link_ksettings);
794 if (err < 0)
795 return err;
Stephen Hemminger71891e22017-12-29 10:02:52 -0800796 convert_link_ksettings_to_legacy_settings(&cmd,
797 &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800798
799 /* send a sensible cmd tag back to user */
800 cmd.cmd = ETHTOOL_GSET;
801 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800802 /* driver doesn't support %ethtool_link_ksettings
803 * API. revert to legacy %ethtool_cmd API, unless it's
804 * not supported either.
805 */
David Decotigny3237fc62016-02-24 10:58:11 -0800806 int err;
807
808 if (!dev->ethtool_ops->get_settings)
809 return -EOPNOTSUPP;
810
811 memset(&cmd, 0, sizeof(cmd));
812 cmd.cmd = ETHTOOL_GSET;
813 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800814 if (err < 0)
815 return err;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
819 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822}
823
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800824/* Update device link settings with given ethtool_cmd.
825 *
826 * Backward compatibility note: for compatibility with legacy ethtool,
827 * this has to work with both drivers implementing set_link_ksettings
828 * API and drivers implementing set_settings API. When drivers
829 * implement set_link_ksettings and user's request updates deprecated
830 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
831 * warning is logged once (with name of 1st driver/device) to
832 * recommend user to upgrade ethtool, and the request is rejected.
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
835{
836 struct ethtool_cmd cmd;
837
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800838 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
841 return -EFAULT;
842
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800843 /* first, try new %ethtool_link_ksettings API. */
844 if (dev->ethtool_ops->set_link_ksettings) {
845 struct ethtool_link_ksettings link_ksettings;
846
847 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
848 &cmd))
849 return -EINVAL;
850
851 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
852 link_ksettings.base.link_mode_masks_nwords
853 = __ETHTOOL_LINK_MODE_MASK_NU32;
854 return dev->ethtool_ops->set_link_ksettings(dev,
855 &link_ksettings);
856 }
857
858 /* legacy %ethtool_cmd API */
859
860 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
861 * disappears internally
862 */
863
864 if (!dev->ethtool_ops->set_settings)
865 return -EOPNOTSUPP;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return dev->ethtool_ops->set_settings(dev, &cmd);
868}
869
chavey97f8aef2010-04-07 21:54:42 -0700870static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
871 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700874 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 memset(&info, 0, sizeof(info));
877 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000878 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700879 ops->get_drvinfo(dev, &info);
880 } else if (dev->dev.parent && dev->dev.parent->driver) {
881 strlcpy(info.bus_info, dev_name(dev->dev.parent),
882 sizeof(info.bus_info));
883 strlcpy(info.driver, dev->dev.parent->driver->name,
884 sizeof(info.driver));
885 } else {
886 return -EOPNOTSUPP;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Jeff Garzik723b2f52010-03-03 22:51:50 +0000889 /*
890 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000891 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000892 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000893 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700894 int rc;
895
896 rc = ops->get_sset_count(dev, ETH_SS_TEST);
897 if (rc >= 0)
898 info.testinfo_len = rc;
899 rc = ops->get_sset_count(dev, ETH_SS_STATS);
900 if (rc >= 0)
901 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700902 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
903 if (rc >= 0)
904 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700905 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000906 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000908 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 info.eedump_len = ops->get_eeprom_len(dev);
910
911 if (copy_to_user(useraddr, &info, sizeof(info)))
912 return -EFAULT;
913 return 0;
914}
915
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800916static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700917 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000918{
919 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000920 u64 sset_mask;
921 int i, idx = 0, n_bits = 0, ret, rc;
922 u32 *info_buf = NULL;
923
Jeff Garzik723b2f52010-03-03 22:51:50 +0000924 if (copy_from_user(&info, useraddr, sizeof(info)))
925 return -EFAULT;
926
927 /* store copy of mask, because we zero struct later on */
928 sset_mask = info.sset_mask;
929 if (!sset_mask)
930 return 0;
931
932 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000933 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000934
935 memset(&info, 0, sizeof(info));
936 info.cmd = ETHTOOL_GSSET_INFO;
937
938 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
939 if (!info_buf)
940 return -ENOMEM;
941
942 /*
943 * fill return buffer based on input bitmask and successful
944 * get_sset_count return
945 */
946 for (i = 0; i < 64; i++) {
947 if (!(sset_mask & (1ULL << i)))
948 continue;
949
Michał Mirosław340ae162011-02-15 16:59:16 +0000950 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000951 if (rc >= 0) {
952 info.sset_mask |= (1ULL << i);
953 info_buf[idx++] = rc;
954 }
955 }
956
957 ret = -EFAULT;
958 if (copy_to_user(useraddr, &info, sizeof(info)))
959 goto out;
960
961 useraddr += offsetof(struct ethtool_sset_info, data);
962 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
963 goto out;
964
965 ret = 0;
966
967out:
968 kfree(info_buf);
969 return ret;
970}
971
chavey97f8aef2010-04-07 21:54:42 -0700972static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000973 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700974{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000975 struct ethtool_rxnfc info;
976 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000977 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700978
Santwona Behera59089d82009-02-20 00:58:13 -0800979 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700980 return -EOPNOTSUPP;
981
Ben Hutchingsbf988432010-06-28 08:45:58 +0000982 /* struct ethtool_rxnfc was originally defined for
983 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
984 * members. User-space might still be using that
985 * definition. */
986 if (cmd == ETHTOOL_SRXFH)
987 info_size = (offsetof(struct ethtool_rxnfc, data) +
988 sizeof(info.data));
989
990 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700991 return -EFAULT;
992
Ben Hutchings55664f32012-01-03 12:04:51 +0000993 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
994 if (rc)
995 return rc;
996
997 if (cmd == ETHTOOL_SRXCLSRLINS &&
998 copy_to_user(useraddr, &info, info_size))
999 return -EFAULT;
1000
1001 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -07001002}
1003
chavey97f8aef2010-04-07 21:54:42 -07001004static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +00001005 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -07001006{
1007 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001008 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -08001009 const struct ethtool_ops *ops = dev->ethtool_ops;
1010 int ret;
1011 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -07001012
Santwona Behera59089d82009-02-20 00:58:13 -08001013 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -07001014 return -EOPNOTSUPP;
1015
Ben Hutchingsbf988432010-06-28 08:45:58 +00001016 /* struct ethtool_rxnfc was originally defined for
1017 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1018 * members. User-space might still be using that
1019 * definition. */
1020 if (cmd == ETHTOOL_GRXFH)
1021 info_size = (offsetof(struct ethtool_rxnfc, data) +
1022 sizeof(info.data));
1023
1024 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001025 return -EFAULT;
1026
Edward Cree84a1d9c42018-03-08 15:45:03 +00001027 /* If FLOW_RSS was requested then user-space must be using the
1028 * new definition, as FLOW_RSS is newer.
1029 */
1030 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1031 info_size = sizeof(info);
1032 if (copy_from_user(&info, useraddr, info_size))
1033 return -EFAULT;
1034 }
1035
Santwona Behera59089d82009-02-20 00:58:13 -08001036 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1037 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001038 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +00001039 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001040 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001041 if (!rule_buf)
1042 return -ENOMEM;
1043 }
1044 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001045
Santwona Behera59089d82009-02-20 00:58:13 -08001046 ret = ops->get_rxnfc(dev, &info, rule_buf);
1047 if (ret < 0)
1048 goto err_out;
1049
1050 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001051 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001052 goto err_out;
1053
1054 if (rule_buf) {
1055 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1056 if (copy_to_user(useraddr, rule_buf,
1057 info.rule_cnt * sizeof(u32)))
1058 goto err_out;
1059 }
1060 ret = 0;
1061
1062err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001063 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001064
1065 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001066}
1067
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301068static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1069 struct ethtool_rxnfc *rx_rings,
1070 u32 size)
1071{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001072 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301073
1074 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001075 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301076
1077 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001078 for (i = 0; i < size; i++)
1079 if (indir[i] >= rx_rings->data)
1080 return -EINVAL;
1081
1082 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301083}
1084
Kim Jonesba905f52016-02-02 03:51:16 +00001085u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001086
1087void netdev_rss_key_fill(void *buffer, size_t len)
1088{
1089 BUG_ON(len > sizeof(netdev_rss_key));
1090 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1091 memcpy(buffer, netdev_rss_key, len);
1092}
1093EXPORT_SYMBOL(netdev_rss_key_fill);
1094
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001095static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1096{
1097 u32 dev_size, current_max = 0;
1098 u32 *indir;
1099 int ret;
1100
1101 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1102 !dev->ethtool_ops->get_rxfh)
1103 return -EOPNOTSUPP;
1104 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1105 if (dev_size == 0)
1106 return -EOPNOTSUPP;
1107
1108 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1109 if (!indir)
1110 return -ENOMEM;
1111
1112 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1113 if (ret)
1114 goto out;
1115
1116 while (dev_size--)
1117 current_max = max(current_max, indir[dev_size]);
1118
1119 *max = current_max;
1120
1121out:
1122 kfree(indir);
1123 return ret;
1124}
1125
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001126static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1127 void __user *useraddr)
1128{
Ben Hutchings7850f632011-12-15 13:55:01 +00001129 u32 user_size, dev_size;
1130 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001131 int ret;
1132
Ben Hutchings7850f632011-12-15 13:55:01 +00001133 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001134 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001135 return -EOPNOTSUPP;
1136 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1137 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001138 return -EOPNOTSUPP;
1139
Ben Hutchings7850f632011-12-15 13:55:01 +00001140 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001141 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001142 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001143 return -EFAULT;
1144
Ben Hutchings7850f632011-12-15 13:55:01 +00001145 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1146 &dev_size, sizeof(dev_size)))
1147 return -EFAULT;
1148
1149 /* If the user buffer size is 0, this is just a query for the
1150 * device table size. Otherwise, if it's smaller than the
1151 * device table size it's an error.
1152 */
1153 if (user_size < dev_size)
1154 return user_size == 0 ? 0 : -EINVAL;
1155
1156 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001157 if (!indir)
1158 return -ENOMEM;
1159
Eyal Perry892311f2014-12-02 18:12:10 +02001160 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001161 if (ret)
1162 goto out;
1163
Ben Hutchings7850f632011-12-15 13:55:01 +00001164 if (copy_to_user(useraddr +
1165 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1166 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001167 ret = -EFAULT;
1168
1169out:
1170 kfree(indir);
1171 return ret;
1172}
1173
1174static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1175 void __user *useraddr)
1176{
Ben Hutchings7850f632011-12-15 13:55:01 +00001177 struct ethtool_rxnfc rx_rings;
1178 u32 user_size, dev_size, i;
1179 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001180 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001181 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301182 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001183
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001184 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001185 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001186 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001187
1188 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001189 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001190 return -EOPNOTSUPP;
1191
Ben Hutchings7850f632011-12-15 13:55:01 +00001192 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001193 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001194 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001195 return -EFAULT;
1196
Ben Hutchings278bc422011-12-15 13:56:49 +00001197 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001198 return -EINVAL;
1199
1200 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001201 if (!indir)
1202 return -ENOMEM;
1203
Ben Hutchings7850f632011-12-15 13:55:01 +00001204 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001205 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001206 if (ret)
1207 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001208
1209 if (user_size == 0) {
1210 for (i = 0; i < dev_size; i++)
1211 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1212 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301213 ret = ethtool_copy_validate_indir(indir,
1214 useraddr + ringidx_offset,
1215 &rx_rings,
1216 dev_size);
1217 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001218 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001219 }
1220
Eyal Perry892311f2014-12-02 18:12:10 +02001221 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001222 if (ret)
1223 goto out;
1224
1225 /* indicate whether rxfh was set to default */
1226 if (user_size == 0)
1227 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1228 else
1229 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001230
1231out:
1232 kfree(indir);
1233 return ret;
1234}
1235
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301236static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1237 void __user *useraddr)
1238{
1239 int ret;
1240 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001241 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301242 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001243 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301244 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001245 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301246 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001247 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301248 u8 *hkey = NULL;
1249 u8 *rss_config;
1250
Eyal Perry892311f2014-12-02 18:12:10 +02001251 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301252 return -EOPNOTSUPP;
1253
1254 if (ops->get_rxfh_indir_size)
1255 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301256 if (ops->get_rxfh_key_size)
1257 dev_key_size = ops->get_rxfh_key_size(dev);
1258
Ben Hutchingsf062a382014-05-15 16:28:07 +01001259 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301260 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001261 user_indir_size = rxfh.indir_size;
1262 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301263
Ben Hutchingsf062a382014-05-15 16:28:07 +01001264 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001265 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001266 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001267 /* Most drivers don't handle rss_context, check it's 0 as well */
1268 if (rxfh.rss_context && !ops->get_rxfh_context)
1269 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001270
1271 rxfh.indir_size = dev_indir_size;
1272 rxfh.key_size = dev_key_size;
1273 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301274 return -EFAULT;
1275
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301276 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1277 (user_key_size && (user_key_size != dev_key_size)))
1278 return -EINVAL;
1279
1280 indir_bytes = user_indir_size * sizeof(indir[0]);
1281 total_size = indir_bytes + user_key_size;
1282 rss_config = kzalloc(total_size, GFP_USER);
1283 if (!rss_config)
1284 return -ENOMEM;
1285
1286 if (user_indir_size)
1287 indir = (u32 *)rss_config;
1288
1289 if (user_key_size)
1290 hkey = rss_config + indir_bytes;
1291
Edward Cree84a1d9c42018-03-08 15:45:03 +00001292 if (rxfh.rss_context)
1293 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1294 &dev_hfunc,
1295 rxfh.rss_context);
1296 else
1297 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001298 if (ret)
1299 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301300
Eyal Perry892311f2014-12-02 18:12:10 +02001301 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1302 &dev_hfunc, sizeof(rxfh.hfunc))) {
1303 ret = -EFAULT;
1304 } else if (copy_to_user(useraddr +
1305 offsetof(struct ethtool_rxfh, rss_config[0]),
1306 rss_config, total_size)) {
1307 ret = -EFAULT;
1308 }
1309out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301310 kfree(rss_config);
1311
1312 return ret;
1313}
1314
1315static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1316 void __user *useraddr)
1317{
1318 int ret;
1319 const struct ethtool_ops *ops = dev->ethtool_ops;
1320 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001321 struct ethtool_rxfh rxfh;
1322 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301323 u32 *indir = NULL, indir_bytes = 0;
1324 u8 *hkey = NULL;
1325 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301326 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001327 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301328
Eyal Perry892311f2014-12-02 18:12:10 +02001329 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301330 return -EOPNOTSUPP;
1331
1332 if (ops->get_rxfh_indir_size)
1333 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301334 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001335 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301336
Ben Hutchingsf062a382014-05-15 16:28:07 +01001337 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301338 return -EFAULT;
1339
Ben Hutchingsf062a382014-05-15 16:28:07 +01001340 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001341 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001342 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001343 /* Most drivers don't handle rss_context, check it's 0 as well */
1344 if (rxfh.rss_context && !ops->set_rxfh_context)
1345 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001346
Eyal Perry892311f2014-12-02 18:12:10 +02001347 /* If either indir, hash key or function is valid, proceed further.
1348 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301349 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001350 if ((rxfh.indir_size &&
1351 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1352 rxfh.indir_size != dev_indir_size) ||
1353 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1354 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001355 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301356 return -EINVAL;
1357
Ben Hutchingsf062a382014-05-15 16:28:07 +01001358 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301359 indir_bytes = dev_indir_size * sizeof(indir[0]);
1360
Ben Hutchingsf062a382014-05-15 16:28:07 +01001361 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301362 if (!rss_config)
1363 return -ENOMEM;
1364
1365 rx_rings.cmd = ETHTOOL_GRXRINGS;
1366 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1367 if (ret)
1368 goto out;
1369
Edward Cree84a1d9c42018-03-08 15:45:03 +00001370 /* rxfh.indir_size == 0 means reset the indir table to default (master
1371 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001372 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301373 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001374 if (rxfh.indir_size &&
1375 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301376 indir = (u32 *)rss_config;
1377 ret = ethtool_copy_validate_indir(indir,
1378 useraddr + rss_cfg_offset,
1379 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001380 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301381 if (ret)
1382 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001383 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001384 if (rxfh.rss_context == 0) {
1385 indir = (u32 *)rss_config;
1386 for (i = 0; i < dev_indir_size; i++)
1387 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1388 } else {
1389 delete = true;
1390 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301391 }
1392
Ben Hutchingsf062a382014-05-15 16:28:07 +01001393 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301394 hkey = rss_config + indir_bytes;
1395 if (copy_from_user(hkey,
1396 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001397 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301398 ret = -EFAULT;
1399 goto out;
1400 }
1401 }
1402
Edward Cree84a1d9c42018-03-08 15:45:03 +00001403 if (rxfh.rss_context)
1404 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1405 &rxfh.rss_context, delete);
1406 else
1407 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001408 if (ret)
1409 goto out;
1410
Edward Cree84a1d9c42018-03-08 15:45:03 +00001411 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1412 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1413 ret = -EFAULT;
1414
1415 if (!rxfh.rss_context) {
1416 /* indicate whether rxfh was set to default */
1417 if (rxfh.indir_size == 0)
1418 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1419 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1420 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1421 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301422
1423out:
1424 kfree(rss_config);
1425 return ret;
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1429{
1430 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001431 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 void *regbuf;
1433 int reglen, ret;
1434
1435 if (!ops->get_regs || !ops->get_regs_len)
1436 return -EOPNOTSUPP;
1437
1438 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1439 return -EFAULT;
1440
1441 reglen = ops->get_regs_len(dev);
1442 if (regs.len > reglen)
1443 regs.len = reglen;
1444
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001445 regbuf = NULL;
1446 if (reglen) {
1447 regbuf = vzalloc(reglen);
1448 if (!regbuf)
1449 return -ENOMEM;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 ops->get_regs(dev, &regs, regbuf);
1453
1454 ret = -EFAULT;
1455 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1456 goto out;
1457 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001458 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 goto out;
1460 ret = 0;
1461
1462 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001463 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return ret;
1465}
1466
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001467static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1468{
1469 struct ethtool_value reset;
1470 int ret;
1471
1472 if (!dev->ethtool_ops->reset)
1473 return -EOPNOTSUPP;
1474
1475 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1476 return -EFAULT;
1477
1478 ret = dev->ethtool_ops->reset(dev, &reset.data);
1479 if (ret)
1480 return ret;
1481
1482 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1483 return -EFAULT;
1484 return 0;
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1488{
Roland Dreier8e557422010-02-11 12:14:23 -08001489 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 if (!dev->ethtool_ops->get_wol)
1492 return -EOPNOTSUPP;
1493
1494 dev->ethtool_ops->get_wol(dev, &wol);
1495
1496 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1497 return -EFAULT;
1498 return 0;
1499}
1500
1501static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1502{
1503 struct ethtool_wolinfo wol;
1504
1505 if (!dev->ethtool_ops->set_wol)
1506 return -EOPNOTSUPP;
1507
1508 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1509 return -EFAULT;
1510
1511 return dev->ethtool_ops->set_wol(dev, &wol);
1512}
1513
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001514static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1515{
1516 struct ethtool_eee edata;
1517 int rc;
1518
1519 if (!dev->ethtool_ops->get_eee)
1520 return -EOPNOTSUPP;
1521
1522 memset(&edata, 0, sizeof(struct ethtool_eee));
1523 edata.cmd = ETHTOOL_GEEE;
1524 rc = dev->ethtool_ops->get_eee(dev, &edata);
1525
1526 if (rc)
1527 return rc;
1528
1529 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1530 return -EFAULT;
1531
1532 return 0;
1533}
1534
1535static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1536{
1537 struct ethtool_eee edata;
1538
1539 if (!dev->ethtool_ops->set_eee)
1540 return -EOPNOTSUPP;
1541
1542 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1543 return -EFAULT;
1544
1545 return dev->ethtool_ops->set_eee(dev, &edata);
1546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548static int ethtool_nway_reset(struct net_device *dev)
1549{
1550 if (!dev->ethtool_ops->nway_reset)
1551 return -EOPNOTSUPP;
1552
1553 return dev->ethtool_ops->nway_reset(dev);
1554}
1555
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001556static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1557{
1558 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1559
1560 if (!dev->ethtool_ops->get_link)
1561 return -EOPNOTSUPP;
1562
1563 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1564
1565 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1566 return -EFAULT;
1567 return 0;
1568}
1569
Ben Hutchings081d0942012-04-12 00:42:12 +00001570static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1571 int (*getter)(struct net_device *,
1572 struct ethtool_eeprom *, u8 *),
1573 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001576 void __user *userbuf = useraddr + sizeof(eeprom);
1577 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001579 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1582 return -EFAULT;
1583
1584 /* Check for wrap and zero */
1585 if (eeprom.offset + eeprom.len <= eeprom.offset)
1586 return -EINVAL;
1587
1588 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001589 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return -EINVAL;
1591
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001592 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (!data)
1594 return -ENOMEM;
1595
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001596 bytes_remaining = eeprom.len;
1597 while (bytes_remaining > 0) {
1598 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Ben Hutchings081d0942012-04-12 00:42:12 +00001600 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001601 if (ret)
1602 break;
1603 if (copy_to_user(userbuf, data, eeprom.len)) {
1604 ret = -EFAULT;
1605 break;
1606 }
1607 userbuf += eeprom.len;
1608 eeprom.offset += eeprom.len;
1609 bytes_remaining -= eeprom.len;
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001612 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1613 eeprom.offset -= eeprom.len;
1614 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1615 ret = -EFAULT;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 kfree(data);
1618 return ret;
1619}
1620
Ben Hutchings081d0942012-04-12 00:42:12 +00001621static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1622{
1623 const struct ethtool_ops *ops = dev->ethtool_ops;
1624
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001625 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1626 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001627 return -EOPNOTSUPP;
1628
1629 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1630 ops->get_eeprom_len(dev));
1631}
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1634{
1635 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001636 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001637 void __user *userbuf = useraddr + sizeof(eeprom);
1638 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001640 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001642 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1643 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return -EOPNOTSUPP;
1645
1646 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1647 return -EFAULT;
1648
1649 /* Check for wrap and zero */
1650 if (eeprom.offset + eeprom.len <= eeprom.offset)
1651 return -EINVAL;
1652
1653 /* Check for exceeding total eeprom len */
1654 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1655 return -EINVAL;
1656
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001657 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (!data)
1659 return -ENOMEM;
1660
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001661 bytes_remaining = eeprom.len;
1662 while (bytes_remaining > 0) {
1663 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001665 if (copy_from_user(data, userbuf, eeprom.len)) {
1666 ret = -EFAULT;
1667 break;
1668 }
1669 ret = ops->set_eeprom(dev, &eeprom, data);
1670 if (ret)
1671 break;
1672 userbuf += eeprom.len;
1673 eeprom.offset += eeprom.len;
1674 bytes_remaining -= eeprom.len;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 kfree(data);
1678 return ret;
1679}
1680
chavey97f8aef2010-04-07 21:54:42 -07001681static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1682 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Roland Dreier8e557422010-02-11 12:14:23 -08001684 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 if (!dev->ethtool_ops->get_coalesce)
1687 return -EOPNOTSUPP;
1688
1689 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1690
1691 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1692 return -EFAULT;
1693 return 0;
1694}
1695
chavey97f8aef2010-04-07 21:54:42 -07001696static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1697 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct ethtool_coalesce coalesce;
1700
David S. Millerfa04ae52005-06-06 15:07:19 -07001701 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return -EOPNOTSUPP;
1703
1704 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1705 return -EFAULT;
1706
1707 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1708}
1709
1710static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1711{
Roland Dreier8e557422010-02-11 12:14:23 -08001712 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 if (!dev->ethtool_ops->get_ringparam)
1715 return -EOPNOTSUPP;
1716
1717 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1718
1719 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1720 return -EFAULT;
1721 return 0;
1722}
1723
1724static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1725{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001726 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001728 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return -EOPNOTSUPP;
1730
1731 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1732 return -EFAULT;
1733
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001734 dev->ethtool_ops->get_ringparam(dev, &max);
1735
1736 /* ensure new ring parameters are within the maximums */
1737 if (ringparam.rx_pending > max.rx_max_pending ||
1738 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1739 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1740 ringparam.tx_pending > max.tx_max_pending)
1741 return -EINVAL;
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1744}
1745
amit salecha8b5933c2011-04-07 01:58:42 +00001746static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1747 void __user *useraddr)
1748{
1749 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1750
1751 if (!dev->ethtool_ops->get_channels)
1752 return -EOPNOTSUPP;
1753
1754 dev->ethtool_ops->get_channels(dev, &channels);
1755
1756 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1757 return -EFAULT;
1758 return 0;
1759}
1760
1761static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1762 void __user *useraddr)
1763{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001764 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001765 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001766
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001767 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001768 return -EOPNOTSUPP;
1769
1770 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1771 return -EFAULT;
1772
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001773 dev->ethtool_ops->get_channels(dev, &max);
1774
1775 /* ensure new counts are within the maximums */
1776 if ((channels.rx_count > max.max_rx) ||
1777 (channels.tx_count > max.max_tx) ||
1778 (channels.combined_count > max.max_combined) ||
1779 (channels.other_count > max.max_other))
1780 return -EINVAL;
1781
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001782 /* ensure the new Rx count fits within the configured Rx flow
1783 * indirection table settings */
1784 if (netif_is_rxfh_configured(dev) &&
1785 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1786 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1787 return -EINVAL;
1788
amit salecha8b5933c2011-04-07 01:58:42 +00001789 return dev->ethtool_ops->set_channels(dev, &channels);
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1793{
1794 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1795
1796 if (!dev->ethtool_ops->get_pauseparam)
1797 return -EOPNOTSUPP;
1798
1799 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1800
1801 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1802 return -EFAULT;
1803 return 0;
1804}
1805
1806static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1807{
1808 struct ethtool_pauseparam pauseparam;
1809
Jeff Garzike1b90c42006-07-17 12:54:40 -04001810 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return -EOPNOTSUPP;
1812
1813 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1814 return -EFAULT;
1815
1816 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1820{
1821 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001822 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001824 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001826 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001827 return -EOPNOTSUPP;
1828
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001829 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001830 if (test_len < 0)
1831 return test_len;
1832 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 if (copy_from_user(&test, useraddr, sizeof(test)))
1835 return -EFAULT;
1836
Jeff Garzikff03d492007-08-15 16:01:08 -07001837 test.len = test_len;
1838 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (!data)
1840 return -ENOMEM;
1841
1842 ops->self_test(dev, &test, data);
1843
1844 ret = -EFAULT;
1845 if (copy_to_user(useraddr, &test, sizeof(test)))
1846 goto out;
1847 useraddr += sizeof(test);
1848 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1849 goto out;
1850 ret = 0;
1851
1852 out:
1853 kfree(data);
1854 return ret;
1855}
1856
1857static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1858{
1859 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 u8 *data;
1861 int ret;
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1864 return -EFAULT;
1865
Michał Mirosław340ae162011-02-15 16:59:16 +00001866 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001867 if (ret < 0)
1868 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001869 if (ret > S32_MAX / ETH_GSTRING_LEN)
1870 return -ENOMEM;
1871 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001872
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001873 gstrings.len = ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001874 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1875 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return -ENOMEM;
1877
Michał Mirosław340ae162011-02-15 16:59:16 +00001878 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 ret = -EFAULT;
1881 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1882 goto out;
1883 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001884 if (gstrings.len &&
1885 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 goto out;
1887 ret = 0;
1888
Michał Mirosław340ae162011-02-15 16:59:16 +00001889out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001890 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return ret;
1892}
1893
1894static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1895{
1896 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001897 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001898 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001899 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001901 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return -EOPNOTSUPP;
1903
Ben Hutchings68f512f2011-04-02 00:35:15 +01001904 if (busy)
1905 return -EBUSY;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (copy_from_user(&id, useraddr, sizeof(id)))
1908 return -EFAULT;
1909
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001910 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001911 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001912 return rc;
1913
1914 /* Drop the RTNL lock while waiting, but prevent reentry or
1915 * removal of the device.
1916 */
1917 busy = true;
1918 dev_hold(dev);
1919 rtnl_unlock();
1920
1921 if (rc == 0) {
1922 /* Driver will handle this itself */
1923 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001924 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001925 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001926 /* Driver expects to be called at twice the frequency in rc */
1927 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001928
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001929 /* Count down seconds */
1930 do {
1931 /* Count down iterations per second */
1932 i = n;
1933 do {
1934 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001935 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001936 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1937 rtnl_unlock();
1938 if (rc)
1939 break;
1940 schedule_timeout_interruptible(interval);
1941 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001942 } while (!signal_pending(current) &&
1943 (id.data == 0 || --id.data != 0));
1944 }
1945
1946 rtnl_lock();
1947 dev_put(dev);
1948 busy = false;
1949
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001950 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001951 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
1954static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1955{
1956 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001957 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001959 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001961 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001962 return -EOPNOTSUPP;
1963
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001964 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001965 if (n_stats < 0)
1966 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001967 if (n_stats > S32_MAX / sizeof(u64))
1968 return -ENOMEM;
1969 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1971 return -EFAULT;
1972
Jeff Garzikff03d492007-08-15 16:01:08 -07001973 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001974 data = vzalloc(n_stats * sizeof(u64));
1975 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return -ENOMEM;
1977
1978 ops->get_ethtool_stats(dev, &stats, data);
1979
1980 ret = -EFAULT;
1981 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1982 goto out;
1983 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001984 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 goto out;
1986 ret = 0;
1987
1988 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001989 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return ret;
1991}
1992
Andrew Lunnf3a40942015-12-30 16:28:25 +01001993static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1994{
1995 struct ethtool_stats stats;
1996 struct phy_device *phydev = dev->phydev;
1997 u64 *data;
1998 int ret, n_stats;
1999
2000 if (!phydev)
2001 return -EOPNOTSUPP;
2002
2003 n_stats = phy_get_sset_count(phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002004 if (n_stats < 0)
2005 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002006 if (n_stats > S32_MAX / sizeof(u64))
2007 return -ENOMEM;
2008 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002009
2010 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2011 return -EFAULT;
2012
2013 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002014 data = vzalloc(n_stats * sizeof(u64));
2015 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01002016 return -ENOMEM;
2017
2018 mutex_lock(&phydev->lock);
2019 phydev->drv->get_stats(phydev, &stats, data);
2020 mutex_unlock(&phydev->lock);
2021
2022 ret = -EFAULT;
2023 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2024 goto out;
2025 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002026 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002027 goto out;
2028 ret = 0;
2029
2030 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002031 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002032 return ret;
2033}
2034
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002035static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002036{
2037 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002038
Matthew Wilcox313674a2007-07-31 14:00:29 -07002039 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002040 return -EFAULT;
2041
Matthew Wilcox313674a2007-07-31 14:00:29 -07002042 if (epaddr.size < dev->addr_len)
2043 return -ETOOSMALL;
2044 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002045
Jon Wetzela6f9a702005-08-20 17:15:54 -07002046 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002047 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002048 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002049 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2050 return -EFAULT;
2051 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002052}
2053
Jeff Garzik13c99b22007-08-15 16:01:56 -07002054static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2055 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002056{
Roland Dreier8e557422010-02-11 12:14:23 -08002057 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002058
Jeff Garzik13c99b22007-08-15 16:01:56 -07002059 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002060 return -EOPNOTSUPP;
2061
Jeff Garzik13c99b22007-08-15 16:01:56 -07002062 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002063
2064 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2065 return -EFAULT;
2066 return 0;
2067}
2068
Jeff Garzik13c99b22007-08-15 16:01:56 -07002069static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2070 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002071{
2072 struct ethtool_value edata;
2073
Jeff Garzik13c99b22007-08-15 16:01:56 -07002074 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002075 return -EOPNOTSUPP;
2076
2077 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2078 return -EFAULT;
2079
Jeff Garzik13c99b22007-08-15 16:01:56 -07002080 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002081 return 0;
2082}
2083
Jeff Garzik13c99b22007-08-15 16:01:56 -07002084static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2085 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002086{
2087 struct ethtool_value edata;
2088
Jeff Garzik13c99b22007-08-15 16:01:56 -07002089 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002090 return -EOPNOTSUPP;
2091
2092 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2093 return -EFAULT;
2094
Jeff Garzik13c99b22007-08-15 16:01:56 -07002095 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002096}
2097
chavey97f8aef2010-04-07 21:54:42 -07002098static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2099 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002100{
2101 struct ethtool_flash efl;
2102
2103 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2104 return -EFAULT;
2105
2106 if (!dev->ethtool_ops->flash_device)
2107 return -EOPNOTSUPP;
2108
Ben Hutchings786f5282012-02-01 09:32:25 +00002109 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2110
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002111 return dev->ethtool_ops->flash_device(dev, &efl);
2112}
2113
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002114static int ethtool_set_dump(struct net_device *dev,
2115 void __user *useraddr)
2116{
2117 struct ethtool_dump dump;
2118
2119 if (!dev->ethtool_ops->set_dump)
2120 return -EOPNOTSUPP;
2121
2122 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2123 return -EFAULT;
2124
2125 return dev->ethtool_ops->set_dump(dev, &dump);
2126}
2127
2128static int ethtool_get_dump_flag(struct net_device *dev,
2129 void __user *useraddr)
2130{
2131 int ret;
2132 struct ethtool_dump dump;
2133 const struct ethtool_ops *ops = dev->ethtool_ops;
2134
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002135 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002136 return -EOPNOTSUPP;
2137
2138 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2139 return -EFAULT;
2140
2141 ret = ops->get_dump_flag(dev, &dump);
2142 if (ret)
2143 return ret;
2144
2145 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2146 return -EFAULT;
2147 return 0;
2148}
2149
2150static int ethtool_get_dump_data(struct net_device *dev,
2151 void __user *useraddr)
2152{
2153 int ret;
2154 __u32 len;
2155 struct ethtool_dump dump, tmp;
2156 const struct ethtool_ops *ops = dev->ethtool_ops;
2157 void *data = NULL;
2158
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002159 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002160 return -EOPNOTSUPP;
2161
2162 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2163 return -EFAULT;
2164
2165 memset(&tmp, 0, sizeof(tmp));
2166 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2167 ret = ops->get_dump_flag(dev, &tmp);
2168 if (ret)
2169 return ret;
2170
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002171 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002172 if (!len)
2173 return -EFAULT;
2174
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002175 /* Don't ever let the driver think there's more space available
2176 * than it requested with .get_dump_flag().
2177 */
2178 dump.len = len;
2179
2180 /* Always allocate enough space to hold the whole thing so that the
2181 * driver does not need to check the length and bother with partial
2182 * dumping.
2183 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002184 data = vzalloc(tmp.len);
2185 if (!data)
2186 return -ENOMEM;
2187 ret = ops->get_dump_data(dev, &dump, data);
2188 if (ret)
2189 goto out;
2190
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002191 /* There are two sane possibilities:
2192 * 1. The driver's .get_dump_data() does not touch dump.len.
2193 * 2. Or it may set dump.len to how much it really writes, which
2194 * should be tmp.len (or len if it can do a partial dump).
2195 * In any case respond to userspace with the actual length of data
2196 * it's receiving.
2197 */
2198 WARN_ON(dump.len != len && dump.len != tmp.len);
2199 dump.len = len;
2200
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002201 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2202 ret = -EFAULT;
2203 goto out;
2204 }
2205 useraddr += offsetof(struct ethtool_dump, data);
2206 if (copy_to_user(useraddr, data, len))
2207 ret = -EFAULT;
2208out:
2209 vfree(data);
2210 return ret;
2211}
2212
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002213static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2214{
2215 int err = 0;
2216 struct ethtool_ts_info info;
2217 const struct ethtool_ops *ops = dev->ethtool_ops;
2218 struct phy_device *phydev = dev->phydev;
2219
2220 memset(&info, 0, sizeof(info));
2221 info.cmd = ETHTOOL_GET_TS_INFO;
2222
2223 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002224 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002225 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002226 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002227 } else {
2228 info.so_timestamping =
2229 SOF_TIMESTAMPING_RX_SOFTWARE |
2230 SOF_TIMESTAMPING_SOFTWARE;
2231 info.phc_index = -1;
2232 }
2233
2234 if (err)
2235 return err;
2236
2237 if (copy_to_user(useraddr, &info, sizeof(info)))
2238 err = -EFAULT;
2239
2240 return err;
2241}
2242
Ed Swierk2f438362015-01-02 17:27:56 -08002243static int __ethtool_get_module_info(struct net_device *dev,
2244 struct ethtool_modinfo *modinfo)
2245{
2246 const struct ethtool_ops *ops = dev->ethtool_ops;
2247 struct phy_device *phydev = dev->phydev;
2248
Russell Kinge679c9c2018-03-28 15:44:16 -07002249 if (dev->sfp_bus)
2250 return sfp_get_module_info(dev->sfp_bus, modinfo);
2251
Ed Swierk2f438362015-01-02 17:27:56 -08002252 if (phydev && phydev->drv && phydev->drv->module_info)
2253 return phydev->drv->module_info(phydev, modinfo);
2254
2255 if (ops->get_module_info)
2256 return ops->get_module_info(dev, modinfo);
2257
2258 return -EOPNOTSUPP;
2259}
2260
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002261static int ethtool_get_module_info(struct net_device *dev,
2262 void __user *useraddr)
2263{
2264 int ret;
2265 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002266
2267 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2268 return -EFAULT;
2269
Ed Swierk2f438362015-01-02 17:27:56 -08002270 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002271 if (ret)
2272 return ret;
2273
2274 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2275 return -EFAULT;
2276
2277 return 0;
2278}
2279
Ed Swierk2f438362015-01-02 17:27:56 -08002280static int __ethtool_get_module_eeprom(struct net_device *dev,
2281 struct ethtool_eeprom *ee, u8 *data)
2282{
2283 const struct ethtool_ops *ops = dev->ethtool_ops;
2284 struct phy_device *phydev = dev->phydev;
2285
Russell Kinge679c9c2018-03-28 15:44:16 -07002286 if (dev->sfp_bus)
2287 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2288
Ed Swierk2f438362015-01-02 17:27:56 -08002289 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2290 return phydev->drv->module_eeprom(phydev, ee, data);
2291
2292 if (ops->get_module_eeprom)
2293 return ops->get_module_eeprom(dev, ee, data);
2294
2295 return -EOPNOTSUPP;
2296}
2297
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002298static int ethtool_get_module_eeprom(struct net_device *dev,
2299 void __user *useraddr)
2300{
2301 int ret;
2302 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002303
Ed Swierk2f438362015-01-02 17:27:56 -08002304 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002305 if (ret)
2306 return ret;
2307
Ed Swierk2f438362015-01-02 17:27:56 -08002308 return ethtool_get_any_eeprom(dev, useraddr,
2309 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002310 modinfo.eeprom_len);
2311}
2312
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302313static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2314{
2315 switch (tuna->id) {
2316 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002317 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302318 if (tuna->len != sizeof(u32) ||
2319 tuna->type_id != ETHTOOL_TUNABLE_U32)
2320 return -EINVAL;
2321 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002322 case ETHTOOL_PFC_PREVENTION_TOUT:
2323 if (tuna->len != sizeof(u16) ||
2324 tuna->type_id != ETHTOOL_TUNABLE_U16)
2325 return -EINVAL;
2326 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302327 default:
2328 return -EINVAL;
2329 }
2330
2331 return 0;
2332}
2333
2334static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2335{
2336 int ret;
2337 struct ethtool_tunable tuna;
2338 const struct ethtool_ops *ops = dev->ethtool_ops;
2339 void *data;
2340
2341 if (!ops->get_tunable)
2342 return -EOPNOTSUPP;
2343 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2344 return -EFAULT;
2345 ret = ethtool_tunable_valid(&tuna);
2346 if (ret)
2347 return ret;
2348 data = kmalloc(tuna.len, GFP_USER);
2349 if (!data)
2350 return -ENOMEM;
2351 ret = ops->get_tunable(dev, &tuna, data);
2352 if (ret)
2353 goto out;
2354 useraddr += sizeof(tuna);
2355 ret = -EFAULT;
2356 if (copy_to_user(useraddr, data, tuna.len))
2357 goto out;
2358 ret = 0;
2359
2360out:
2361 kfree(data);
2362 return ret;
2363}
2364
2365static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2366{
2367 int ret;
2368 struct ethtool_tunable tuna;
2369 const struct ethtool_ops *ops = dev->ethtool_ops;
2370 void *data;
2371
2372 if (!ops->set_tunable)
2373 return -EOPNOTSUPP;
2374 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2375 return -EFAULT;
2376 ret = ethtool_tunable_valid(&tuna);
2377 if (ret)
2378 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302379 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002380 data = memdup_user(useraddr, tuna.len);
2381 if (IS_ERR(data))
2382 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302383 ret = ops->set_tunable(dev, &tuna, data);
2384
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302385 kfree(data);
2386 return ret;
2387}
2388
Kan Liang421797b2016-02-19 09:24:02 -05002389static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2390 void __user *useraddr,
2391 struct ethtool_per_queue_op *per_queue_opt)
2392{
2393 u32 bit;
2394 int ret;
2395 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2396
2397 if (!dev->ethtool_ops->get_per_queue_coalesce)
2398 return -EOPNOTSUPP;
2399
2400 useraddr += sizeof(*per_queue_opt);
2401
Yury Norov3aa56882018-02-06 15:38:06 -08002402 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2403 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002404
2405 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2406 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2407
2408 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2409 if (ret != 0)
2410 return ret;
2411 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2412 return -EFAULT;
2413 useraddr += sizeof(coalesce);
2414 }
2415
2416 return 0;
2417}
2418
Kan Liangf38d1382016-02-19 09:24:03 -05002419static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2420 void __user *useraddr,
2421 struct ethtool_per_queue_op *per_queue_opt)
2422{
2423 u32 bit;
2424 int i, ret = 0;
2425 int n_queue;
2426 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2427 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2428
2429 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2430 (!dev->ethtool_ops->get_per_queue_coalesce))
2431 return -EOPNOTSUPP;
2432
2433 useraddr += sizeof(*per_queue_opt);
2434
Yury Norov3aa56882018-02-06 15:38:06 -08002435 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002436 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2437 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2438 if (!backup)
2439 return -ENOMEM;
2440
2441 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2442 struct ethtool_coalesce coalesce;
2443
2444 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2445 if (ret != 0)
2446 goto roll_back;
2447
2448 tmp++;
2449
2450 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2451 ret = -EFAULT;
2452 goto roll_back;
2453 }
2454
2455 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2456 if (ret != 0)
2457 goto roll_back;
2458
2459 useraddr += sizeof(coalesce);
2460 }
2461
2462roll_back:
2463 if (ret != 0) {
2464 tmp = backup;
2465 for_each_set_bit(i, queue_mask, bit) {
2466 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2467 tmp++;
2468 }
2469 }
2470 kfree(backup);
2471
2472 return ret;
2473}
2474
Kan Liangac2c7ad2016-02-19 09:24:01 -05002475static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2476{
2477 struct ethtool_per_queue_op per_queue_opt;
2478
2479 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2480 return -EFAULT;
2481
2482 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002483 case ETHTOOL_GCOALESCE:
2484 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002485 case ETHTOOL_SCOALESCE:
2486 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002487 default:
2488 return -EOPNOTSUPP;
2489 };
2490}
2491
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002492static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2493{
2494 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002495 case ETHTOOL_PHY_DOWNSHIFT:
2496 if (tuna->len != sizeof(u8) ||
2497 tuna->type_id != ETHTOOL_TUNABLE_U8)
2498 return -EINVAL;
2499 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002500 default:
2501 return -EINVAL;
2502 }
2503
2504 return 0;
2505}
2506
2507static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2508{
2509 int ret;
2510 struct ethtool_tunable tuna;
2511 struct phy_device *phydev = dev->phydev;
2512 void *data;
2513
2514 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2515 return -EOPNOTSUPP;
2516
2517 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2518 return -EFAULT;
2519 ret = ethtool_phy_tunable_valid(&tuna);
2520 if (ret)
2521 return ret;
2522 data = kmalloc(tuna.len, GFP_USER);
2523 if (!data)
2524 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002525 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002526 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002527 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002528 if (ret)
2529 goto out;
2530 useraddr += sizeof(tuna);
2531 ret = -EFAULT;
2532 if (copy_to_user(useraddr, data, tuna.len))
2533 goto out;
2534 ret = 0;
2535
2536out:
2537 kfree(data);
2538 return ret;
2539}
2540
2541static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2542{
2543 int ret;
2544 struct ethtool_tunable tuna;
2545 struct phy_device *phydev = dev->phydev;
2546 void *data;
2547
2548 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2549 return -EOPNOTSUPP;
2550 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2551 return -EFAULT;
2552 ret = ethtool_phy_tunable_valid(&tuna);
2553 if (ret)
2554 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002555 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002556 data = memdup_user(useraddr, tuna.len);
2557 if (IS_ERR(data))
2558 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002559 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002560 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002561 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002562
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002563 kfree(data);
2564 return ret;
2565}
2566
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002567static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2568{
2569 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002570 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002571
2572 if (!dev->ethtool_ops->get_fecparam)
2573 return -EOPNOTSUPP;
2574
Edward Creea6d50512018-02-28 19:15:58 +00002575 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2576 if (rc)
2577 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002578
2579 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2580 return -EFAULT;
2581 return 0;
2582}
2583
2584static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2585{
2586 struct ethtool_fecparam fecparam;
2587
2588 if (!dev->ethtool_ops->set_fecparam)
2589 return -EOPNOTSUPP;
2590
2591 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2592 return -EFAULT;
2593
2594 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2595}
2596
Yan Burman600fed52013-06-03 02:03:34 +00002597/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Eric W. Biederman881d9662007-09-17 11:56:21 -07002599int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002601 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002603 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002605 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (!dev || !netif_device_present(dev))
2608 return -ENODEV;
2609
chavey97f8aef2010-04-07 21:54:42 -07002610 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return -EFAULT;
2612
Kan Liangac2c7ad2016-02-19 09:24:01 -05002613 if (ethcmd == ETHTOOL_PERQUEUE) {
2614 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2615 return -EFAULT;
2616 } else {
2617 sub_cmd = ethcmd;
2618 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002619 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002620 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002621 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002622 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002623 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002624 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002625 case ETHTOOL_GCOALESCE:
2626 case ETHTOOL_GRINGPARAM:
2627 case ETHTOOL_GPAUSEPARAM:
2628 case ETHTOOL_GRXCSUM:
2629 case ETHTOOL_GTXCSUM:
2630 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002631 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002632 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002633 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002634 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002635 case ETHTOOL_GTSO:
2636 case ETHTOOL_GPERMADDR:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002637 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002638 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002639 case ETHTOOL_GFLAGS:
2640 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002641 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002642 case ETHTOOL_GRXRINGS:
2643 case ETHTOOL_GRXCLSRLCNT:
2644 case ETHTOOL_GRXCLSRULE:
2645 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002646 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302647 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002648 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002649 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002650 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002651 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302652 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002653 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002654 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002655 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002656 break;
2657 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002658 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002659 return -EPERM;
2660 }
2661
chavey97f8aef2010-04-07 21:54:42 -07002662 if (dev->ethtool_ops->begin) {
2663 rc = dev->ethtool_ops->begin(dev);
2664 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002666 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002667 old_features = dev->features;
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 switch (ethcmd) {
2670 case ETHTOOL_GSET:
2671 rc = ethtool_get_settings(dev, useraddr);
2672 break;
2673 case ETHTOOL_SSET:
2674 rc = ethtool_set_settings(dev, useraddr);
2675 break;
2676 case ETHTOOL_GDRVINFO:
2677 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 break;
2679 case ETHTOOL_GREGS:
2680 rc = ethtool_get_regs(dev, useraddr);
2681 break;
2682 case ETHTOOL_GWOL:
2683 rc = ethtool_get_wol(dev, useraddr);
2684 break;
2685 case ETHTOOL_SWOL:
2686 rc = ethtool_set_wol(dev, useraddr);
2687 break;
2688 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002689 rc = ethtool_get_value(dev, useraddr, ethcmd,
2690 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 break;
2692 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002693 rc = ethtool_set_value_void(dev, useraddr,
2694 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002696 case ETHTOOL_GEEE:
2697 rc = ethtool_get_eee(dev, useraddr);
2698 break;
2699 case ETHTOOL_SEEE:
2700 rc = ethtool_set_eee(dev, useraddr);
2701 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 case ETHTOOL_NWAY_RST:
2703 rc = ethtool_nway_reset(dev);
2704 break;
2705 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002706 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
2708 case ETHTOOL_GEEPROM:
2709 rc = ethtool_get_eeprom(dev, useraddr);
2710 break;
2711 case ETHTOOL_SEEPROM:
2712 rc = ethtool_set_eeprom(dev, useraddr);
2713 break;
2714 case ETHTOOL_GCOALESCE:
2715 rc = ethtool_get_coalesce(dev, useraddr);
2716 break;
2717 case ETHTOOL_SCOALESCE:
2718 rc = ethtool_set_coalesce(dev, useraddr);
2719 break;
2720 case ETHTOOL_GRINGPARAM:
2721 rc = ethtool_get_ringparam(dev, useraddr);
2722 break;
2723 case ETHTOOL_SRINGPARAM:
2724 rc = ethtool_set_ringparam(dev, useraddr);
2725 break;
2726 case ETHTOOL_GPAUSEPARAM:
2727 rc = ethtool_get_pauseparam(dev, useraddr);
2728 break;
2729 case ETHTOOL_SPAUSEPARAM:
2730 rc = ethtool_set_pauseparam(dev, useraddr);
2731 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 case ETHTOOL_TEST:
2733 rc = ethtool_self_test(dev, useraddr);
2734 break;
2735 case ETHTOOL_GSTRINGS:
2736 rc = ethtool_get_strings(dev, useraddr);
2737 break;
2738 case ETHTOOL_PHYS_ID:
2739 rc = ethtool_phys_id(dev, useraddr);
2740 break;
2741 case ETHTOOL_GSTATS:
2742 rc = ethtool_get_stats(dev, useraddr);
2743 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002744 case ETHTOOL_GPERMADDR:
2745 rc = ethtool_get_perm_addr(dev, useraddr);
2746 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002747 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002748 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002749 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002750 break;
2751 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002752 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002753 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002754 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002755 rc = ethtool_get_value(dev, useraddr, ethcmd,
2756 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002757 break;
2758 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002759 rc = ethtool_set_value(dev, useraddr,
2760 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002761 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002762 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002763 case ETHTOOL_GRXRINGS:
2764 case ETHTOOL_GRXCLSRLCNT:
2765 case ETHTOOL_GRXCLSRULE:
2766 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002767 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002768 break;
2769 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002770 case ETHTOOL_SRXCLSRLDEL:
2771 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002772 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002773 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002774 case ETHTOOL_FLASHDEV:
2775 rc = ethtool_flash_device(dev, useraddr);
2776 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002777 case ETHTOOL_RESET:
2778 rc = ethtool_reset(dev, useraddr);
2779 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002780 case ETHTOOL_GSSET_INFO:
2781 rc = ethtool_get_sset_info(dev, useraddr);
2782 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002783 case ETHTOOL_GRXFHINDIR:
2784 rc = ethtool_get_rxfh_indir(dev, useraddr);
2785 break;
2786 case ETHTOOL_SRXFHINDIR:
2787 rc = ethtool_set_rxfh_indir(dev, useraddr);
2788 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302789 case ETHTOOL_GRSSH:
2790 rc = ethtool_get_rxfh(dev, useraddr);
2791 break;
2792 case ETHTOOL_SRSSH:
2793 rc = ethtool_set_rxfh(dev, useraddr);
2794 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002795 case ETHTOOL_GFEATURES:
2796 rc = ethtool_get_features(dev, useraddr);
2797 break;
2798 case ETHTOOL_SFEATURES:
2799 rc = ethtool_set_features(dev, useraddr);
2800 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002801 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002802 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002803 case ETHTOOL_GSG:
2804 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002805 case ETHTOOL_GGSO:
2806 case ETHTOOL_GGRO:
2807 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2808 break;
2809 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002810 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002811 case ETHTOOL_SSG:
2812 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002813 case ETHTOOL_SGSO:
2814 case ETHTOOL_SGRO:
2815 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2816 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002817 case ETHTOOL_GCHANNELS:
2818 rc = ethtool_get_channels(dev, useraddr);
2819 break;
2820 case ETHTOOL_SCHANNELS:
2821 rc = ethtool_set_channels(dev, useraddr);
2822 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002823 case ETHTOOL_SET_DUMP:
2824 rc = ethtool_set_dump(dev, useraddr);
2825 break;
2826 case ETHTOOL_GET_DUMP_FLAG:
2827 rc = ethtool_get_dump_flag(dev, useraddr);
2828 break;
2829 case ETHTOOL_GET_DUMP_DATA:
2830 rc = ethtool_get_dump_data(dev, useraddr);
2831 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002832 case ETHTOOL_GET_TS_INFO:
2833 rc = ethtool_get_ts_info(dev, useraddr);
2834 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002835 case ETHTOOL_GMODULEINFO:
2836 rc = ethtool_get_module_info(dev, useraddr);
2837 break;
2838 case ETHTOOL_GMODULEEEPROM:
2839 rc = ethtool_get_module_eeprom(dev, useraddr);
2840 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302841 case ETHTOOL_GTUNABLE:
2842 rc = ethtool_get_tunable(dev, useraddr);
2843 break;
2844 case ETHTOOL_STUNABLE:
2845 rc = ethtool_set_tunable(dev, useraddr);
2846 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002847 case ETHTOOL_GPHYSTATS:
2848 rc = ethtool_get_phy_stats(dev, useraddr);
2849 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002850 case ETHTOOL_PERQUEUE:
2851 rc = ethtool_set_per_queue(dev, useraddr);
2852 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002853 case ETHTOOL_GLINKSETTINGS:
2854 rc = ethtool_get_link_ksettings(dev, useraddr);
2855 break;
2856 case ETHTOOL_SLINKSETTINGS:
2857 rc = ethtool_set_link_ksettings(dev, useraddr);
2858 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002859 case ETHTOOL_PHY_GTUNABLE:
2860 rc = get_phy_tunable(dev, useraddr);
2861 break;
2862 case ETHTOOL_PHY_STUNABLE:
2863 rc = set_phy_tunable(dev, useraddr);
2864 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002865 case ETHTOOL_GFECPARAM:
2866 rc = ethtool_get_fecparam(dev, useraddr);
2867 break;
2868 case ETHTOOL_SFECPARAM:
2869 rc = ethtool_set_fecparam(dev, useraddr);
2870 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002872 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002874
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002875 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002877
2878 if (old_features != dev->features)
2879 netdev_features_change(dev);
2880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}