blob: b849fdae7e87ec517ef2a8d72c161564590a58a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4 *
5 * This file is where we call all the ethtool_ops commands to get
Matthew Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000020#include <linux/net_tstamp.h>
21#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000022#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070023#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070024#include <linux/vmalloc.h>
Russell Kinge679c9c2018-03-28 15:44:16 -070025#include <linux/sfp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010027#include <linux/rtnetlink.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010028#include <linux/sched/signal.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080029#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090031/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Some useful ethtool_ops methods that're device independent.
33 * If we find that all drivers want to do the same thing here,
34 * we can turn these into dev_() function calls.
35 */
36
37u32 ethtool_op_get_link(struct net_device *dev)
38{
39 return netif_carrier_ok(dev) ? 1 : 0;
40}
chavey97f8aef2010-04-07 21:54:42 -070041EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Richard Cochran02eacbd2012-04-03 22:59:22 +000043int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
44{
45 info->so_timestamping =
46 SOF_TIMESTAMPING_TX_SOFTWARE |
47 SOF_TIMESTAMPING_RX_SOFTWARE |
48 SOF_TIMESTAMPING_SOFTWARE;
49 info->phc_index = -1;
50 return 0;
51}
52EXPORT_SYMBOL(ethtool_op_get_ts_info);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Handlers for each ethtool command */
55
Michał Mirosław475414f2011-11-15 15:29:55 +000056#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000057
Michał Mirosław9d921542011-11-15 15:29:55 +000058static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
59 [NETIF_F_SG_BIT] = "tx-scatter-gather",
60 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000061 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
62 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
63 [NETIF_F_HIGHDMA_BIT] = "highdma",
64 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090065 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000066
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090067 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
68 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040069 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
70 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
71 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000072 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
73 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
74 [NETIF_F_LLTX_BIT] = "tx-lockless",
75 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
76 [NETIF_F_GRO_BIT] = "rx-gro",
Michael Chanfb1f5f72017-12-16 03:09:40 -050077 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
Michał Mirosław9d921542011-11-15 15:29:55 +000078 [NETIF_F_LRO_BIT] = "rx-lro",
79
80 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000081 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
82 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
Alexander Duyckcbc53e02016-04-10 21:44:51 -040083 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000084 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
85 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000086 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040087 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
Tom Herbert7e133182016-05-18 09:06:10 -070088 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
89 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000090 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040091 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
Alexander Duyck802ab552016-04-10 21:45:03 -040092 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030093 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +020094 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
Willem de Bruijn83aa0252018-04-26 13:42:21 -040095 [NETIF_F_GSO_UDP_L4_BIT] = "tx-udp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000096
97 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080098 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000099 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
100 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
101 [NETIF_F_RXHASH_BIT] = "rx-hashing",
102 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
103 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
104 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +0000105 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +0000106 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -0800107 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
John Fastabend1c78c642016-02-16 21:17:37 -0800108 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +0200109 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
110 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
Sabrina Dubrocad764a122017-07-21 12:49:28 +0200111 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
Atul Guptae0be6be2018-03-31 21:41:53 +0530112 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
Michał Mirosław9d921542011-11-15 15:29:55 +0000113};
114
Eyal Perry892311f2014-12-02 18:12:10 +0200115static const char
116rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
117 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
118 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800119 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200120};
121
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300122static const char
123tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
124 [ETHTOOL_ID_UNSPEC] = "Unspec",
125 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
126 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
Inbar Karmye1577c12017-11-20 16:14:30 +0200127 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300128};
129
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100130static const char
131phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
132 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100133 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100134};
135
Michał Mirosław5455c692011-02-15 16:59:17 +0000136static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
137{
138 struct ethtool_gfeatures cmd = {
139 .cmd = ETHTOOL_GFEATURES,
140 .size = ETHTOOL_DEV_FEATURE_WORDS,
141 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000142 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000143 u32 __user *sizeaddr;
144 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000145 int i;
146
147 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000148 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000149
150 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000151 features[i].available = (u32)(dev->hw_features >> (32 * i));
152 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
153 features[i].active = (u32)(dev->features >> (32 * i));
154 features[i].never_changed =
155 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000156 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000157
158 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
159 if (get_user(copy_size, sizeaddr))
160 return -EFAULT;
161
162 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
163 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
164
165 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
166 return -EFAULT;
167 useraddr += sizeof(cmd);
168 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
169 return -EFAULT;
170
171 return 0;
172}
173
174static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
175{
176 struct ethtool_sfeatures cmd;
177 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000178 netdev_features_t wanted = 0, valid = 0;
179 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000180
181 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
182 return -EFAULT;
183 useraddr += sizeof(cmd);
184
185 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
186 return -EINVAL;
187
188 if (copy_from_user(features, useraddr, sizeof(features)))
189 return -EFAULT;
190
Michał Mirosław475414f2011-11-15 15:29:55 +0000191 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000192 valid |= (netdev_features_t)features[i].valid << (32 * i);
193 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000194 }
195
196 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000197 return -EINVAL;
198
Michał Mirosław475414f2011-11-15 15:29:55 +0000199 if (valid & ~dev->hw_features) {
200 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000201 ret |= ETHTOOL_F_UNSUPPORTED;
202 }
203
Michał Mirosław475414f2011-11-15 15:29:55 +0000204 dev->wanted_features &= ~valid;
205 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700206 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000207
Michał Mirosław475414f2011-11-15 15:29:55 +0000208 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000209 ret |= ETHTOOL_F_WISH;
210
211 return ret;
212}
213
Michał Mirosław340ae162011-02-15 16:59:16 +0000214static int __ethtool_get_sset_count(struct net_device *dev, int sset)
215{
216 const struct ethtool_ops *ops = dev->ethtool_ops;
217
Michał Mirosław5455c692011-02-15 16:59:17 +0000218 if (sset == ETH_SS_FEATURES)
219 return ARRAY_SIZE(netdev_features_strings);
220
Eyal Perry892311f2014-12-02 18:12:10 +0200221 if (sset == ETH_SS_RSS_HASH_FUNCS)
222 return ARRAY_SIZE(rss_hash_func_strings);
223
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300224 if (sset == ETH_SS_TUNABLES)
225 return ARRAY_SIZE(tunable_strings);
226
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100227 if (sset == ETH_SS_PHY_TUNABLES)
228 return ARRAY_SIZE(phy_tunable_strings);
229
Florian Fainelli99943382018-04-25 12:12:48 -0700230 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
231 !ops->get_ethtool_phy_stats)
232 return phy_ethtool_get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100233
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000234 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000235 return ops->get_sset_count(dev, sset);
236 else
237 return -EOPNOTSUPP;
238}
239
240static void __ethtool_get_strings(struct net_device *dev,
241 u32 stringset, u8 *data)
242{
243 const struct ethtool_ops *ops = dev->ethtool_ops;
244
Michał Mirosław5455c692011-02-15 16:59:17 +0000245 if (stringset == ETH_SS_FEATURES)
246 memcpy(data, netdev_features_strings,
247 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200248 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
249 memcpy(data, rss_hash_func_strings,
250 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300251 else if (stringset == ETH_SS_TUNABLES)
252 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100253 else if (stringset == ETH_SS_PHY_TUNABLES)
254 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Florian Fainelli99943382018-04-25 12:12:48 -0700255 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
256 !ops->get_ethtool_phy_stats)
257 phy_ethtool_get_strings(dev->phydev, data);
258 else
Michał Mirosław5455c692011-02-15 16:59:17 +0000259 /* ops->get_strings is valid because checked earlier */
260 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000261}
262
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000263static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000264{
265 /* feature masks of legacy discrete ethtool ops */
266
267 switch (eth_cmd) {
268 case ETHTOOL_GTXCSUM:
269 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800270 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000271 case ETHTOOL_GRXCSUM:
272 case ETHTOOL_SRXCSUM:
273 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000274 case ETHTOOL_GSG:
275 case ETHTOOL_SSG:
276 return NETIF_F_SG;
277 case ETHTOOL_GTSO:
278 case ETHTOOL_STSO:
279 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000280 case ETHTOOL_GGSO:
281 case ETHTOOL_SGSO:
282 return NETIF_F_GSO;
283 case ETHTOOL_GGRO:
284 case ETHTOOL_SGRO:
285 return NETIF_F_GRO;
286 default:
287 BUG();
288 }
289}
290
Michał Mirosław0a417702011-02-15 16:59:17 +0000291static int ethtool_get_one_feature(struct net_device *dev,
292 char __user *useraddr, u32 ethcmd)
293{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000294 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000295 struct ethtool_value edata = {
296 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000297 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000298 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000299
Michał Mirosław0a417702011-02-15 16:59:17 +0000300 if (copy_to_user(useraddr, &edata, sizeof(edata)))
301 return -EFAULT;
302 return 0;
303}
304
Michał Mirosław0a417702011-02-15 16:59:17 +0000305static int ethtool_set_one_feature(struct net_device *dev,
306 void __user *useraddr, u32 ethcmd)
307{
308 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000309 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000310
311 if (copy_from_user(&edata, useraddr, sizeof(edata)))
312 return -EFAULT;
313
Michał Mirosław86794882011-02-15 16:59:17 +0000314 mask = ethtool_get_feature_mask(ethcmd);
315 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000316 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000317 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000318
319 if (edata.data)
320 dev->wanted_features |= mask;
321 else
322 dev->wanted_features &= ~mask;
323
324 __netdev_update_features(dev);
325
326 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000327}
328
Michał Mirosław02b3a552011-11-15 15:29:55 +0000329#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
330 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000331#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
332 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
333 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000334
335static u32 __ethtool_get_flags(struct net_device *dev)
336{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000337 u32 flags = 0;
338
Dragos Foianu8a731252013-07-13 14:43:00 +0100339 if (dev->features & NETIF_F_LRO)
340 flags |= ETH_FLAG_LRO;
341 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
342 flags |= ETH_FLAG_RXVLAN;
343 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
344 flags |= ETH_FLAG_TXVLAN;
345 if (dev->features & NETIF_F_NTUPLE)
346 flags |= ETH_FLAG_NTUPLE;
347 if (dev->features & NETIF_F_RXHASH)
348 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000349
350 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000351}
352
353static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000354{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000355 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000356
Michał Mirosław02b3a552011-11-15 15:29:55 +0000357 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000358 return -EINVAL;
359
Dragos Foianu8a731252013-07-13 14:43:00 +0100360 if (data & ETH_FLAG_LRO)
361 features |= NETIF_F_LRO;
362 if (data & ETH_FLAG_RXVLAN)
363 features |= NETIF_F_HW_VLAN_CTAG_RX;
364 if (data & ETH_FLAG_TXVLAN)
365 features |= NETIF_F_HW_VLAN_CTAG_TX;
366 if (data & ETH_FLAG_NTUPLE)
367 features |= NETIF_F_NTUPLE;
368 if (data & ETH_FLAG_RXHASH)
369 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000370
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000371 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000372 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000373 if (changed & ~dev->hw_features)
374 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
375
376 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000377 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000378
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700379 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000380
381 return 0;
382}
383
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700384/* Given two link masks, AND them together and save the result in dst. */
385void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
386 struct ethtool_link_ksettings *src)
387{
388 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
389 unsigned int idx = 0;
390
391 for (; idx < size; idx++) {
392 dst->link_modes.supported[idx] &=
393 src->link_modes.supported[idx];
394 dst->link_modes.advertising[idx] &=
395 src->link_modes.advertising[idx];
396 }
397}
398EXPORT_SYMBOL(ethtool_intersect_link_masks);
399
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200400void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
401 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800402{
403 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
404 dst[0] = legacy_u32;
405}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200406EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800407
408/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200409bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
410 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800411{
412 bool retval = true;
413
414 /* TODO: following test will soon always be true */
415 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
416 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
417
418 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
419 bitmap_fill(ext, 32);
420 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
421 if (bitmap_intersects(ext, src,
422 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
423 /* src mask goes beyond bit 31 */
424 retval = false;
425 }
426 }
427 *legacy_u32 = src[0];
428 return retval;
429}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200430EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800431
432/* return false if legacy contained non-0 deprecated fields
Niklas Söderlund95491e32017-10-20 01:32:08 +0200433 * maxtxpkt/maxrxpkt. rest of ksettings always updated
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800434 */
435static bool
436convert_legacy_settings_to_link_ksettings(
437 struct ethtool_link_ksettings *link_ksettings,
438 const struct ethtool_cmd *legacy_settings)
439{
440 bool retval = true;
441
442 memset(link_ksettings, 0, sizeof(*link_ksettings));
443
444 /* This is used to tell users that driver is still using these
445 * deprecated legacy fields, and they should not use
446 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
447 */
Niklas Söderlund95491e32017-10-20 01:32:08 +0200448 if (legacy_settings->maxtxpkt ||
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800449 legacy_settings->maxrxpkt)
450 retval = false;
451
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200452 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800453 link_ksettings->link_modes.supported,
454 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200455 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800456 link_ksettings->link_modes.advertising,
457 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200458 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800459 link_ksettings->link_modes.lp_advertising,
460 legacy_settings->lp_advertising);
461 link_ksettings->base.speed
462 = ethtool_cmd_speed(legacy_settings);
463 link_ksettings->base.duplex
464 = legacy_settings->duplex;
465 link_ksettings->base.port
466 = legacy_settings->port;
467 link_ksettings->base.phy_address
468 = legacy_settings->phy_address;
469 link_ksettings->base.autoneg
470 = legacy_settings->autoneg;
471 link_ksettings->base.mdio_support
472 = legacy_settings->mdio_support;
473 link_ksettings->base.eth_tp_mdix
474 = legacy_settings->eth_tp_mdix;
475 link_ksettings->base.eth_tp_mdix_ctrl
476 = legacy_settings->eth_tp_mdix_ctrl;
477 return retval;
478}
479
480/* return false if ksettings link modes had higher bits
481 * set. legacy_settings always updated (best effort)
482 */
483static bool
484convert_link_ksettings_to_legacy_settings(
485 struct ethtool_cmd *legacy_settings,
486 const struct ethtool_link_ksettings *link_ksettings)
487{
488 bool retval = true;
489
490 memset(legacy_settings, 0, sizeof(*legacy_settings));
491 /* this also clears the deprecated fields in legacy structure:
492 * __u8 transceiver;
493 * __u32 maxtxpkt;
494 * __u32 maxrxpkt;
495 */
496
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200497 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800498 &legacy_settings->supported,
499 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200500 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800501 &legacy_settings->advertising,
502 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200503 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800504 &legacy_settings->lp_advertising,
505 link_ksettings->link_modes.lp_advertising);
506 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
507 legacy_settings->duplex
508 = link_ksettings->base.duplex;
509 legacy_settings->port
510 = link_ksettings->base.port;
511 legacy_settings->phy_address
512 = link_ksettings->base.phy_address;
513 legacy_settings->autoneg
514 = link_ksettings->base.autoneg;
515 legacy_settings->mdio_support
516 = link_ksettings->base.mdio_support;
517 legacy_settings->eth_tp_mdix
518 = link_ksettings->base.eth_tp_mdix;
519 legacy_settings->eth_tp_mdix_ctrl
520 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700521 legacy_settings->transceiver
522 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800523 return retval;
524}
525
526/* number of 32-bit words to store the user's link mode bitmaps */
527#define __ETHTOOL_LINK_MODE_MASK_NU32 \
528 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
529
530/* layout of the struct passed from/to userland */
531struct ethtool_link_usettings {
532 struct ethtool_link_settings base;
533 struct {
534 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
535 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
536 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
537 } link_modes;
538};
539
540/* Internal kernel helper to query a device ethtool_link_settings.
541 *
542 * Backward compatibility note: for compatibility with legacy drivers
543 * that implement only the ethtool_cmd API, this has to work with both
544 * drivers implementing get_link_ksettings API and drivers
545 * implementing get_settings API. When drivers implement get_settings
546 * and report ethtool_cmd deprecated fields
547 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
548 * because the resulting struct ethtool_link_settings does not report them.
549 */
550int __ethtool_get_link_ksettings(struct net_device *dev,
551 struct ethtool_link_ksettings *link_ksettings)
552{
553 int err;
554 struct ethtool_cmd cmd;
555
556 ASSERT_RTNL();
557
558 if (dev->ethtool_ops->get_link_ksettings) {
559 memset(link_ksettings, 0, sizeof(*link_ksettings));
560 return dev->ethtool_ops->get_link_ksettings(dev,
561 link_ksettings);
562 }
563
564 /* driver doesn't support %ethtool_link_ksettings API. revert to
565 * legacy %ethtool_cmd API, unless it's not supported either.
566 * TODO: remove when ethtool_ops::get_settings disappears internally
567 */
David Decotigny3237fc62016-02-24 10:58:11 -0800568 if (!dev->ethtool_ops->get_settings)
569 return -EOPNOTSUPP;
570
571 memset(&cmd, 0, sizeof(cmd));
572 cmd.cmd = ETHTOOL_GSET;
573 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800574 if (err < 0)
575 return err;
576
577 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
578 */
579 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
580 return err;
581}
582EXPORT_SYMBOL(__ethtool_get_link_ksettings);
583
584/* convert ethtool_link_usettings in user space to a kernel internal
585 * ethtool_link_ksettings. return 0 on success, errno on error.
586 */
587static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
588 const void __user *from)
589{
590 struct ethtool_link_usettings link_usettings;
591
592 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
593 return -EFAULT;
594
595 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800596 bitmap_from_arr32(to->link_modes.supported,
597 link_usettings.link_modes.supported,
598 __ETHTOOL_LINK_MODE_MASK_NBITS);
599 bitmap_from_arr32(to->link_modes.advertising,
600 link_usettings.link_modes.advertising,
601 __ETHTOOL_LINK_MODE_MASK_NBITS);
602 bitmap_from_arr32(to->link_modes.lp_advertising,
603 link_usettings.link_modes.lp_advertising,
604 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800605
606 return 0;
607}
608
609/* convert a kernel internal ethtool_link_ksettings to
610 * ethtool_link_usettings in user space. return 0 on success, errno on
611 * error.
612 */
613static int
614store_link_ksettings_for_user(void __user *to,
615 const struct ethtool_link_ksettings *from)
616{
617 struct ethtool_link_usettings link_usettings;
618
619 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800620 bitmap_to_arr32(link_usettings.link_modes.supported,
621 from->link_modes.supported,
622 __ETHTOOL_LINK_MODE_MASK_NBITS);
623 bitmap_to_arr32(link_usettings.link_modes.advertising,
624 from->link_modes.advertising,
625 __ETHTOOL_LINK_MODE_MASK_NBITS);
626 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
627 from->link_modes.lp_advertising,
628 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800629
630 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
631 return -EFAULT;
632
633 return 0;
634}
635
636/* Query device for its ethtool_link_settings.
637 *
638 * Backward compatibility note: this function must fail when driver
639 * does not implement ethtool::get_link_ksettings, even if legacy
640 * ethtool_ops::get_settings is implemented. This tells new versions
641 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
642 * this driver, so that they can correctly access the ethtool_cmd
643 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
644 * implements ethtool_ops::get_settings anymore.
645 */
646static int ethtool_get_link_ksettings(struct net_device *dev,
647 void __user *useraddr)
648{
649 int err = 0;
650 struct ethtool_link_ksettings link_ksettings;
651
652 ASSERT_RTNL();
653
654 if (!dev->ethtool_ops->get_link_ksettings)
655 return -EOPNOTSUPP;
656
657 /* handle bitmap nbits handshake */
658 if (copy_from_user(&link_ksettings.base, useraddr,
659 sizeof(link_ksettings.base)))
660 return -EFAULT;
661
662 if (__ETHTOOL_LINK_MODE_MASK_NU32
663 != link_ksettings.base.link_mode_masks_nwords) {
664 /* wrong link mode nbits requested */
665 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000666 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800667 /* send back number of words required as negative val */
668 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
669 "need too many bits for link modes!");
670 link_ksettings.base.link_mode_masks_nwords
671 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
672
673 /* copy the base fields back to user, not the link
674 * mode bitmaps
675 */
676 if (copy_to_user(useraddr, &link_ksettings.base,
677 sizeof(link_ksettings.base)))
678 return -EFAULT;
679
680 return 0;
681 }
682
683 /* handshake successful: user/kernel agree on
684 * link_mode_masks_nwords
685 */
686
687 memset(&link_ksettings, 0, sizeof(link_ksettings));
688 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
689 if (err < 0)
690 return err;
691
692 /* make sure we tell the right values to user */
693 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
694 link_ksettings.base.link_mode_masks_nwords
695 = __ETHTOOL_LINK_MODE_MASK_NU32;
696
697 return store_link_ksettings_for_user(useraddr, &link_ksettings);
698}
699
700/* Update device ethtool_link_settings.
701 *
702 * Backward compatibility note: this function must fail when driver
703 * does not implement ethtool::set_link_ksettings, even if legacy
704 * ethtool_ops::set_settings is implemented. This tells new versions
705 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
706 * this driver, so that they can correctly update the ethtool_cmd
707 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
708 * implements ethtool_ops::get_settings anymore.
709 */
710static int ethtool_set_link_ksettings(struct net_device *dev,
711 void __user *useraddr)
712{
713 int err;
714 struct ethtool_link_ksettings link_ksettings;
715
716 ASSERT_RTNL();
717
718 if (!dev->ethtool_ops->set_link_ksettings)
719 return -EOPNOTSUPP;
720
721 /* make sure nbits field has expected value */
722 if (copy_from_user(&link_ksettings.base, useraddr,
723 sizeof(link_ksettings.base)))
724 return -EFAULT;
725
726 if (__ETHTOOL_LINK_MODE_MASK_NU32
727 != link_ksettings.base.link_mode_masks_nwords)
728 return -EINVAL;
729
730 /* copy the whole structure, now that we know it has expected
731 * format
732 */
733 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
734 if (err)
735 return err;
736
737 /* re-check nwords field, just in case */
738 if (__ETHTOOL_LINK_MODE_MASK_NU32
739 != link_ksettings.base.link_mode_masks_nwords)
740 return -EINVAL;
741
742 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
743}
744
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800745/* Query device for its ethtool_cmd settings.
746 *
747 * Backward compatibility note: for compatibility with legacy ethtool,
748 * this has to work with both drivers implementing get_link_ksettings
749 * API and drivers implementing get_settings API. When drivers
750 * implement get_link_ksettings and report higher link mode bits, a
751 * kernel warning is logged once (with name of 1st driver/device) to
752 * recommend user to upgrade ethtool, but the command is successful
753 * (only the lower link mode bits reported back to user).
754 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000755static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
756{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000757 struct ethtool_cmd cmd;
758
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800759 ASSERT_RTNL();
760
761 if (dev->ethtool_ops->get_link_ksettings) {
762 /* First, use link_ksettings API if it is supported */
763 int err;
764 struct ethtool_link_ksettings link_ksettings;
765
766 memset(&link_ksettings, 0, sizeof(link_ksettings));
767 err = dev->ethtool_ops->get_link_ksettings(dev,
768 &link_ksettings);
769 if (err < 0)
770 return err;
Stephen Hemminger71891e22017-12-29 10:02:52 -0800771 convert_link_ksettings_to_legacy_settings(&cmd,
772 &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800773
774 /* send a sensible cmd tag back to user */
775 cmd.cmd = ETHTOOL_GSET;
776 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800777 /* driver doesn't support %ethtool_link_ksettings
778 * API. revert to legacy %ethtool_cmd API, unless it's
779 * not supported either.
780 */
David Decotigny3237fc62016-02-24 10:58:11 -0800781 int err;
782
783 if (!dev->ethtool_ops->get_settings)
784 return -EOPNOTSUPP;
785
786 memset(&cmd, 0, sizeof(cmd));
787 cmd.cmd = ETHTOOL_GSET;
788 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800789 if (err < 0)
790 return err;
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
794 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
797}
798
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800799/* Update device link settings with given ethtool_cmd.
800 *
801 * Backward compatibility note: for compatibility with legacy ethtool,
802 * this has to work with both drivers implementing set_link_ksettings
803 * API and drivers implementing set_settings API. When drivers
804 * implement set_link_ksettings and user's request updates deprecated
805 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
806 * warning is logged once (with name of 1st driver/device) to
807 * recommend user to upgrade ethtool, and the request is rejected.
808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
810{
811 struct ethtool_cmd cmd;
812
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800813 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
816 return -EFAULT;
817
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800818 /* first, try new %ethtool_link_ksettings API. */
819 if (dev->ethtool_ops->set_link_ksettings) {
820 struct ethtool_link_ksettings link_ksettings;
821
822 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
823 &cmd))
824 return -EINVAL;
825
826 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
827 link_ksettings.base.link_mode_masks_nwords
828 = __ETHTOOL_LINK_MODE_MASK_NU32;
829 return dev->ethtool_ops->set_link_ksettings(dev,
830 &link_ksettings);
831 }
832
833 /* legacy %ethtool_cmd API */
834
835 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
836 * disappears internally
837 */
838
839 if (!dev->ethtool_ops->set_settings)
840 return -EOPNOTSUPP;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return dev->ethtool_ops->set_settings(dev, &cmd);
843}
844
chavey97f8aef2010-04-07 21:54:42 -0700845static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
846 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700849 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 memset(&info, 0, sizeof(info));
852 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000853 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700854 ops->get_drvinfo(dev, &info);
855 } else if (dev->dev.parent && dev->dev.parent->driver) {
856 strlcpy(info.bus_info, dev_name(dev->dev.parent),
857 sizeof(info.bus_info));
858 strlcpy(info.driver, dev->dev.parent->driver->name,
859 sizeof(info.driver));
860 } else {
861 return -EOPNOTSUPP;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Jeff Garzik723b2f52010-03-03 22:51:50 +0000864 /*
865 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000866 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000867 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000868 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700869 int rc;
870
871 rc = ops->get_sset_count(dev, ETH_SS_TEST);
872 if (rc >= 0)
873 info.testinfo_len = rc;
874 rc = ops->get_sset_count(dev, ETH_SS_STATS);
875 if (rc >= 0)
876 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700877 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
878 if (rc >= 0)
879 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700880 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000881 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000883 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 info.eedump_len = ops->get_eeprom_len(dev);
885
886 if (copy_to_user(useraddr, &info, sizeof(info)))
887 return -EFAULT;
888 return 0;
889}
890
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800891static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700892 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000893{
894 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000895 u64 sset_mask;
896 int i, idx = 0, n_bits = 0, ret, rc;
897 u32 *info_buf = NULL;
898
Jeff Garzik723b2f52010-03-03 22:51:50 +0000899 if (copy_from_user(&info, useraddr, sizeof(info)))
900 return -EFAULT;
901
902 /* store copy of mask, because we zero struct later on */
903 sset_mask = info.sset_mask;
904 if (!sset_mask)
905 return 0;
906
907 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000908 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000909
910 memset(&info, 0, sizeof(info));
911 info.cmd = ETHTOOL_GSSET_INFO;
912
913 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
914 if (!info_buf)
915 return -ENOMEM;
916
917 /*
918 * fill return buffer based on input bitmask and successful
919 * get_sset_count return
920 */
921 for (i = 0; i < 64; i++) {
922 if (!(sset_mask & (1ULL << i)))
923 continue;
924
Michał Mirosław340ae162011-02-15 16:59:16 +0000925 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000926 if (rc >= 0) {
927 info.sset_mask |= (1ULL << i);
928 info_buf[idx++] = rc;
929 }
930 }
931
932 ret = -EFAULT;
933 if (copy_to_user(useraddr, &info, sizeof(info)))
934 goto out;
935
936 useraddr += offsetof(struct ethtool_sset_info, data);
937 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
938 goto out;
939
940 ret = 0;
941
942out:
943 kfree(info_buf);
944 return ret;
945}
946
chavey97f8aef2010-04-07 21:54:42 -0700947static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000948 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700949{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000950 struct ethtool_rxnfc info;
951 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000952 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700953
Santwona Behera59089d82009-02-20 00:58:13 -0800954 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700955 return -EOPNOTSUPP;
956
Ben Hutchingsbf988432010-06-28 08:45:58 +0000957 /* struct ethtool_rxnfc was originally defined for
958 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
959 * members. User-space might still be using that
960 * definition. */
961 if (cmd == ETHTOOL_SRXFH)
962 info_size = (offsetof(struct ethtool_rxnfc, data) +
963 sizeof(info.data));
964
965 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700966 return -EFAULT;
967
Ben Hutchings55664f32012-01-03 12:04:51 +0000968 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
969 if (rc)
970 return rc;
971
972 if (cmd == ETHTOOL_SRXCLSRLINS &&
973 copy_to_user(useraddr, &info, info_size))
974 return -EFAULT;
975
976 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700977}
978
chavey97f8aef2010-04-07 21:54:42 -0700979static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000980 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700981{
982 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000983 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800984 const struct ethtool_ops *ops = dev->ethtool_ops;
985 int ret;
986 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700987
Santwona Behera59089d82009-02-20 00:58:13 -0800988 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700989 return -EOPNOTSUPP;
990
Ben Hutchingsbf988432010-06-28 08:45:58 +0000991 /* struct ethtool_rxnfc was originally defined for
992 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
993 * members. User-space might still be using that
994 * definition. */
995 if (cmd == ETHTOOL_GRXFH)
996 info_size = (offsetof(struct ethtool_rxnfc, data) +
997 sizeof(info.data));
998
999 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001000 return -EFAULT;
1001
Edward Cree84a1d9c42018-03-08 15:45:03 +00001002 /* If FLOW_RSS was requested then user-space must be using the
1003 * new definition, as FLOW_RSS is newer.
1004 */
1005 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1006 info_size = sizeof(info);
1007 if (copy_from_user(&info, useraddr, info_size))
1008 return -EFAULT;
1009 }
1010
Santwona Behera59089d82009-02-20 00:58:13 -08001011 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1012 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001013 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +00001014 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001015 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001016 if (!rule_buf)
1017 return -ENOMEM;
1018 }
1019 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001020
Santwona Behera59089d82009-02-20 00:58:13 -08001021 ret = ops->get_rxnfc(dev, &info, rule_buf);
1022 if (ret < 0)
1023 goto err_out;
1024
1025 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001026 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001027 goto err_out;
1028
1029 if (rule_buf) {
1030 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1031 if (copy_to_user(useraddr, rule_buf,
1032 info.rule_cnt * sizeof(u32)))
1033 goto err_out;
1034 }
1035 ret = 0;
1036
1037err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001038 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001039
1040 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001041}
1042
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301043static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1044 struct ethtool_rxnfc *rx_rings,
1045 u32 size)
1046{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001047 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301048
1049 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001050 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301051
1052 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001053 for (i = 0; i < size; i++)
1054 if (indir[i] >= rx_rings->data)
1055 return -EINVAL;
1056
1057 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301058}
1059
Kim Jonesba905f52016-02-02 03:51:16 +00001060u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001061
1062void netdev_rss_key_fill(void *buffer, size_t len)
1063{
1064 BUG_ON(len > sizeof(netdev_rss_key));
1065 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1066 memcpy(buffer, netdev_rss_key, len);
1067}
1068EXPORT_SYMBOL(netdev_rss_key_fill);
1069
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001070static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1071{
1072 u32 dev_size, current_max = 0;
1073 u32 *indir;
1074 int ret;
1075
1076 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1077 !dev->ethtool_ops->get_rxfh)
1078 return -EOPNOTSUPP;
1079 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1080 if (dev_size == 0)
1081 return -EOPNOTSUPP;
1082
1083 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1084 if (!indir)
1085 return -ENOMEM;
1086
1087 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1088 if (ret)
1089 goto out;
1090
1091 while (dev_size--)
1092 current_max = max(current_max, indir[dev_size]);
1093
1094 *max = current_max;
1095
1096out:
1097 kfree(indir);
1098 return ret;
1099}
1100
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001101static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1102 void __user *useraddr)
1103{
Ben Hutchings7850f632011-12-15 13:55:01 +00001104 u32 user_size, dev_size;
1105 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001106 int ret;
1107
Ben Hutchings7850f632011-12-15 13:55:01 +00001108 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001109 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001110 return -EOPNOTSUPP;
1111 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1112 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001113 return -EOPNOTSUPP;
1114
Ben Hutchings7850f632011-12-15 13:55:01 +00001115 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001116 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001117 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001118 return -EFAULT;
1119
Ben Hutchings7850f632011-12-15 13:55:01 +00001120 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1121 &dev_size, sizeof(dev_size)))
1122 return -EFAULT;
1123
1124 /* If the user buffer size is 0, this is just a query for the
1125 * device table size. Otherwise, if it's smaller than the
1126 * device table size it's an error.
1127 */
1128 if (user_size < dev_size)
1129 return user_size == 0 ? 0 : -EINVAL;
1130
1131 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001132 if (!indir)
1133 return -ENOMEM;
1134
Eyal Perry892311f2014-12-02 18:12:10 +02001135 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001136 if (ret)
1137 goto out;
1138
Ben Hutchings7850f632011-12-15 13:55:01 +00001139 if (copy_to_user(useraddr +
1140 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1141 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001142 ret = -EFAULT;
1143
1144out:
1145 kfree(indir);
1146 return ret;
1147}
1148
1149static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1150 void __user *useraddr)
1151{
Ben Hutchings7850f632011-12-15 13:55:01 +00001152 struct ethtool_rxnfc rx_rings;
1153 u32 user_size, dev_size, i;
1154 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001155 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001156 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301157 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001158
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001159 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001160 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001161 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001162
1163 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001164 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001165 return -EOPNOTSUPP;
1166
Ben Hutchings7850f632011-12-15 13:55:01 +00001167 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001168 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001169 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001170 return -EFAULT;
1171
Ben Hutchings278bc422011-12-15 13:56:49 +00001172 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001173 return -EINVAL;
1174
1175 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001176 if (!indir)
1177 return -ENOMEM;
1178
Ben Hutchings7850f632011-12-15 13:55:01 +00001179 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001180 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001181 if (ret)
1182 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001183
1184 if (user_size == 0) {
1185 for (i = 0; i < dev_size; i++)
1186 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1187 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301188 ret = ethtool_copy_validate_indir(indir,
1189 useraddr + ringidx_offset,
1190 &rx_rings,
1191 dev_size);
1192 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001193 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001194 }
1195
Eyal Perry892311f2014-12-02 18:12:10 +02001196 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001197 if (ret)
1198 goto out;
1199
1200 /* indicate whether rxfh was set to default */
1201 if (user_size == 0)
1202 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1203 else
1204 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001205
1206out:
1207 kfree(indir);
1208 return ret;
1209}
1210
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301211static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1212 void __user *useraddr)
1213{
1214 int ret;
1215 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001216 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301217 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001218 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301219 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001220 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301221 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001222 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301223 u8 *hkey = NULL;
1224 u8 *rss_config;
1225
Eyal Perry892311f2014-12-02 18:12:10 +02001226 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301227 return -EOPNOTSUPP;
1228
1229 if (ops->get_rxfh_indir_size)
1230 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301231 if (ops->get_rxfh_key_size)
1232 dev_key_size = ops->get_rxfh_key_size(dev);
1233
Ben Hutchingsf062a382014-05-15 16:28:07 +01001234 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301235 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001236 user_indir_size = rxfh.indir_size;
1237 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301238
Ben Hutchingsf062a382014-05-15 16:28:07 +01001239 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001240 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001241 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001242 /* Most drivers don't handle rss_context, check it's 0 as well */
1243 if (rxfh.rss_context && !ops->get_rxfh_context)
1244 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001245
1246 rxfh.indir_size = dev_indir_size;
1247 rxfh.key_size = dev_key_size;
1248 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301249 return -EFAULT;
1250
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301251 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1252 (user_key_size && (user_key_size != dev_key_size)))
1253 return -EINVAL;
1254
1255 indir_bytes = user_indir_size * sizeof(indir[0]);
1256 total_size = indir_bytes + user_key_size;
1257 rss_config = kzalloc(total_size, GFP_USER);
1258 if (!rss_config)
1259 return -ENOMEM;
1260
1261 if (user_indir_size)
1262 indir = (u32 *)rss_config;
1263
1264 if (user_key_size)
1265 hkey = rss_config + indir_bytes;
1266
Edward Cree84a1d9c42018-03-08 15:45:03 +00001267 if (rxfh.rss_context)
1268 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1269 &dev_hfunc,
1270 rxfh.rss_context);
1271 else
1272 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001273 if (ret)
1274 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301275
Eyal Perry892311f2014-12-02 18:12:10 +02001276 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1277 &dev_hfunc, sizeof(rxfh.hfunc))) {
1278 ret = -EFAULT;
1279 } else if (copy_to_user(useraddr +
1280 offsetof(struct ethtool_rxfh, rss_config[0]),
1281 rss_config, total_size)) {
1282 ret = -EFAULT;
1283 }
1284out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301285 kfree(rss_config);
1286
1287 return ret;
1288}
1289
1290static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1291 void __user *useraddr)
1292{
1293 int ret;
1294 const struct ethtool_ops *ops = dev->ethtool_ops;
1295 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001296 struct ethtool_rxfh rxfh;
1297 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301298 u32 *indir = NULL, indir_bytes = 0;
1299 u8 *hkey = NULL;
1300 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301301 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001302 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301303
Eyal Perry892311f2014-12-02 18:12:10 +02001304 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301305 return -EOPNOTSUPP;
1306
1307 if (ops->get_rxfh_indir_size)
1308 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301309 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001310 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301311
Ben Hutchingsf062a382014-05-15 16:28:07 +01001312 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301313 return -EFAULT;
1314
Ben Hutchingsf062a382014-05-15 16:28:07 +01001315 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001316 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001317 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001318 /* Most drivers don't handle rss_context, check it's 0 as well */
1319 if (rxfh.rss_context && !ops->set_rxfh_context)
1320 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001321
Eyal Perry892311f2014-12-02 18:12:10 +02001322 /* If either indir, hash key or function is valid, proceed further.
1323 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301324 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001325 if ((rxfh.indir_size &&
1326 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1327 rxfh.indir_size != dev_indir_size) ||
1328 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1329 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001330 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301331 return -EINVAL;
1332
Ben Hutchingsf062a382014-05-15 16:28:07 +01001333 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301334 indir_bytes = dev_indir_size * sizeof(indir[0]);
1335
Ben Hutchingsf062a382014-05-15 16:28:07 +01001336 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301337 if (!rss_config)
1338 return -ENOMEM;
1339
1340 rx_rings.cmd = ETHTOOL_GRXRINGS;
1341 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1342 if (ret)
1343 goto out;
1344
Edward Cree84a1d9c42018-03-08 15:45:03 +00001345 /* rxfh.indir_size == 0 means reset the indir table to default (master
1346 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001347 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301348 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001349 if (rxfh.indir_size &&
1350 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301351 indir = (u32 *)rss_config;
1352 ret = ethtool_copy_validate_indir(indir,
1353 useraddr + rss_cfg_offset,
1354 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001355 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301356 if (ret)
1357 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001358 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001359 if (rxfh.rss_context == 0) {
1360 indir = (u32 *)rss_config;
1361 for (i = 0; i < dev_indir_size; i++)
1362 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1363 } else {
1364 delete = true;
1365 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301366 }
1367
Ben Hutchingsf062a382014-05-15 16:28:07 +01001368 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301369 hkey = rss_config + indir_bytes;
1370 if (copy_from_user(hkey,
1371 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001372 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301373 ret = -EFAULT;
1374 goto out;
1375 }
1376 }
1377
Edward Cree84a1d9c42018-03-08 15:45:03 +00001378 if (rxfh.rss_context)
1379 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1380 &rxfh.rss_context, delete);
1381 else
1382 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001383 if (ret)
1384 goto out;
1385
Edward Cree84a1d9c42018-03-08 15:45:03 +00001386 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1387 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1388 ret = -EFAULT;
1389
1390 if (!rxfh.rss_context) {
1391 /* indicate whether rxfh was set to default */
1392 if (rxfh.indir_size == 0)
1393 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1394 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1395 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1396 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301397
1398out:
1399 kfree(rss_config);
1400 return ret;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1404{
1405 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001406 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 void *regbuf;
1408 int reglen, ret;
1409
1410 if (!ops->get_regs || !ops->get_regs_len)
1411 return -EOPNOTSUPP;
1412
1413 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1414 return -EFAULT;
1415
1416 reglen = ops->get_regs_len(dev);
1417 if (regs.len > reglen)
1418 regs.len = reglen;
1419
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001420 regbuf = NULL;
1421 if (reglen) {
1422 regbuf = vzalloc(reglen);
1423 if (!regbuf)
1424 return -ENOMEM;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 ops->get_regs(dev, &regs, regbuf);
1428
1429 ret = -EFAULT;
1430 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1431 goto out;
1432 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001433 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 goto out;
1435 ret = 0;
1436
1437 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001438 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return ret;
1440}
1441
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001442static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1443{
1444 struct ethtool_value reset;
1445 int ret;
1446
1447 if (!dev->ethtool_ops->reset)
1448 return -EOPNOTSUPP;
1449
1450 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1451 return -EFAULT;
1452
1453 ret = dev->ethtool_ops->reset(dev, &reset.data);
1454 if (ret)
1455 return ret;
1456
1457 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1458 return -EFAULT;
1459 return 0;
1460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1463{
Roland Dreier8e557422010-02-11 12:14:23 -08001464 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 if (!dev->ethtool_ops->get_wol)
1467 return -EOPNOTSUPP;
1468
1469 dev->ethtool_ops->get_wol(dev, &wol);
1470
1471 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1472 return -EFAULT;
1473 return 0;
1474}
1475
1476static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1477{
1478 struct ethtool_wolinfo wol;
1479
1480 if (!dev->ethtool_ops->set_wol)
1481 return -EOPNOTSUPP;
1482
1483 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1484 return -EFAULT;
1485
1486 return dev->ethtool_ops->set_wol(dev, &wol);
1487}
1488
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001489static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1490{
1491 struct ethtool_eee edata;
1492 int rc;
1493
1494 if (!dev->ethtool_ops->get_eee)
1495 return -EOPNOTSUPP;
1496
1497 memset(&edata, 0, sizeof(struct ethtool_eee));
1498 edata.cmd = ETHTOOL_GEEE;
1499 rc = dev->ethtool_ops->get_eee(dev, &edata);
1500
1501 if (rc)
1502 return rc;
1503
1504 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1505 return -EFAULT;
1506
1507 return 0;
1508}
1509
1510static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1511{
1512 struct ethtool_eee edata;
1513
1514 if (!dev->ethtool_ops->set_eee)
1515 return -EOPNOTSUPP;
1516
1517 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1518 return -EFAULT;
1519
1520 return dev->ethtool_ops->set_eee(dev, &edata);
1521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523static int ethtool_nway_reset(struct net_device *dev)
1524{
1525 if (!dev->ethtool_ops->nway_reset)
1526 return -EOPNOTSUPP;
1527
1528 return dev->ethtool_ops->nway_reset(dev);
1529}
1530
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001531static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1532{
1533 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1534
1535 if (!dev->ethtool_ops->get_link)
1536 return -EOPNOTSUPP;
1537
1538 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1539
1540 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1541 return -EFAULT;
1542 return 0;
1543}
1544
Ben Hutchings081d0942012-04-12 00:42:12 +00001545static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1546 int (*getter)(struct net_device *,
1547 struct ethtool_eeprom *, u8 *),
1548 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001551 void __user *userbuf = useraddr + sizeof(eeprom);
1552 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001554 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1557 return -EFAULT;
1558
1559 /* Check for wrap and zero */
1560 if (eeprom.offset + eeprom.len <= eeprom.offset)
1561 return -EINVAL;
1562
1563 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001564 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return -EINVAL;
1566
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001567 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (!data)
1569 return -ENOMEM;
1570
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001571 bytes_remaining = eeprom.len;
1572 while (bytes_remaining > 0) {
1573 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Ben Hutchings081d0942012-04-12 00:42:12 +00001575 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001576 if (ret)
1577 break;
1578 if (copy_to_user(userbuf, data, eeprom.len)) {
1579 ret = -EFAULT;
1580 break;
1581 }
1582 userbuf += eeprom.len;
1583 eeprom.offset += eeprom.len;
1584 bytes_remaining -= eeprom.len;
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001587 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1588 eeprom.offset -= eeprom.len;
1589 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1590 ret = -EFAULT;
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 kfree(data);
1593 return ret;
1594}
1595
Ben Hutchings081d0942012-04-12 00:42:12 +00001596static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1597{
1598 const struct ethtool_ops *ops = dev->ethtool_ops;
1599
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001600 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1601 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001602 return -EOPNOTSUPP;
1603
1604 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1605 ops->get_eeprom_len(dev));
1606}
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1609{
1610 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001611 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001612 void __user *userbuf = useraddr + sizeof(eeprom);
1613 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001615 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001617 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1618 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return -EOPNOTSUPP;
1620
1621 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1622 return -EFAULT;
1623
1624 /* Check for wrap and zero */
1625 if (eeprom.offset + eeprom.len <= eeprom.offset)
1626 return -EINVAL;
1627
1628 /* Check for exceeding total eeprom len */
1629 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1630 return -EINVAL;
1631
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001632 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (!data)
1634 return -ENOMEM;
1635
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001636 bytes_remaining = eeprom.len;
1637 while (bytes_remaining > 0) {
1638 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001640 if (copy_from_user(data, userbuf, eeprom.len)) {
1641 ret = -EFAULT;
1642 break;
1643 }
1644 ret = ops->set_eeprom(dev, &eeprom, data);
1645 if (ret)
1646 break;
1647 userbuf += eeprom.len;
1648 eeprom.offset += eeprom.len;
1649 bytes_remaining -= eeprom.len;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 kfree(data);
1653 return ret;
1654}
1655
chavey97f8aef2010-04-07 21:54:42 -07001656static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1657 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Roland Dreier8e557422010-02-11 12:14:23 -08001659 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 if (!dev->ethtool_ops->get_coalesce)
1662 return -EOPNOTSUPP;
1663
1664 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1665
1666 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1667 return -EFAULT;
1668 return 0;
1669}
1670
chavey97f8aef2010-04-07 21:54:42 -07001671static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1672 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 struct ethtool_coalesce coalesce;
1675
David S. Millerfa04ae52005-06-06 15:07:19 -07001676 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return -EOPNOTSUPP;
1678
1679 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1680 return -EFAULT;
1681
1682 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1683}
1684
1685static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1686{
Roland Dreier8e557422010-02-11 12:14:23 -08001687 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (!dev->ethtool_ops->get_ringparam)
1690 return -EOPNOTSUPP;
1691
1692 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1693
1694 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1695 return -EFAULT;
1696 return 0;
1697}
1698
1699static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1700{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001701 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001703 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -EOPNOTSUPP;
1705
1706 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1707 return -EFAULT;
1708
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001709 dev->ethtool_ops->get_ringparam(dev, &max);
1710
1711 /* ensure new ring parameters are within the maximums */
1712 if (ringparam.rx_pending > max.rx_max_pending ||
1713 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1714 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1715 ringparam.tx_pending > max.tx_max_pending)
1716 return -EINVAL;
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1719}
1720
amit salecha8b5933c2011-04-07 01:58:42 +00001721static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1722 void __user *useraddr)
1723{
1724 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1725
1726 if (!dev->ethtool_ops->get_channels)
1727 return -EOPNOTSUPP;
1728
1729 dev->ethtool_ops->get_channels(dev, &channels);
1730
1731 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1732 return -EFAULT;
1733 return 0;
1734}
1735
1736static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1737 void __user *useraddr)
1738{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001739 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001740 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001741
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001742 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001743 return -EOPNOTSUPP;
1744
1745 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1746 return -EFAULT;
1747
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001748 dev->ethtool_ops->get_channels(dev, &max);
1749
1750 /* ensure new counts are within the maximums */
1751 if ((channels.rx_count > max.max_rx) ||
1752 (channels.tx_count > max.max_tx) ||
1753 (channels.combined_count > max.max_combined) ||
1754 (channels.other_count > max.max_other))
1755 return -EINVAL;
1756
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001757 /* ensure the new Rx count fits within the configured Rx flow
1758 * indirection table settings */
1759 if (netif_is_rxfh_configured(dev) &&
1760 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1761 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1762 return -EINVAL;
1763
amit salecha8b5933c2011-04-07 01:58:42 +00001764 return dev->ethtool_ops->set_channels(dev, &channels);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1768{
1769 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1770
1771 if (!dev->ethtool_ops->get_pauseparam)
1772 return -EOPNOTSUPP;
1773
1774 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1775
1776 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1777 return -EFAULT;
1778 return 0;
1779}
1780
1781static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1782{
1783 struct ethtool_pauseparam pauseparam;
1784
Jeff Garzike1b90c42006-07-17 12:54:40 -04001785 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return -EOPNOTSUPP;
1787
1788 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1789 return -EFAULT;
1790
1791 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1792}
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1795{
1796 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001797 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001799 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001801 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001802 return -EOPNOTSUPP;
1803
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001804 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001805 if (test_len < 0)
1806 return test_len;
1807 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 if (copy_from_user(&test, useraddr, sizeof(test)))
1810 return -EFAULT;
1811
Jeff Garzikff03d492007-08-15 16:01:08 -07001812 test.len = test_len;
1813 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (!data)
1815 return -ENOMEM;
1816
1817 ops->self_test(dev, &test, data);
1818
1819 ret = -EFAULT;
1820 if (copy_to_user(useraddr, &test, sizeof(test)))
1821 goto out;
1822 useraddr += sizeof(test);
1823 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1824 goto out;
1825 ret = 0;
1826
1827 out:
1828 kfree(data);
1829 return ret;
1830}
1831
1832static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1833{
1834 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 u8 *data;
1836 int ret;
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1839 return -EFAULT;
1840
Michał Mirosław340ae162011-02-15 16:59:16 +00001841 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001842 if (ret < 0)
1843 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001844 if (ret > S32_MAX / ETH_GSTRING_LEN)
1845 return -ENOMEM;
1846 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001847
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001848 gstrings.len = ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001849 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1850 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return -ENOMEM;
1852
Michał Mirosław340ae162011-02-15 16:59:16 +00001853 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 ret = -EFAULT;
1856 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1857 goto out;
1858 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001859 if (gstrings.len &&
1860 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 goto out;
1862 ret = 0;
1863
Michał Mirosław340ae162011-02-15 16:59:16 +00001864out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001865 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return ret;
1867}
1868
1869static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1870{
1871 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001872 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001873 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001874 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001876 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return -EOPNOTSUPP;
1878
Ben Hutchings68f512f2011-04-02 00:35:15 +01001879 if (busy)
1880 return -EBUSY;
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (copy_from_user(&id, useraddr, sizeof(id)))
1883 return -EFAULT;
1884
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001885 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001886 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001887 return rc;
1888
1889 /* Drop the RTNL lock while waiting, but prevent reentry or
1890 * removal of the device.
1891 */
1892 busy = true;
1893 dev_hold(dev);
1894 rtnl_unlock();
1895
1896 if (rc == 0) {
1897 /* Driver will handle this itself */
1898 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001899 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001900 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001901 /* Driver expects to be called at twice the frequency in rc */
1902 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001903
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001904 /* Count down seconds */
1905 do {
1906 /* Count down iterations per second */
1907 i = n;
1908 do {
1909 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001910 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001911 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1912 rtnl_unlock();
1913 if (rc)
1914 break;
1915 schedule_timeout_interruptible(interval);
1916 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001917 } while (!signal_pending(current) &&
1918 (id.data == 0 || --id.data != 0));
1919 }
1920
1921 rtnl_lock();
1922 dev_put(dev);
1923 busy = false;
1924
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001925 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001926 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1930{
1931 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001932 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001934 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001936 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001937 return -EOPNOTSUPP;
1938
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001939 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001940 if (n_stats < 0)
1941 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001942 if (n_stats > S32_MAX / sizeof(u64))
1943 return -ENOMEM;
1944 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1946 return -EFAULT;
1947
Jeff Garzikff03d492007-08-15 16:01:08 -07001948 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001949 data = vzalloc(n_stats * sizeof(u64));
1950 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return -ENOMEM;
1952
1953 ops->get_ethtool_stats(dev, &stats, data);
1954
1955 ret = -EFAULT;
1956 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1957 goto out;
1958 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001959 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto out;
1961 ret = 0;
1962
1963 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001964 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return ret;
1966}
1967
Andrew Lunnf3a40942015-12-30 16:28:25 +01001968static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1969{
Florian Fainelli99943382018-04-25 12:12:48 -07001970 const struct ethtool_ops *ops = dev->ethtool_ops;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001971 struct phy_device *phydev = dev->phydev;
Florian Fainelli99943382018-04-25 12:12:48 -07001972 struct ethtool_stats stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001973 u64 *data;
1974 int ret, n_stats;
1975
Florian Fainelli99943382018-04-25 12:12:48 -07001976 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
Andrew Lunnf3a40942015-12-30 16:28:25 +01001977 return -EOPNOTSUPP;
1978
Florian Fainelli99943382018-04-25 12:12:48 -07001979 if (dev->phydev && !ops->get_ethtool_phy_stats)
1980 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1981 else
1982 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001983 if (n_stats < 0)
1984 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001985 if (n_stats > S32_MAX / sizeof(u64))
1986 return -ENOMEM;
1987 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001988
1989 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1990 return -EFAULT;
1991
1992 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001993 data = vzalloc(n_stats * sizeof(u64));
1994 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01001995 return -ENOMEM;
1996
Florian Fainelli99943382018-04-25 12:12:48 -07001997 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1998 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1999 if (ret < 0)
2000 return ret;
2001 } else {
2002 ops->get_ethtool_phy_stats(dev, &stats, data);
2003 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01002004
2005 ret = -EFAULT;
2006 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2007 goto out;
2008 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002009 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002010 goto out;
2011 ret = 0;
2012
2013 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002014 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002015 return ret;
2016}
2017
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002018static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002019{
2020 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002021
Matthew Wilcox313674a2007-07-31 14:00:29 -07002022 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002023 return -EFAULT;
2024
Matthew Wilcox313674a2007-07-31 14:00:29 -07002025 if (epaddr.size < dev->addr_len)
2026 return -ETOOSMALL;
2027 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002028
Jon Wetzela6f9a702005-08-20 17:15:54 -07002029 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002030 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002031 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002032 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2033 return -EFAULT;
2034 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002035}
2036
Jeff Garzik13c99b22007-08-15 16:01:56 -07002037static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2038 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002039{
Roland Dreier8e557422010-02-11 12:14:23 -08002040 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002041
Jeff Garzik13c99b22007-08-15 16:01:56 -07002042 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002043 return -EOPNOTSUPP;
2044
Jeff Garzik13c99b22007-08-15 16:01:56 -07002045 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002046
2047 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2048 return -EFAULT;
2049 return 0;
2050}
2051
Jeff Garzik13c99b22007-08-15 16:01:56 -07002052static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2053 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002054{
2055 struct ethtool_value edata;
2056
Jeff Garzik13c99b22007-08-15 16:01:56 -07002057 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002058 return -EOPNOTSUPP;
2059
2060 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2061 return -EFAULT;
2062
Jeff Garzik13c99b22007-08-15 16:01:56 -07002063 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002064 return 0;
2065}
2066
Jeff Garzik13c99b22007-08-15 16:01:56 -07002067static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2068 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002069{
2070 struct ethtool_value edata;
2071
Jeff Garzik13c99b22007-08-15 16:01:56 -07002072 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002073 return -EOPNOTSUPP;
2074
2075 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2076 return -EFAULT;
2077
Jeff Garzik13c99b22007-08-15 16:01:56 -07002078 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002079}
2080
chavey97f8aef2010-04-07 21:54:42 -07002081static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2082 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002083{
2084 struct ethtool_flash efl;
2085
2086 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2087 return -EFAULT;
2088
2089 if (!dev->ethtool_ops->flash_device)
2090 return -EOPNOTSUPP;
2091
Ben Hutchings786f5282012-02-01 09:32:25 +00002092 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2093
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002094 return dev->ethtool_ops->flash_device(dev, &efl);
2095}
2096
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002097static int ethtool_set_dump(struct net_device *dev,
2098 void __user *useraddr)
2099{
2100 struct ethtool_dump dump;
2101
2102 if (!dev->ethtool_ops->set_dump)
2103 return -EOPNOTSUPP;
2104
2105 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2106 return -EFAULT;
2107
2108 return dev->ethtool_ops->set_dump(dev, &dump);
2109}
2110
2111static int ethtool_get_dump_flag(struct net_device *dev,
2112 void __user *useraddr)
2113{
2114 int ret;
2115 struct ethtool_dump dump;
2116 const struct ethtool_ops *ops = dev->ethtool_ops;
2117
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002118 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002119 return -EOPNOTSUPP;
2120
2121 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2122 return -EFAULT;
2123
2124 ret = ops->get_dump_flag(dev, &dump);
2125 if (ret)
2126 return ret;
2127
2128 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2129 return -EFAULT;
2130 return 0;
2131}
2132
2133static int ethtool_get_dump_data(struct net_device *dev,
2134 void __user *useraddr)
2135{
2136 int ret;
2137 __u32 len;
2138 struct ethtool_dump dump, tmp;
2139 const struct ethtool_ops *ops = dev->ethtool_ops;
2140 void *data = NULL;
2141
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002142 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002143 return -EOPNOTSUPP;
2144
2145 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2146 return -EFAULT;
2147
2148 memset(&tmp, 0, sizeof(tmp));
2149 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2150 ret = ops->get_dump_flag(dev, &tmp);
2151 if (ret)
2152 return ret;
2153
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002154 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002155 if (!len)
2156 return -EFAULT;
2157
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002158 /* Don't ever let the driver think there's more space available
2159 * than it requested with .get_dump_flag().
2160 */
2161 dump.len = len;
2162
2163 /* Always allocate enough space to hold the whole thing so that the
2164 * driver does not need to check the length and bother with partial
2165 * dumping.
2166 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002167 data = vzalloc(tmp.len);
2168 if (!data)
2169 return -ENOMEM;
2170 ret = ops->get_dump_data(dev, &dump, data);
2171 if (ret)
2172 goto out;
2173
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002174 /* There are two sane possibilities:
2175 * 1. The driver's .get_dump_data() does not touch dump.len.
2176 * 2. Or it may set dump.len to how much it really writes, which
2177 * should be tmp.len (or len if it can do a partial dump).
2178 * In any case respond to userspace with the actual length of data
2179 * it's receiving.
2180 */
2181 WARN_ON(dump.len != len && dump.len != tmp.len);
2182 dump.len = len;
2183
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002184 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2185 ret = -EFAULT;
2186 goto out;
2187 }
2188 useraddr += offsetof(struct ethtool_dump, data);
2189 if (copy_to_user(useraddr, data, len))
2190 ret = -EFAULT;
2191out:
2192 vfree(data);
2193 return ret;
2194}
2195
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002196static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2197{
2198 int err = 0;
2199 struct ethtool_ts_info info;
2200 const struct ethtool_ops *ops = dev->ethtool_ops;
2201 struct phy_device *phydev = dev->phydev;
2202
2203 memset(&info, 0, sizeof(info));
2204 info.cmd = ETHTOOL_GET_TS_INFO;
2205
2206 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002207 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002208 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002209 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002210 } else {
2211 info.so_timestamping =
2212 SOF_TIMESTAMPING_RX_SOFTWARE |
2213 SOF_TIMESTAMPING_SOFTWARE;
2214 info.phc_index = -1;
2215 }
2216
2217 if (err)
2218 return err;
2219
2220 if (copy_to_user(useraddr, &info, sizeof(info)))
2221 err = -EFAULT;
2222
2223 return err;
2224}
2225
Ed Swierk2f438362015-01-02 17:27:56 -08002226static int __ethtool_get_module_info(struct net_device *dev,
2227 struct ethtool_modinfo *modinfo)
2228{
2229 const struct ethtool_ops *ops = dev->ethtool_ops;
2230 struct phy_device *phydev = dev->phydev;
2231
Russell Kinge679c9c2018-03-28 15:44:16 -07002232 if (dev->sfp_bus)
2233 return sfp_get_module_info(dev->sfp_bus, modinfo);
2234
Ed Swierk2f438362015-01-02 17:27:56 -08002235 if (phydev && phydev->drv && phydev->drv->module_info)
2236 return phydev->drv->module_info(phydev, modinfo);
2237
2238 if (ops->get_module_info)
2239 return ops->get_module_info(dev, modinfo);
2240
2241 return -EOPNOTSUPP;
2242}
2243
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002244static int ethtool_get_module_info(struct net_device *dev,
2245 void __user *useraddr)
2246{
2247 int ret;
2248 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002249
2250 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2251 return -EFAULT;
2252
Ed Swierk2f438362015-01-02 17:27:56 -08002253 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002254 if (ret)
2255 return ret;
2256
2257 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2258 return -EFAULT;
2259
2260 return 0;
2261}
2262
Ed Swierk2f438362015-01-02 17:27:56 -08002263static int __ethtool_get_module_eeprom(struct net_device *dev,
2264 struct ethtool_eeprom *ee, u8 *data)
2265{
2266 const struct ethtool_ops *ops = dev->ethtool_ops;
2267 struct phy_device *phydev = dev->phydev;
2268
Russell Kinge679c9c2018-03-28 15:44:16 -07002269 if (dev->sfp_bus)
2270 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2271
Ed Swierk2f438362015-01-02 17:27:56 -08002272 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2273 return phydev->drv->module_eeprom(phydev, ee, data);
2274
2275 if (ops->get_module_eeprom)
2276 return ops->get_module_eeprom(dev, ee, data);
2277
2278 return -EOPNOTSUPP;
2279}
2280
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002281static int ethtool_get_module_eeprom(struct net_device *dev,
2282 void __user *useraddr)
2283{
2284 int ret;
2285 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002286
Ed Swierk2f438362015-01-02 17:27:56 -08002287 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002288 if (ret)
2289 return ret;
2290
Ed Swierk2f438362015-01-02 17:27:56 -08002291 return ethtool_get_any_eeprom(dev, useraddr,
2292 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002293 modinfo.eeprom_len);
2294}
2295
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302296static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2297{
2298 switch (tuna->id) {
2299 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002300 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302301 if (tuna->len != sizeof(u32) ||
2302 tuna->type_id != ETHTOOL_TUNABLE_U32)
2303 return -EINVAL;
2304 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002305 case ETHTOOL_PFC_PREVENTION_TOUT:
2306 if (tuna->len != sizeof(u16) ||
2307 tuna->type_id != ETHTOOL_TUNABLE_U16)
2308 return -EINVAL;
2309 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302310 default:
2311 return -EINVAL;
2312 }
2313
2314 return 0;
2315}
2316
2317static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2318{
2319 int ret;
2320 struct ethtool_tunable tuna;
2321 const struct ethtool_ops *ops = dev->ethtool_ops;
2322 void *data;
2323
2324 if (!ops->get_tunable)
2325 return -EOPNOTSUPP;
2326 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2327 return -EFAULT;
2328 ret = ethtool_tunable_valid(&tuna);
2329 if (ret)
2330 return ret;
2331 data = kmalloc(tuna.len, GFP_USER);
2332 if (!data)
2333 return -ENOMEM;
2334 ret = ops->get_tunable(dev, &tuna, data);
2335 if (ret)
2336 goto out;
2337 useraddr += sizeof(tuna);
2338 ret = -EFAULT;
2339 if (copy_to_user(useraddr, data, tuna.len))
2340 goto out;
2341 ret = 0;
2342
2343out:
2344 kfree(data);
2345 return ret;
2346}
2347
2348static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2349{
2350 int ret;
2351 struct ethtool_tunable tuna;
2352 const struct ethtool_ops *ops = dev->ethtool_ops;
2353 void *data;
2354
2355 if (!ops->set_tunable)
2356 return -EOPNOTSUPP;
2357 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2358 return -EFAULT;
2359 ret = ethtool_tunable_valid(&tuna);
2360 if (ret)
2361 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302362 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002363 data = memdup_user(useraddr, tuna.len);
2364 if (IS_ERR(data))
2365 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302366 ret = ops->set_tunable(dev, &tuna, data);
2367
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302368 kfree(data);
2369 return ret;
2370}
2371
Kan Liang421797b2016-02-19 09:24:02 -05002372static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2373 void __user *useraddr,
2374 struct ethtool_per_queue_op *per_queue_opt)
2375{
2376 u32 bit;
2377 int ret;
2378 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2379
2380 if (!dev->ethtool_ops->get_per_queue_coalesce)
2381 return -EOPNOTSUPP;
2382
2383 useraddr += sizeof(*per_queue_opt);
2384
Yury Norov3aa56882018-02-06 15:38:06 -08002385 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2386 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002387
2388 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2389 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2390
2391 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2392 if (ret != 0)
2393 return ret;
2394 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2395 return -EFAULT;
2396 useraddr += sizeof(coalesce);
2397 }
2398
2399 return 0;
2400}
2401
Kan Liangf38d1382016-02-19 09:24:03 -05002402static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2403 void __user *useraddr,
2404 struct ethtool_per_queue_op *per_queue_opt)
2405{
2406 u32 bit;
2407 int i, ret = 0;
2408 int n_queue;
2409 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2410 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2411
2412 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2413 (!dev->ethtool_ops->get_per_queue_coalesce))
2414 return -EOPNOTSUPP;
2415
2416 useraddr += sizeof(*per_queue_opt);
2417
Yury Norov3aa56882018-02-06 15:38:06 -08002418 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002419 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2420 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2421 if (!backup)
2422 return -ENOMEM;
2423
2424 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2425 struct ethtool_coalesce coalesce;
2426
2427 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2428 if (ret != 0)
2429 goto roll_back;
2430
2431 tmp++;
2432
2433 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2434 ret = -EFAULT;
2435 goto roll_back;
2436 }
2437
2438 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2439 if (ret != 0)
2440 goto roll_back;
2441
2442 useraddr += sizeof(coalesce);
2443 }
2444
2445roll_back:
2446 if (ret != 0) {
2447 tmp = backup;
2448 for_each_set_bit(i, queue_mask, bit) {
2449 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2450 tmp++;
2451 }
2452 }
2453 kfree(backup);
2454
2455 return ret;
2456}
2457
Kan Liangac2c7ad2016-02-19 09:24:01 -05002458static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2459{
2460 struct ethtool_per_queue_op per_queue_opt;
2461
2462 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2463 return -EFAULT;
2464
2465 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002466 case ETHTOOL_GCOALESCE:
2467 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002468 case ETHTOOL_SCOALESCE:
2469 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002470 default:
2471 return -EOPNOTSUPP;
2472 };
2473}
2474
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002475static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2476{
2477 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002478 case ETHTOOL_PHY_DOWNSHIFT:
2479 if (tuna->len != sizeof(u8) ||
2480 tuna->type_id != ETHTOOL_TUNABLE_U8)
2481 return -EINVAL;
2482 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002483 default:
2484 return -EINVAL;
2485 }
2486
2487 return 0;
2488}
2489
2490static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2491{
2492 int ret;
2493 struct ethtool_tunable tuna;
2494 struct phy_device *phydev = dev->phydev;
2495 void *data;
2496
2497 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2498 return -EOPNOTSUPP;
2499
2500 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2501 return -EFAULT;
2502 ret = ethtool_phy_tunable_valid(&tuna);
2503 if (ret)
2504 return ret;
2505 data = kmalloc(tuna.len, GFP_USER);
2506 if (!data)
2507 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002508 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002509 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002510 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002511 if (ret)
2512 goto out;
2513 useraddr += sizeof(tuna);
2514 ret = -EFAULT;
2515 if (copy_to_user(useraddr, data, tuna.len))
2516 goto out;
2517 ret = 0;
2518
2519out:
2520 kfree(data);
2521 return ret;
2522}
2523
2524static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2525{
2526 int ret;
2527 struct ethtool_tunable tuna;
2528 struct phy_device *phydev = dev->phydev;
2529 void *data;
2530
2531 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2532 return -EOPNOTSUPP;
2533 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2534 return -EFAULT;
2535 ret = ethtool_phy_tunable_valid(&tuna);
2536 if (ret)
2537 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002538 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002539 data = memdup_user(useraddr, tuna.len);
2540 if (IS_ERR(data))
2541 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002542 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002543 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002544 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002545
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002546 kfree(data);
2547 return ret;
2548}
2549
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002550static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2551{
2552 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002553 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002554
2555 if (!dev->ethtool_ops->get_fecparam)
2556 return -EOPNOTSUPP;
2557
Edward Creea6d50512018-02-28 19:15:58 +00002558 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2559 if (rc)
2560 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002561
2562 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2563 return -EFAULT;
2564 return 0;
2565}
2566
2567static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2568{
2569 struct ethtool_fecparam fecparam;
2570
2571 if (!dev->ethtool_ops->set_fecparam)
2572 return -EOPNOTSUPP;
2573
2574 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2575 return -EFAULT;
2576
2577 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2578}
2579
Yan Burman600fed52013-06-03 02:03:34 +00002580/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Eric W. Biederman881d9662007-09-17 11:56:21 -07002582int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002584 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002586 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002588 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (!dev || !netif_device_present(dev))
2591 return -ENODEV;
2592
chavey97f8aef2010-04-07 21:54:42 -07002593 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return -EFAULT;
2595
Kan Liangac2c7ad2016-02-19 09:24:01 -05002596 if (ethcmd == ETHTOOL_PERQUEUE) {
2597 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2598 return -EFAULT;
2599 } else {
2600 sub_cmd = ethcmd;
2601 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002602 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002603 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002604 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002605 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002606 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002607 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002608 case ETHTOOL_GCOALESCE:
2609 case ETHTOOL_GRINGPARAM:
2610 case ETHTOOL_GPAUSEPARAM:
2611 case ETHTOOL_GRXCSUM:
2612 case ETHTOOL_GTXCSUM:
2613 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002614 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002615 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002616 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002617 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002618 case ETHTOOL_GTSO:
2619 case ETHTOOL_GPERMADDR:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002620 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002621 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002622 case ETHTOOL_GFLAGS:
2623 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002624 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002625 case ETHTOOL_GRXRINGS:
2626 case ETHTOOL_GRXCLSRLCNT:
2627 case ETHTOOL_GRXCLSRULE:
2628 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002629 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302630 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002631 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002632 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002633 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002634 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302635 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002636 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002637 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002638 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002639 break;
2640 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002641 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002642 return -EPERM;
2643 }
2644
chavey97f8aef2010-04-07 21:54:42 -07002645 if (dev->ethtool_ops->begin) {
2646 rc = dev->ethtool_ops->begin(dev);
2647 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002649 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002650 old_features = dev->features;
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 switch (ethcmd) {
2653 case ETHTOOL_GSET:
2654 rc = ethtool_get_settings(dev, useraddr);
2655 break;
2656 case ETHTOOL_SSET:
2657 rc = ethtool_set_settings(dev, useraddr);
2658 break;
2659 case ETHTOOL_GDRVINFO:
2660 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 break;
2662 case ETHTOOL_GREGS:
2663 rc = ethtool_get_regs(dev, useraddr);
2664 break;
2665 case ETHTOOL_GWOL:
2666 rc = ethtool_get_wol(dev, useraddr);
2667 break;
2668 case ETHTOOL_SWOL:
2669 rc = ethtool_set_wol(dev, useraddr);
2670 break;
2671 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002672 rc = ethtool_get_value(dev, useraddr, ethcmd,
2673 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 break;
2675 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002676 rc = ethtool_set_value_void(dev, useraddr,
2677 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002679 case ETHTOOL_GEEE:
2680 rc = ethtool_get_eee(dev, useraddr);
2681 break;
2682 case ETHTOOL_SEEE:
2683 rc = ethtool_set_eee(dev, useraddr);
2684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 case ETHTOOL_NWAY_RST:
2686 rc = ethtool_nway_reset(dev);
2687 break;
2688 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002689 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 break;
2691 case ETHTOOL_GEEPROM:
2692 rc = ethtool_get_eeprom(dev, useraddr);
2693 break;
2694 case ETHTOOL_SEEPROM:
2695 rc = ethtool_set_eeprom(dev, useraddr);
2696 break;
2697 case ETHTOOL_GCOALESCE:
2698 rc = ethtool_get_coalesce(dev, useraddr);
2699 break;
2700 case ETHTOOL_SCOALESCE:
2701 rc = ethtool_set_coalesce(dev, useraddr);
2702 break;
2703 case ETHTOOL_GRINGPARAM:
2704 rc = ethtool_get_ringparam(dev, useraddr);
2705 break;
2706 case ETHTOOL_SRINGPARAM:
2707 rc = ethtool_set_ringparam(dev, useraddr);
2708 break;
2709 case ETHTOOL_GPAUSEPARAM:
2710 rc = ethtool_get_pauseparam(dev, useraddr);
2711 break;
2712 case ETHTOOL_SPAUSEPARAM:
2713 rc = ethtool_set_pauseparam(dev, useraddr);
2714 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 case ETHTOOL_TEST:
2716 rc = ethtool_self_test(dev, useraddr);
2717 break;
2718 case ETHTOOL_GSTRINGS:
2719 rc = ethtool_get_strings(dev, useraddr);
2720 break;
2721 case ETHTOOL_PHYS_ID:
2722 rc = ethtool_phys_id(dev, useraddr);
2723 break;
2724 case ETHTOOL_GSTATS:
2725 rc = ethtool_get_stats(dev, useraddr);
2726 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002727 case ETHTOOL_GPERMADDR:
2728 rc = ethtool_get_perm_addr(dev, useraddr);
2729 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002730 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002731 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002732 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002733 break;
2734 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002735 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002736 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002737 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002738 rc = ethtool_get_value(dev, useraddr, ethcmd,
2739 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002740 break;
2741 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002742 rc = ethtool_set_value(dev, useraddr,
2743 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002744 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002745 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002746 case ETHTOOL_GRXRINGS:
2747 case ETHTOOL_GRXCLSRLCNT:
2748 case ETHTOOL_GRXCLSRULE:
2749 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002750 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002751 break;
2752 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002753 case ETHTOOL_SRXCLSRLDEL:
2754 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002755 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002756 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002757 case ETHTOOL_FLASHDEV:
2758 rc = ethtool_flash_device(dev, useraddr);
2759 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002760 case ETHTOOL_RESET:
2761 rc = ethtool_reset(dev, useraddr);
2762 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002763 case ETHTOOL_GSSET_INFO:
2764 rc = ethtool_get_sset_info(dev, useraddr);
2765 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002766 case ETHTOOL_GRXFHINDIR:
2767 rc = ethtool_get_rxfh_indir(dev, useraddr);
2768 break;
2769 case ETHTOOL_SRXFHINDIR:
2770 rc = ethtool_set_rxfh_indir(dev, useraddr);
2771 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302772 case ETHTOOL_GRSSH:
2773 rc = ethtool_get_rxfh(dev, useraddr);
2774 break;
2775 case ETHTOOL_SRSSH:
2776 rc = ethtool_set_rxfh(dev, useraddr);
2777 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002778 case ETHTOOL_GFEATURES:
2779 rc = ethtool_get_features(dev, useraddr);
2780 break;
2781 case ETHTOOL_SFEATURES:
2782 rc = ethtool_set_features(dev, useraddr);
2783 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002784 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002785 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002786 case ETHTOOL_GSG:
2787 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002788 case ETHTOOL_GGSO:
2789 case ETHTOOL_GGRO:
2790 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2791 break;
2792 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002793 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002794 case ETHTOOL_SSG:
2795 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002796 case ETHTOOL_SGSO:
2797 case ETHTOOL_SGRO:
2798 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2799 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002800 case ETHTOOL_GCHANNELS:
2801 rc = ethtool_get_channels(dev, useraddr);
2802 break;
2803 case ETHTOOL_SCHANNELS:
2804 rc = ethtool_set_channels(dev, useraddr);
2805 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002806 case ETHTOOL_SET_DUMP:
2807 rc = ethtool_set_dump(dev, useraddr);
2808 break;
2809 case ETHTOOL_GET_DUMP_FLAG:
2810 rc = ethtool_get_dump_flag(dev, useraddr);
2811 break;
2812 case ETHTOOL_GET_DUMP_DATA:
2813 rc = ethtool_get_dump_data(dev, useraddr);
2814 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002815 case ETHTOOL_GET_TS_INFO:
2816 rc = ethtool_get_ts_info(dev, useraddr);
2817 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002818 case ETHTOOL_GMODULEINFO:
2819 rc = ethtool_get_module_info(dev, useraddr);
2820 break;
2821 case ETHTOOL_GMODULEEEPROM:
2822 rc = ethtool_get_module_eeprom(dev, useraddr);
2823 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302824 case ETHTOOL_GTUNABLE:
2825 rc = ethtool_get_tunable(dev, useraddr);
2826 break;
2827 case ETHTOOL_STUNABLE:
2828 rc = ethtool_set_tunable(dev, useraddr);
2829 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002830 case ETHTOOL_GPHYSTATS:
2831 rc = ethtool_get_phy_stats(dev, useraddr);
2832 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002833 case ETHTOOL_PERQUEUE:
2834 rc = ethtool_set_per_queue(dev, useraddr);
2835 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002836 case ETHTOOL_GLINKSETTINGS:
2837 rc = ethtool_get_link_ksettings(dev, useraddr);
2838 break;
2839 case ETHTOOL_SLINKSETTINGS:
2840 rc = ethtool_set_link_ksettings(dev, useraddr);
2841 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002842 case ETHTOOL_PHY_GTUNABLE:
2843 rc = get_phy_tunable(dev, useraddr);
2844 break;
2845 case ETHTOOL_PHY_STUNABLE:
2846 rc = set_phy_tunable(dev, useraddr);
2847 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002848 case ETHTOOL_GFECPARAM:
2849 rc = ethtool_get_fecparam(dev, useraddr);
2850 break;
2851 case ETHTOOL_SFECPARAM:
2852 rc = ethtool_set_fecparam(dev, useraddr);
2853 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002855 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002857
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002858 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002860
2861 if (old_features != dev->features)
2862 netdev_features_change(dev);
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}