blob: 7cc97f43f13859a256fc0a7fb2c0ad5fd703aaab [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 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000883 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000885 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 info.eedump_len = ops->get_eeprom_len(dev);
887
888 if (copy_to_user(useraddr, &info, sizeof(info)))
889 return -EFAULT;
890 return 0;
891}
892
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800893static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700894 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000895{
896 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000897 u64 sset_mask;
898 int i, idx = 0, n_bits = 0, ret, rc;
899 u32 *info_buf = NULL;
900
Jeff Garzik723b2f52010-03-03 22:51:50 +0000901 if (copy_from_user(&info, useraddr, sizeof(info)))
902 return -EFAULT;
903
904 /* store copy of mask, because we zero struct later on */
905 sset_mask = info.sset_mask;
906 if (!sset_mask)
907 return 0;
908
909 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000910 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000911
912 memset(&info, 0, sizeof(info));
913 info.cmd = ETHTOOL_GSSET_INFO;
914
Kees Cook6396bb22018-06-12 14:03:40 -0700915 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000916 if (!info_buf)
917 return -ENOMEM;
918
919 /*
920 * fill return buffer based on input bitmask and successful
921 * get_sset_count return
922 */
923 for (i = 0; i < 64; i++) {
924 if (!(sset_mask & (1ULL << i)))
925 continue;
926
Michał Mirosław340ae162011-02-15 16:59:16 +0000927 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000928 if (rc >= 0) {
929 info.sset_mask |= (1ULL << i);
930 info_buf[idx++] = rc;
931 }
932 }
933
934 ret = -EFAULT;
935 if (copy_to_user(useraddr, &info, sizeof(info)))
936 goto out;
937
938 useraddr += offsetof(struct ethtool_sset_info, data);
939 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
940 goto out;
941
942 ret = 0;
943
944out:
945 kfree(info_buf);
946 return ret;
947}
948
chavey97f8aef2010-04-07 21:54:42 -0700949static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000950 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700951{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000952 struct ethtool_rxnfc info;
953 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000954 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700955
Santwona Behera59089d82009-02-20 00:58:13 -0800956 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700957 return -EOPNOTSUPP;
958
Ben Hutchingsbf988432010-06-28 08:45:58 +0000959 /* struct ethtool_rxnfc was originally defined for
960 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
961 * members. User-space might still be using that
962 * definition. */
963 if (cmd == ETHTOOL_SRXFH)
964 info_size = (offsetof(struct ethtool_rxnfc, data) +
965 sizeof(info.data));
966
967 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700968 return -EFAULT;
969
Ben Hutchings55664f32012-01-03 12:04:51 +0000970 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
971 if (rc)
972 return rc;
973
974 if (cmd == ETHTOOL_SRXCLSRLINS &&
975 copy_to_user(useraddr, &info, info_size))
976 return -EFAULT;
977
978 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700979}
980
chavey97f8aef2010-04-07 21:54:42 -0700981static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000982 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700983{
984 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000985 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800986 const struct ethtool_ops *ops = dev->ethtool_ops;
987 int ret;
988 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700989
Santwona Behera59089d82009-02-20 00:58:13 -0800990 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700991 return -EOPNOTSUPP;
992
Ben Hutchingsbf988432010-06-28 08:45:58 +0000993 /* struct ethtool_rxnfc was originally defined for
994 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
995 * members. User-space might still be using that
996 * definition. */
997 if (cmd == ETHTOOL_GRXFH)
998 info_size = (offsetof(struct ethtool_rxnfc, data) +
999 sizeof(info.data));
1000
1001 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001002 return -EFAULT;
1003
Edward Cree84a1d9c42018-03-08 15:45:03 +00001004 /* If FLOW_RSS was requested then user-space must be using the
1005 * new definition, as FLOW_RSS is newer.
1006 */
1007 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1008 info_size = sizeof(info);
1009 if (copy_from_user(&info, useraddr, info_size))
1010 return -EFAULT;
Wenwen Wangd656fe42018-04-30 12:31:13 -05001011 /* Since malicious users may modify the original data,
1012 * we need to check whether FLOW_RSS is still requested.
1013 */
1014 if (!(info.flow_type & FLOW_RSS))
1015 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001016 }
1017
Wenwen Wang2bb32072018-10-09 08:15:38 -05001018 if (info.cmd != cmd)
1019 return -EINVAL;
1020
Santwona Behera59089d82009-02-20 00:58:13 -08001021 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1022 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001023 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cook6396bb22018-06-12 14:03:40 -07001024 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001025 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001026 if (!rule_buf)
1027 return -ENOMEM;
1028 }
1029 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001030
Santwona Behera59089d82009-02-20 00:58:13 -08001031 ret = ops->get_rxnfc(dev, &info, rule_buf);
1032 if (ret < 0)
1033 goto err_out;
1034
1035 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001036 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001037 goto err_out;
1038
1039 if (rule_buf) {
1040 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1041 if (copy_to_user(useraddr, rule_buf,
1042 info.rule_cnt * sizeof(u32)))
1043 goto err_out;
1044 }
1045 ret = 0;
1046
1047err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001048 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001049
1050 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001051}
1052
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301053static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1054 struct ethtool_rxnfc *rx_rings,
1055 u32 size)
1056{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001057 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301058
1059 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001060 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301061
1062 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001063 for (i = 0; i < size; i++)
1064 if (indir[i] >= rx_rings->data)
1065 return -EINVAL;
1066
1067 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301068}
1069
Kim Jonesba905f52016-02-02 03:51:16 +00001070u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001071
1072void netdev_rss_key_fill(void *buffer, size_t len)
1073{
1074 BUG_ON(len > sizeof(netdev_rss_key));
1075 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1076 memcpy(buffer, netdev_rss_key, len);
1077}
1078EXPORT_SYMBOL(netdev_rss_key_fill);
1079
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001080static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1081{
1082 u32 dev_size, current_max = 0;
1083 u32 *indir;
1084 int ret;
1085
1086 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1087 !dev->ethtool_ops->get_rxfh)
1088 return -EOPNOTSUPP;
1089 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1090 if (dev_size == 0)
1091 return -EOPNOTSUPP;
1092
1093 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1094 if (!indir)
1095 return -ENOMEM;
1096
1097 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1098 if (ret)
1099 goto out;
1100
1101 while (dev_size--)
1102 current_max = max(current_max, indir[dev_size]);
1103
1104 *max = current_max;
1105
1106out:
1107 kfree(indir);
1108 return ret;
1109}
1110
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001111static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1112 void __user *useraddr)
1113{
Ben Hutchings7850f632011-12-15 13:55:01 +00001114 u32 user_size, dev_size;
1115 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001116 int ret;
1117
Ben Hutchings7850f632011-12-15 13:55:01 +00001118 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001119 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001120 return -EOPNOTSUPP;
1121 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1122 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001123 return -EOPNOTSUPP;
1124
Ben Hutchings7850f632011-12-15 13:55:01 +00001125 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001126 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001127 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001128 return -EFAULT;
1129
Ben Hutchings7850f632011-12-15 13:55:01 +00001130 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1131 &dev_size, sizeof(dev_size)))
1132 return -EFAULT;
1133
1134 /* If the user buffer size is 0, this is just a query for the
1135 * device table size. Otherwise, if it's smaller than the
1136 * device table size it's an error.
1137 */
1138 if (user_size < dev_size)
1139 return user_size == 0 ? 0 : -EINVAL;
1140
1141 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001142 if (!indir)
1143 return -ENOMEM;
1144
Eyal Perry892311f2014-12-02 18:12:10 +02001145 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001146 if (ret)
1147 goto out;
1148
Ben Hutchings7850f632011-12-15 13:55:01 +00001149 if (copy_to_user(useraddr +
1150 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1151 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001152 ret = -EFAULT;
1153
1154out:
1155 kfree(indir);
1156 return ret;
1157}
1158
1159static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1160 void __user *useraddr)
1161{
Ben Hutchings7850f632011-12-15 13:55:01 +00001162 struct ethtool_rxnfc rx_rings;
1163 u32 user_size, dev_size, i;
1164 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001165 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001166 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301167 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001168
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001169 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001170 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001171 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001172
1173 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001174 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001175 return -EOPNOTSUPP;
1176
Ben Hutchings7850f632011-12-15 13:55:01 +00001177 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001178 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001179 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001180 return -EFAULT;
1181
Ben Hutchings278bc422011-12-15 13:56:49 +00001182 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001183 return -EINVAL;
1184
1185 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001186 if (!indir)
1187 return -ENOMEM;
1188
Ben Hutchings7850f632011-12-15 13:55:01 +00001189 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001190 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001191 if (ret)
1192 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001193
1194 if (user_size == 0) {
1195 for (i = 0; i < dev_size; i++)
1196 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1197 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301198 ret = ethtool_copy_validate_indir(indir,
1199 useraddr + ringidx_offset,
1200 &rx_rings,
1201 dev_size);
1202 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001203 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001204 }
1205
Eyal Perry892311f2014-12-02 18:12:10 +02001206 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001207 if (ret)
1208 goto out;
1209
1210 /* indicate whether rxfh was set to default */
1211 if (user_size == 0)
1212 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1213 else
1214 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001215
1216out:
1217 kfree(indir);
1218 return ret;
1219}
1220
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301221static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1222 void __user *useraddr)
1223{
1224 int ret;
1225 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001226 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301227 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001228 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301229 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001230 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301231 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001232 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301233 u8 *hkey = NULL;
1234 u8 *rss_config;
1235
Eyal Perry892311f2014-12-02 18:12:10 +02001236 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301237 return -EOPNOTSUPP;
1238
1239 if (ops->get_rxfh_indir_size)
1240 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301241 if (ops->get_rxfh_key_size)
1242 dev_key_size = ops->get_rxfh_key_size(dev);
1243
Ben Hutchingsf062a382014-05-15 16:28:07 +01001244 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301245 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001246 user_indir_size = rxfh.indir_size;
1247 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301248
Ben Hutchingsf062a382014-05-15 16:28:07 +01001249 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001250 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001251 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001252 /* Most drivers don't handle rss_context, check it's 0 as well */
1253 if (rxfh.rss_context && !ops->get_rxfh_context)
1254 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001255
1256 rxfh.indir_size = dev_indir_size;
1257 rxfh.key_size = dev_key_size;
1258 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301259 return -EFAULT;
1260
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301261 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1262 (user_key_size && (user_key_size != dev_key_size)))
1263 return -EINVAL;
1264
1265 indir_bytes = user_indir_size * sizeof(indir[0]);
1266 total_size = indir_bytes + user_key_size;
1267 rss_config = kzalloc(total_size, GFP_USER);
1268 if (!rss_config)
1269 return -ENOMEM;
1270
1271 if (user_indir_size)
1272 indir = (u32 *)rss_config;
1273
1274 if (user_key_size)
1275 hkey = rss_config + indir_bytes;
1276
Edward Cree84a1d9c42018-03-08 15:45:03 +00001277 if (rxfh.rss_context)
1278 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1279 &dev_hfunc,
1280 rxfh.rss_context);
1281 else
1282 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001283 if (ret)
1284 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301285
Eyal Perry892311f2014-12-02 18:12:10 +02001286 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1287 &dev_hfunc, sizeof(rxfh.hfunc))) {
1288 ret = -EFAULT;
1289 } else if (copy_to_user(useraddr +
1290 offsetof(struct ethtool_rxfh, rss_config[0]),
1291 rss_config, total_size)) {
1292 ret = -EFAULT;
1293 }
1294out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301295 kfree(rss_config);
1296
1297 return ret;
1298}
1299
1300static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1301 void __user *useraddr)
1302{
1303 int ret;
1304 const struct ethtool_ops *ops = dev->ethtool_ops;
1305 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001306 struct ethtool_rxfh rxfh;
1307 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301308 u32 *indir = NULL, indir_bytes = 0;
1309 u8 *hkey = NULL;
1310 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301311 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001312 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301313
Eyal Perry892311f2014-12-02 18:12:10 +02001314 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301315 return -EOPNOTSUPP;
1316
1317 if (ops->get_rxfh_indir_size)
1318 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301319 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001320 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301321
Ben Hutchingsf062a382014-05-15 16:28:07 +01001322 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301323 return -EFAULT;
1324
Ben Hutchingsf062a382014-05-15 16:28:07 +01001325 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001326 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001327 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001328 /* Most drivers don't handle rss_context, check it's 0 as well */
1329 if (rxfh.rss_context && !ops->set_rxfh_context)
1330 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001331
Eyal Perry892311f2014-12-02 18:12:10 +02001332 /* If either indir, hash key or function is valid, proceed further.
1333 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301334 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001335 if ((rxfh.indir_size &&
1336 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1337 rxfh.indir_size != dev_indir_size) ||
1338 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1339 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001340 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301341 return -EINVAL;
1342
Ben Hutchingsf062a382014-05-15 16:28:07 +01001343 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301344 indir_bytes = dev_indir_size * sizeof(indir[0]);
1345
Ben Hutchingsf062a382014-05-15 16:28:07 +01001346 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301347 if (!rss_config)
1348 return -ENOMEM;
1349
1350 rx_rings.cmd = ETHTOOL_GRXRINGS;
1351 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1352 if (ret)
1353 goto out;
1354
Edward Cree84a1d9c42018-03-08 15:45:03 +00001355 /* rxfh.indir_size == 0 means reset the indir table to default (master
1356 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001357 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301358 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001359 if (rxfh.indir_size &&
1360 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301361 indir = (u32 *)rss_config;
1362 ret = ethtool_copy_validate_indir(indir,
1363 useraddr + rss_cfg_offset,
1364 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001365 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301366 if (ret)
1367 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001368 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001369 if (rxfh.rss_context == 0) {
1370 indir = (u32 *)rss_config;
1371 for (i = 0; i < dev_indir_size; i++)
1372 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1373 } else {
1374 delete = true;
1375 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301376 }
1377
Ben Hutchingsf062a382014-05-15 16:28:07 +01001378 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301379 hkey = rss_config + indir_bytes;
1380 if (copy_from_user(hkey,
1381 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001382 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301383 ret = -EFAULT;
1384 goto out;
1385 }
1386 }
1387
Edward Cree84a1d9c42018-03-08 15:45:03 +00001388 if (rxfh.rss_context)
1389 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1390 &rxfh.rss_context, delete);
1391 else
1392 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001393 if (ret)
1394 goto out;
1395
Edward Cree84a1d9c42018-03-08 15:45:03 +00001396 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1397 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1398 ret = -EFAULT;
1399
1400 if (!rxfh.rss_context) {
1401 /* indicate whether rxfh was set to default */
1402 if (rxfh.indir_size == 0)
1403 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1404 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1405 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1406 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301407
1408out:
1409 kfree(rss_config);
1410 return ret;
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1414{
1415 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001416 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 void *regbuf;
1418 int reglen, ret;
1419
1420 if (!ops->get_regs || !ops->get_regs_len)
1421 return -EOPNOTSUPP;
1422
1423 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1424 return -EFAULT;
1425
1426 reglen = ops->get_regs_len(dev);
1427 if (regs.len > reglen)
1428 regs.len = reglen;
1429
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001430 regbuf = NULL;
1431 if (reglen) {
1432 regbuf = vzalloc(reglen);
1433 if (!regbuf)
1434 return -ENOMEM;
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 ops->get_regs(dev, &regs, regbuf);
1438
1439 ret = -EFAULT;
1440 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1441 goto out;
1442 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001443 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 goto out;
1445 ret = 0;
1446
1447 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001448 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return ret;
1450}
1451
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001452static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1453{
1454 struct ethtool_value reset;
1455 int ret;
1456
1457 if (!dev->ethtool_ops->reset)
1458 return -EOPNOTSUPP;
1459
1460 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1461 return -EFAULT;
1462
1463 ret = dev->ethtool_ops->reset(dev, &reset.data);
1464 if (ret)
1465 return ret;
1466
1467 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1468 return -EFAULT;
1469 return 0;
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1473{
Roland Dreier8e557422010-02-11 12:14:23 -08001474 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 if (!dev->ethtool_ops->get_wol)
1477 return -EOPNOTSUPP;
1478
1479 dev->ethtool_ops->get_wol(dev, &wol);
1480
1481 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1482 return -EFAULT;
1483 return 0;
1484}
1485
1486static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1487{
1488 struct ethtool_wolinfo wol;
Heiner Kallweit61941142018-09-24 21:58:59 +02001489 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 if (!dev->ethtool_ops->set_wol)
1492 return -EOPNOTSUPP;
1493
1494 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1495 return -EFAULT;
1496
Heiner Kallweit61941142018-09-24 21:58:59 +02001497 ret = dev->ethtool_ops->set_wol(dev, &wol);
1498 if (ret)
1499 return ret;
1500
1501 dev->wol_enabled = !!wol.wolopts;
1502
1503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001506static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1507{
1508 struct ethtool_eee edata;
1509 int rc;
1510
1511 if (!dev->ethtool_ops->get_eee)
1512 return -EOPNOTSUPP;
1513
1514 memset(&edata, 0, sizeof(struct ethtool_eee));
1515 edata.cmd = ETHTOOL_GEEE;
1516 rc = dev->ethtool_ops->get_eee(dev, &edata);
1517
1518 if (rc)
1519 return rc;
1520
1521 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1522 return -EFAULT;
1523
1524 return 0;
1525}
1526
1527static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1528{
1529 struct ethtool_eee edata;
1530
1531 if (!dev->ethtool_ops->set_eee)
1532 return -EOPNOTSUPP;
1533
1534 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1535 return -EFAULT;
1536
1537 return dev->ethtool_ops->set_eee(dev, &edata);
1538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540static int ethtool_nway_reset(struct net_device *dev)
1541{
1542 if (!dev->ethtool_ops->nway_reset)
1543 return -EOPNOTSUPP;
1544
1545 return dev->ethtool_ops->nway_reset(dev);
1546}
1547
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001548static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1549{
1550 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1551
1552 if (!dev->ethtool_ops->get_link)
1553 return -EOPNOTSUPP;
1554
1555 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1556
1557 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1558 return -EFAULT;
1559 return 0;
1560}
1561
Ben Hutchings081d0942012-04-12 00:42:12 +00001562static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1563 int (*getter)(struct net_device *,
1564 struct ethtool_eeprom *, u8 *),
1565 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001568 void __user *userbuf = useraddr + sizeof(eeprom);
1569 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001571 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1574 return -EFAULT;
1575
1576 /* Check for wrap and zero */
1577 if (eeprom.offset + eeprom.len <= eeprom.offset)
1578 return -EINVAL;
1579
1580 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001581 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return -EINVAL;
1583
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001584 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (!data)
1586 return -ENOMEM;
1587
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001588 bytes_remaining = eeprom.len;
1589 while (bytes_remaining > 0) {
1590 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Ben Hutchings081d0942012-04-12 00:42:12 +00001592 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001593 if (ret)
1594 break;
1595 if (copy_to_user(userbuf, data, eeprom.len)) {
1596 ret = -EFAULT;
1597 break;
1598 }
1599 userbuf += eeprom.len;
1600 eeprom.offset += eeprom.len;
1601 bytes_remaining -= eeprom.len;
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001604 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1605 eeprom.offset -= eeprom.len;
1606 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1607 ret = -EFAULT;
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 kfree(data);
1610 return ret;
1611}
1612
Ben Hutchings081d0942012-04-12 00:42:12 +00001613static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1614{
1615 const struct ethtool_ops *ops = dev->ethtool_ops;
1616
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001617 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1618 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001619 return -EOPNOTSUPP;
1620
1621 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1622 ops->get_eeprom_len(dev));
1623}
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1626{
1627 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001628 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001629 void __user *userbuf = useraddr + sizeof(eeprom);
1630 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001632 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001634 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1635 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return -EOPNOTSUPP;
1637
1638 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1639 return -EFAULT;
1640
1641 /* Check for wrap and zero */
1642 if (eeprom.offset + eeprom.len <= eeprom.offset)
1643 return -EINVAL;
1644
1645 /* Check for exceeding total eeprom len */
1646 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1647 return -EINVAL;
1648
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001649 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (!data)
1651 return -ENOMEM;
1652
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001653 bytes_remaining = eeprom.len;
1654 while (bytes_remaining > 0) {
1655 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001657 if (copy_from_user(data, userbuf, eeprom.len)) {
1658 ret = -EFAULT;
1659 break;
1660 }
1661 ret = ops->set_eeprom(dev, &eeprom, data);
1662 if (ret)
1663 break;
1664 userbuf += eeprom.len;
1665 eeprom.offset += eeprom.len;
1666 bytes_remaining -= eeprom.len;
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 kfree(data);
1670 return ret;
1671}
1672
chavey97f8aef2010-04-07 21:54:42 -07001673static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1674 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Roland Dreier8e557422010-02-11 12:14:23 -08001676 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (!dev->ethtool_ops->get_coalesce)
1679 return -EOPNOTSUPP;
1680
1681 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1682
1683 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1684 return -EFAULT;
1685 return 0;
1686}
1687
chavey97f8aef2010-04-07 21:54:42 -07001688static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1689 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 struct ethtool_coalesce coalesce;
1692
David S. Millerfa04ae52005-06-06 15:07:19 -07001693 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return -EOPNOTSUPP;
1695
1696 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1697 return -EFAULT;
1698
1699 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1700}
1701
1702static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1703{
Roland Dreier8e557422010-02-11 12:14:23 -08001704 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 if (!dev->ethtool_ops->get_ringparam)
1707 return -EOPNOTSUPP;
1708
1709 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1710
1711 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1712 return -EFAULT;
1713 return 0;
1714}
1715
1716static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1717{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001718 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001720 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return -EOPNOTSUPP;
1722
1723 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1724 return -EFAULT;
1725
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001726 dev->ethtool_ops->get_ringparam(dev, &max);
1727
1728 /* ensure new ring parameters are within the maximums */
1729 if (ringparam.rx_pending > max.rx_max_pending ||
1730 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1731 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1732 ringparam.tx_pending > max.tx_max_pending)
1733 return -EINVAL;
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1736}
1737
amit salecha8b5933c2011-04-07 01:58:42 +00001738static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1739 void __user *useraddr)
1740{
1741 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1742
1743 if (!dev->ethtool_ops->get_channels)
1744 return -EOPNOTSUPP;
1745
1746 dev->ethtool_ops->get_channels(dev, &channels);
1747
1748 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1749 return -EFAULT;
1750 return 0;
1751}
1752
1753static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1754 void __user *useraddr)
1755{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001756 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001757 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001758
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001759 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001760 return -EOPNOTSUPP;
1761
1762 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1763 return -EFAULT;
1764
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001765 dev->ethtool_ops->get_channels(dev, &max);
1766
1767 /* ensure new counts are within the maximums */
1768 if ((channels.rx_count > max.max_rx) ||
1769 (channels.tx_count > max.max_tx) ||
1770 (channels.combined_count > max.max_combined) ||
1771 (channels.other_count > max.max_other))
1772 return -EINVAL;
1773
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001774 /* ensure the new Rx count fits within the configured Rx flow
1775 * indirection table settings */
1776 if (netif_is_rxfh_configured(dev) &&
1777 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1778 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1779 return -EINVAL;
1780
amit salecha8b5933c2011-04-07 01:58:42 +00001781 return dev->ethtool_ops->set_channels(dev, &channels);
1782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1785{
1786 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1787
1788 if (!dev->ethtool_ops->get_pauseparam)
1789 return -EOPNOTSUPP;
1790
1791 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1792
1793 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1794 return -EFAULT;
1795 return 0;
1796}
1797
1798static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1799{
1800 struct ethtool_pauseparam pauseparam;
1801
Jeff Garzike1b90c42006-07-17 12:54:40 -04001802 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return -EOPNOTSUPP;
1804
1805 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1806 return -EFAULT;
1807
1808 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1812{
1813 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001814 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001816 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001818 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001819 return -EOPNOTSUPP;
1820
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001821 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001822 if (test_len < 0)
1823 return test_len;
1824 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 if (copy_from_user(&test, useraddr, sizeof(test)))
1827 return -EFAULT;
1828
Jeff Garzikff03d492007-08-15 16:01:08 -07001829 test.len = test_len;
Kees Cook6da2ec52018-06-12 13:55:00 -07001830 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (!data)
1832 return -ENOMEM;
1833
1834 ops->self_test(dev, &test, data);
1835
1836 ret = -EFAULT;
1837 if (copy_to_user(useraddr, &test, sizeof(test)))
1838 goto out;
1839 useraddr += sizeof(test);
1840 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1841 goto out;
1842 ret = 0;
1843
1844 out:
1845 kfree(data);
1846 return ret;
1847}
1848
1849static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1850{
1851 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 u8 *data;
1853 int ret;
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1856 return -EFAULT;
1857
Michał Mirosław340ae162011-02-15 16:59:16 +00001858 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001859 if (ret < 0)
1860 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001861 if (ret > S32_MAX / ETH_GSTRING_LEN)
1862 return -ENOMEM;
1863 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001864
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001865 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Li RongQing80c20582019-03-29 09:18:02 +08001867 if (gstrings.len) {
1868 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1869 if (!data)
1870 return -ENOMEM;
1871
1872 __ethtool_get_strings(dev, gstrings.string_set, data);
1873 } else {
1874 data = NULL;
1875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 ret = -EFAULT;
1878 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1879 goto out;
1880 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001881 if (gstrings.len &&
1882 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto out;
1884 ret = 0;
1885
Michał Mirosław340ae162011-02-15 16:59:16 +00001886out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001887 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return ret;
1889}
1890
1891static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1892{
1893 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001894 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001895 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001896 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001898 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return -EOPNOTSUPP;
1900
Ben Hutchings68f512f2011-04-02 00:35:15 +01001901 if (busy)
1902 return -EBUSY;
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (copy_from_user(&id, useraddr, sizeof(id)))
1905 return -EFAULT;
1906
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001907 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001908 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001909 return rc;
1910
1911 /* Drop the RTNL lock while waiting, but prevent reentry or
1912 * removal of the device.
1913 */
1914 busy = true;
1915 dev_hold(dev);
1916 rtnl_unlock();
1917
1918 if (rc == 0) {
1919 /* Driver will handle this itself */
1920 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001921 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001922 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001923 /* Driver expects to be called at twice the frequency in rc */
1924 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001925
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001926 /* Count down seconds */
1927 do {
1928 /* Count down iterations per second */
1929 i = n;
1930 do {
1931 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001932 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001933 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1934 rtnl_unlock();
1935 if (rc)
1936 break;
1937 schedule_timeout_interruptible(interval);
1938 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001939 } while (!signal_pending(current) &&
1940 (id.data == 0 || --id.data != 0));
1941 }
1942
1943 rtnl_lock();
1944 dev_put(dev);
1945 busy = false;
1946
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001947 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001948 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1952{
1953 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001954 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001956 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001958 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001959 return -EOPNOTSUPP;
1960
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001961 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001962 if (n_stats < 0)
1963 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001964 if (n_stats > S32_MAX / sizeof(u64))
1965 return -ENOMEM;
1966 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1968 return -EFAULT;
1969
Jeff Garzikff03d492007-08-15 16:01:08 -07001970 stats.n_stats = n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Li RongQing80c20582019-03-29 09:18:02 +08001972 if (n_stats) {
1973 data = vzalloc(array_size(n_stats, sizeof(u64)));
1974 if (!data)
1975 return -ENOMEM;
1976 ops->get_ethtool_stats(dev, &stats, data);
1977 } else {
1978 data = NULL;
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 ret = -EFAULT;
1982 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1983 goto out;
1984 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001985 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 goto out;
1987 ret = 0;
1988
1989 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001990 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return ret;
1992}
1993
Andrew Lunnf3a40942015-12-30 16:28:25 +01001994static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1995{
Florian Fainelli99943382018-04-25 12:12:48 -07001996 const struct ethtool_ops *ops = dev->ethtool_ops;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001997 struct phy_device *phydev = dev->phydev;
Florian Fainelli99943382018-04-25 12:12:48 -07001998 struct ethtool_stats stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001999 u64 *data;
2000 int ret, n_stats;
2001
Florian Fainelli99943382018-04-25 12:12:48 -07002002 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002003 return -EOPNOTSUPP;
2004
Florian Fainelli99943382018-04-25 12:12:48 -07002005 if (dev->phydev && !ops->get_ethtool_phy_stats)
2006 n_stats = phy_ethtool_get_sset_count(dev->phydev);
2007 else
2008 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002009 if (n_stats < 0)
2010 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002011 if (n_stats > S32_MAX / sizeof(u64))
2012 return -ENOMEM;
2013 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002014
2015 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2016 return -EFAULT;
2017
2018 stats.n_stats = n_stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002019
Li RongQing80c20582019-03-29 09:18:02 +08002020 if (n_stats) {
2021 data = vzalloc(array_size(n_stats, sizeof(u64)));
2022 if (!data)
2023 return -ENOMEM;
2024
2025 if (dev->phydev && !ops->get_ethtool_phy_stats) {
2026 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
2027 if (ret < 0)
2028 goto out;
2029 } else {
2030 ops->get_ethtool_phy_stats(dev, &stats, data);
2031 }
Florian Fainelli99943382018-04-25 12:12:48 -07002032 } else {
Li RongQing80c20582019-03-29 09:18:02 +08002033 data = NULL;
Florian Fainelli99943382018-04-25 12:12:48 -07002034 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01002035
2036 ret = -EFAULT;
2037 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2038 goto out;
2039 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002040 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002041 goto out;
2042 ret = 0;
2043
2044 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002045 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002046 return ret;
2047}
2048
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002049static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002050{
2051 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002052
Matthew Wilcox313674a2007-07-31 14:00:29 -07002053 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002054 return -EFAULT;
2055
Matthew Wilcox313674a2007-07-31 14:00:29 -07002056 if (epaddr.size < dev->addr_len)
2057 return -ETOOSMALL;
2058 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002059
Jon Wetzela6f9a702005-08-20 17:15:54 -07002060 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002061 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002062 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002063 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2064 return -EFAULT;
2065 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002066}
2067
Jeff Garzik13c99b22007-08-15 16:01:56 -07002068static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2069 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002070{
Roland Dreier8e557422010-02-11 12:14:23 -08002071 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002072
Jeff Garzik13c99b22007-08-15 16:01:56 -07002073 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002074 return -EOPNOTSUPP;
2075
Jeff Garzik13c99b22007-08-15 16:01:56 -07002076 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002077
2078 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2079 return -EFAULT;
2080 return 0;
2081}
2082
Jeff Garzik13c99b22007-08-15 16:01:56 -07002083static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2084 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002085{
2086 struct ethtool_value edata;
2087
Jeff Garzik13c99b22007-08-15 16:01:56 -07002088 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002089 return -EOPNOTSUPP;
2090
2091 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2092 return -EFAULT;
2093
Jeff Garzik13c99b22007-08-15 16:01:56 -07002094 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002095 return 0;
2096}
2097
Jeff Garzik13c99b22007-08-15 16:01:56 -07002098static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2099 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002100{
2101 struct ethtool_value edata;
2102
Jeff Garzik13c99b22007-08-15 16:01:56 -07002103 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002104 return -EOPNOTSUPP;
2105
2106 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2107 return -EFAULT;
2108
Jeff Garzik13c99b22007-08-15 16:01:56 -07002109 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002110}
2111
chavey97f8aef2010-04-07 21:54:42 -07002112static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2113 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002114{
2115 struct ethtool_flash efl;
2116
2117 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2118 return -EFAULT;
2119
2120 if (!dev->ethtool_ops->flash_device)
2121 return -EOPNOTSUPP;
2122
Ben Hutchings786f5282012-02-01 09:32:25 +00002123 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2124
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002125 return dev->ethtool_ops->flash_device(dev, &efl);
2126}
2127
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002128static int ethtool_set_dump(struct net_device *dev,
2129 void __user *useraddr)
2130{
2131 struct ethtool_dump dump;
2132
2133 if (!dev->ethtool_ops->set_dump)
2134 return -EOPNOTSUPP;
2135
2136 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2137 return -EFAULT;
2138
2139 return dev->ethtool_ops->set_dump(dev, &dump);
2140}
2141
2142static int ethtool_get_dump_flag(struct net_device *dev,
2143 void __user *useraddr)
2144{
2145 int ret;
2146 struct ethtool_dump dump;
2147 const struct ethtool_ops *ops = dev->ethtool_ops;
2148
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002149 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002150 return -EOPNOTSUPP;
2151
2152 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2153 return -EFAULT;
2154
2155 ret = ops->get_dump_flag(dev, &dump);
2156 if (ret)
2157 return ret;
2158
2159 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2160 return -EFAULT;
2161 return 0;
2162}
2163
2164static int ethtool_get_dump_data(struct net_device *dev,
2165 void __user *useraddr)
2166{
2167 int ret;
2168 __u32 len;
2169 struct ethtool_dump dump, tmp;
2170 const struct ethtool_ops *ops = dev->ethtool_ops;
2171 void *data = NULL;
2172
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002173 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002174 return -EOPNOTSUPP;
2175
2176 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2177 return -EFAULT;
2178
2179 memset(&tmp, 0, sizeof(tmp));
2180 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2181 ret = ops->get_dump_flag(dev, &tmp);
2182 if (ret)
2183 return ret;
2184
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002185 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002186 if (!len)
2187 return -EFAULT;
2188
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002189 /* Don't ever let the driver think there's more space available
2190 * than it requested with .get_dump_flag().
2191 */
2192 dump.len = len;
2193
2194 /* Always allocate enough space to hold the whole thing so that the
2195 * driver does not need to check the length and bother with partial
2196 * dumping.
2197 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002198 data = vzalloc(tmp.len);
2199 if (!data)
2200 return -ENOMEM;
2201 ret = ops->get_dump_data(dev, &dump, data);
2202 if (ret)
2203 goto out;
2204
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002205 /* There are two sane possibilities:
2206 * 1. The driver's .get_dump_data() does not touch dump.len.
2207 * 2. Or it may set dump.len to how much it really writes, which
2208 * should be tmp.len (or len if it can do a partial dump).
2209 * In any case respond to userspace with the actual length of data
2210 * it's receiving.
2211 */
2212 WARN_ON(dump.len != len && dump.len != tmp.len);
2213 dump.len = len;
2214
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002215 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2216 ret = -EFAULT;
2217 goto out;
2218 }
2219 useraddr += offsetof(struct ethtool_dump, data);
2220 if (copy_to_user(useraddr, data, len))
2221 ret = -EFAULT;
2222out:
2223 vfree(data);
2224 return ret;
2225}
2226
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002227static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2228{
2229 int err = 0;
2230 struct ethtool_ts_info info;
2231 const struct ethtool_ops *ops = dev->ethtool_ops;
2232 struct phy_device *phydev = dev->phydev;
2233
2234 memset(&info, 0, sizeof(info));
2235 info.cmd = ETHTOOL_GET_TS_INFO;
2236
2237 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002238 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002239 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002240 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002241 } else {
2242 info.so_timestamping =
2243 SOF_TIMESTAMPING_RX_SOFTWARE |
2244 SOF_TIMESTAMPING_SOFTWARE;
2245 info.phc_index = -1;
2246 }
2247
2248 if (err)
2249 return err;
2250
2251 if (copy_to_user(useraddr, &info, sizeof(info)))
2252 err = -EFAULT;
2253
2254 return err;
2255}
2256
Ed Swierk2f438362015-01-02 17:27:56 -08002257static int __ethtool_get_module_info(struct net_device *dev,
2258 struct ethtool_modinfo *modinfo)
2259{
2260 const struct ethtool_ops *ops = dev->ethtool_ops;
2261 struct phy_device *phydev = dev->phydev;
2262
Russell Kinge679c9c2018-03-28 15:44:16 -07002263 if (dev->sfp_bus)
2264 return sfp_get_module_info(dev->sfp_bus, modinfo);
2265
Ed Swierk2f438362015-01-02 17:27:56 -08002266 if (phydev && phydev->drv && phydev->drv->module_info)
2267 return phydev->drv->module_info(phydev, modinfo);
2268
2269 if (ops->get_module_info)
2270 return ops->get_module_info(dev, modinfo);
2271
2272 return -EOPNOTSUPP;
2273}
2274
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002275static int ethtool_get_module_info(struct net_device *dev,
2276 void __user *useraddr)
2277{
2278 int ret;
2279 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002280
2281 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2282 return -EFAULT;
2283
Ed Swierk2f438362015-01-02 17:27:56 -08002284 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002285 if (ret)
2286 return ret;
2287
2288 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2289 return -EFAULT;
2290
2291 return 0;
2292}
2293
Ed Swierk2f438362015-01-02 17:27:56 -08002294static int __ethtool_get_module_eeprom(struct net_device *dev,
2295 struct ethtool_eeprom *ee, u8 *data)
2296{
2297 const struct ethtool_ops *ops = dev->ethtool_ops;
2298 struct phy_device *phydev = dev->phydev;
2299
Russell Kinge679c9c2018-03-28 15:44:16 -07002300 if (dev->sfp_bus)
2301 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2302
Ed Swierk2f438362015-01-02 17:27:56 -08002303 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2304 return phydev->drv->module_eeprom(phydev, ee, data);
2305
2306 if (ops->get_module_eeprom)
2307 return ops->get_module_eeprom(dev, ee, data);
2308
2309 return -EOPNOTSUPP;
2310}
2311
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002312static int ethtool_get_module_eeprom(struct net_device *dev,
2313 void __user *useraddr)
2314{
2315 int ret;
2316 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002317
Ed Swierk2f438362015-01-02 17:27:56 -08002318 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002319 if (ret)
2320 return ret;
2321
Ed Swierk2f438362015-01-02 17:27:56 -08002322 return ethtool_get_any_eeprom(dev, useraddr,
2323 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002324 modinfo.eeprom_len);
2325}
2326
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302327static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2328{
2329 switch (tuna->id) {
2330 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002331 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302332 if (tuna->len != sizeof(u32) ||
2333 tuna->type_id != ETHTOOL_TUNABLE_U32)
2334 return -EINVAL;
2335 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002336 case ETHTOOL_PFC_PREVENTION_TOUT:
2337 if (tuna->len != sizeof(u16) ||
2338 tuna->type_id != ETHTOOL_TUNABLE_U16)
2339 return -EINVAL;
2340 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302341 default:
2342 return -EINVAL;
2343 }
2344
2345 return 0;
2346}
2347
2348static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2349{
2350 int ret;
2351 struct ethtool_tunable tuna;
2352 const struct ethtool_ops *ops = dev->ethtool_ops;
2353 void *data;
2354
2355 if (!ops->get_tunable)
2356 return -EOPNOTSUPP;
2357 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2358 return -EFAULT;
2359 ret = ethtool_tunable_valid(&tuna);
2360 if (ret)
2361 return ret;
2362 data = kmalloc(tuna.len, GFP_USER);
2363 if (!data)
2364 return -ENOMEM;
2365 ret = ops->get_tunable(dev, &tuna, data);
2366 if (ret)
2367 goto out;
2368 useraddr += sizeof(tuna);
2369 ret = -EFAULT;
2370 if (copy_to_user(useraddr, data, tuna.len))
2371 goto out;
2372 ret = 0;
2373
2374out:
2375 kfree(data);
2376 return ret;
2377}
2378
2379static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2380{
2381 int ret;
2382 struct ethtool_tunable tuna;
2383 const struct ethtool_ops *ops = dev->ethtool_ops;
2384 void *data;
2385
2386 if (!ops->set_tunable)
2387 return -EOPNOTSUPP;
2388 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2389 return -EFAULT;
2390 ret = ethtool_tunable_valid(&tuna);
2391 if (ret)
2392 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302393 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002394 data = memdup_user(useraddr, tuna.len);
2395 if (IS_ERR(data))
2396 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302397 ret = ops->set_tunable(dev, &tuna, data);
2398
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302399 kfree(data);
2400 return ret;
2401}
2402
Kan Liang421797b2016-02-19 09:24:02 -05002403static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2404 void __user *useraddr,
2405 struct ethtool_per_queue_op *per_queue_opt)
2406{
2407 u32 bit;
2408 int ret;
2409 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2410
2411 if (!dev->ethtool_ops->get_per_queue_coalesce)
2412 return -EOPNOTSUPP;
2413
2414 useraddr += sizeof(*per_queue_opt);
2415
Yury Norov3aa56882018-02-06 15:38:06 -08002416 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2417 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002418
2419 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2420 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2421
2422 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2423 if (ret != 0)
2424 return ret;
2425 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2426 return -EFAULT;
2427 useraddr += sizeof(coalesce);
2428 }
2429
2430 return 0;
2431}
2432
Kan Liangf38d1382016-02-19 09:24:03 -05002433static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2434 void __user *useraddr,
2435 struct ethtool_per_queue_op *per_queue_opt)
2436{
2437 u32 bit;
2438 int i, ret = 0;
2439 int n_queue;
2440 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2441 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2442
2443 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2444 (!dev->ethtool_ops->get_per_queue_coalesce))
2445 return -EOPNOTSUPP;
2446
2447 useraddr += sizeof(*per_queue_opt);
2448
Yury Norov3aa56882018-02-06 15:38:06 -08002449 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002450 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2451 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2452 if (!backup)
2453 return -ENOMEM;
2454
2455 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2456 struct ethtool_coalesce coalesce;
2457
2458 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2459 if (ret != 0)
2460 goto roll_back;
2461
2462 tmp++;
2463
2464 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2465 ret = -EFAULT;
2466 goto roll_back;
2467 }
2468
2469 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2470 if (ret != 0)
2471 goto roll_back;
2472
2473 useraddr += sizeof(coalesce);
2474 }
2475
2476roll_back:
2477 if (ret != 0) {
2478 tmp = backup;
2479 for_each_set_bit(i, queue_mask, bit) {
2480 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2481 tmp++;
2482 }
2483 }
2484 kfree(backup);
2485
2486 return ret;
2487}
2488
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002489static int ethtool_set_per_queue(struct net_device *dev,
2490 void __user *useraddr, u32 sub_cmd)
Kan Liangac2c7ad2016-02-19 09:24:01 -05002491{
2492 struct ethtool_per_queue_op per_queue_opt;
2493
2494 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2495 return -EFAULT;
2496
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002497 if (per_queue_opt.sub_command != sub_cmd)
2498 return -EINVAL;
2499
Kan Liangac2c7ad2016-02-19 09:24:01 -05002500 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002501 case ETHTOOL_GCOALESCE:
2502 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002503 case ETHTOOL_SCOALESCE:
2504 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002505 default:
2506 return -EOPNOTSUPP;
2507 };
2508}
2509
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002510static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2511{
2512 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002513 case ETHTOOL_PHY_DOWNSHIFT:
2514 if (tuna->len != sizeof(u8) ||
2515 tuna->type_id != ETHTOOL_TUNABLE_U8)
2516 return -EINVAL;
2517 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002518 default:
2519 return -EINVAL;
2520 }
2521
2522 return 0;
2523}
2524
2525static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2526{
2527 int ret;
2528 struct ethtool_tunable tuna;
2529 struct phy_device *phydev = dev->phydev;
2530 void *data;
2531
2532 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2533 return -EOPNOTSUPP;
2534
2535 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2536 return -EFAULT;
2537 ret = ethtool_phy_tunable_valid(&tuna);
2538 if (ret)
2539 return ret;
2540 data = kmalloc(tuna.len, GFP_USER);
2541 if (!data)
2542 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002543 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002544 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002545 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002546 if (ret)
2547 goto out;
2548 useraddr += sizeof(tuna);
2549 ret = -EFAULT;
2550 if (copy_to_user(useraddr, data, tuna.len))
2551 goto out;
2552 ret = 0;
2553
2554out:
2555 kfree(data);
2556 return ret;
2557}
2558
2559static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2560{
2561 int ret;
2562 struct ethtool_tunable tuna;
2563 struct phy_device *phydev = dev->phydev;
2564 void *data;
2565
2566 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2567 return -EOPNOTSUPP;
2568 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2569 return -EFAULT;
2570 ret = ethtool_phy_tunable_valid(&tuna);
2571 if (ret)
2572 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002573 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002574 data = memdup_user(useraddr, tuna.len);
2575 if (IS_ERR(data))
2576 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002577 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002578 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002579 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002580
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002581 kfree(data);
2582 return ret;
2583}
2584
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002585static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2586{
2587 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002588 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002589
2590 if (!dev->ethtool_ops->get_fecparam)
2591 return -EOPNOTSUPP;
2592
Edward Creea6d50512018-02-28 19:15:58 +00002593 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2594 if (rc)
2595 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002596
2597 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2598 return -EFAULT;
2599 return 0;
2600}
2601
2602static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2603{
2604 struct ethtool_fecparam fecparam;
2605
2606 if (!dev->ethtool_ops->set_fecparam)
2607 return -EOPNOTSUPP;
2608
2609 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2610 return -EFAULT;
2611
2612 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2613}
2614
Yan Burman600fed52013-06-03 02:03:34 +00002615/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Eric W. Biederman881d9662007-09-17 11:56:21 -07002617int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002619 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002621 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002623 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (!dev || !netif_device_present(dev))
2626 return -ENODEV;
2627
chavey97f8aef2010-04-07 21:54:42 -07002628 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return -EFAULT;
2630
Kan Liangac2c7ad2016-02-19 09:24:01 -05002631 if (ethcmd == ETHTOOL_PERQUEUE) {
2632 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2633 return -EFAULT;
2634 } else {
2635 sub_cmd = ethcmd;
2636 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002637 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002638 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002639 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002640 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002641 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002642 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002643 case ETHTOOL_GCOALESCE:
2644 case ETHTOOL_GRINGPARAM:
2645 case ETHTOOL_GPAUSEPARAM:
2646 case ETHTOOL_GRXCSUM:
2647 case ETHTOOL_GTXCSUM:
2648 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002649 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002650 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002651 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002652 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002653 case ETHTOOL_GTSO:
2654 case ETHTOOL_GPERMADDR:
Maciej Żenczykowski474ff262018-09-22 01:34:01 -07002655 case ETHTOOL_GUFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002656 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002657 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002658 case ETHTOOL_GFLAGS:
2659 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002660 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002661 case ETHTOOL_GRXRINGS:
2662 case ETHTOOL_GRXCLSRLCNT:
2663 case ETHTOOL_GRXCLSRULE:
2664 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002665 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302666 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002667 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002668 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002669 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002670 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302671 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002672 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002673 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002674 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002675 break;
2676 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002677 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002678 return -EPERM;
2679 }
2680
chavey97f8aef2010-04-07 21:54:42 -07002681 if (dev->ethtool_ops->begin) {
2682 rc = dev->ethtool_ops->begin(dev);
2683 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002685 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002686 old_features = dev->features;
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 switch (ethcmd) {
2689 case ETHTOOL_GSET:
2690 rc = ethtool_get_settings(dev, useraddr);
2691 break;
2692 case ETHTOOL_SSET:
2693 rc = ethtool_set_settings(dev, useraddr);
2694 break;
2695 case ETHTOOL_GDRVINFO:
2696 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 break;
2698 case ETHTOOL_GREGS:
2699 rc = ethtool_get_regs(dev, useraddr);
2700 break;
2701 case ETHTOOL_GWOL:
2702 rc = ethtool_get_wol(dev, useraddr);
2703 break;
2704 case ETHTOOL_SWOL:
2705 rc = ethtool_set_wol(dev, useraddr);
2706 break;
2707 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002708 rc = ethtool_get_value(dev, useraddr, ethcmd,
2709 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 break;
2711 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002712 rc = ethtool_set_value_void(dev, useraddr,
2713 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002715 case ETHTOOL_GEEE:
2716 rc = ethtool_get_eee(dev, useraddr);
2717 break;
2718 case ETHTOOL_SEEE:
2719 rc = ethtool_set_eee(dev, useraddr);
2720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 case ETHTOOL_NWAY_RST:
2722 rc = ethtool_nway_reset(dev);
2723 break;
2724 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002725 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 break;
2727 case ETHTOOL_GEEPROM:
2728 rc = ethtool_get_eeprom(dev, useraddr);
2729 break;
2730 case ETHTOOL_SEEPROM:
2731 rc = ethtool_set_eeprom(dev, useraddr);
2732 break;
2733 case ETHTOOL_GCOALESCE:
2734 rc = ethtool_get_coalesce(dev, useraddr);
2735 break;
2736 case ETHTOOL_SCOALESCE:
2737 rc = ethtool_set_coalesce(dev, useraddr);
2738 break;
2739 case ETHTOOL_GRINGPARAM:
2740 rc = ethtool_get_ringparam(dev, useraddr);
2741 break;
2742 case ETHTOOL_SRINGPARAM:
2743 rc = ethtool_set_ringparam(dev, useraddr);
2744 break;
2745 case ETHTOOL_GPAUSEPARAM:
2746 rc = ethtool_get_pauseparam(dev, useraddr);
2747 break;
2748 case ETHTOOL_SPAUSEPARAM:
2749 rc = ethtool_set_pauseparam(dev, useraddr);
2750 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 case ETHTOOL_TEST:
2752 rc = ethtool_self_test(dev, useraddr);
2753 break;
2754 case ETHTOOL_GSTRINGS:
2755 rc = ethtool_get_strings(dev, useraddr);
2756 break;
2757 case ETHTOOL_PHYS_ID:
2758 rc = ethtool_phys_id(dev, useraddr);
2759 break;
2760 case ETHTOOL_GSTATS:
2761 rc = ethtool_get_stats(dev, useraddr);
2762 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002763 case ETHTOOL_GPERMADDR:
2764 rc = ethtool_get_perm_addr(dev, useraddr);
2765 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002766 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002767 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002768 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002769 break;
2770 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002771 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002772 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002773 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002774 rc = ethtool_get_value(dev, useraddr, ethcmd,
2775 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002776 break;
2777 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002778 rc = ethtool_set_value(dev, useraddr,
2779 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002780 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002781 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002782 case ETHTOOL_GRXRINGS:
2783 case ETHTOOL_GRXCLSRLCNT:
2784 case ETHTOOL_GRXCLSRULE:
2785 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002786 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002787 break;
2788 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002789 case ETHTOOL_SRXCLSRLDEL:
2790 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002791 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002792 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002793 case ETHTOOL_FLASHDEV:
2794 rc = ethtool_flash_device(dev, useraddr);
2795 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002796 case ETHTOOL_RESET:
2797 rc = ethtool_reset(dev, useraddr);
2798 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002799 case ETHTOOL_GSSET_INFO:
2800 rc = ethtool_get_sset_info(dev, useraddr);
2801 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002802 case ETHTOOL_GRXFHINDIR:
2803 rc = ethtool_get_rxfh_indir(dev, useraddr);
2804 break;
2805 case ETHTOOL_SRXFHINDIR:
2806 rc = ethtool_set_rxfh_indir(dev, useraddr);
2807 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302808 case ETHTOOL_GRSSH:
2809 rc = ethtool_get_rxfh(dev, useraddr);
2810 break;
2811 case ETHTOOL_SRSSH:
2812 rc = ethtool_set_rxfh(dev, useraddr);
2813 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002814 case ETHTOOL_GFEATURES:
2815 rc = ethtool_get_features(dev, useraddr);
2816 break;
2817 case ETHTOOL_SFEATURES:
2818 rc = ethtool_set_features(dev, useraddr);
2819 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002820 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002821 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002822 case ETHTOOL_GSG:
2823 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002824 case ETHTOOL_GGSO:
2825 case ETHTOOL_GGRO:
2826 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2827 break;
2828 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002829 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002830 case ETHTOOL_SSG:
2831 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002832 case ETHTOOL_SGSO:
2833 case ETHTOOL_SGRO:
2834 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2835 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002836 case ETHTOOL_GCHANNELS:
2837 rc = ethtool_get_channels(dev, useraddr);
2838 break;
2839 case ETHTOOL_SCHANNELS:
2840 rc = ethtool_set_channels(dev, useraddr);
2841 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002842 case ETHTOOL_SET_DUMP:
2843 rc = ethtool_set_dump(dev, useraddr);
2844 break;
2845 case ETHTOOL_GET_DUMP_FLAG:
2846 rc = ethtool_get_dump_flag(dev, useraddr);
2847 break;
2848 case ETHTOOL_GET_DUMP_DATA:
2849 rc = ethtool_get_dump_data(dev, useraddr);
2850 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002851 case ETHTOOL_GET_TS_INFO:
2852 rc = ethtool_get_ts_info(dev, useraddr);
2853 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002854 case ETHTOOL_GMODULEINFO:
2855 rc = ethtool_get_module_info(dev, useraddr);
2856 break;
2857 case ETHTOOL_GMODULEEEPROM:
2858 rc = ethtool_get_module_eeprom(dev, useraddr);
2859 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302860 case ETHTOOL_GTUNABLE:
2861 rc = ethtool_get_tunable(dev, useraddr);
2862 break;
2863 case ETHTOOL_STUNABLE:
2864 rc = ethtool_set_tunable(dev, useraddr);
2865 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002866 case ETHTOOL_GPHYSTATS:
2867 rc = ethtool_get_phy_stats(dev, useraddr);
2868 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002869 case ETHTOOL_PERQUEUE:
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002870 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002871 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002872 case ETHTOOL_GLINKSETTINGS:
2873 rc = ethtool_get_link_ksettings(dev, useraddr);
2874 break;
2875 case ETHTOOL_SLINKSETTINGS:
2876 rc = ethtool_set_link_ksettings(dev, useraddr);
2877 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002878 case ETHTOOL_PHY_GTUNABLE:
2879 rc = get_phy_tunable(dev, useraddr);
2880 break;
2881 case ETHTOOL_PHY_STUNABLE:
2882 rc = set_phy_tunable(dev, useraddr);
2883 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002884 case ETHTOOL_GFECPARAM:
2885 rc = ethtool_get_fecparam(dev, useraddr);
2886 break;
2887 case ETHTOOL_SFECPARAM:
2888 rc = ethtool_set_fecparam(dev, useraddr);
2889 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002891 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002893
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002894 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002896
2897 if (old_features != dev->features)
2898 netdev_features_change(dev);
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}