blob: 996813f345d545082e758162b9beeab0afb4f76f [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",
Ilya Lesokhin2342a852018-04-30 10:16:14 +0300113 [NETIF_F_HW_TLS_TX_BIT] = "tls-hw-tx-offload",
Ilya Lesokhin14136562018-07-13 14:33:36 +0300114 [NETIF_F_HW_TLS_RX_BIT] = "tls-hw-rx-offload",
Michał Mirosław9d921542011-11-15 15:29:55 +0000115};
116
Eyal Perry892311f2014-12-02 18:12:10 +0200117static const char
118rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
119 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
120 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800121 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200122};
123
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300124static const char
125tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
126 [ETHTOOL_ID_UNSPEC] = "Unspec",
127 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
128 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
Inbar Karmye1577c12017-11-20 16:14:30 +0200129 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300130};
131
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100132static const char
133phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
134 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100135 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100136};
137
Michał Mirosław5455c692011-02-15 16:59:17 +0000138static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
139{
140 struct ethtool_gfeatures cmd = {
141 .cmd = ETHTOOL_GFEATURES,
142 .size = ETHTOOL_DEV_FEATURE_WORDS,
143 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000144 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000145 u32 __user *sizeaddr;
146 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000147 int i;
148
149 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000150 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000151
152 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000153 features[i].available = (u32)(dev->hw_features >> (32 * i));
154 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
155 features[i].active = (u32)(dev->features >> (32 * i));
156 features[i].never_changed =
157 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000158 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000159
160 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
161 if (get_user(copy_size, sizeaddr))
162 return -EFAULT;
163
164 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
165 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
166
167 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
168 return -EFAULT;
169 useraddr += sizeof(cmd);
170 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
171 return -EFAULT;
172
173 return 0;
174}
175
176static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
177{
178 struct ethtool_sfeatures cmd;
179 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000180 netdev_features_t wanted = 0, valid = 0;
181 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000182
183 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
184 return -EFAULT;
185 useraddr += sizeof(cmd);
186
187 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
188 return -EINVAL;
189
190 if (copy_from_user(features, useraddr, sizeof(features)))
191 return -EFAULT;
192
Michał Mirosław475414f2011-11-15 15:29:55 +0000193 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000194 valid |= (netdev_features_t)features[i].valid << (32 * i);
195 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000196 }
197
198 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000199 return -EINVAL;
200
Michał Mirosław475414f2011-11-15 15:29:55 +0000201 if (valid & ~dev->hw_features) {
202 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000203 ret |= ETHTOOL_F_UNSUPPORTED;
204 }
205
Michał Mirosław475414f2011-11-15 15:29:55 +0000206 dev->wanted_features &= ~valid;
207 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700208 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000209
Michał Mirosław475414f2011-11-15 15:29:55 +0000210 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000211 ret |= ETHTOOL_F_WISH;
212
213 return ret;
214}
215
Michał Mirosław340ae162011-02-15 16:59:16 +0000216static int __ethtool_get_sset_count(struct net_device *dev, int sset)
217{
218 const struct ethtool_ops *ops = dev->ethtool_ops;
219
Michał Mirosław5455c692011-02-15 16:59:17 +0000220 if (sset == ETH_SS_FEATURES)
221 return ARRAY_SIZE(netdev_features_strings);
222
Eyal Perry892311f2014-12-02 18:12:10 +0200223 if (sset == ETH_SS_RSS_HASH_FUNCS)
224 return ARRAY_SIZE(rss_hash_func_strings);
225
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300226 if (sset == ETH_SS_TUNABLES)
227 return ARRAY_SIZE(tunable_strings);
228
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100229 if (sset == ETH_SS_PHY_TUNABLES)
230 return ARRAY_SIZE(phy_tunable_strings);
231
Florian Fainelli99943382018-04-25 12:12:48 -0700232 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
233 !ops->get_ethtool_phy_stats)
234 return phy_ethtool_get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100235
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000236 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000237 return ops->get_sset_count(dev, sset);
238 else
239 return -EOPNOTSUPP;
240}
241
242static void __ethtool_get_strings(struct net_device *dev,
243 u32 stringset, u8 *data)
244{
245 const struct ethtool_ops *ops = dev->ethtool_ops;
246
Michał Mirosław5455c692011-02-15 16:59:17 +0000247 if (stringset == ETH_SS_FEATURES)
248 memcpy(data, netdev_features_strings,
249 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200250 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
251 memcpy(data, rss_hash_func_strings,
252 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300253 else if (stringset == ETH_SS_TUNABLES)
254 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100255 else if (stringset == ETH_SS_PHY_TUNABLES)
256 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Florian Fainelli99943382018-04-25 12:12:48 -0700257 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
258 !ops->get_ethtool_phy_stats)
259 phy_ethtool_get_strings(dev->phydev, data);
260 else
Michał Mirosław5455c692011-02-15 16:59:17 +0000261 /* ops->get_strings is valid because checked earlier */
262 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000263}
264
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000265static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000266{
267 /* feature masks of legacy discrete ethtool ops */
268
269 switch (eth_cmd) {
270 case ETHTOOL_GTXCSUM:
271 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800272 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000273 case ETHTOOL_GRXCSUM:
274 case ETHTOOL_SRXCSUM:
275 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000276 case ETHTOOL_GSG:
277 case ETHTOOL_SSG:
278 return NETIF_F_SG;
279 case ETHTOOL_GTSO:
280 case ETHTOOL_STSO:
281 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000282 case ETHTOOL_GGSO:
283 case ETHTOOL_SGSO:
284 return NETIF_F_GSO;
285 case ETHTOOL_GGRO:
286 case ETHTOOL_SGRO:
287 return NETIF_F_GRO;
288 default:
289 BUG();
290 }
291}
292
Michał Mirosław0a417702011-02-15 16:59:17 +0000293static int ethtool_get_one_feature(struct net_device *dev,
294 char __user *useraddr, u32 ethcmd)
295{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000296 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000297 struct ethtool_value edata = {
298 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000299 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000300 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000301
Michał Mirosław0a417702011-02-15 16:59:17 +0000302 if (copy_to_user(useraddr, &edata, sizeof(edata)))
303 return -EFAULT;
304 return 0;
305}
306
Michał Mirosław0a417702011-02-15 16:59:17 +0000307static int ethtool_set_one_feature(struct net_device *dev,
308 void __user *useraddr, u32 ethcmd)
309{
310 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000311 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000312
313 if (copy_from_user(&edata, useraddr, sizeof(edata)))
314 return -EFAULT;
315
Michał Mirosław86794882011-02-15 16:59:17 +0000316 mask = ethtool_get_feature_mask(ethcmd);
317 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000318 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000319 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000320
321 if (edata.data)
322 dev->wanted_features |= mask;
323 else
324 dev->wanted_features &= ~mask;
325
326 __netdev_update_features(dev);
327
328 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000329}
330
Michał Mirosław02b3a552011-11-15 15:29:55 +0000331#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
332 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000333#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
334 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
335 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000336
337static u32 __ethtool_get_flags(struct net_device *dev)
338{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000339 u32 flags = 0;
340
Dragos Foianu8a731252013-07-13 14:43:00 +0100341 if (dev->features & NETIF_F_LRO)
342 flags |= ETH_FLAG_LRO;
343 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
344 flags |= ETH_FLAG_RXVLAN;
345 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
346 flags |= ETH_FLAG_TXVLAN;
347 if (dev->features & NETIF_F_NTUPLE)
348 flags |= ETH_FLAG_NTUPLE;
349 if (dev->features & NETIF_F_RXHASH)
350 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000351
352 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000353}
354
355static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000356{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000357 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000358
Michał Mirosław02b3a552011-11-15 15:29:55 +0000359 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000360 return -EINVAL;
361
Dragos Foianu8a731252013-07-13 14:43:00 +0100362 if (data & ETH_FLAG_LRO)
363 features |= NETIF_F_LRO;
364 if (data & ETH_FLAG_RXVLAN)
365 features |= NETIF_F_HW_VLAN_CTAG_RX;
366 if (data & ETH_FLAG_TXVLAN)
367 features |= NETIF_F_HW_VLAN_CTAG_TX;
368 if (data & ETH_FLAG_NTUPLE)
369 features |= NETIF_F_NTUPLE;
370 if (data & ETH_FLAG_RXHASH)
371 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000372
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000373 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000374 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000375 if (changed & ~dev->hw_features)
376 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
377
378 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000379 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000380
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700381 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000382
383 return 0;
384}
385
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700386/* Given two link masks, AND them together and save the result in dst. */
387void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
388 struct ethtool_link_ksettings *src)
389{
390 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
391 unsigned int idx = 0;
392
393 for (; idx < size; idx++) {
394 dst->link_modes.supported[idx] &=
395 src->link_modes.supported[idx];
396 dst->link_modes.advertising[idx] &=
397 src->link_modes.advertising[idx];
398 }
399}
400EXPORT_SYMBOL(ethtool_intersect_link_masks);
401
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200402void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
403 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800404{
405 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
406 dst[0] = legacy_u32;
407}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200408EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800409
410/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200411bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
412 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800413{
414 bool retval = true;
415
416 /* TODO: following test will soon always be true */
417 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
418 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
419
420 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
421 bitmap_fill(ext, 32);
422 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
423 if (bitmap_intersects(ext, src,
424 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
425 /* src mask goes beyond bit 31 */
426 retval = false;
427 }
428 }
429 *legacy_u32 = src[0];
430 return retval;
431}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200432EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800433
434/* return false if legacy contained non-0 deprecated fields
Niklas Söderlund95491e32017-10-20 01:32:08 +0200435 * maxtxpkt/maxrxpkt. rest of ksettings always updated
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800436 */
437static bool
438convert_legacy_settings_to_link_ksettings(
439 struct ethtool_link_ksettings *link_ksettings,
440 const struct ethtool_cmd *legacy_settings)
441{
442 bool retval = true;
443
444 memset(link_ksettings, 0, sizeof(*link_ksettings));
445
446 /* This is used to tell users that driver is still using these
447 * deprecated legacy fields, and they should not use
448 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
449 */
Niklas Söderlund95491e32017-10-20 01:32:08 +0200450 if (legacy_settings->maxtxpkt ||
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800451 legacy_settings->maxrxpkt)
452 retval = false;
453
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200454 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800455 link_ksettings->link_modes.supported,
456 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200457 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800458 link_ksettings->link_modes.advertising,
459 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200460 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800461 link_ksettings->link_modes.lp_advertising,
462 legacy_settings->lp_advertising);
463 link_ksettings->base.speed
464 = ethtool_cmd_speed(legacy_settings);
465 link_ksettings->base.duplex
466 = legacy_settings->duplex;
467 link_ksettings->base.port
468 = legacy_settings->port;
469 link_ksettings->base.phy_address
470 = legacy_settings->phy_address;
471 link_ksettings->base.autoneg
472 = legacy_settings->autoneg;
473 link_ksettings->base.mdio_support
474 = legacy_settings->mdio_support;
475 link_ksettings->base.eth_tp_mdix
476 = legacy_settings->eth_tp_mdix;
477 link_ksettings->base.eth_tp_mdix_ctrl
478 = legacy_settings->eth_tp_mdix_ctrl;
479 return retval;
480}
481
482/* return false if ksettings link modes had higher bits
483 * set. legacy_settings always updated (best effort)
484 */
485static bool
486convert_link_ksettings_to_legacy_settings(
487 struct ethtool_cmd *legacy_settings,
488 const struct ethtool_link_ksettings *link_ksettings)
489{
490 bool retval = true;
491
492 memset(legacy_settings, 0, sizeof(*legacy_settings));
493 /* this also clears the deprecated fields in legacy structure:
494 * __u8 transceiver;
495 * __u32 maxtxpkt;
496 * __u32 maxrxpkt;
497 */
498
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200499 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800500 &legacy_settings->supported,
501 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200502 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800503 &legacy_settings->advertising,
504 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200505 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800506 &legacy_settings->lp_advertising,
507 link_ksettings->link_modes.lp_advertising);
508 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
509 legacy_settings->duplex
510 = link_ksettings->base.duplex;
511 legacy_settings->port
512 = link_ksettings->base.port;
513 legacy_settings->phy_address
514 = link_ksettings->base.phy_address;
515 legacy_settings->autoneg
516 = link_ksettings->base.autoneg;
517 legacy_settings->mdio_support
518 = link_ksettings->base.mdio_support;
519 legacy_settings->eth_tp_mdix
520 = link_ksettings->base.eth_tp_mdix;
521 legacy_settings->eth_tp_mdix_ctrl
522 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700523 legacy_settings->transceiver
524 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800525 return retval;
526}
527
528/* number of 32-bit words to store the user's link mode bitmaps */
529#define __ETHTOOL_LINK_MODE_MASK_NU32 \
530 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
531
532/* layout of the struct passed from/to userland */
533struct ethtool_link_usettings {
534 struct ethtool_link_settings base;
535 struct {
536 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
537 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
538 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
539 } link_modes;
540};
541
542/* Internal kernel helper to query a device ethtool_link_settings.
543 *
544 * Backward compatibility note: for compatibility with legacy drivers
545 * that implement only the ethtool_cmd API, this has to work with both
546 * drivers implementing get_link_ksettings API and drivers
547 * implementing get_settings API. When drivers implement get_settings
548 * and report ethtool_cmd deprecated fields
549 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
550 * because the resulting struct ethtool_link_settings does not report them.
551 */
552int __ethtool_get_link_ksettings(struct net_device *dev,
553 struct ethtool_link_ksettings *link_ksettings)
554{
555 int err;
556 struct ethtool_cmd cmd;
557
558 ASSERT_RTNL();
559
560 if (dev->ethtool_ops->get_link_ksettings) {
561 memset(link_ksettings, 0, sizeof(*link_ksettings));
562 return dev->ethtool_ops->get_link_ksettings(dev,
563 link_ksettings);
564 }
565
566 /* driver doesn't support %ethtool_link_ksettings API. revert to
567 * legacy %ethtool_cmd API, unless it's not supported either.
568 * TODO: remove when ethtool_ops::get_settings disappears internally
569 */
David Decotigny3237fc62016-02-24 10:58:11 -0800570 if (!dev->ethtool_ops->get_settings)
571 return -EOPNOTSUPP;
572
573 memset(&cmd, 0, sizeof(cmd));
574 cmd.cmd = ETHTOOL_GSET;
575 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800576 if (err < 0)
577 return err;
578
579 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
580 */
581 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
582 return err;
583}
584EXPORT_SYMBOL(__ethtool_get_link_ksettings);
585
586/* convert ethtool_link_usettings in user space to a kernel internal
587 * ethtool_link_ksettings. return 0 on success, errno on error.
588 */
589static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
590 const void __user *from)
591{
592 struct ethtool_link_usettings link_usettings;
593
594 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
595 return -EFAULT;
596
597 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800598 bitmap_from_arr32(to->link_modes.supported,
599 link_usettings.link_modes.supported,
600 __ETHTOOL_LINK_MODE_MASK_NBITS);
601 bitmap_from_arr32(to->link_modes.advertising,
602 link_usettings.link_modes.advertising,
603 __ETHTOOL_LINK_MODE_MASK_NBITS);
604 bitmap_from_arr32(to->link_modes.lp_advertising,
605 link_usettings.link_modes.lp_advertising,
606 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800607
608 return 0;
609}
610
611/* convert a kernel internal ethtool_link_ksettings to
612 * ethtool_link_usettings in user space. return 0 on success, errno on
613 * error.
614 */
615static int
616store_link_ksettings_for_user(void __user *to,
617 const struct ethtool_link_ksettings *from)
618{
619 struct ethtool_link_usettings link_usettings;
620
621 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800622 bitmap_to_arr32(link_usettings.link_modes.supported,
623 from->link_modes.supported,
624 __ETHTOOL_LINK_MODE_MASK_NBITS);
625 bitmap_to_arr32(link_usettings.link_modes.advertising,
626 from->link_modes.advertising,
627 __ETHTOOL_LINK_MODE_MASK_NBITS);
628 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
629 from->link_modes.lp_advertising,
630 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800631
632 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
633 return -EFAULT;
634
635 return 0;
636}
637
638/* Query device for its ethtool_link_settings.
639 *
640 * Backward compatibility note: this function must fail when driver
641 * does not implement ethtool::get_link_ksettings, even if legacy
642 * ethtool_ops::get_settings is implemented. This tells new versions
643 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
644 * this driver, so that they can correctly access the ethtool_cmd
645 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
646 * implements ethtool_ops::get_settings anymore.
647 */
648static int ethtool_get_link_ksettings(struct net_device *dev,
649 void __user *useraddr)
650{
651 int err = 0;
652 struct ethtool_link_ksettings link_ksettings;
653
654 ASSERT_RTNL();
655
656 if (!dev->ethtool_ops->get_link_ksettings)
657 return -EOPNOTSUPP;
658
659 /* handle bitmap nbits handshake */
660 if (copy_from_user(&link_ksettings.base, useraddr,
661 sizeof(link_ksettings.base)))
662 return -EFAULT;
663
664 if (__ETHTOOL_LINK_MODE_MASK_NU32
665 != link_ksettings.base.link_mode_masks_nwords) {
666 /* wrong link mode nbits requested */
667 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000668 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800669 /* send back number of words required as negative val */
670 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
671 "need too many bits for link modes!");
672 link_ksettings.base.link_mode_masks_nwords
673 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
674
675 /* copy the base fields back to user, not the link
676 * mode bitmaps
677 */
678 if (copy_to_user(useraddr, &link_ksettings.base,
679 sizeof(link_ksettings.base)))
680 return -EFAULT;
681
682 return 0;
683 }
684
685 /* handshake successful: user/kernel agree on
686 * link_mode_masks_nwords
687 */
688
689 memset(&link_ksettings, 0, sizeof(link_ksettings));
690 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
691 if (err < 0)
692 return err;
693
694 /* make sure we tell the right values to user */
695 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
696 link_ksettings.base.link_mode_masks_nwords
697 = __ETHTOOL_LINK_MODE_MASK_NU32;
698
699 return store_link_ksettings_for_user(useraddr, &link_ksettings);
700}
701
702/* Update device ethtool_link_settings.
703 *
704 * Backward compatibility note: this function must fail when driver
705 * does not implement ethtool::set_link_ksettings, even if legacy
706 * ethtool_ops::set_settings is implemented. This tells new versions
707 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
708 * this driver, so that they can correctly update the ethtool_cmd
709 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
710 * implements ethtool_ops::get_settings anymore.
711 */
712static int ethtool_set_link_ksettings(struct net_device *dev,
713 void __user *useraddr)
714{
715 int err;
716 struct ethtool_link_ksettings link_ksettings;
717
718 ASSERT_RTNL();
719
720 if (!dev->ethtool_ops->set_link_ksettings)
721 return -EOPNOTSUPP;
722
723 /* make sure nbits field has expected value */
724 if (copy_from_user(&link_ksettings.base, useraddr,
725 sizeof(link_ksettings.base)))
726 return -EFAULT;
727
728 if (__ETHTOOL_LINK_MODE_MASK_NU32
729 != link_ksettings.base.link_mode_masks_nwords)
730 return -EINVAL;
731
732 /* copy the whole structure, now that we know it has expected
733 * format
734 */
735 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
736 if (err)
737 return err;
738
739 /* re-check nwords field, just in case */
740 if (__ETHTOOL_LINK_MODE_MASK_NU32
741 != link_ksettings.base.link_mode_masks_nwords)
742 return -EINVAL;
743
744 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
745}
746
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800747/* Query device for its ethtool_cmd settings.
748 *
749 * Backward compatibility note: for compatibility with legacy ethtool,
750 * this has to work with both drivers implementing get_link_ksettings
751 * API and drivers implementing get_settings API. When drivers
752 * implement get_link_ksettings and report higher link mode bits, a
753 * kernel warning is logged once (with name of 1st driver/device) to
754 * recommend user to upgrade ethtool, but the command is successful
755 * (only the lower link mode bits reported back to user).
756 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000757static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
758{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000759 struct ethtool_cmd cmd;
760
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800761 ASSERT_RTNL();
762
763 if (dev->ethtool_ops->get_link_ksettings) {
764 /* First, use link_ksettings API if it is supported */
765 int err;
766 struct ethtool_link_ksettings link_ksettings;
767
768 memset(&link_ksettings, 0, sizeof(link_ksettings));
769 err = dev->ethtool_ops->get_link_ksettings(dev,
770 &link_ksettings);
771 if (err < 0)
772 return err;
Stephen Hemminger71891e22017-12-29 10:02:52 -0800773 convert_link_ksettings_to_legacy_settings(&cmd,
774 &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800775
776 /* send a sensible cmd tag back to user */
777 cmd.cmd = ETHTOOL_GSET;
778 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800779 /* driver doesn't support %ethtool_link_ksettings
780 * API. revert to legacy %ethtool_cmd API, unless it's
781 * not supported either.
782 */
David Decotigny3237fc62016-02-24 10:58:11 -0800783 int err;
784
785 if (!dev->ethtool_ops->get_settings)
786 return -EOPNOTSUPP;
787
788 memset(&cmd, 0, sizeof(cmd));
789 cmd.cmd = ETHTOOL_GSET;
790 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800791 if (err < 0)
792 return err;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
796 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799}
800
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800801/* Update device link settings with given ethtool_cmd.
802 *
803 * Backward compatibility note: for compatibility with legacy ethtool,
804 * this has to work with both drivers implementing set_link_ksettings
805 * API and drivers implementing set_settings API. When drivers
806 * implement set_link_ksettings and user's request updates deprecated
807 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
808 * warning is logged once (with name of 1st driver/device) to
809 * recommend user to upgrade ethtool, and the request is rejected.
810 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
812{
813 struct ethtool_cmd cmd;
814
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800815 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
818 return -EFAULT;
819
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800820 /* first, try new %ethtool_link_ksettings API. */
821 if (dev->ethtool_ops->set_link_ksettings) {
822 struct ethtool_link_ksettings link_ksettings;
823
824 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
825 &cmd))
826 return -EINVAL;
827
828 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
829 link_ksettings.base.link_mode_masks_nwords
830 = __ETHTOOL_LINK_MODE_MASK_NU32;
831 return dev->ethtool_ops->set_link_ksettings(dev,
832 &link_ksettings);
833 }
834
835 /* legacy %ethtool_cmd API */
836
837 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
838 * disappears internally
839 */
840
841 if (!dev->ethtool_ops->set_settings)
842 return -EOPNOTSUPP;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return dev->ethtool_ops->set_settings(dev, &cmd);
845}
846
chavey97f8aef2010-04-07 21:54:42 -0700847static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
848 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700851 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 memset(&info, 0, sizeof(info));
854 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000855 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700856 ops->get_drvinfo(dev, &info);
857 } else if (dev->dev.parent && dev->dev.parent->driver) {
858 strlcpy(info.bus_info, dev_name(dev->dev.parent),
859 sizeof(info.bus_info));
860 strlcpy(info.driver, dev->dev.parent->driver->name,
861 sizeof(info.driver));
862 } else {
863 return -EOPNOTSUPP;
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Jeff Garzik723b2f52010-03-03 22:51:50 +0000866 /*
867 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000868 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000869 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000870 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700871 int rc;
872
873 rc = ops->get_sset_count(dev, ETH_SS_TEST);
874 if (rc >= 0)
875 info.testinfo_len = rc;
876 rc = ops->get_sset_count(dev, ETH_SS_STATS);
877 if (rc >= 0)
878 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700879 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
880 if (rc >= 0)
881 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700882 }
Yunsheng Lin51dc2842018-12-26 19:51:46 +0800883 if (ops->get_regs_len) {
884 int ret = ops->get_regs_len(dev);
885
886 if (ret > 0)
887 info.regdump_len = ret;
888 }
889
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000890 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 info.eedump_len = ops->get_eeprom_len(dev);
892
893 if (copy_to_user(useraddr, &info, sizeof(info)))
894 return -EFAULT;
895 return 0;
896}
897
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800898static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700899 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000900{
901 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000902 u64 sset_mask;
903 int i, idx = 0, n_bits = 0, ret, rc;
904 u32 *info_buf = NULL;
905
Jeff Garzik723b2f52010-03-03 22:51:50 +0000906 if (copy_from_user(&info, useraddr, sizeof(info)))
907 return -EFAULT;
908
909 /* store copy of mask, because we zero struct later on */
910 sset_mask = info.sset_mask;
911 if (!sset_mask)
912 return 0;
913
914 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000915 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000916
917 memset(&info, 0, sizeof(info));
918 info.cmd = ETHTOOL_GSSET_INFO;
919
Kees Cook6396bb22018-06-12 14:03:40 -0700920 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000921 if (!info_buf)
922 return -ENOMEM;
923
924 /*
925 * fill return buffer based on input bitmask and successful
926 * get_sset_count return
927 */
928 for (i = 0; i < 64; i++) {
929 if (!(sset_mask & (1ULL << i)))
930 continue;
931
Michał Mirosław340ae162011-02-15 16:59:16 +0000932 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000933 if (rc >= 0) {
934 info.sset_mask |= (1ULL << i);
935 info_buf[idx++] = rc;
936 }
937 }
938
939 ret = -EFAULT;
940 if (copy_to_user(useraddr, &info, sizeof(info)))
941 goto out;
942
943 useraddr += offsetof(struct ethtool_sset_info, data);
944 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
945 goto out;
946
947 ret = 0;
948
949out:
950 kfree(info_buf);
951 return ret;
952}
953
chavey97f8aef2010-04-07 21:54:42 -0700954static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000955 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700956{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000957 struct ethtool_rxnfc info;
958 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000959 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700960
Santwona Behera59089d82009-02-20 00:58:13 -0800961 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700962 return -EOPNOTSUPP;
963
Ben Hutchingsbf988432010-06-28 08:45:58 +0000964 /* struct ethtool_rxnfc was originally defined for
965 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
966 * members. User-space might still be using that
967 * definition. */
968 if (cmd == ETHTOOL_SRXFH)
969 info_size = (offsetof(struct ethtool_rxnfc, data) +
970 sizeof(info.data));
971
972 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700973 return -EFAULT;
974
Ben Hutchings55664f32012-01-03 12:04:51 +0000975 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
976 if (rc)
977 return rc;
978
979 if (cmd == ETHTOOL_SRXCLSRLINS &&
980 copy_to_user(useraddr, &info, info_size))
981 return -EFAULT;
982
983 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700984}
985
chavey97f8aef2010-04-07 21:54:42 -0700986static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000987 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700988{
989 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000990 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800991 const struct ethtool_ops *ops = dev->ethtool_ops;
992 int ret;
993 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700994
Santwona Behera59089d82009-02-20 00:58:13 -0800995 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700996 return -EOPNOTSUPP;
997
Ben Hutchingsbf988432010-06-28 08:45:58 +0000998 /* struct ethtool_rxnfc was originally defined for
999 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1000 * members. User-space might still be using that
1001 * definition. */
1002 if (cmd == ETHTOOL_GRXFH)
1003 info_size = (offsetof(struct ethtool_rxnfc, data) +
1004 sizeof(info.data));
1005
1006 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001007 return -EFAULT;
1008
Edward Cree84a1d9c42018-03-08 15:45:03 +00001009 /* If FLOW_RSS was requested then user-space must be using the
1010 * new definition, as FLOW_RSS is newer.
1011 */
1012 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1013 info_size = sizeof(info);
1014 if (copy_from_user(&info, useraddr, info_size))
1015 return -EFAULT;
Wenwen Wangd656fe42018-04-30 12:31:13 -05001016 /* Since malicious users may modify the original data,
1017 * we need to check whether FLOW_RSS is still requested.
1018 */
1019 if (!(info.flow_type & FLOW_RSS))
1020 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001021 }
1022
Wenwen Wang2bb32072018-10-09 08:15:38 -05001023 if (info.cmd != cmd)
1024 return -EINVAL;
1025
Santwona Behera59089d82009-02-20 00:58:13 -08001026 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1027 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001028 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cook6396bb22018-06-12 14:03:40 -07001029 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001030 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001031 if (!rule_buf)
1032 return -ENOMEM;
1033 }
1034 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001035
Santwona Behera59089d82009-02-20 00:58:13 -08001036 ret = ops->get_rxnfc(dev, &info, rule_buf);
1037 if (ret < 0)
1038 goto err_out;
1039
1040 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001041 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001042 goto err_out;
1043
1044 if (rule_buf) {
1045 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1046 if (copy_to_user(useraddr, rule_buf,
1047 info.rule_cnt * sizeof(u32)))
1048 goto err_out;
1049 }
1050 ret = 0;
1051
1052err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001053 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001054
1055 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001056}
1057
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301058static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1059 struct ethtool_rxnfc *rx_rings,
1060 u32 size)
1061{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001062 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301063
1064 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001065 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301066
1067 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001068 for (i = 0; i < size; i++)
1069 if (indir[i] >= rx_rings->data)
1070 return -EINVAL;
1071
1072 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301073}
1074
Kim Jonesba905f52016-02-02 03:51:16 +00001075u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001076
1077void netdev_rss_key_fill(void *buffer, size_t len)
1078{
1079 BUG_ON(len > sizeof(netdev_rss_key));
1080 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1081 memcpy(buffer, netdev_rss_key, len);
1082}
1083EXPORT_SYMBOL(netdev_rss_key_fill);
1084
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001085static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1086{
1087 u32 dev_size, current_max = 0;
1088 u32 *indir;
1089 int ret;
1090
1091 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1092 !dev->ethtool_ops->get_rxfh)
1093 return -EOPNOTSUPP;
1094 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1095 if (dev_size == 0)
1096 return -EOPNOTSUPP;
1097
1098 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1099 if (!indir)
1100 return -ENOMEM;
1101
1102 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1103 if (ret)
1104 goto out;
1105
1106 while (dev_size--)
1107 current_max = max(current_max, indir[dev_size]);
1108
1109 *max = current_max;
1110
1111out:
1112 kfree(indir);
1113 return ret;
1114}
1115
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001116static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1117 void __user *useraddr)
1118{
Ben Hutchings7850f632011-12-15 13:55:01 +00001119 u32 user_size, dev_size;
1120 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001121 int ret;
1122
Ben Hutchings7850f632011-12-15 13:55:01 +00001123 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001124 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001125 return -EOPNOTSUPP;
1126 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1127 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001128 return -EOPNOTSUPP;
1129
Ben Hutchings7850f632011-12-15 13:55:01 +00001130 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001131 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001132 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001133 return -EFAULT;
1134
Ben Hutchings7850f632011-12-15 13:55:01 +00001135 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1136 &dev_size, sizeof(dev_size)))
1137 return -EFAULT;
1138
1139 /* If the user buffer size is 0, this is just a query for the
1140 * device table size. Otherwise, if it's smaller than the
1141 * device table size it's an error.
1142 */
1143 if (user_size < dev_size)
1144 return user_size == 0 ? 0 : -EINVAL;
1145
1146 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001147 if (!indir)
1148 return -ENOMEM;
1149
Eyal Perry892311f2014-12-02 18:12:10 +02001150 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001151 if (ret)
1152 goto out;
1153
Ben Hutchings7850f632011-12-15 13:55:01 +00001154 if (copy_to_user(useraddr +
1155 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1156 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001157 ret = -EFAULT;
1158
1159out:
1160 kfree(indir);
1161 return ret;
1162}
1163
1164static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1165 void __user *useraddr)
1166{
Ben Hutchings7850f632011-12-15 13:55:01 +00001167 struct ethtool_rxnfc rx_rings;
1168 u32 user_size, dev_size, i;
1169 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001170 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001171 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301172 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001173
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001174 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001175 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001176 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001177
1178 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001179 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001180 return -EOPNOTSUPP;
1181
Ben Hutchings7850f632011-12-15 13:55:01 +00001182 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001183 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001184 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001185 return -EFAULT;
1186
Ben Hutchings278bc422011-12-15 13:56:49 +00001187 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001188 return -EINVAL;
1189
1190 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001191 if (!indir)
1192 return -ENOMEM;
1193
Ben Hutchings7850f632011-12-15 13:55:01 +00001194 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001195 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001196 if (ret)
1197 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001198
1199 if (user_size == 0) {
1200 for (i = 0; i < dev_size; i++)
1201 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1202 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301203 ret = ethtool_copy_validate_indir(indir,
1204 useraddr + ringidx_offset,
1205 &rx_rings,
1206 dev_size);
1207 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001208 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001209 }
1210
Eyal Perry892311f2014-12-02 18:12:10 +02001211 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001212 if (ret)
1213 goto out;
1214
1215 /* indicate whether rxfh was set to default */
1216 if (user_size == 0)
1217 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1218 else
1219 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001220
1221out:
1222 kfree(indir);
1223 return ret;
1224}
1225
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301226static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1227 void __user *useraddr)
1228{
1229 int ret;
1230 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001231 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301232 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001233 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301234 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001235 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301236 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001237 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301238 u8 *hkey = NULL;
1239 u8 *rss_config;
1240
Eyal Perry892311f2014-12-02 18:12:10 +02001241 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301242 return -EOPNOTSUPP;
1243
1244 if (ops->get_rxfh_indir_size)
1245 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301246 if (ops->get_rxfh_key_size)
1247 dev_key_size = ops->get_rxfh_key_size(dev);
1248
Ben Hutchingsf062a382014-05-15 16:28:07 +01001249 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301250 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001251 user_indir_size = rxfh.indir_size;
1252 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301253
Ben Hutchingsf062a382014-05-15 16:28:07 +01001254 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001255 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001256 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001257 /* Most drivers don't handle rss_context, check it's 0 as well */
1258 if (rxfh.rss_context && !ops->get_rxfh_context)
1259 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001260
1261 rxfh.indir_size = dev_indir_size;
1262 rxfh.key_size = dev_key_size;
1263 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301264 return -EFAULT;
1265
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301266 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1267 (user_key_size && (user_key_size != dev_key_size)))
1268 return -EINVAL;
1269
1270 indir_bytes = user_indir_size * sizeof(indir[0]);
1271 total_size = indir_bytes + user_key_size;
1272 rss_config = kzalloc(total_size, GFP_USER);
1273 if (!rss_config)
1274 return -ENOMEM;
1275
1276 if (user_indir_size)
1277 indir = (u32 *)rss_config;
1278
1279 if (user_key_size)
1280 hkey = rss_config + indir_bytes;
1281
Edward Cree84a1d9c42018-03-08 15:45:03 +00001282 if (rxfh.rss_context)
1283 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1284 &dev_hfunc,
1285 rxfh.rss_context);
1286 else
1287 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001288 if (ret)
1289 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301290
Eyal Perry892311f2014-12-02 18:12:10 +02001291 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1292 &dev_hfunc, sizeof(rxfh.hfunc))) {
1293 ret = -EFAULT;
1294 } else if (copy_to_user(useraddr +
1295 offsetof(struct ethtool_rxfh, rss_config[0]),
1296 rss_config, total_size)) {
1297 ret = -EFAULT;
1298 }
1299out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301300 kfree(rss_config);
1301
1302 return ret;
1303}
1304
1305static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1306 void __user *useraddr)
1307{
1308 int ret;
1309 const struct ethtool_ops *ops = dev->ethtool_ops;
1310 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001311 struct ethtool_rxfh rxfh;
1312 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301313 u32 *indir = NULL, indir_bytes = 0;
1314 u8 *hkey = NULL;
1315 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301316 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001317 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301318
Eyal Perry892311f2014-12-02 18:12:10 +02001319 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301320 return -EOPNOTSUPP;
1321
1322 if (ops->get_rxfh_indir_size)
1323 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301324 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001325 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301326
Ben Hutchingsf062a382014-05-15 16:28:07 +01001327 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301328 return -EFAULT;
1329
Ben Hutchingsf062a382014-05-15 16:28:07 +01001330 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001331 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001332 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001333 /* Most drivers don't handle rss_context, check it's 0 as well */
1334 if (rxfh.rss_context && !ops->set_rxfh_context)
1335 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001336
Eyal Perry892311f2014-12-02 18:12:10 +02001337 /* If either indir, hash key or function is valid, proceed further.
1338 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301339 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001340 if ((rxfh.indir_size &&
1341 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1342 rxfh.indir_size != dev_indir_size) ||
1343 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1344 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001345 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301346 return -EINVAL;
1347
Ben Hutchingsf062a382014-05-15 16:28:07 +01001348 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301349 indir_bytes = dev_indir_size * sizeof(indir[0]);
1350
Ben Hutchingsf062a382014-05-15 16:28:07 +01001351 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301352 if (!rss_config)
1353 return -ENOMEM;
1354
1355 rx_rings.cmd = ETHTOOL_GRXRINGS;
1356 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1357 if (ret)
1358 goto out;
1359
Edward Cree84a1d9c42018-03-08 15:45:03 +00001360 /* rxfh.indir_size == 0 means reset the indir table to default (master
1361 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001362 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301363 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001364 if (rxfh.indir_size &&
1365 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301366 indir = (u32 *)rss_config;
1367 ret = ethtool_copy_validate_indir(indir,
1368 useraddr + rss_cfg_offset,
1369 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001370 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301371 if (ret)
1372 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001373 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001374 if (rxfh.rss_context == 0) {
1375 indir = (u32 *)rss_config;
1376 for (i = 0; i < dev_indir_size; i++)
1377 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1378 } else {
1379 delete = true;
1380 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301381 }
1382
Ben Hutchingsf062a382014-05-15 16:28:07 +01001383 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301384 hkey = rss_config + indir_bytes;
1385 if (copy_from_user(hkey,
1386 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001387 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301388 ret = -EFAULT;
1389 goto out;
1390 }
1391 }
1392
Edward Cree84a1d9c42018-03-08 15:45:03 +00001393 if (rxfh.rss_context)
1394 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1395 &rxfh.rss_context, delete);
1396 else
1397 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001398 if (ret)
1399 goto out;
1400
Edward Cree84a1d9c42018-03-08 15:45:03 +00001401 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1402 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1403 ret = -EFAULT;
1404
1405 if (!rxfh.rss_context) {
1406 /* indicate whether rxfh was set to default */
1407 if (rxfh.indir_size == 0)
1408 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1409 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1410 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1411 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301412
1413out:
1414 kfree(rss_config);
1415 return ret;
1416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1419{
1420 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001421 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 void *regbuf;
1423 int reglen, ret;
1424
1425 if (!ops->get_regs || !ops->get_regs_len)
1426 return -EOPNOTSUPP;
1427
1428 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1429 return -EFAULT;
1430
1431 reglen = ops->get_regs_len(dev);
Yunsheng Lin51dc2842018-12-26 19:51:46 +08001432 if (reglen <= 0)
1433 return reglen;
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (regs.len > reglen)
1436 regs.len = reglen;
1437
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001438 regbuf = NULL;
1439 if (reglen) {
1440 regbuf = vzalloc(reglen);
1441 if (!regbuf)
1442 return -ENOMEM;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Vivien Didelotd6782b82019-06-03 16:57:13 -04001445 if (regs.len < reglen)
1446 reglen = regs.len;
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ops->get_regs(dev, &regs, regbuf);
1449
1450 ret = -EFAULT;
1451 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1452 goto out;
1453 useraddr += offsetof(struct ethtool_regs, data);
Vivien Didelotd6782b82019-06-03 16:57:13 -04001454 if (copy_to_user(useraddr, regbuf, reglen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto out;
1456 ret = 0;
1457
1458 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001459 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return ret;
1461}
1462
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001463static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1464{
1465 struct ethtool_value reset;
1466 int ret;
1467
1468 if (!dev->ethtool_ops->reset)
1469 return -EOPNOTSUPP;
1470
1471 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1472 return -EFAULT;
1473
1474 ret = dev->ethtool_ops->reset(dev, &reset.data);
1475 if (ret)
1476 return ret;
1477
1478 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1479 return -EFAULT;
1480 return 0;
1481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1484{
Roland Dreier8e557422010-02-11 12:14:23 -08001485 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (!dev->ethtool_ops->get_wol)
1488 return -EOPNOTSUPP;
1489
1490 dev->ethtool_ops->get_wol(dev, &wol);
1491
1492 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1493 return -EFAULT;
1494 return 0;
1495}
1496
1497static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1498{
1499 struct ethtool_wolinfo wol;
Heiner Kallweit61941142018-09-24 21:58:59 +02001500 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 if (!dev->ethtool_ops->set_wol)
1503 return -EOPNOTSUPP;
1504
1505 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1506 return -EFAULT;
1507
Heiner Kallweit61941142018-09-24 21:58:59 +02001508 ret = dev->ethtool_ops->set_wol(dev, &wol);
1509 if (ret)
1510 return ret;
1511
1512 dev->wol_enabled = !!wol.wolopts;
1513
1514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001517static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1518{
1519 struct ethtool_eee edata;
1520 int rc;
1521
1522 if (!dev->ethtool_ops->get_eee)
1523 return -EOPNOTSUPP;
1524
1525 memset(&edata, 0, sizeof(struct ethtool_eee));
1526 edata.cmd = ETHTOOL_GEEE;
1527 rc = dev->ethtool_ops->get_eee(dev, &edata);
1528
1529 if (rc)
1530 return rc;
1531
1532 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1533 return -EFAULT;
1534
1535 return 0;
1536}
1537
1538static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1539{
1540 struct ethtool_eee edata;
1541
1542 if (!dev->ethtool_ops->set_eee)
1543 return -EOPNOTSUPP;
1544
1545 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1546 return -EFAULT;
1547
1548 return dev->ethtool_ops->set_eee(dev, &edata);
1549}
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551static int ethtool_nway_reset(struct net_device *dev)
1552{
1553 if (!dev->ethtool_ops->nway_reset)
1554 return -EOPNOTSUPP;
1555
1556 return dev->ethtool_ops->nway_reset(dev);
1557}
1558
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001559static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1560{
1561 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1562
1563 if (!dev->ethtool_ops->get_link)
1564 return -EOPNOTSUPP;
1565
1566 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1567
1568 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1569 return -EFAULT;
1570 return 0;
1571}
1572
Ben Hutchings081d0942012-04-12 00:42:12 +00001573static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1574 int (*getter)(struct net_device *,
1575 struct ethtool_eeprom *, u8 *),
1576 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001579 void __user *userbuf = useraddr + sizeof(eeprom);
1580 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001582 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1585 return -EFAULT;
1586
1587 /* Check for wrap and zero */
1588 if (eeprom.offset + eeprom.len <= eeprom.offset)
1589 return -EINVAL;
1590
1591 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001592 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return -EINVAL;
1594
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001595 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!data)
1597 return -ENOMEM;
1598
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001599 bytes_remaining = eeprom.len;
1600 while (bytes_remaining > 0) {
1601 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Ben Hutchings081d0942012-04-12 00:42:12 +00001603 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001604 if (ret)
1605 break;
1606 if (copy_to_user(userbuf, data, eeprom.len)) {
1607 ret = -EFAULT;
1608 break;
1609 }
1610 userbuf += eeprom.len;
1611 eeprom.offset += eeprom.len;
1612 bytes_remaining -= eeprom.len;
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001615 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1616 eeprom.offset -= eeprom.len;
1617 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1618 ret = -EFAULT;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 kfree(data);
1621 return ret;
1622}
1623
Ben Hutchings081d0942012-04-12 00:42:12 +00001624static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1625{
1626 const struct ethtool_ops *ops = dev->ethtool_ops;
1627
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001628 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1629 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001630 return -EOPNOTSUPP;
1631
1632 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1633 ops->get_eeprom_len(dev));
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1637{
1638 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001639 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001640 void __user *userbuf = useraddr + sizeof(eeprom);
1641 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001643 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001645 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1646 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return -EOPNOTSUPP;
1648
1649 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1650 return -EFAULT;
1651
1652 /* Check for wrap and zero */
1653 if (eeprom.offset + eeprom.len <= eeprom.offset)
1654 return -EINVAL;
1655
1656 /* Check for exceeding total eeprom len */
1657 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1658 return -EINVAL;
1659
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001660 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (!data)
1662 return -ENOMEM;
1663
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001664 bytes_remaining = eeprom.len;
1665 while (bytes_remaining > 0) {
1666 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001668 if (copy_from_user(data, userbuf, eeprom.len)) {
1669 ret = -EFAULT;
1670 break;
1671 }
1672 ret = ops->set_eeprom(dev, &eeprom, data);
1673 if (ret)
1674 break;
1675 userbuf += eeprom.len;
1676 eeprom.offset += eeprom.len;
1677 bytes_remaining -= eeprom.len;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 kfree(data);
1681 return ret;
1682}
1683
chavey97f8aef2010-04-07 21:54:42 -07001684static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1685 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Roland Dreier8e557422010-02-11 12:14:23 -08001687 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (!dev->ethtool_ops->get_coalesce)
1690 return -EOPNOTSUPP;
1691
1692 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1693
1694 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1695 return -EFAULT;
1696 return 0;
1697}
1698
chavey97f8aef2010-04-07 21:54:42 -07001699static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1700 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 struct ethtool_coalesce coalesce;
1703
David S. Millerfa04ae52005-06-06 15:07:19 -07001704 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return -EOPNOTSUPP;
1706
1707 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1708 return -EFAULT;
1709
1710 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1711}
1712
1713static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1714{
Roland Dreier8e557422010-02-11 12:14:23 -08001715 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 if (!dev->ethtool_ops->get_ringparam)
1718 return -EOPNOTSUPP;
1719
1720 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1721
1722 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1723 return -EFAULT;
1724 return 0;
1725}
1726
1727static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1728{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001729 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001731 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return -EOPNOTSUPP;
1733
1734 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1735 return -EFAULT;
1736
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001737 dev->ethtool_ops->get_ringparam(dev, &max);
1738
1739 /* ensure new ring parameters are within the maximums */
1740 if (ringparam.rx_pending > max.rx_max_pending ||
1741 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1742 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1743 ringparam.tx_pending > max.tx_max_pending)
1744 return -EINVAL;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1747}
1748
amit salecha8b5933c2011-04-07 01:58:42 +00001749static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1750 void __user *useraddr)
1751{
1752 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1753
1754 if (!dev->ethtool_ops->get_channels)
1755 return -EOPNOTSUPP;
1756
1757 dev->ethtool_ops->get_channels(dev, &channels);
1758
1759 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1760 return -EFAULT;
1761 return 0;
1762}
1763
1764static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1765 void __user *useraddr)
1766{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001767 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001768 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001769
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001770 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001771 return -EOPNOTSUPP;
1772
1773 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1774 return -EFAULT;
1775
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001776 dev->ethtool_ops->get_channels(dev, &max);
1777
1778 /* ensure new counts are within the maximums */
1779 if ((channels.rx_count > max.max_rx) ||
1780 (channels.tx_count > max.max_tx) ||
1781 (channels.combined_count > max.max_combined) ||
1782 (channels.other_count > max.max_other))
1783 return -EINVAL;
1784
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001785 /* ensure the new Rx count fits within the configured Rx flow
1786 * indirection table settings */
1787 if (netif_is_rxfh_configured(dev) &&
1788 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1789 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1790 return -EINVAL;
1791
amit salecha8b5933c2011-04-07 01:58:42 +00001792 return dev->ethtool_ops->set_channels(dev, &channels);
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1796{
1797 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1798
1799 if (!dev->ethtool_ops->get_pauseparam)
1800 return -EOPNOTSUPP;
1801
1802 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1803
1804 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1805 return -EFAULT;
1806 return 0;
1807}
1808
1809static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1810{
1811 struct ethtool_pauseparam pauseparam;
1812
Jeff Garzike1b90c42006-07-17 12:54:40 -04001813 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return -EOPNOTSUPP;
1815
1816 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1817 return -EFAULT;
1818
1819 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1823{
1824 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001825 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001827 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001829 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001830 return -EOPNOTSUPP;
1831
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001832 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001833 if (test_len < 0)
1834 return test_len;
1835 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 if (copy_from_user(&test, useraddr, sizeof(test)))
1838 return -EFAULT;
1839
Jeff Garzikff03d492007-08-15 16:01:08 -07001840 test.len = test_len;
Kees Cook6da2ec52018-06-12 13:55:00 -07001841 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (!data)
1843 return -ENOMEM;
1844
1845 ops->self_test(dev, &test, data);
1846
1847 ret = -EFAULT;
1848 if (copy_to_user(useraddr, &test, sizeof(test)))
1849 goto out;
1850 useraddr += sizeof(test);
1851 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1852 goto out;
1853 ret = 0;
1854
1855 out:
1856 kfree(data);
1857 return ret;
1858}
1859
1860static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1861{
1862 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 u8 *data;
1864 int ret;
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1867 return -EFAULT;
1868
Michał Mirosław340ae162011-02-15 16:59:16 +00001869 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001870 if (ret < 0)
1871 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001872 if (ret > S32_MAX / ETH_GSTRING_LEN)
1873 return -ENOMEM;
1874 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001875
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001876 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Li RongQing80c20582019-03-29 09:18:02 +08001878 if (gstrings.len) {
1879 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1880 if (!data)
1881 return -ENOMEM;
1882
1883 __ethtool_get_strings(dev, gstrings.string_set, data);
1884 } else {
1885 data = NULL;
1886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 ret = -EFAULT;
1889 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1890 goto out;
1891 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001892 if (gstrings.len &&
1893 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 goto out;
1895 ret = 0;
1896
Michał Mirosław340ae162011-02-15 16:59:16 +00001897out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001898 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return ret;
1900}
1901
1902static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1903{
1904 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001905 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001906 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001907 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001909 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return -EOPNOTSUPP;
1911
Ben Hutchings68f512f2011-04-02 00:35:15 +01001912 if (busy)
1913 return -EBUSY;
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (copy_from_user(&id, useraddr, sizeof(id)))
1916 return -EFAULT;
1917
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001918 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001919 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001920 return rc;
1921
1922 /* Drop the RTNL lock while waiting, but prevent reentry or
1923 * removal of the device.
1924 */
1925 busy = true;
1926 dev_hold(dev);
1927 rtnl_unlock();
1928
1929 if (rc == 0) {
1930 /* Driver will handle this itself */
1931 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001932 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001933 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001934 /* Driver expects to be called at twice the frequency in rc */
1935 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001936
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001937 /* Count down seconds */
1938 do {
1939 /* Count down iterations per second */
1940 i = n;
1941 do {
1942 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001943 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001944 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1945 rtnl_unlock();
1946 if (rc)
1947 break;
1948 schedule_timeout_interruptible(interval);
1949 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001950 } while (!signal_pending(current) &&
1951 (id.data == 0 || --id.data != 0));
1952 }
1953
1954 rtnl_lock();
1955 dev_put(dev);
1956 busy = false;
1957
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001958 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001959 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1963{
1964 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001965 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001967 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001969 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001970 return -EOPNOTSUPP;
1971
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001972 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001973 if (n_stats < 0)
1974 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001975 if (n_stats > S32_MAX / sizeof(u64))
1976 return -ENOMEM;
1977 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1979 return -EFAULT;
1980
Jeff Garzikff03d492007-08-15 16:01:08 -07001981 stats.n_stats = n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Li RongQing80c20582019-03-29 09:18:02 +08001983 if (n_stats) {
1984 data = vzalloc(array_size(n_stats, sizeof(u64)));
1985 if (!data)
1986 return -ENOMEM;
1987 ops->get_ethtool_stats(dev, &stats, data);
1988 } else {
1989 data = NULL;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 ret = -EFAULT;
1993 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1994 goto out;
1995 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001996 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 goto out;
1998 ret = 0;
1999
2000 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002001 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return ret;
2003}
2004
Andrew Lunnf3a40942015-12-30 16:28:25 +01002005static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2006{
Florian Fainelli99943382018-04-25 12:12:48 -07002007 const struct ethtool_ops *ops = dev->ethtool_ops;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002008 struct phy_device *phydev = dev->phydev;
Florian Fainelli99943382018-04-25 12:12:48 -07002009 struct ethtool_stats stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002010 u64 *data;
2011 int ret, n_stats;
2012
Florian Fainelli99943382018-04-25 12:12:48 -07002013 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002014 return -EOPNOTSUPP;
2015
Florian Fainelli99943382018-04-25 12:12:48 -07002016 if (dev->phydev && !ops->get_ethtool_phy_stats)
2017 n_stats = phy_ethtool_get_sset_count(dev->phydev);
2018 else
2019 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002020 if (n_stats < 0)
2021 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002022 if (n_stats > S32_MAX / sizeof(u64))
2023 return -ENOMEM;
2024 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002025
2026 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2027 return -EFAULT;
2028
2029 stats.n_stats = n_stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002030
Li RongQing80c20582019-03-29 09:18:02 +08002031 if (n_stats) {
2032 data = vzalloc(array_size(n_stats, sizeof(u64)));
2033 if (!data)
2034 return -ENOMEM;
2035
2036 if (dev->phydev && !ops->get_ethtool_phy_stats) {
2037 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
2038 if (ret < 0)
2039 goto out;
2040 } else {
2041 ops->get_ethtool_phy_stats(dev, &stats, data);
2042 }
Florian Fainelli99943382018-04-25 12:12:48 -07002043 } else {
Li RongQing80c20582019-03-29 09:18:02 +08002044 data = NULL;
Florian Fainelli99943382018-04-25 12:12:48 -07002045 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01002046
2047 ret = -EFAULT;
2048 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2049 goto out;
2050 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002051 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002052 goto out;
2053 ret = 0;
2054
2055 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002056 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002057 return ret;
2058}
2059
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002060static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002061{
2062 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002063
Matthew Wilcox313674a2007-07-31 14:00:29 -07002064 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002065 return -EFAULT;
2066
Matthew Wilcox313674a2007-07-31 14:00:29 -07002067 if (epaddr.size < dev->addr_len)
2068 return -ETOOSMALL;
2069 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002070
Jon Wetzela6f9a702005-08-20 17:15:54 -07002071 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002072 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002073 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002074 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2075 return -EFAULT;
2076 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002077}
2078
Jeff Garzik13c99b22007-08-15 16:01:56 -07002079static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2080 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002081{
Roland Dreier8e557422010-02-11 12:14:23 -08002082 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002083
Jeff Garzik13c99b22007-08-15 16:01:56 -07002084 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002085 return -EOPNOTSUPP;
2086
Jeff Garzik13c99b22007-08-15 16:01:56 -07002087 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002088
2089 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2090 return -EFAULT;
2091 return 0;
2092}
2093
Jeff Garzik13c99b22007-08-15 16:01:56 -07002094static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2095 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002096{
2097 struct ethtool_value edata;
2098
Jeff Garzik13c99b22007-08-15 16:01:56 -07002099 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002100 return -EOPNOTSUPP;
2101
2102 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2103 return -EFAULT;
2104
Jeff Garzik13c99b22007-08-15 16:01:56 -07002105 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002106 return 0;
2107}
2108
Jeff Garzik13c99b22007-08-15 16:01:56 -07002109static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2110 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002111{
2112 struct ethtool_value edata;
2113
Jeff Garzik13c99b22007-08-15 16:01:56 -07002114 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002115 return -EOPNOTSUPP;
2116
2117 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2118 return -EFAULT;
2119
Jeff Garzik13c99b22007-08-15 16:01:56 -07002120 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002121}
2122
chavey97f8aef2010-04-07 21:54:42 -07002123static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2124 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002125{
2126 struct ethtool_flash efl;
2127
2128 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2129 return -EFAULT;
2130
2131 if (!dev->ethtool_ops->flash_device)
2132 return -EOPNOTSUPP;
2133
Ben Hutchings786f5282012-02-01 09:32:25 +00002134 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2135
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002136 return dev->ethtool_ops->flash_device(dev, &efl);
2137}
2138
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002139static int ethtool_set_dump(struct net_device *dev,
2140 void __user *useraddr)
2141{
2142 struct ethtool_dump dump;
2143
2144 if (!dev->ethtool_ops->set_dump)
2145 return -EOPNOTSUPP;
2146
2147 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2148 return -EFAULT;
2149
2150 return dev->ethtool_ops->set_dump(dev, &dump);
2151}
2152
2153static int ethtool_get_dump_flag(struct net_device *dev,
2154 void __user *useraddr)
2155{
2156 int ret;
2157 struct ethtool_dump dump;
2158 const struct ethtool_ops *ops = dev->ethtool_ops;
2159
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002160 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002161 return -EOPNOTSUPP;
2162
2163 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2164 return -EFAULT;
2165
2166 ret = ops->get_dump_flag(dev, &dump);
2167 if (ret)
2168 return ret;
2169
2170 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2171 return -EFAULT;
2172 return 0;
2173}
2174
2175static int ethtool_get_dump_data(struct net_device *dev,
2176 void __user *useraddr)
2177{
2178 int ret;
2179 __u32 len;
2180 struct ethtool_dump dump, tmp;
2181 const struct ethtool_ops *ops = dev->ethtool_ops;
2182 void *data = NULL;
2183
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002184 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002185 return -EOPNOTSUPP;
2186
2187 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2188 return -EFAULT;
2189
2190 memset(&tmp, 0, sizeof(tmp));
2191 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2192 ret = ops->get_dump_flag(dev, &tmp);
2193 if (ret)
2194 return ret;
2195
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002196 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002197 if (!len)
2198 return -EFAULT;
2199
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002200 /* Don't ever let the driver think there's more space available
2201 * than it requested with .get_dump_flag().
2202 */
2203 dump.len = len;
2204
2205 /* Always allocate enough space to hold the whole thing so that the
2206 * driver does not need to check the length and bother with partial
2207 * dumping.
2208 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002209 data = vzalloc(tmp.len);
2210 if (!data)
2211 return -ENOMEM;
2212 ret = ops->get_dump_data(dev, &dump, data);
2213 if (ret)
2214 goto out;
2215
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002216 /* There are two sane possibilities:
2217 * 1. The driver's .get_dump_data() does not touch dump.len.
2218 * 2. Or it may set dump.len to how much it really writes, which
2219 * should be tmp.len (or len if it can do a partial dump).
2220 * In any case respond to userspace with the actual length of data
2221 * it's receiving.
2222 */
2223 WARN_ON(dump.len != len && dump.len != tmp.len);
2224 dump.len = len;
2225
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002226 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2227 ret = -EFAULT;
2228 goto out;
2229 }
2230 useraddr += offsetof(struct ethtool_dump, data);
2231 if (copy_to_user(useraddr, data, len))
2232 ret = -EFAULT;
2233out:
2234 vfree(data);
2235 return ret;
2236}
2237
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002238static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2239{
2240 int err = 0;
2241 struct ethtool_ts_info info;
2242 const struct ethtool_ops *ops = dev->ethtool_ops;
2243 struct phy_device *phydev = dev->phydev;
2244
2245 memset(&info, 0, sizeof(info));
2246 info.cmd = ETHTOOL_GET_TS_INFO;
2247
2248 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002249 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002250 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002251 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002252 } else {
2253 info.so_timestamping =
2254 SOF_TIMESTAMPING_RX_SOFTWARE |
2255 SOF_TIMESTAMPING_SOFTWARE;
2256 info.phc_index = -1;
2257 }
2258
2259 if (err)
2260 return err;
2261
2262 if (copy_to_user(useraddr, &info, sizeof(info)))
2263 err = -EFAULT;
2264
2265 return err;
2266}
2267
Ed Swierk2f438362015-01-02 17:27:56 -08002268static int __ethtool_get_module_info(struct net_device *dev,
2269 struct ethtool_modinfo *modinfo)
2270{
2271 const struct ethtool_ops *ops = dev->ethtool_ops;
2272 struct phy_device *phydev = dev->phydev;
2273
Russell Kinge679c9c2018-03-28 15:44:16 -07002274 if (dev->sfp_bus)
2275 return sfp_get_module_info(dev->sfp_bus, modinfo);
2276
Ed Swierk2f438362015-01-02 17:27:56 -08002277 if (phydev && phydev->drv && phydev->drv->module_info)
2278 return phydev->drv->module_info(phydev, modinfo);
2279
2280 if (ops->get_module_info)
2281 return ops->get_module_info(dev, modinfo);
2282
2283 return -EOPNOTSUPP;
2284}
2285
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002286static int ethtool_get_module_info(struct net_device *dev,
2287 void __user *useraddr)
2288{
2289 int ret;
2290 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002291
2292 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2293 return -EFAULT;
2294
Ed Swierk2f438362015-01-02 17:27:56 -08002295 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002296 if (ret)
2297 return ret;
2298
2299 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2300 return -EFAULT;
2301
2302 return 0;
2303}
2304
Ed Swierk2f438362015-01-02 17:27:56 -08002305static int __ethtool_get_module_eeprom(struct net_device *dev,
2306 struct ethtool_eeprom *ee, u8 *data)
2307{
2308 const struct ethtool_ops *ops = dev->ethtool_ops;
2309 struct phy_device *phydev = dev->phydev;
2310
Russell Kinge679c9c2018-03-28 15:44:16 -07002311 if (dev->sfp_bus)
2312 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2313
Ed Swierk2f438362015-01-02 17:27:56 -08002314 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2315 return phydev->drv->module_eeprom(phydev, ee, data);
2316
2317 if (ops->get_module_eeprom)
2318 return ops->get_module_eeprom(dev, ee, data);
2319
2320 return -EOPNOTSUPP;
2321}
2322
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002323static int ethtool_get_module_eeprom(struct net_device *dev,
2324 void __user *useraddr)
2325{
2326 int ret;
2327 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002328
Ed Swierk2f438362015-01-02 17:27:56 -08002329 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002330 if (ret)
2331 return ret;
2332
Ed Swierk2f438362015-01-02 17:27:56 -08002333 return ethtool_get_any_eeprom(dev, useraddr,
2334 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002335 modinfo.eeprom_len);
2336}
2337
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302338static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2339{
2340 switch (tuna->id) {
2341 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002342 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302343 if (tuna->len != sizeof(u32) ||
2344 tuna->type_id != ETHTOOL_TUNABLE_U32)
2345 return -EINVAL;
2346 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002347 case ETHTOOL_PFC_PREVENTION_TOUT:
2348 if (tuna->len != sizeof(u16) ||
2349 tuna->type_id != ETHTOOL_TUNABLE_U16)
2350 return -EINVAL;
2351 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302352 default:
2353 return -EINVAL;
2354 }
2355
2356 return 0;
2357}
2358
2359static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2360{
2361 int ret;
2362 struct ethtool_tunable tuna;
2363 const struct ethtool_ops *ops = dev->ethtool_ops;
2364 void *data;
2365
2366 if (!ops->get_tunable)
2367 return -EOPNOTSUPP;
2368 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2369 return -EFAULT;
2370 ret = ethtool_tunable_valid(&tuna);
2371 if (ret)
2372 return ret;
2373 data = kmalloc(tuna.len, GFP_USER);
2374 if (!data)
2375 return -ENOMEM;
2376 ret = ops->get_tunable(dev, &tuna, data);
2377 if (ret)
2378 goto out;
2379 useraddr += sizeof(tuna);
2380 ret = -EFAULT;
2381 if (copy_to_user(useraddr, data, tuna.len))
2382 goto out;
2383 ret = 0;
2384
2385out:
2386 kfree(data);
2387 return ret;
2388}
2389
2390static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2391{
2392 int ret;
2393 struct ethtool_tunable tuna;
2394 const struct ethtool_ops *ops = dev->ethtool_ops;
2395 void *data;
2396
2397 if (!ops->set_tunable)
2398 return -EOPNOTSUPP;
2399 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2400 return -EFAULT;
2401 ret = ethtool_tunable_valid(&tuna);
2402 if (ret)
2403 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302404 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002405 data = memdup_user(useraddr, tuna.len);
2406 if (IS_ERR(data))
2407 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302408 ret = ops->set_tunable(dev, &tuna, data);
2409
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302410 kfree(data);
2411 return ret;
2412}
2413
Kan Liang421797b2016-02-19 09:24:02 -05002414static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2415 void __user *useraddr,
2416 struct ethtool_per_queue_op *per_queue_opt)
2417{
2418 u32 bit;
2419 int ret;
2420 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2421
2422 if (!dev->ethtool_ops->get_per_queue_coalesce)
2423 return -EOPNOTSUPP;
2424
2425 useraddr += sizeof(*per_queue_opt);
2426
Yury Norov3aa56882018-02-06 15:38:06 -08002427 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2428 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002429
2430 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2431 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2432
2433 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2434 if (ret != 0)
2435 return ret;
2436 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2437 return -EFAULT;
2438 useraddr += sizeof(coalesce);
2439 }
2440
2441 return 0;
2442}
2443
Kan Liangf38d1382016-02-19 09:24:03 -05002444static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2445 void __user *useraddr,
2446 struct ethtool_per_queue_op *per_queue_opt)
2447{
2448 u32 bit;
2449 int i, ret = 0;
2450 int n_queue;
2451 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2452 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2453
2454 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2455 (!dev->ethtool_ops->get_per_queue_coalesce))
2456 return -EOPNOTSUPP;
2457
2458 useraddr += sizeof(*per_queue_opt);
2459
Yury Norov3aa56882018-02-06 15:38:06 -08002460 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002461 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2462 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2463 if (!backup)
2464 return -ENOMEM;
2465
2466 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2467 struct ethtool_coalesce coalesce;
2468
2469 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2470 if (ret != 0)
2471 goto roll_back;
2472
2473 tmp++;
2474
2475 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2476 ret = -EFAULT;
2477 goto roll_back;
2478 }
2479
2480 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2481 if (ret != 0)
2482 goto roll_back;
2483
2484 useraddr += sizeof(coalesce);
2485 }
2486
2487roll_back:
2488 if (ret != 0) {
2489 tmp = backup;
2490 for_each_set_bit(i, queue_mask, bit) {
2491 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2492 tmp++;
2493 }
2494 }
2495 kfree(backup);
2496
2497 return ret;
2498}
2499
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002500static int ethtool_set_per_queue(struct net_device *dev,
2501 void __user *useraddr, u32 sub_cmd)
Kan Liangac2c7ad2016-02-19 09:24:01 -05002502{
2503 struct ethtool_per_queue_op per_queue_opt;
2504
2505 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2506 return -EFAULT;
2507
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002508 if (per_queue_opt.sub_command != sub_cmd)
2509 return -EINVAL;
2510
Kan Liangac2c7ad2016-02-19 09:24:01 -05002511 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002512 case ETHTOOL_GCOALESCE:
2513 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002514 case ETHTOOL_SCOALESCE:
2515 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002516 default:
2517 return -EOPNOTSUPP;
2518 };
2519}
2520
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002521static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2522{
2523 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002524 case ETHTOOL_PHY_DOWNSHIFT:
2525 if (tuna->len != sizeof(u8) ||
2526 tuna->type_id != ETHTOOL_TUNABLE_U8)
2527 return -EINVAL;
2528 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002529 default:
2530 return -EINVAL;
2531 }
2532
2533 return 0;
2534}
2535
2536static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2537{
2538 int ret;
2539 struct ethtool_tunable tuna;
2540 struct phy_device *phydev = dev->phydev;
2541 void *data;
2542
2543 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2544 return -EOPNOTSUPP;
2545
2546 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2547 return -EFAULT;
2548 ret = ethtool_phy_tunable_valid(&tuna);
2549 if (ret)
2550 return ret;
2551 data = kmalloc(tuna.len, GFP_USER);
2552 if (!data)
2553 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002554 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002555 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002556 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002557 if (ret)
2558 goto out;
2559 useraddr += sizeof(tuna);
2560 ret = -EFAULT;
2561 if (copy_to_user(useraddr, data, tuna.len))
2562 goto out;
2563 ret = 0;
2564
2565out:
2566 kfree(data);
2567 return ret;
2568}
2569
2570static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2571{
2572 int ret;
2573 struct ethtool_tunable tuna;
2574 struct phy_device *phydev = dev->phydev;
2575 void *data;
2576
2577 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2578 return -EOPNOTSUPP;
2579 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2580 return -EFAULT;
2581 ret = ethtool_phy_tunable_valid(&tuna);
2582 if (ret)
2583 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002584 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002585 data = memdup_user(useraddr, tuna.len);
2586 if (IS_ERR(data))
2587 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002588 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002589 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002590 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002591
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002592 kfree(data);
2593 return ret;
2594}
2595
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002596static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2597{
2598 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002599 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002600
2601 if (!dev->ethtool_ops->get_fecparam)
2602 return -EOPNOTSUPP;
2603
Edward Creea6d50512018-02-28 19:15:58 +00002604 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2605 if (rc)
2606 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002607
2608 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2609 return -EFAULT;
2610 return 0;
2611}
2612
2613static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2614{
2615 struct ethtool_fecparam fecparam;
2616
2617 if (!dev->ethtool_ops->set_fecparam)
2618 return -EOPNOTSUPP;
2619
2620 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2621 return -EFAULT;
2622
2623 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2624}
2625
Yan Burman600fed52013-06-03 02:03:34 +00002626/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Eric W. Biederman881d9662007-09-17 11:56:21 -07002628int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002630 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002632 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002634 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (!dev || !netif_device_present(dev))
2637 return -ENODEV;
2638
chavey97f8aef2010-04-07 21:54:42 -07002639 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return -EFAULT;
2641
Kan Liangac2c7ad2016-02-19 09:24:01 -05002642 if (ethcmd == ETHTOOL_PERQUEUE) {
2643 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2644 return -EFAULT;
2645 } else {
2646 sub_cmd = ethcmd;
2647 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002648 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002649 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002650 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002651 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002652 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002653 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002654 case ETHTOOL_GCOALESCE:
2655 case ETHTOOL_GRINGPARAM:
2656 case ETHTOOL_GPAUSEPARAM:
2657 case ETHTOOL_GRXCSUM:
2658 case ETHTOOL_GTXCSUM:
2659 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002660 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002661 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002662 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002663 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002664 case ETHTOOL_GTSO:
2665 case ETHTOOL_GPERMADDR:
Maciej Żenczykowski474ff262018-09-22 01:34:01 -07002666 case ETHTOOL_GUFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002667 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002668 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002669 case ETHTOOL_GFLAGS:
2670 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002671 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002672 case ETHTOOL_GRXRINGS:
2673 case ETHTOOL_GRXCLSRLCNT:
2674 case ETHTOOL_GRXCLSRULE:
2675 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002676 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302677 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002678 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002679 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002680 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002681 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302682 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002683 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002684 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002685 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002686 break;
2687 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002688 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002689 return -EPERM;
2690 }
2691
chavey97f8aef2010-04-07 21:54:42 -07002692 if (dev->ethtool_ops->begin) {
2693 rc = dev->ethtool_ops->begin(dev);
2694 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002696 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002697 old_features = dev->features;
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 switch (ethcmd) {
2700 case ETHTOOL_GSET:
2701 rc = ethtool_get_settings(dev, useraddr);
2702 break;
2703 case ETHTOOL_SSET:
2704 rc = ethtool_set_settings(dev, useraddr);
2705 break;
2706 case ETHTOOL_GDRVINFO:
2707 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 break;
2709 case ETHTOOL_GREGS:
2710 rc = ethtool_get_regs(dev, useraddr);
2711 break;
2712 case ETHTOOL_GWOL:
2713 rc = ethtool_get_wol(dev, useraddr);
2714 break;
2715 case ETHTOOL_SWOL:
2716 rc = ethtool_set_wol(dev, useraddr);
2717 break;
2718 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002719 rc = ethtool_get_value(dev, useraddr, ethcmd,
2720 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 break;
2722 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002723 rc = ethtool_set_value_void(dev, useraddr,
2724 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002726 case ETHTOOL_GEEE:
2727 rc = ethtool_get_eee(dev, useraddr);
2728 break;
2729 case ETHTOOL_SEEE:
2730 rc = ethtool_set_eee(dev, useraddr);
2731 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 case ETHTOOL_NWAY_RST:
2733 rc = ethtool_nway_reset(dev);
2734 break;
2735 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002736 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 break;
2738 case ETHTOOL_GEEPROM:
2739 rc = ethtool_get_eeprom(dev, useraddr);
2740 break;
2741 case ETHTOOL_SEEPROM:
2742 rc = ethtool_set_eeprom(dev, useraddr);
2743 break;
2744 case ETHTOOL_GCOALESCE:
2745 rc = ethtool_get_coalesce(dev, useraddr);
2746 break;
2747 case ETHTOOL_SCOALESCE:
2748 rc = ethtool_set_coalesce(dev, useraddr);
2749 break;
2750 case ETHTOOL_GRINGPARAM:
2751 rc = ethtool_get_ringparam(dev, useraddr);
2752 break;
2753 case ETHTOOL_SRINGPARAM:
2754 rc = ethtool_set_ringparam(dev, useraddr);
2755 break;
2756 case ETHTOOL_GPAUSEPARAM:
2757 rc = ethtool_get_pauseparam(dev, useraddr);
2758 break;
2759 case ETHTOOL_SPAUSEPARAM:
2760 rc = ethtool_set_pauseparam(dev, useraddr);
2761 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 case ETHTOOL_TEST:
2763 rc = ethtool_self_test(dev, useraddr);
2764 break;
2765 case ETHTOOL_GSTRINGS:
2766 rc = ethtool_get_strings(dev, useraddr);
2767 break;
2768 case ETHTOOL_PHYS_ID:
2769 rc = ethtool_phys_id(dev, useraddr);
2770 break;
2771 case ETHTOOL_GSTATS:
2772 rc = ethtool_get_stats(dev, useraddr);
2773 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002774 case ETHTOOL_GPERMADDR:
2775 rc = ethtool_get_perm_addr(dev, useraddr);
2776 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002777 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002778 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002779 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002780 break;
2781 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002782 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002783 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002784 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002785 rc = ethtool_get_value(dev, useraddr, ethcmd,
2786 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002787 break;
2788 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002789 rc = ethtool_set_value(dev, useraddr,
2790 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002791 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002792 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002793 case ETHTOOL_GRXRINGS:
2794 case ETHTOOL_GRXCLSRLCNT:
2795 case ETHTOOL_GRXCLSRULE:
2796 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002797 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002798 break;
2799 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002800 case ETHTOOL_SRXCLSRLDEL:
2801 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002802 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002803 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002804 case ETHTOOL_FLASHDEV:
2805 rc = ethtool_flash_device(dev, useraddr);
2806 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002807 case ETHTOOL_RESET:
2808 rc = ethtool_reset(dev, useraddr);
2809 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002810 case ETHTOOL_GSSET_INFO:
2811 rc = ethtool_get_sset_info(dev, useraddr);
2812 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002813 case ETHTOOL_GRXFHINDIR:
2814 rc = ethtool_get_rxfh_indir(dev, useraddr);
2815 break;
2816 case ETHTOOL_SRXFHINDIR:
2817 rc = ethtool_set_rxfh_indir(dev, useraddr);
2818 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302819 case ETHTOOL_GRSSH:
2820 rc = ethtool_get_rxfh(dev, useraddr);
2821 break;
2822 case ETHTOOL_SRSSH:
2823 rc = ethtool_set_rxfh(dev, useraddr);
2824 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002825 case ETHTOOL_GFEATURES:
2826 rc = ethtool_get_features(dev, useraddr);
2827 break;
2828 case ETHTOOL_SFEATURES:
2829 rc = ethtool_set_features(dev, useraddr);
2830 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002831 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002832 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002833 case ETHTOOL_GSG:
2834 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002835 case ETHTOOL_GGSO:
2836 case ETHTOOL_GGRO:
2837 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2838 break;
2839 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002840 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002841 case ETHTOOL_SSG:
2842 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002843 case ETHTOOL_SGSO:
2844 case ETHTOOL_SGRO:
2845 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2846 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002847 case ETHTOOL_GCHANNELS:
2848 rc = ethtool_get_channels(dev, useraddr);
2849 break;
2850 case ETHTOOL_SCHANNELS:
2851 rc = ethtool_set_channels(dev, useraddr);
2852 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002853 case ETHTOOL_SET_DUMP:
2854 rc = ethtool_set_dump(dev, useraddr);
2855 break;
2856 case ETHTOOL_GET_DUMP_FLAG:
2857 rc = ethtool_get_dump_flag(dev, useraddr);
2858 break;
2859 case ETHTOOL_GET_DUMP_DATA:
2860 rc = ethtool_get_dump_data(dev, useraddr);
2861 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002862 case ETHTOOL_GET_TS_INFO:
2863 rc = ethtool_get_ts_info(dev, useraddr);
2864 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002865 case ETHTOOL_GMODULEINFO:
2866 rc = ethtool_get_module_info(dev, useraddr);
2867 break;
2868 case ETHTOOL_GMODULEEEPROM:
2869 rc = ethtool_get_module_eeprom(dev, useraddr);
2870 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302871 case ETHTOOL_GTUNABLE:
2872 rc = ethtool_get_tunable(dev, useraddr);
2873 break;
2874 case ETHTOOL_STUNABLE:
2875 rc = ethtool_set_tunable(dev, useraddr);
2876 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002877 case ETHTOOL_GPHYSTATS:
2878 rc = ethtool_get_phy_stats(dev, useraddr);
2879 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002880 case ETHTOOL_PERQUEUE:
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002881 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002882 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002883 case ETHTOOL_GLINKSETTINGS:
2884 rc = ethtool_get_link_ksettings(dev, useraddr);
2885 break;
2886 case ETHTOOL_SLINKSETTINGS:
2887 rc = ethtool_set_link_ksettings(dev, useraddr);
2888 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002889 case ETHTOOL_PHY_GTUNABLE:
2890 rc = get_phy_tunable(dev, useraddr);
2891 break;
2892 case ETHTOOL_PHY_STUNABLE:
2893 rc = set_phy_tunable(dev, useraddr);
2894 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002895 case ETHTOOL_GFECPARAM:
2896 rc = ethtool_get_fecparam(dev, useraddr);
2897 break;
2898 case ETHTOOL_SFECPARAM:
2899 rc = ethtool_set_fecparam(dev, useraddr);
2900 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002902 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002904
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002905 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002907
2908 if (old_features != dev->features)
2909 netdev_features_change(dev);
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}