blob: e677a20180cf304a27154d12c338da046c96a546 [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",
Michał Mirosław9d921542011-11-15 15:29:55 +0000114};
115
Eyal Perry892311f2014-12-02 18:12:10 +0200116static const char
117rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
118 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
119 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800120 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200121};
122
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300123static const char
124tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
125 [ETHTOOL_ID_UNSPEC] = "Unspec",
126 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
127 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
Inbar Karmye1577c12017-11-20 16:14:30 +0200128 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300129};
130
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100131static const char
132phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
133 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100134 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100135};
136
Michał Mirosław5455c692011-02-15 16:59:17 +0000137static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
138{
139 struct ethtool_gfeatures cmd = {
140 .cmd = ETHTOOL_GFEATURES,
141 .size = ETHTOOL_DEV_FEATURE_WORDS,
142 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000143 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000144 u32 __user *sizeaddr;
145 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000146 int i;
147
148 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000149 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000150
151 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000152 features[i].available = (u32)(dev->hw_features >> (32 * i));
153 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
154 features[i].active = (u32)(dev->features >> (32 * i));
155 features[i].never_changed =
156 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000157 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000158
159 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
160 if (get_user(copy_size, sizeaddr))
161 return -EFAULT;
162
163 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
164 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
165
166 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
167 return -EFAULT;
168 useraddr += sizeof(cmd);
169 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
170 return -EFAULT;
171
172 return 0;
173}
174
175static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
176{
177 struct ethtool_sfeatures cmd;
178 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000179 netdev_features_t wanted = 0, valid = 0;
180 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000181
182 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
183 return -EFAULT;
184 useraddr += sizeof(cmd);
185
186 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
187 return -EINVAL;
188
189 if (copy_from_user(features, useraddr, sizeof(features)))
190 return -EFAULT;
191
Michał Mirosław475414f2011-11-15 15:29:55 +0000192 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000193 valid |= (netdev_features_t)features[i].valid << (32 * i);
194 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000195 }
196
197 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000198 return -EINVAL;
199
Michał Mirosław475414f2011-11-15 15:29:55 +0000200 if (valid & ~dev->hw_features) {
201 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000202 ret |= ETHTOOL_F_UNSUPPORTED;
203 }
204
Michał Mirosław475414f2011-11-15 15:29:55 +0000205 dev->wanted_features &= ~valid;
206 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700207 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000208
Michał Mirosław475414f2011-11-15 15:29:55 +0000209 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000210 ret |= ETHTOOL_F_WISH;
211
212 return ret;
213}
214
Michał Mirosław340ae162011-02-15 16:59:16 +0000215static int __ethtool_get_sset_count(struct net_device *dev, int sset)
216{
217 const struct ethtool_ops *ops = dev->ethtool_ops;
218
Michał Mirosław5455c692011-02-15 16:59:17 +0000219 if (sset == ETH_SS_FEATURES)
220 return ARRAY_SIZE(netdev_features_strings);
221
Eyal Perry892311f2014-12-02 18:12:10 +0200222 if (sset == ETH_SS_RSS_HASH_FUNCS)
223 return ARRAY_SIZE(rss_hash_func_strings);
224
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300225 if (sset == ETH_SS_TUNABLES)
226 return ARRAY_SIZE(tunable_strings);
227
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100228 if (sset == ETH_SS_PHY_TUNABLES)
229 return ARRAY_SIZE(phy_tunable_strings);
230
Florian Fainelli99943382018-04-25 12:12:48 -0700231 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
232 !ops->get_ethtool_phy_stats)
233 return phy_ethtool_get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100234
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000235 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000236 return ops->get_sset_count(dev, sset);
237 else
238 return -EOPNOTSUPP;
239}
240
241static void __ethtool_get_strings(struct net_device *dev,
242 u32 stringset, u8 *data)
243{
244 const struct ethtool_ops *ops = dev->ethtool_ops;
245
Michał Mirosław5455c692011-02-15 16:59:17 +0000246 if (stringset == ETH_SS_FEATURES)
247 memcpy(data, netdev_features_strings,
248 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200249 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
250 memcpy(data, rss_hash_func_strings,
251 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300252 else if (stringset == ETH_SS_TUNABLES)
253 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100254 else if (stringset == ETH_SS_PHY_TUNABLES)
255 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Florian Fainelli99943382018-04-25 12:12:48 -0700256 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
257 !ops->get_ethtool_phy_stats)
258 phy_ethtool_get_strings(dev->phydev, data);
259 else
Michał Mirosław5455c692011-02-15 16:59:17 +0000260 /* ops->get_strings is valid because checked earlier */
261 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000262}
263
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000264static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000265{
266 /* feature masks of legacy discrete ethtool ops */
267
268 switch (eth_cmd) {
269 case ETHTOOL_GTXCSUM:
270 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800271 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000272 case ETHTOOL_GRXCSUM:
273 case ETHTOOL_SRXCSUM:
274 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000275 case ETHTOOL_GSG:
276 case ETHTOOL_SSG:
277 return NETIF_F_SG;
278 case ETHTOOL_GTSO:
279 case ETHTOOL_STSO:
280 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000281 case ETHTOOL_GGSO:
282 case ETHTOOL_SGSO:
283 return NETIF_F_GSO;
284 case ETHTOOL_GGRO:
285 case ETHTOOL_SGRO:
286 return NETIF_F_GRO;
287 default:
288 BUG();
289 }
290}
291
Michał Mirosław0a417702011-02-15 16:59:17 +0000292static int ethtool_get_one_feature(struct net_device *dev,
293 char __user *useraddr, u32 ethcmd)
294{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000295 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000296 struct ethtool_value edata = {
297 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000298 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000299 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000300
Michał Mirosław0a417702011-02-15 16:59:17 +0000301 if (copy_to_user(useraddr, &edata, sizeof(edata)))
302 return -EFAULT;
303 return 0;
304}
305
Michał Mirosław0a417702011-02-15 16:59:17 +0000306static int ethtool_set_one_feature(struct net_device *dev,
307 void __user *useraddr, u32 ethcmd)
308{
309 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000310 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000311
312 if (copy_from_user(&edata, useraddr, sizeof(edata)))
313 return -EFAULT;
314
Michał Mirosław86794882011-02-15 16:59:17 +0000315 mask = ethtool_get_feature_mask(ethcmd);
316 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000317 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000318 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000319
320 if (edata.data)
321 dev->wanted_features |= mask;
322 else
323 dev->wanted_features &= ~mask;
324
325 __netdev_update_features(dev);
326
327 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000328}
329
Michał Mirosław02b3a552011-11-15 15:29:55 +0000330#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
331 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000332#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
333 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
334 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000335
336static u32 __ethtool_get_flags(struct net_device *dev)
337{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000338 u32 flags = 0;
339
Dragos Foianu8a731252013-07-13 14:43:00 +0100340 if (dev->features & NETIF_F_LRO)
341 flags |= ETH_FLAG_LRO;
342 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
343 flags |= ETH_FLAG_RXVLAN;
344 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
345 flags |= ETH_FLAG_TXVLAN;
346 if (dev->features & NETIF_F_NTUPLE)
347 flags |= ETH_FLAG_NTUPLE;
348 if (dev->features & NETIF_F_RXHASH)
349 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000350
351 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000352}
353
354static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000355{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000356 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000357
Michał Mirosław02b3a552011-11-15 15:29:55 +0000358 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000359 return -EINVAL;
360
Dragos Foianu8a731252013-07-13 14:43:00 +0100361 if (data & ETH_FLAG_LRO)
362 features |= NETIF_F_LRO;
363 if (data & ETH_FLAG_RXVLAN)
364 features |= NETIF_F_HW_VLAN_CTAG_RX;
365 if (data & ETH_FLAG_TXVLAN)
366 features |= NETIF_F_HW_VLAN_CTAG_TX;
367 if (data & ETH_FLAG_NTUPLE)
368 features |= NETIF_F_NTUPLE;
369 if (data & ETH_FLAG_RXHASH)
370 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000371
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000372 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000373 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000374 if (changed & ~dev->hw_features)
375 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
376
377 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000378 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000379
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700380 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000381
382 return 0;
383}
384
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700385/* Given two link masks, AND them together and save the result in dst. */
386void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
387 struct ethtool_link_ksettings *src)
388{
389 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
390 unsigned int idx = 0;
391
392 for (; idx < size; idx++) {
393 dst->link_modes.supported[idx] &=
394 src->link_modes.supported[idx];
395 dst->link_modes.advertising[idx] &=
396 src->link_modes.advertising[idx];
397 }
398}
399EXPORT_SYMBOL(ethtool_intersect_link_masks);
400
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200401void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
402 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800403{
404 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
405 dst[0] = legacy_u32;
406}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200407EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800408
409/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200410bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
411 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800412{
413 bool retval = true;
414
415 /* TODO: following test will soon always be true */
416 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
417 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
418
419 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
420 bitmap_fill(ext, 32);
421 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
422 if (bitmap_intersects(ext, src,
423 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
424 /* src mask goes beyond bit 31 */
425 retval = false;
426 }
427 }
428 *legacy_u32 = src[0];
429 return retval;
430}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200431EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800432
433/* return false if legacy contained non-0 deprecated fields
Niklas Söderlund95491e32017-10-20 01:32:08 +0200434 * maxtxpkt/maxrxpkt. rest of ksettings always updated
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800435 */
436static bool
437convert_legacy_settings_to_link_ksettings(
438 struct ethtool_link_ksettings *link_ksettings,
439 const struct ethtool_cmd *legacy_settings)
440{
441 bool retval = true;
442
443 memset(link_ksettings, 0, sizeof(*link_ksettings));
444
445 /* This is used to tell users that driver is still using these
446 * deprecated legacy fields, and they should not use
447 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
448 */
Niklas Söderlund95491e32017-10-20 01:32:08 +0200449 if (legacy_settings->maxtxpkt ||
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800450 legacy_settings->maxrxpkt)
451 retval = false;
452
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200453 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800454 link_ksettings->link_modes.supported,
455 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200456 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800457 link_ksettings->link_modes.advertising,
458 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200459 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800460 link_ksettings->link_modes.lp_advertising,
461 legacy_settings->lp_advertising);
462 link_ksettings->base.speed
463 = ethtool_cmd_speed(legacy_settings);
464 link_ksettings->base.duplex
465 = legacy_settings->duplex;
466 link_ksettings->base.port
467 = legacy_settings->port;
468 link_ksettings->base.phy_address
469 = legacy_settings->phy_address;
470 link_ksettings->base.autoneg
471 = legacy_settings->autoneg;
472 link_ksettings->base.mdio_support
473 = legacy_settings->mdio_support;
474 link_ksettings->base.eth_tp_mdix
475 = legacy_settings->eth_tp_mdix;
476 link_ksettings->base.eth_tp_mdix_ctrl
477 = legacy_settings->eth_tp_mdix_ctrl;
478 return retval;
479}
480
481/* return false if ksettings link modes had higher bits
482 * set. legacy_settings always updated (best effort)
483 */
484static bool
485convert_link_ksettings_to_legacy_settings(
486 struct ethtool_cmd *legacy_settings,
487 const struct ethtool_link_ksettings *link_ksettings)
488{
489 bool retval = true;
490
491 memset(legacy_settings, 0, sizeof(*legacy_settings));
492 /* this also clears the deprecated fields in legacy structure:
493 * __u8 transceiver;
494 * __u32 maxtxpkt;
495 * __u32 maxrxpkt;
496 */
497
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200498 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800499 &legacy_settings->supported,
500 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200501 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800502 &legacy_settings->advertising,
503 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200504 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800505 &legacy_settings->lp_advertising,
506 link_ksettings->link_modes.lp_advertising);
507 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
508 legacy_settings->duplex
509 = link_ksettings->base.duplex;
510 legacy_settings->port
511 = link_ksettings->base.port;
512 legacy_settings->phy_address
513 = link_ksettings->base.phy_address;
514 legacy_settings->autoneg
515 = link_ksettings->base.autoneg;
516 legacy_settings->mdio_support
517 = link_ksettings->base.mdio_support;
518 legacy_settings->eth_tp_mdix
519 = link_ksettings->base.eth_tp_mdix;
520 legacy_settings->eth_tp_mdix_ctrl
521 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700522 legacy_settings->transceiver
523 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800524 return retval;
525}
526
527/* number of 32-bit words to store the user's link mode bitmaps */
528#define __ETHTOOL_LINK_MODE_MASK_NU32 \
529 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
530
531/* layout of the struct passed from/to userland */
532struct ethtool_link_usettings {
533 struct ethtool_link_settings base;
534 struct {
535 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
536 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
537 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
538 } link_modes;
539};
540
541/* Internal kernel helper to query a device ethtool_link_settings.
542 *
543 * Backward compatibility note: for compatibility with legacy drivers
544 * that implement only the ethtool_cmd API, this has to work with both
545 * drivers implementing get_link_ksettings API and drivers
546 * implementing get_settings API. When drivers implement get_settings
547 * and report ethtool_cmd deprecated fields
548 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
549 * because the resulting struct ethtool_link_settings does not report them.
550 */
551int __ethtool_get_link_ksettings(struct net_device *dev,
552 struct ethtool_link_ksettings *link_ksettings)
553{
554 int err;
555 struct ethtool_cmd cmd;
556
557 ASSERT_RTNL();
558
559 if (dev->ethtool_ops->get_link_ksettings) {
560 memset(link_ksettings, 0, sizeof(*link_ksettings));
561 return dev->ethtool_ops->get_link_ksettings(dev,
562 link_ksettings);
563 }
564
565 /* driver doesn't support %ethtool_link_ksettings API. revert to
566 * legacy %ethtool_cmd API, unless it's not supported either.
567 * TODO: remove when ethtool_ops::get_settings disappears internally
568 */
David Decotigny3237fc62016-02-24 10:58:11 -0800569 if (!dev->ethtool_ops->get_settings)
570 return -EOPNOTSUPP;
571
572 memset(&cmd, 0, sizeof(cmd));
573 cmd.cmd = ETHTOOL_GSET;
574 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800575 if (err < 0)
576 return err;
577
578 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
579 */
580 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
581 return err;
582}
583EXPORT_SYMBOL(__ethtool_get_link_ksettings);
584
585/* convert ethtool_link_usettings in user space to a kernel internal
586 * ethtool_link_ksettings. return 0 on success, errno on error.
587 */
588static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
589 const void __user *from)
590{
591 struct ethtool_link_usettings link_usettings;
592
593 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
594 return -EFAULT;
595
596 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800597 bitmap_from_arr32(to->link_modes.supported,
598 link_usettings.link_modes.supported,
599 __ETHTOOL_LINK_MODE_MASK_NBITS);
600 bitmap_from_arr32(to->link_modes.advertising,
601 link_usettings.link_modes.advertising,
602 __ETHTOOL_LINK_MODE_MASK_NBITS);
603 bitmap_from_arr32(to->link_modes.lp_advertising,
604 link_usettings.link_modes.lp_advertising,
605 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800606
607 return 0;
608}
609
610/* convert a kernel internal ethtool_link_ksettings to
611 * ethtool_link_usettings in user space. return 0 on success, errno on
612 * error.
613 */
614static int
615store_link_ksettings_for_user(void __user *to,
616 const struct ethtool_link_ksettings *from)
617{
618 struct ethtool_link_usettings link_usettings;
619
620 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800621 bitmap_to_arr32(link_usettings.link_modes.supported,
622 from->link_modes.supported,
623 __ETHTOOL_LINK_MODE_MASK_NBITS);
624 bitmap_to_arr32(link_usettings.link_modes.advertising,
625 from->link_modes.advertising,
626 __ETHTOOL_LINK_MODE_MASK_NBITS);
627 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
628 from->link_modes.lp_advertising,
629 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800630
631 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
632 return -EFAULT;
633
634 return 0;
635}
636
637/* Query device for its ethtool_link_settings.
638 *
639 * Backward compatibility note: this function must fail when driver
640 * does not implement ethtool::get_link_ksettings, even if legacy
641 * ethtool_ops::get_settings is implemented. This tells new versions
642 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
643 * this driver, so that they can correctly access the ethtool_cmd
644 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
645 * implements ethtool_ops::get_settings anymore.
646 */
647static int ethtool_get_link_ksettings(struct net_device *dev,
648 void __user *useraddr)
649{
650 int err = 0;
651 struct ethtool_link_ksettings link_ksettings;
652
653 ASSERT_RTNL();
654
655 if (!dev->ethtool_ops->get_link_ksettings)
656 return -EOPNOTSUPP;
657
658 /* handle bitmap nbits handshake */
659 if (copy_from_user(&link_ksettings.base, useraddr,
660 sizeof(link_ksettings.base)))
661 return -EFAULT;
662
663 if (__ETHTOOL_LINK_MODE_MASK_NU32
664 != link_ksettings.base.link_mode_masks_nwords) {
665 /* wrong link mode nbits requested */
666 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000667 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800668 /* send back number of words required as negative val */
669 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
670 "need too many bits for link modes!");
671 link_ksettings.base.link_mode_masks_nwords
672 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
673
674 /* copy the base fields back to user, not the link
675 * mode bitmaps
676 */
677 if (copy_to_user(useraddr, &link_ksettings.base,
678 sizeof(link_ksettings.base)))
679 return -EFAULT;
680
681 return 0;
682 }
683
684 /* handshake successful: user/kernel agree on
685 * link_mode_masks_nwords
686 */
687
688 memset(&link_ksettings, 0, sizeof(link_ksettings));
689 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
690 if (err < 0)
691 return err;
692
693 /* make sure we tell the right values to user */
694 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
695 link_ksettings.base.link_mode_masks_nwords
696 = __ETHTOOL_LINK_MODE_MASK_NU32;
697
698 return store_link_ksettings_for_user(useraddr, &link_ksettings);
699}
700
701/* Update device ethtool_link_settings.
702 *
703 * Backward compatibility note: this function must fail when driver
704 * does not implement ethtool::set_link_ksettings, even if legacy
705 * ethtool_ops::set_settings is implemented. This tells new versions
706 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
707 * this driver, so that they can correctly update the ethtool_cmd
708 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
709 * implements ethtool_ops::get_settings anymore.
710 */
711static int ethtool_set_link_ksettings(struct net_device *dev,
712 void __user *useraddr)
713{
714 int err;
715 struct ethtool_link_ksettings link_ksettings;
716
717 ASSERT_RTNL();
718
719 if (!dev->ethtool_ops->set_link_ksettings)
720 return -EOPNOTSUPP;
721
722 /* make sure nbits field has expected value */
723 if (copy_from_user(&link_ksettings.base, useraddr,
724 sizeof(link_ksettings.base)))
725 return -EFAULT;
726
727 if (__ETHTOOL_LINK_MODE_MASK_NU32
728 != link_ksettings.base.link_mode_masks_nwords)
729 return -EINVAL;
730
731 /* copy the whole structure, now that we know it has expected
732 * format
733 */
734 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
735 if (err)
736 return err;
737
738 /* re-check nwords field, just in case */
739 if (__ETHTOOL_LINK_MODE_MASK_NU32
740 != link_ksettings.base.link_mode_masks_nwords)
741 return -EINVAL;
742
743 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
744}
745
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800746/* Query device for its ethtool_cmd settings.
747 *
748 * Backward compatibility note: for compatibility with legacy ethtool,
749 * this has to work with both drivers implementing get_link_ksettings
750 * API and drivers implementing get_settings API. When drivers
751 * implement get_link_ksettings and report higher link mode bits, a
752 * kernel warning is logged once (with name of 1st driver/device) to
753 * recommend user to upgrade ethtool, but the command is successful
754 * (only the lower link mode bits reported back to user).
755 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000756static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
757{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000758 struct ethtool_cmd cmd;
759
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800760 ASSERT_RTNL();
761
762 if (dev->ethtool_ops->get_link_ksettings) {
763 /* First, use link_ksettings API if it is supported */
764 int err;
765 struct ethtool_link_ksettings link_ksettings;
766
767 memset(&link_ksettings, 0, sizeof(link_ksettings));
768 err = dev->ethtool_ops->get_link_ksettings(dev,
769 &link_ksettings);
770 if (err < 0)
771 return err;
Stephen Hemminger71891e22017-12-29 10:02:52 -0800772 convert_link_ksettings_to_legacy_settings(&cmd,
773 &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800774
775 /* send a sensible cmd tag back to user */
776 cmd.cmd = ETHTOOL_GSET;
777 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800778 /* driver doesn't support %ethtool_link_ksettings
779 * API. revert to legacy %ethtool_cmd API, unless it's
780 * not supported either.
781 */
David Decotigny3237fc62016-02-24 10:58:11 -0800782 int err;
783
784 if (!dev->ethtool_ops->get_settings)
785 return -EOPNOTSUPP;
786
787 memset(&cmd, 0, sizeof(cmd));
788 cmd.cmd = ETHTOOL_GSET;
789 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800790 if (err < 0)
791 return err;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
795 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798}
799
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800800/* Update device link settings with given ethtool_cmd.
801 *
802 * Backward compatibility note: for compatibility with legacy ethtool,
803 * this has to work with both drivers implementing set_link_ksettings
804 * API and drivers implementing set_settings API. When drivers
805 * implement set_link_ksettings and user's request updates deprecated
806 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
807 * warning is logged once (with name of 1st driver/device) to
808 * recommend user to upgrade ethtool, and the request is rejected.
809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
811{
812 struct ethtool_cmd cmd;
813
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800814 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
817 return -EFAULT;
818
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800819 /* first, try new %ethtool_link_ksettings API. */
820 if (dev->ethtool_ops->set_link_ksettings) {
821 struct ethtool_link_ksettings link_ksettings;
822
823 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
824 &cmd))
825 return -EINVAL;
826
827 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
828 link_ksettings.base.link_mode_masks_nwords
829 = __ETHTOOL_LINK_MODE_MASK_NU32;
830 return dev->ethtool_ops->set_link_ksettings(dev,
831 &link_ksettings);
832 }
833
834 /* legacy %ethtool_cmd API */
835
836 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
837 * disappears internally
838 */
839
840 if (!dev->ethtool_ops->set_settings)
841 return -EOPNOTSUPP;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return dev->ethtool_ops->set_settings(dev, &cmd);
844}
845
chavey97f8aef2010-04-07 21:54:42 -0700846static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
847 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700850 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 memset(&info, 0, sizeof(info));
853 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000854 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700855 ops->get_drvinfo(dev, &info);
856 } else if (dev->dev.parent && dev->dev.parent->driver) {
857 strlcpy(info.bus_info, dev_name(dev->dev.parent),
858 sizeof(info.bus_info));
859 strlcpy(info.driver, dev->dev.parent->driver->name,
860 sizeof(info.driver));
861 } else {
862 return -EOPNOTSUPP;
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Jeff Garzik723b2f52010-03-03 22:51:50 +0000865 /*
866 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000867 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000868 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000869 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700870 int rc;
871
872 rc = ops->get_sset_count(dev, ETH_SS_TEST);
873 if (rc >= 0)
874 info.testinfo_len = rc;
875 rc = ops->get_sset_count(dev, ETH_SS_STATS);
876 if (rc >= 0)
877 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700878 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
879 if (rc >= 0)
880 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700881 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000882 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000884 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 info.eedump_len = ops->get_eeprom_len(dev);
886
887 if (copy_to_user(useraddr, &info, sizeof(info)))
888 return -EFAULT;
889 return 0;
890}
891
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800892static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700893 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000894{
895 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000896 u64 sset_mask;
897 int i, idx = 0, n_bits = 0, ret, rc;
898 u32 *info_buf = NULL;
899
Jeff Garzik723b2f52010-03-03 22:51:50 +0000900 if (copy_from_user(&info, useraddr, sizeof(info)))
901 return -EFAULT;
902
903 /* store copy of mask, because we zero struct later on */
904 sset_mask = info.sset_mask;
905 if (!sset_mask)
906 return 0;
907
908 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000909 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000910
911 memset(&info, 0, sizeof(info));
912 info.cmd = ETHTOOL_GSSET_INFO;
913
Kees Cook6396bb22018-06-12 14:03:40 -0700914 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000915 if (!info_buf)
916 return -ENOMEM;
917
918 /*
919 * fill return buffer based on input bitmask and successful
920 * get_sset_count return
921 */
922 for (i = 0; i < 64; i++) {
923 if (!(sset_mask & (1ULL << i)))
924 continue;
925
Michał Mirosław340ae162011-02-15 16:59:16 +0000926 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000927 if (rc >= 0) {
928 info.sset_mask |= (1ULL << i);
929 info_buf[idx++] = rc;
930 }
931 }
932
933 ret = -EFAULT;
934 if (copy_to_user(useraddr, &info, sizeof(info)))
935 goto out;
936
937 useraddr += offsetof(struct ethtool_sset_info, data);
938 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
939 goto out;
940
941 ret = 0;
942
943out:
944 kfree(info_buf);
945 return ret;
946}
947
chavey97f8aef2010-04-07 21:54:42 -0700948static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000949 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700950{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000951 struct ethtool_rxnfc info;
952 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000953 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700954
Santwona Behera59089d82009-02-20 00:58:13 -0800955 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700956 return -EOPNOTSUPP;
957
Ben Hutchingsbf988432010-06-28 08:45:58 +0000958 /* struct ethtool_rxnfc was originally defined for
959 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
960 * members. User-space might still be using that
961 * definition. */
962 if (cmd == ETHTOOL_SRXFH)
963 info_size = (offsetof(struct ethtool_rxnfc, data) +
964 sizeof(info.data));
965
966 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700967 return -EFAULT;
968
Ben Hutchings55664f32012-01-03 12:04:51 +0000969 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
970 if (rc)
971 return rc;
972
973 if (cmd == ETHTOOL_SRXCLSRLINS &&
974 copy_to_user(useraddr, &info, info_size))
975 return -EFAULT;
976
977 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700978}
979
chavey97f8aef2010-04-07 21:54:42 -0700980static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000981 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700982{
983 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000984 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800985 const struct ethtool_ops *ops = dev->ethtool_ops;
986 int ret;
987 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700988
Santwona Behera59089d82009-02-20 00:58:13 -0800989 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700990 return -EOPNOTSUPP;
991
Ben Hutchingsbf988432010-06-28 08:45:58 +0000992 /* struct ethtool_rxnfc was originally defined for
993 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
994 * members. User-space might still be using that
995 * definition. */
996 if (cmd == ETHTOOL_GRXFH)
997 info_size = (offsetof(struct ethtool_rxnfc, data) +
998 sizeof(info.data));
999
1000 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001001 return -EFAULT;
1002
Edward Cree84a1d9c42018-03-08 15:45:03 +00001003 /* If FLOW_RSS was requested then user-space must be using the
1004 * new definition, as FLOW_RSS is newer.
1005 */
1006 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1007 info_size = sizeof(info);
1008 if (copy_from_user(&info, useraddr, info_size))
1009 return -EFAULT;
Wenwen Wangd656fe42018-04-30 12:31:13 -05001010 /* Since malicious users may modify the original data,
1011 * we need to check whether FLOW_RSS is still requested.
1012 */
1013 if (!(info.flow_type & FLOW_RSS))
1014 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001015 }
1016
Santwona Behera59089d82009-02-20 00:58:13 -08001017 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1018 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001019 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cook6396bb22018-06-12 14:03:40 -07001020 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001021 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001022 if (!rule_buf)
1023 return -ENOMEM;
1024 }
1025 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001026
Santwona Behera59089d82009-02-20 00:58:13 -08001027 ret = ops->get_rxnfc(dev, &info, rule_buf);
1028 if (ret < 0)
1029 goto err_out;
1030
1031 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001032 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001033 goto err_out;
1034
1035 if (rule_buf) {
1036 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1037 if (copy_to_user(useraddr, rule_buf,
1038 info.rule_cnt * sizeof(u32)))
1039 goto err_out;
1040 }
1041 ret = 0;
1042
1043err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001044 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001045
1046 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001047}
1048
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301049static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1050 struct ethtool_rxnfc *rx_rings,
1051 u32 size)
1052{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001053 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301054
1055 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001056 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301057
1058 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001059 for (i = 0; i < size; i++)
1060 if (indir[i] >= rx_rings->data)
1061 return -EINVAL;
1062
1063 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301064}
1065
Kim Jonesba905f52016-02-02 03:51:16 +00001066u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001067
1068void netdev_rss_key_fill(void *buffer, size_t len)
1069{
1070 BUG_ON(len > sizeof(netdev_rss_key));
1071 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1072 memcpy(buffer, netdev_rss_key, len);
1073}
1074EXPORT_SYMBOL(netdev_rss_key_fill);
1075
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001076static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1077{
1078 u32 dev_size, current_max = 0;
1079 u32 *indir;
1080 int ret;
1081
1082 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1083 !dev->ethtool_ops->get_rxfh)
1084 return -EOPNOTSUPP;
1085 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1086 if (dev_size == 0)
1087 return -EOPNOTSUPP;
1088
1089 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1090 if (!indir)
1091 return -ENOMEM;
1092
1093 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1094 if (ret)
1095 goto out;
1096
1097 while (dev_size--)
1098 current_max = max(current_max, indir[dev_size]);
1099
1100 *max = current_max;
1101
1102out:
1103 kfree(indir);
1104 return ret;
1105}
1106
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001107static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1108 void __user *useraddr)
1109{
Ben Hutchings7850f632011-12-15 13:55:01 +00001110 u32 user_size, dev_size;
1111 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001112 int ret;
1113
Ben Hutchings7850f632011-12-15 13:55:01 +00001114 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001115 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001116 return -EOPNOTSUPP;
1117 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1118 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001119 return -EOPNOTSUPP;
1120
Ben Hutchings7850f632011-12-15 13:55:01 +00001121 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001122 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001123 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001124 return -EFAULT;
1125
Ben Hutchings7850f632011-12-15 13:55:01 +00001126 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1127 &dev_size, sizeof(dev_size)))
1128 return -EFAULT;
1129
1130 /* If the user buffer size is 0, this is just a query for the
1131 * device table size. Otherwise, if it's smaller than the
1132 * device table size it's an error.
1133 */
1134 if (user_size < dev_size)
1135 return user_size == 0 ? 0 : -EINVAL;
1136
1137 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001138 if (!indir)
1139 return -ENOMEM;
1140
Eyal Perry892311f2014-12-02 18:12:10 +02001141 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001142 if (ret)
1143 goto out;
1144
Ben Hutchings7850f632011-12-15 13:55:01 +00001145 if (copy_to_user(useraddr +
1146 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1147 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001148 ret = -EFAULT;
1149
1150out:
1151 kfree(indir);
1152 return ret;
1153}
1154
1155static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1156 void __user *useraddr)
1157{
Ben Hutchings7850f632011-12-15 13:55:01 +00001158 struct ethtool_rxnfc rx_rings;
1159 u32 user_size, dev_size, i;
1160 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001161 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001162 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301163 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001164
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001165 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001166 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001167 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001168
1169 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001170 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001171 return -EOPNOTSUPP;
1172
Ben Hutchings7850f632011-12-15 13:55:01 +00001173 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001174 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001175 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001176 return -EFAULT;
1177
Ben Hutchings278bc422011-12-15 13:56:49 +00001178 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001179 return -EINVAL;
1180
1181 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001182 if (!indir)
1183 return -ENOMEM;
1184
Ben Hutchings7850f632011-12-15 13:55:01 +00001185 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001186 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001187 if (ret)
1188 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001189
1190 if (user_size == 0) {
1191 for (i = 0; i < dev_size; i++)
1192 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1193 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301194 ret = ethtool_copy_validate_indir(indir,
1195 useraddr + ringidx_offset,
1196 &rx_rings,
1197 dev_size);
1198 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001199 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001200 }
1201
Eyal Perry892311f2014-12-02 18:12:10 +02001202 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001203 if (ret)
1204 goto out;
1205
1206 /* indicate whether rxfh was set to default */
1207 if (user_size == 0)
1208 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1209 else
1210 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001211
1212out:
1213 kfree(indir);
1214 return ret;
1215}
1216
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301217static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1218 void __user *useraddr)
1219{
1220 int ret;
1221 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001222 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301223 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001224 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301225 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001226 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301227 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001228 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301229 u8 *hkey = NULL;
1230 u8 *rss_config;
1231
Eyal Perry892311f2014-12-02 18:12:10 +02001232 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301233 return -EOPNOTSUPP;
1234
1235 if (ops->get_rxfh_indir_size)
1236 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301237 if (ops->get_rxfh_key_size)
1238 dev_key_size = ops->get_rxfh_key_size(dev);
1239
Ben Hutchingsf062a382014-05-15 16:28:07 +01001240 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301241 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001242 user_indir_size = rxfh.indir_size;
1243 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301244
Ben Hutchingsf062a382014-05-15 16:28:07 +01001245 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001246 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001247 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001248 /* Most drivers don't handle rss_context, check it's 0 as well */
1249 if (rxfh.rss_context && !ops->get_rxfh_context)
1250 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001251
1252 rxfh.indir_size = dev_indir_size;
1253 rxfh.key_size = dev_key_size;
1254 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301255 return -EFAULT;
1256
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301257 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1258 (user_key_size && (user_key_size != dev_key_size)))
1259 return -EINVAL;
1260
1261 indir_bytes = user_indir_size * sizeof(indir[0]);
1262 total_size = indir_bytes + user_key_size;
1263 rss_config = kzalloc(total_size, GFP_USER);
1264 if (!rss_config)
1265 return -ENOMEM;
1266
1267 if (user_indir_size)
1268 indir = (u32 *)rss_config;
1269
1270 if (user_key_size)
1271 hkey = rss_config + indir_bytes;
1272
Edward Cree84a1d9c42018-03-08 15:45:03 +00001273 if (rxfh.rss_context)
1274 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1275 &dev_hfunc,
1276 rxfh.rss_context);
1277 else
1278 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001279 if (ret)
1280 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301281
Eyal Perry892311f2014-12-02 18:12:10 +02001282 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1283 &dev_hfunc, sizeof(rxfh.hfunc))) {
1284 ret = -EFAULT;
1285 } else if (copy_to_user(useraddr +
1286 offsetof(struct ethtool_rxfh, rss_config[0]),
1287 rss_config, total_size)) {
1288 ret = -EFAULT;
1289 }
1290out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301291 kfree(rss_config);
1292
1293 return ret;
1294}
1295
1296static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1297 void __user *useraddr)
1298{
1299 int ret;
1300 const struct ethtool_ops *ops = dev->ethtool_ops;
1301 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001302 struct ethtool_rxfh rxfh;
1303 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301304 u32 *indir = NULL, indir_bytes = 0;
1305 u8 *hkey = NULL;
1306 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301307 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001308 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301309
Eyal Perry892311f2014-12-02 18:12:10 +02001310 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301311 return -EOPNOTSUPP;
1312
1313 if (ops->get_rxfh_indir_size)
1314 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301315 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001316 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301317
Ben Hutchingsf062a382014-05-15 16:28:07 +01001318 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301319 return -EFAULT;
1320
Ben Hutchingsf062a382014-05-15 16:28:07 +01001321 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001322 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001323 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001324 /* Most drivers don't handle rss_context, check it's 0 as well */
1325 if (rxfh.rss_context && !ops->set_rxfh_context)
1326 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001327
Eyal Perry892311f2014-12-02 18:12:10 +02001328 /* If either indir, hash key or function is valid, proceed further.
1329 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301330 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001331 if ((rxfh.indir_size &&
1332 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1333 rxfh.indir_size != dev_indir_size) ||
1334 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1335 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001336 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301337 return -EINVAL;
1338
Ben Hutchingsf062a382014-05-15 16:28:07 +01001339 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301340 indir_bytes = dev_indir_size * sizeof(indir[0]);
1341
Ben Hutchingsf062a382014-05-15 16:28:07 +01001342 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301343 if (!rss_config)
1344 return -ENOMEM;
1345
1346 rx_rings.cmd = ETHTOOL_GRXRINGS;
1347 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1348 if (ret)
1349 goto out;
1350
Edward Cree84a1d9c42018-03-08 15:45:03 +00001351 /* rxfh.indir_size == 0 means reset the indir table to default (master
1352 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001353 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301354 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001355 if (rxfh.indir_size &&
1356 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301357 indir = (u32 *)rss_config;
1358 ret = ethtool_copy_validate_indir(indir,
1359 useraddr + rss_cfg_offset,
1360 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001361 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301362 if (ret)
1363 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001364 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001365 if (rxfh.rss_context == 0) {
1366 indir = (u32 *)rss_config;
1367 for (i = 0; i < dev_indir_size; i++)
1368 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1369 } else {
1370 delete = true;
1371 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301372 }
1373
Ben Hutchingsf062a382014-05-15 16:28:07 +01001374 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301375 hkey = rss_config + indir_bytes;
1376 if (copy_from_user(hkey,
1377 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001378 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301379 ret = -EFAULT;
1380 goto out;
1381 }
1382 }
1383
Edward Cree84a1d9c42018-03-08 15:45:03 +00001384 if (rxfh.rss_context)
1385 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1386 &rxfh.rss_context, delete);
1387 else
1388 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001389 if (ret)
1390 goto out;
1391
Edward Cree84a1d9c42018-03-08 15:45:03 +00001392 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1393 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1394 ret = -EFAULT;
1395
1396 if (!rxfh.rss_context) {
1397 /* indicate whether rxfh was set to default */
1398 if (rxfh.indir_size == 0)
1399 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1400 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1401 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1402 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301403
1404out:
1405 kfree(rss_config);
1406 return ret;
1407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1410{
1411 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001412 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 void *regbuf;
1414 int reglen, ret;
1415
1416 if (!ops->get_regs || !ops->get_regs_len)
1417 return -EOPNOTSUPP;
1418
1419 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1420 return -EFAULT;
1421
1422 reglen = ops->get_regs_len(dev);
1423 if (regs.len > reglen)
1424 regs.len = reglen;
1425
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001426 regbuf = NULL;
1427 if (reglen) {
1428 regbuf = vzalloc(reglen);
1429 if (!regbuf)
1430 return -ENOMEM;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 ops->get_regs(dev, &regs, regbuf);
1434
1435 ret = -EFAULT;
1436 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1437 goto out;
1438 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001439 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 goto out;
1441 ret = 0;
1442
1443 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001444 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return ret;
1446}
1447
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001448static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1449{
1450 struct ethtool_value reset;
1451 int ret;
1452
1453 if (!dev->ethtool_ops->reset)
1454 return -EOPNOTSUPP;
1455
1456 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1457 return -EFAULT;
1458
1459 ret = dev->ethtool_ops->reset(dev, &reset.data);
1460 if (ret)
1461 return ret;
1462
1463 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1464 return -EFAULT;
1465 return 0;
1466}
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1469{
Roland Dreier8e557422010-02-11 12:14:23 -08001470 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (!dev->ethtool_ops->get_wol)
1473 return -EOPNOTSUPP;
1474
1475 dev->ethtool_ops->get_wol(dev, &wol);
1476
1477 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1478 return -EFAULT;
1479 return 0;
1480}
1481
1482static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1483{
1484 struct ethtool_wolinfo wol;
1485
1486 if (!dev->ethtool_ops->set_wol)
1487 return -EOPNOTSUPP;
1488
1489 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1490 return -EFAULT;
1491
1492 return dev->ethtool_ops->set_wol(dev, &wol);
1493}
1494
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001495static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1496{
1497 struct ethtool_eee edata;
1498 int rc;
1499
1500 if (!dev->ethtool_ops->get_eee)
1501 return -EOPNOTSUPP;
1502
1503 memset(&edata, 0, sizeof(struct ethtool_eee));
1504 edata.cmd = ETHTOOL_GEEE;
1505 rc = dev->ethtool_ops->get_eee(dev, &edata);
1506
1507 if (rc)
1508 return rc;
1509
1510 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1511 return -EFAULT;
1512
1513 return 0;
1514}
1515
1516static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1517{
1518 struct ethtool_eee edata;
1519
1520 if (!dev->ethtool_ops->set_eee)
1521 return -EOPNOTSUPP;
1522
1523 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1524 return -EFAULT;
1525
1526 return dev->ethtool_ops->set_eee(dev, &edata);
1527}
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529static int ethtool_nway_reset(struct net_device *dev)
1530{
1531 if (!dev->ethtool_ops->nway_reset)
1532 return -EOPNOTSUPP;
1533
1534 return dev->ethtool_ops->nway_reset(dev);
1535}
1536
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001537static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1538{
1539 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1540
1541 if (!dev->ethtool_ops->get_link)
1542 return -EOPNOTSUPP;
1543
1544 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1545
1546 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1547 return -EFAULT;
1548 return 0;
1549}
1550
Ben Hutchings081d0942012-04-12 00:42:12 +00001551static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1552 int (*getter)(struct net_device *,
1553 struct ethtool_eeprom *, u8 *),
1554 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001557 void __user *userbuf = useraddr + sizeof(eeprom);
1558 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001560 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1563 return -EFAULT;
1564
1565 /* Check for wrap and zero */
1566 if (eeprom.offset + eeprom.len <= eeprom.offset)
1567 return -EINVAL;
1568
1569 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001570 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return -EINVAL;
1572
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001573 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (!data)
1575 return -ENOMEM;
1576
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001577 bytes_remaining = eeprom.len;
1578 while (bytes_remaining > 0) {
1579 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Ben Hutchings081d0942012-04-12 00:42:12 +00001581 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001582 if (ret)
1583 break;
1584 if (copy_to_user(userbuf, data, eeprom.len)) {
1585 ret = -EFAULT;
1586 break;
1587 }
1588 userbuf += eeprom.len;
1589 eeprom.offset += eeprom.len;
1590 bytes_remaining -= eeprom.len;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001593 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1594 eeprom.offset -= eeprom.len;
1595 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1596 ret = -EFAULT;
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 kfree(data);
1599 return ret;
1600}
1601
Ben Hutchings081d0942012-04-12 00:42:12 +00001602static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1603{
1604 const struct ethtool_ops *ops = dev->ethtool_ops;
1605
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001606 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1607 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001608 return -EOPNOTSUPP;
1609
1610 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1611 ops->get_eeprom_len(dev));
1612}
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1615{
1616 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001617 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001618 void __user *userbuf = useraddr + sizeof(eeprom);
1619 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001621 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001623 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1624 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return -EOPNOTSUPP;
1626
1627 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1628 return -EFAULT;
1629
1630 /* Check for wrap and zero */
1631 if (eeprom.offset + eeprom.len <= eeprom.offset)
1632 return -EINVAL;
1633
1634 /* Check for exceeding total eeprom len */
1635 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1636 return -EINVAL;
1637
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001638 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (!data)
1640 return -ENOMEM;
1641
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001642 bytes_remaining = eeprom.len;
1643 while (bytes_remaining > 0) {
1644 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001646 if (copy_from_user(data, userbuf, eeprom.len)) {
1647 ret = -EFAULT;
1648 break;
1649 }
1650 ret = ops->set_eeprom(dev, &eeprom, data);
1651 if (ret)
1652 break;
1653 userbuf += eeprom.len;
1654 eeprom.offset += eeprom.len;
1655 bytes_remaining -= eeprom.len;
1656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 kfree(data);
1659 return ret;
1660}
1661
chavey97f8aef2010-04-07 21:54:42 -07001662static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1663 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Roland Dreier8e557422010-02-11 12:14:23 -08001665 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 if (!dev->ethtool_ops->get_coalesce)
1668 return -EOPNOTSUPP;
1669
1670 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1671
1672 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1673 return -EFAULT;
1674 return 0;
1675}
1676
chavey97f8aef2010-04-07 21:54:42 -07001677static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1678 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 struct ethtool_coalesce coalesce;
1681
David S. Millerfa04ae52005-06-06 15:07:19 -07001682 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return -EOPNOTSUPP;
1684
1685 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1686 return -EFAULT;
1687
1688 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1689}
1690
1691static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1692{
Roland Dreier8e557422010-02-11 12:14:23 -08001693 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 if (!dev->ethtool_ops->get_ringparam)
1696 return -EOPNOTSUPP;
1697
1698 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1699
1700 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1701 return -EFAULT;
1702 return 0;
1703}
1704
1705static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1706{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001707 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001709 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return -EOPNOTSUPP;
1711
1712 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1713 return -EFAULT;
1714
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001715 dev->ethtool_ops->get_ringparam(dev, &max);
1716
1717 /* ensure new ring parameters are within the maximums */
1718 if (ringparam.rx_pending > max.rx_max_pending ||
1719 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1720 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1721 ringparam.tx_pending > max.tx_max_pending)
1722 return -EINVAL;
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1725}
1726
amit salecha8b5933c2011-04-07 01:58:42 +00001727static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1728 void __user *useraddr)
1729{
1730 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1731
1732 if (!dev->ethtool_ops->get_channels)
1733 return -EOPNOTSUPP;
1734
1735 dev->ethtool_ops->get_channels(dev, &channels);
1736
1737 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1738 return -EFAULT;
1739 return 0;
1740}
1741
1742static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1743 void __user *useraddr)
1744{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001745 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001746 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001747
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001748 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001749 return -EOPNOTSUPP;
1750
1751 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1752 return -EFAULT;
1753
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001754 dev->ethtool_ops->get_channels(dev, &max);
1755
1756 /* ensure new counts are within the maximums */
1757 if ((channels.rx_count > max.max_rx) ||
1758 (channels.tx_count > max.max_tx) ||
1759 (channels.combined_count > max.max_combined) ||
1760 (channels.other_count > max.max_other))
1761 return -EINVAL;
1762
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001763 /* ensure the new Rx count fits within the configured Rx flow
1764 * indirection table settings */
1765 if (netif_is_rxfh_configured(dev) &&
1766 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1767 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1768 return -EINVAL;
1769
amit salecha8b5933c2011-04-07 01:58:42 +00001770 return dev->ethtool_ops->set_channels(dev, &channels);
1771}
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1774{
1775 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1776
1777 if (!dev->ethtool_ops->get_pauseparam)
1778 return -EOPNOTSUPP;
1779
1780 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1781
1782 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1783 return -EFAULT;
1784 return 0;
1785}
1786
1787static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1788{
1789 struct ethtool_pauseparam pauseparam;
1790
Jeff Garzike1b90c42006-07-17 12:54:40 -04001791 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return -EOPNOTSUPP;
1793
1794 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1795 return -EFAULT;
1796
1797 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1801{
1802 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001803 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001805 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001807 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001808 return -EOPNOTSUPP;
1809
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001810 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001811 if (test_len < 0)
1812 return test_len;
1813 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 if (copy_from_user(&test, useraddr, sizeof(test)))
1816 return -EFAULT;
1817
Jeff Garzikff03d492007-08-15 16:01:08 -07001818 test.len = test_len;
Kees Cook6da2ec52018-06-12 13:55:00 -07001819 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (!data)
1821 return -ENOMEM;
1822
1823 ops->self_test(dev, &test, data);
1824
1825 ret = -EFAULT;
1826 if (copy_to_user(useraddr, &test, sizeof(test)))
1827 goto out;
1828 useraddr += sizeof(test);
1829 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1830 goto out;
1831 ret = 0;
1832
1833 out:
1834 kfree(data);
1835 return ret;
1836}
1837
1838static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1839{
1840 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 u8 *data;
1842 int ret;
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1845 return -EFAULT;
1846
Michał Mirosław340ae162011-02-15 16:59:16 +00001847 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001848 if (ret < 0)
1849 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001850 if (ret > S32_MAX / ETH_GSTRING_LEN)
1851 return -ENOMEM;
1852 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001853
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001854 gstrings.len = ret;
Kees Cookfad953c2018-06-12 14:27:37 -07001855 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001856 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return -ENOMEM;
1858
Michał Mirosław340ae162011-02-15 16:59:16 +00001859 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 ret = -EFAULT;
1862 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1863 goto out;
1864 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001865 if (gstrings.len &&
1866 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 goto out;
1868 ret = 0;
1869
Michał Mirosław340ae162011-02-15 16:59:16 +00001870out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001871 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return ret;
1873}
1874
1875static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1876{
1877 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001878 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001879 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001880 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001882 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return -EOPNOTSUPP;
1884
Ben Hutchings68f512f2011-04-02 00:35:15 +01001885 if (busy)
1886 return -EBUSY;
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (copy_from_user(&id, useraddr, sizeof(id)))
1889 return -EFAULT;
1890
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001891 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001892 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001893 return rc;
1894
1895 /* Drop the RTNL lock while waiting, but prevent reentry or
1896 * removal of the device.
1897 */
1898 busy = true;
1899 dev_hold(dev);
1900 rtnl_unlock();
1901
1902 if (rc == 0) {
1903 /* Driver will handle this itself */
1904 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001905 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001906 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001907 /* Driver expects to be called at twice the frequency in rc */
1908 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001909
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001910 /* Count down seconds */
1911 do {
1912 /* Count down iterations per second */
1913 i = n;
1914 do {
1915 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001916 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001917 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1918 rtnl_unlock();
1919 if (rc)
1920 break;
1921 schedule_timeout_interruptible(interval);
1922 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001923 } while (!signal_pending(current) &&
1924 (id.data == 0 || --id.data != 0));
1925 }
1926
1927 rtnl_lock();
1928 dev_put(dev);
1929 busy = false;
1930
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001931 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001932 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1936{
1937 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001938 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001940 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001942 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001943 return -EOPNOTSUPP;
1944
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001945 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001946 if (n_stats < 0)
1947 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001948 if (n_stats > S32_MAX / sizeof(u64))
1949 return -ENOMEM;
1950 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1952 return -EFAULT;
1953
Jeff Garzikff03d492007-08-15 16:01:08 -07001954 stats.n_stats = n_stats;
Kees Cookfad953c2018-06-12 14:27:37 -07001955 data = vzalloc(array_size(n_stats, sizeof(u64)));
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001956 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -ENOMEM;
1958
1959 ops->get_ethtool_stats(dev, &stats, data);
1960
1961 ret = -EFAULT;
1962 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1963 goto out;
1964 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001965 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto out;
1967 ret = 0;
1968
1969 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001970 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return ret;
1972}
1973
Andrew Lunnf3a40942015-12-30 16:28:25 +01001974static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1975{
Florian Fainelli99943382018-04-25 12:12:48 -07001976 const struct ethtool_ops *ops = dev->ethtool_ops;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001977 struct phy_device *phydev = dev->phydev;
Florian Fainelli99943382018-04-25 12:12:48 -07001978 struct ethtool_stats stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001979 u64 *data;
1980 int ret, n_stats;
1981
Florian Fainelli99943382018-04-25 12:12:48 -07001982 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
Andrew Lunnf3a40942015-12-30 16:28:25 +01001983 return -EOPNOTSUPP;
1984
Florian Fainelli99943382018-04-25 12:12:48 -07001985 if (dev->phydev && !ops->get_ethtool_phy_stats)
1986 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1987 else
1988 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001989 if (n_stats < 0)
1990 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001991 if (n_stats > S32_MAX / sizeof(u64))
1992 return -ENOMEM;
1993 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001994
1995 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1996 return -EFAULT;
1997
1998 stats.n_stats = n_stats;
Kees Cookfad953c2018-06-12 14:27:37 -07001999 data = vzalloc(array_size(n_stats, sizeof(u64)));
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002000 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01002001 return -ENOMEM;
2002
Florian Fainelli99943382018-04-25 12:12:48 -07002003 if (dev->phydev && !ops->get_ethtool_phy_stats) {
2004 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
2005 if (ret < 0)
2006 return ret;
2007 } else {
2008 ops->get_ethtool_phy_stats(dev, &stats, data);
2009 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01002010
2011 ret = -EFAULT;
2012 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2013 goto out;
2014 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002015 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002016 goto out;
2017 ret = 0;
2018
2019 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002020 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002021 return ret;
2022}
2023
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002024static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002025{
2026 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002027
Matthew Wilcox313674a2007-07-31 14:00:29 -07002028 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002029 return -EFAULT;
2030
Matthew Wilcox313674a2007-07-31 14:00:29 -07002031 if (epaddr.size < dev->addr_len)
2032 return -ETOOSMALL;
2033 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002034
Jon Wetzela6f9a702005-08-20 17:15:54 -07002035 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002036 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002037 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002038 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2039 return -EFAULT;
2040 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002041}
2042
Jeff Garzik13c99b22007-08-15 16:01:56 -07002043static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2044 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002045{
Roland Dreier8e557422010-02-11 12:14:23 -08002046 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002047
Jeff Garzik13c99b22007-08-15 16:01:56 -07002048 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002049 return -EOPNOTSUPP;
2050
Jeff Garzik13c99b22007-08-15 16:01:56 -07002051 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002052
2053 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2054 return -EFAULT;
2055 return 0;
2056}
2057
Jeff Garzik13c99b22007-08-15 16:01:56 -07002058static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2059 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002060{
2061 struct ethtool_value edata;
2062
Jeff Garzik13c99b22007-08-15 16:01:56 -07002063 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002064 return -EOPNOTSUPP;
2065
2066 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2067 return -EFAULT;
2068
Jeff Garzik13c99b22007-08-15 16:01:56 -07002069 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002070 return 0;
2071}
2072
Jeff Garzik13c99b22007-08-15 16:01:56 -07002073static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2074 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002075{
2076 struct ethtool_value edata;
2077
Jeff Garzik13c99b22007-08-15 16:01:56 -07002078 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002079 return -EOPNOTSUPP;
2080
2081 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2082 return -EFAULT;
2083
Jeff Garzik13c99b22007-08-15 16:01:56 -07002084 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002085}
2086
chavey97f8aef2010-04-07 21:54:42 -07002087static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2088 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002089{
2090 struct ethtool_flash efl;
2091
2092 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2093 return -EFAULT;
2094
2095 if (!dev->ethtool_ops->flash_device)
2096 return -EOPNOTSUPP;
2097
Ben Hutchings786f5282012-02-01 09:32:25 +00002098 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2099
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002100 return dev->ethtool_ops->flash_device(dev, &efl);
2101}
2102
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002103static int ethtool_set_dump(struct net_device *dev,
2104 void __user *useraddr)
2105{
2106 struct ethtool_dump dump;
2107
2108 if (!dev->ethtool_ops->set_dump)
2109 return -EOPNOTSUPP;
2110
2111 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2112 return -EFAULT;
2113
2114 return dev->ethtool_ops->set_dump(dev, &dump);
2115}
2116
2117static int ethtool_get_dump_flag(struct net_device *dev,
2118 void __user *useraddr)
2119{
2120 int ret;
2121 struct ethtool_dump dump;
2122 const struct ethtool_ops *ops = dev->ethtool_ops;
2123
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002124 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002125 return -EOPNOTSUPP;
2126
2127 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2128 return -EFAULT;
2129
2130 ret = ops->get_dump_flag(dev, &dump);
2131 if (ret)
2132 return ret;
2133
2134 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2135 return -EFAULT;
2136 return 0;
2137}
2138
2139static int ethtool_get_dump_data(struct net_device *dev,
2140 void __user *useraddr)
2141{
2142 int ret;
2143 __u32 len;
2144 struct ethtool_dump dump, tmp;
2145 const struct ethtool_ops *ops = dev->ethtool_ops;
2146 void *data = NULL;
2147
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002148 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002149 return -EOPNOTSUPP;
2150
2151 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2152 return -EFAULT;
2153
2154 memset(&tmp, 0, sizeof(tmp));
2155 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2156 ret = ops->get_dump_flag(dev, &tmp);
2157 if (ret)
2158 return ret;
2159
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002160 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002161 if (!len)
2162 return -EFAULT;
2163
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002164 /* Don't ever let the driver think there's more space available
2165 * than it requested with .get_dump_flag().
2166 */
2167 dump.len = len;
2168
2169 /* Always allocate enough space to hold the whole thing so that the
2170 * driver does not need to check the length and bother with partial
2171 * dumping.
2172 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002173 data = vzalloc(tmp.len);
2174 if (!data)
2175 return -ENOMEM;
2176 ret = ops->get_dump_data(dev, &dump, data);
2177 if (ret)
2178 goto out;
2179
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002180 /* There are two sane possibilities:
2181 * 1. The driver's .get_dump_data() does not touch dump.len.
2182 * 2. Or it may set dump.len to how much it really writes, which
2183 * should be tmp.len (or len if it can do a partial dump).
2184 * In any case respond to userspace with the actual length of data
2185 * it's receiving.
2186 */
2187 WARN_ON(dump.len != len && dump.len != tmp.len);
2188 dump.len = len;
2189
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002190 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2191 ret = -EFAULT;
2192 goto out;
2193 }
2194 useraddr += offsetof(struct ethtool_dump, data);
2195 if (copy_to_user(useraddr, data, len))
2196 ret = -EFAULT;
2197out:
2198 vfree(data);
2199 return ret;
2200}
2201
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002202static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2203{
2204 int err = 0;
2205 struct ethtool_ts_info info;
2206 const struct ethtool_ops *ops = dev->ethtool_ops;
2207 struct phy_device *phydev = dev->phydev;
2208
2209 memset(&info, 0, sizeof(info));
2210 info.cmd = ETHTOOL_GET_TS_INFO;
2211
2212 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002213 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002214 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002215 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002216 } else {
2217 info.so_timestamping =
2218 SOF_TIMESTAMPING_RX_SOFTWARE |
2219 SOF_TIMESTAMPING_SOFTWARE;
2220 info.phc_index = -1;
2221 }
2222
2223 if (err)
2224 return err;
2225
2226 if (copy_to_user(useraddr, &info, sizeof(info)))
2227 err = -EFAULT;
2228
2229 return err;
2230}
2231
Ed Swierk2f438362015-01-02 17:27:56 -08002232static int __ethtool_get_module_info(struct net_device *dev,
2233 struct ethtool_modinfo *modinfo)
2234{
2235 const struct ethtool_ops *ops = dev->ethtool_ops;
2236 struct phy_device *phydev = dev->phydev;
2237
Russell Kinge679c9c2018-03-28 15:44:16 -07002238 if (dev->sfp_bus)
2239 return sfp_get_module_info(dev->sfp_bus, modinfo);
2240
Ed Swierk2f438362015-01-02 17:27:56 -08002241 if (phydev && phydev->drv && phydev->drv->module_info)
2242 return phydev->drv->module_info(phydev, modinfo);
2243
2244 if (ops->get_module_info)
2245 return ops->get_module_info(dev, modinfo);
2246
2247 return -EOPNOTSUPP;
2248}
2249
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002250static int ethtool_get_module_info(struct net_device *dev,
2251 void __user *useraddr)
2252{
2253 int ret;
2254 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002255
2256 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2257 return -EFAULT;
2258
Ed Swierk2f438362015-01-02 17:27:56 -08002259 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002260 if (ret)
2261 return ret;
2262
2263 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2264 return -EFAULT;
2265
2266 return 0;
2267}
2268
Ed Swierk2f438362015-01-02 17:27:56 -08002269static int __ethtool_get_module_eeprom(struct net_device *dev,
2270 struct ethtool_eeprom *ee, u8 *data)
2271{
2272 const struct ethtool_ops *ops = dev->ethtool_ops;
2273 struct phy_device *phydev = dev->phydev;
2274
Russell Kinge679c9c2018-03-28 15:44:16 -07002275 if (dev->sfp_bus)
2276 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2277
Ed Swierk2f438362015-01-02 17:27:56 -08002278 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2279 return phydev->drv->module_eeprom(phydev, ee, data);
2280
2281 if (ops->get_module_eeprom)
2282 return ops->get_module_eeprom(dev, ee, data);
2283
2284 return -EOPNOTSUPP;
2285}
2286
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002287static int ethtool_get_module_eeprom(struct net_device *dev,
2288 void __user *useraddr)
2289{
2290 int ret;
2291 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002292
Ed Swierk2f438362015-01-02 17:27:56 -08002293 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002294 if (ret)
2295 return ret;
2296
Ed Swierk2f438362015-01-02 17:27:56 -08002297 return ethtool_get_any_eeprom(dev, useraddr,
2298 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002299 modinfo.eeprom_len);
2300}
2301
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302302static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2303{
2304 switch (tuna->id) {
2305 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002306 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302307 if (tuna->len != sizeof(u32) ||
2308 tuna->type_id != ETHTOOL_TUNABLE_U32)
2309 return -EINVAL;
2310 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002311 case ETHTOOL_PFC_PREVENTION_TOUT:
2312 if (tuna->len != sizeof(u16) ||
2313 tuna->type_id != ETHTOOL_TUNABLE_U16)
2314 return -EINVAL;
2315 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302316 default:
2317 return -EINVAL;
2318 }
2319
2320 return 0;
2321}
2322
2323static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2324{
2325 int ret;
2326 struct ethtool_tunable tuna;
2327 const struct ethtool_ops *ops = dev->ethtool_ops;
2328 void *data;
2329
2330 if (!ops->get_tunable)
2331 return -EOPNOTSUPP;
2332 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2333 return -EFAULT;
2334 ret = ethtool_tunable_valid(&tuna);
2335 if (ret)
2336 return ret;
2337 data = kmalloc(tuna.len, GFP_USER);
2338 if (!data)
2339 return -ENOMEM;
2340 ret = ops->get_tunable(dev, &tuna, data);
2341 if (ret)
2342 goto out;
2343 useraddr += sizeof(tuna);
2344 ret = -EFAULT;
2345 if (copy_to_user(useraddr, data, tuna.len))
2346 goto out;
2347 ret = 0;
2348
2349out:
2350 kfree(data);
2351 return ret;
2352}
2353
2354static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2355{
2356 int ret;
2357 struct ethtool_tunable tuna;
2358 const struct ethtool_ops *ops = dev->ethtool_ops;
2359 void *data;
2360
2361 if (!ops->set_tunable)
2362 return -EOPNOTSUPP;
2363 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2364 return -EFAULT;
2365 ret = ethtool_tunable_valid(&tuna);
2366 if (ret)
2367 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302368 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002369 data = memdup_user(useraddr, tuna.len);
2370 if (IS_ERR(data))
2371 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302372 ret = ops->set_tunable(dev, &tuna, data);
2373
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302374 kfree(data);
2375 return ret;
2376}
2377
Kan Liang421797b2016-02-19 09:24:02 -05002378static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2379 void __user *useraddr,
2380 struct ethtool_per_queue_op *per_queue_opt)
2381{
2382 u32 bit;
2383 int ret;
2384 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2385
2386 if (!dev->ethtool_ops->get_per_queue_coalesce)
2387 return -EOPNOTSUPP;
2388
2389 useraddr += sizeof(*per_queue_opt);
2390
Yury Norov3aa56882018-02-06 15:38:06 -08002391 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2392 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002393
2394 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2395 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2396
2397 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2398 if (ret != 0)
2399 return ret;
2400 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2401 return -EFAULT;
2402 useraddr += sizeof(coalesce);
2403 }
2404
2405 return 0;
2406}
2407
Kan Liangf38d1382016-02-19 09:24:03 -05002408static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2409 void __user *useraddr,
2410 struct ethtool_per_queue_op *per_queue_opt)
2411{
2412 u32 bit;
2413 int i, ret = 0;
2414 int n_queue;
2415 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2416 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2417
2418 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2419 (!dev->ethtool_ops->get_per_queue_coalesce))
2420 return -EOPNOTSUPP;
2421
2422 useraddr += sizeof(*per_queue_opt);
2423
Yury Norov3aa56882018-02-06 15:38:06 -08002424 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002425 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2426 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2427 if (!backup)
2428 return -ENOMEM;
2429
2430 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2431 struct ethtool_coalesce coalesce;
2432
2433 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2434 if (ret != 0)
2435 goto roll_back;
2436
2437 tmp++;
2438
2439 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2440 ret = -EFAULT;
2441 goto roll_back;
2442 }
2443
2444 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2445 if (ret != 0)
2446 goto roll_back;
2447
2448 useraddr += sizeof(coalesce);
2449 }
2450
2451roll_back:
2452 if (ret != 0) {
2453 tmp = backup;
2454 for_each_set_bit(i, queue_mask, bit) {
2455 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2456 tmp++;
2457 }
2458 }
2459 kfree(backup);
2460
2461 return ret;
2462}
2463
Kan Liangac2c7ad2016-02-19 09:24:01 -05002464static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2465{
2466 struct ethtool_per_queue_op per_queue_opt;
2467
2468 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2469 return -EFAULT;
2470
2471 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002472 case ETHTOOL_GCOALESCE:
2473 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002474 case ETHTOOL_SCOALESCE:
2475 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002476 default:
2477 return -EOPNOTSUPP;
2478 };
2479}
2480
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002481static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2482{
2483 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002484 case ETHTOOL_PHY_DOWNSHIFT:
2485 if (tuna->len != sizeof(u8) ||
2486 tuna->type_id != ETHTOOL_TUNABLE_U8)
2487 return -EINVAL;
2488 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002489 default:
2490 return -EINVAL;
2491 }
2492
2493 return 0;
2494}
2495
2496static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2497{
2498 int ret;
2499 struct ethtool_tunable tuna;
2500 struct phy_device *phydev = dev->phydev;
2501 void *data;
2502
2503 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2504 return -EOPNOTSUPP;
2505
2506 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2507 return -EFAULT;
2508 ret = ethtool_phy_tunable_valid(&tuna);
2509 if (ret)
2510 return ret;
2511 data = kmalloc(tuna.len, GFP_USER);
2512 if (!data)
2513 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002514 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002515 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002516 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002517 if (ret)
2518 goto out;
2519 useraddr += sizeof(tuna);
2520 ret = -EFAULT;
2521 if (copy_to_user(useraddr, data, tuna.len))
2522 goto out;
2523 ret = 0;
2524
2525out:
2526 kfree(data);
2527 return ret;
2528}
2529
2530static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2531{
2532 int ret;
2533 struct ethtool_tunable tuna;
2534 struct phy_device *phydev = dev->phydev;
2535 void *data;
2536
2537 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2538 return -EOPNOTSUPP;
2539 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2540 return -EFAULT;
2541 ret = ethtool_phy_tunable_valid(&tuna);
2542 if (ret)
2543 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002544 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002545 data = memdup_user(useraddr, tuna.len);
2546 if (IS_ERR(data))
2547 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002548 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002549 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002550 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002551
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002552 kfree(data);
2553 return ret;
2554}
2555
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002556static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2557{
2558 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002559 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002560
2561 if (!dev->ethtool_ops->get_fecparam)
2562 return -EOPNOTSUPP;
2563
Edward Creea6d50512018-02-28 19:15:58 +00002564 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2565 if (rc)
2566 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002567
2568 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2569 return -EFAULT;
2570 return 0;
2571}
2572
2573static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2574{
2575 struct ethtool_fecparam fecparam;
2576
2577 if (!dev->ethtool_ops->set_fecparam)
2578 return -EOPNOTSUPP;
2579
2580 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2581 return -EFAULT;
2582
2583 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2584}
2585
Yan Burman600fed52013-06-03 02:03:34 +00002586/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Eric W. Biederman881d9662007-09-17 11:56:21 -07002588int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002590 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002592 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002594 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (!dev || !netif_device_present(dev))
2597 return -ENODEV;
2598
chavey97f8aef2010-04-07 21:54:42 -07002599 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return -EFAULT;
2601
Kan Liangac2c7ad2016-02-19 09:24:01 -05002602 if (ethcmd == ETHTOOL_PERQUEUE) {
2603 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2604 return -EFAULT;
2605 } else {
2606 sub_cmd = ethcmd;
2607 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002608 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002609 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002610 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002611 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002612 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002613 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002614 case ETHTOOL_GCOALESCE:
2615 case ETHTOOL_GRINGPARAM:
2616 case ETHTOOL_GPAUSEPARAM:
2617 case ETHTOOL_GRXCSUM:
2618 case ETHTOOL_GTXCSUM:
2619 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002620 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002621 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002622 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002623 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002624 case ETHTOOL_GTSO:
2625 case ETHTOOL_GPERMADDR:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002626 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002627 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002628 case ETHTOOL_GFLAGS:
2629 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002630 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002631 case ETHTOOL_GRXRINGS:
2632 case ETHTOOL_GRXCLSRLCNT:
2633 case ETHTOOL_GRXCLSRULE:
2634 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002635 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302636 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002637 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002638 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002639 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002640 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302641 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002642 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002643 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002644 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002645 break;
2646 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002647 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002648 return -EPERM;
2649 }
2650
chavey97f8aef2010-04-07 21:54:42 -07002651 if (dev->ethtool_ops->begin) {
2652 rc = dev->ethtool_ops->begin(dev);
2653 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002655 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002656 old_features = dev->features;
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 switch (ethcmd) {
2659 case ETHTOOL_GSET:
2660 rc = ethtool_get_settings(dev, useraddr);
2661 break;
2662 case ETHTOOL_SSET:
2663 rc = ethtool_set_settings(dev, useraddr);
2664 break;
2665 case ETHTOOL_GDRVINFO:
2666 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 break;
2668 case ETHTOOL_GREGS:
2669 rc = ethtool_get_regs(dev, useraddr);
2670 break;
2671 case ETHTOOL_GWOL:
2672 rc = ethtool_get_wol(dev, useraddr);
2673 break;
2674 case ETHTOOL_SWOL:
2675 rc = ethtool_set_wol(dev, useraddr);
2676 break;
2677 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002678 rc = ethtool_get_value(dev, useraddr, ethcmd,
2679 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 break;
2681 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002682 rc = ethtool_set_value_void(dev, useraddr,
2683 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002685 case ETHTOOL_GEEE:
2686 rc = ethtool_get_eee(dev, useraddr);
2687 break;
2688 case ETHTOOL_SEEE:
2689 rc = ethtool_set_eee(dev, useraddr);
2690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 case ETHTOOL_NWAY_RST:
2692 rc = ethtool_nway_reset(dev);
2693 break;
2694 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002695 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 break;
2697 case ETHTOOL_GEEPROM:
2698 rc = ethtool_get_eeprom(dev, useraddr);
2699 break;
2700 case ETHTOOL_SEEPROM:
2701 rc = ethtool_set_eeprom(dev, useraddr);
2702 break;
2703 case ETHTOOL_GCOALESCE:
2704 rc = ethtool_get_coalesce(dev, useraddr);
2705 break;
2706 case ETHTOOL_SCOALESCE:
2707 rc = ethtool_set_coalesce(dev, useraddr);
2708 break;
2709 case ETHTOOL_GRINGPARAM:
2710 rc = ethtool_get_ringparam(dev, useraddr);
2711 break;
2712 case ETHTOOL_SRINGPARAM:
2713 rc = ethtool_set_ringparam(dev, useraddr);
2714 break;
2715 case ETHTOOL_GPAUSEPARAM:
2716 rc = ethtool_get_pauseparam(dev, useraddr);
2717 break;
2718 case ETHTOOL_SPAUSEPARAM:
2719 rc = ethtool_set_pauseparam(dev, useraddr);
2720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 case ETHTOOL_TEST:
2722 rc = ethtool_self_test(dev, useraddr);
2723 break;
2724 case ETHTOOL_GSTRINGS:
2725 rc = ethtool_get_strings(dev, useraddr);
2726 break;
2727 case ETHTOOL_PHYS_ID:
2728 rc = ethtool_phys_id(dev, useraddr);
2729 break;
2730 case ETHTOOL_GSTATS:
2731 rc = ethtool_get_stats(dev, useraddr);
2732 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002733 case ETHTOOL_GPERMADDR:
2734 rc = ethtool_get_perm_addr(dev, useraddr);
2735 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002736 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002737 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002738 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002739 break;
2740 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002741 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002742 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002743 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002744 rc = ethtool_get_value(dev, useraddr, ethcmd,
2745 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002746 break;
2747 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002748 rc = ethtool_set_value(dev, useraddr,
2749 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002750 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002751 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002752 case ETHTOOL_GRXRINGS:
2753 case ETHTOOL_GRXCLSRLCNT:
2754 case ETHTOOL_GRXCLSRULE:
2755 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002756 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002757 break;
2758 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002759 case ETHTOOL_SRXCLSRLDEL:
2760 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002761 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002762 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002763 case ETHTOOL_FLASHDEV:
2764 rc = ethtool_flash_device(dev, useraddr);
2765 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002766 case ETHTOOL_RESET:
2767 rc = ethtool_reset(dev, useraddr);
2768 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002769 case ETHTOOL_GSSET_INFO:
2770 rc = ethtool_get_sset_info(dev, useraddr);
2771 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002772 case ETHTOOL_GRXFHINDIR:
2773 rc = ethtool_get_rxfh_indir(dev, useraddr);
2774 break;
2775 case ETHTOOL_SRXFHINDIR:
2776 rc = ethtool_set_rxfh_indir(dev, useraddr);
2777 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302778 case ETHTOOL_GRSSH:
2779 rc = ethtool_get_rxfh(dev, useraddr);
2780 break;
2781 case ETHTOOL_SRSSH:
2782 rc = ethtool_set_rxfh(dev, useraddr);
2783 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002784 case ETHTOOL_GFEATURES:
2785 rc = ethtool_get_features(dev, useraddr);
2786 break;
2787 case ETHTOOL_SFEATURES:
2788 rc = ethtool_set_features(dev, useraddr);
2789 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002790 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002791 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002792 case ETHTOOL_GSG:
2793 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002794 case ETHTOOL_GGSO:
2795 case ETHTOOL_GGRO:
2796 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2797 break;
2798 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002799 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002800 case ETHTOOL_SSG:
2801 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002802 case ETHTOOL_SGSO:
2803 case ETHTOOL_SGRO:
2804 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2805 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002806 case ETHTOOL_GCHANNELS:
2807 rc = ethtool_get_channels(dev, useraddr);
2808 break;
2809 case ETHTOOL_SCHANNELS:
2810 rc = ethtool_set_channels(dev, useraddr);
2811 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002812 case ETHTOOL_SET_DUMP:
2813 rc = ethtool_set_dump(dev, useraddr);
2814 break;
2815 case ETHTOOL_GET_DUMP_FLAG:
2816 rc = ethtool_get_dump_flag(dev, useraddr);
2817 break;
2818 case ETHTOOL_GET_DUMP_DATA:
2819 rc = ethtool_get_dump_data(dev, useraddr);
2820 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002821 case ETHTOOL_GET_TS_INFO:
2822 rc = ethtool_get_ts_info(dev, useraddr);
2823 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002824 case ETHTOOL_GMODULEINFO:
2825 rc = ethtool_get_module_info(dev, useraddr);
2826 break;
2827 case ETHTOOL_GMODULEEEPROM:
2828 rc = ethtool_get_module_eeprom(dev, useraddr);
2829 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302830 case ETHTOOL_GTUNABLE:
2831 rc = ethtool_get_tunable(dev, useraddr);
2832 break;
2833 case ETHTOOL_STUNABLE:
2834 rc = ethtool_set_tunable(dev, useraddr);
2835 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002836 case ETHTOOL_GPHYSTATS:
2837 rc = ethtool_get_phy_stats(dev, useraddr);
2838 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002839 case ETHTOOL_PERQUEUE:
2840 rc = ethtool_set_per_queue(dev, useraddr);
2841 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002842 case ETHTOOL_GLINKSETTINGS:
2843 rc = ethtool_get_link_ksettings(dev, useraddr);
2844 break;
2845 case ETHTOOL_SLINKSETTINGS:
2846 rc = ethtool_set_link_ksettings(dev, useraddr);
2847 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002848 case ETHTOOL_PHY_GTUNABLE:
2849 rc = get_phy_tunable(dev, useraddr);
2850 break;
2851 case ETHTOOL_PHY_STUNABLE:
2852 rc = set_phy_tunable(dev, useraddr);
2853 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002854 case ETHTOOL_GFECPARAM:
2855 rc = ethtool_get_fecparam(dev, useraddr);
2856 break;
2857 case ETHTOOL_SFECPARAM:
2858 rc = ethtool_set_fecparam(dev, useraddr);
2859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002861 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002863
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002864 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002866
2867 if (old_features != dev->features)
2868 netdev_features_change(dev);
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}