blob: efcd8e6207964baa58a63963ce5caa7c78300b2d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4 *
5 * This file is where we call all the ethtool_ops commands to get
Matthew Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000020#include <linux/net_tstamp.h>
21#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000022#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070023#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070024#include <linux/vmalloc.h>
Russell Kinge679c9c2018-03-28 15:44:16 -070025#include <linux/sfp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010027#include <linux/rtnetlink.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010028#include <linux/sched/signal.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080029#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090031/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Some useful ethtool_ops methods that're device independent.
33 * If we find that all drivers want to do the same thing here,
34 * we can turn these into dev_() function calls.
35 */
36
37u32 ethtool_op_get_link(struct net_device *dev)
38{
39 return netif_carrier_ok(dev) ? 1 : 0;
40}
chavey97f8aef2010-04-07 21:54:42 -070041EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Richard Cochran02eacbd2012-04-03 22:59:22 +000043int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
44{
45 info->so_timestamping =
46 SOF_TIMESTAMPING_TX_SOFTWARE |
47 SOF_TIMESTAMPING_RX_SOFTWARE |
48 SOF_TIMESTAMPING_SOFTWARE;
49 info->phc_index = -1;
50 return 0;
51}
52EXPORT_SYMBOL(ethtool_op_get_ts_info);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Handlers for each ethtool command */
55
Michał Mirosław475414f2011-11-15 15:29:55 +000056#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000057
Michał Mirosław9d921542011-11-15 15:29:55 +000058static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
59 [NETIF_F_SG_BIT] = "tx-scatter-gather",
60 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000061 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
62 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
63 [NETIF_F_HIGHDMA_BIT] = "highdma",
64 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090065 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000066
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090067 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
68 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040069 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
70 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
71 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000072 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
73 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
74 [NETIF_F_LLTX_BIT] = "tx-lockless",
75 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
76 [NETIF_F_GRO_BIT] = "rx-gro",
Michael Chanfb1f5f72017-12-16 03:09:40 -050077 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
Michał Mirosław9d921542011-11-15 15:29:55 +000078 [NETIF_F_LRO_BIT] = "rx-lro",
79
80 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000081 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
82 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
Alexander Duyckcbc53e02016-04-10 21:44:51 -040083 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000084 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
85 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000086 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040087 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
Tom Herbert7e133182016-05-18 09:06:10 -070088 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
89 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000090 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040091 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
Alexander Duyck802ab552016-04-10 21:45:03 -040092 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030093 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +020094 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
Willem de Bruijn83aa0252018-04-26 13:42:21 -040095 [NETIF_F_GSO_UDP_L4_BIT] = "tx-udp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000096
97 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080098 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000099 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
100 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
101 [NETIF_F_RXHASH_BIT] = "rx-hashing",
102 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
103 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
104 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +0000105 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +0000106 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -0800107 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
John Fastabend1c78c642016-02-16 21:17:37 -0800108 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +0200109 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
110 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
Sabrina Dubrocad764a122017-07-21 12:49:28 +0200111 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
Atul Guptae0be6be2018-03-31 21:41:53 +0530112 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
Michał Mirosław9d921542011-11-15 15:29:55 +0000113};
114
Eyal Perry892311f2014-12-02 18:12:10 +0200115static const char
116rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
117 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
118 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800119 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200120};
121
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300122static const char
123tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
124 [ETHTOOL_ID_UNSPEC] = "Unspec",
125 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
126 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
Inbar Karmye1577c12017-11-20 16:14:30 +0200127 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300128};
129
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100130static const char
131phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
132 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100133 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100134};
135
Michał Mirosław5455c692011-02-15 16:59:17 +0000136static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
137{
138 struct ethtool_gfeatures cmd = {
139 .cmd = ETHTOOL_GFEATURES,
140 .size = ETHTOOL_DEV_FEATURE_WORDS,
141 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000142 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000143 u32 __user *sizeaddr;
144 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000145 int i;
146
147 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000148 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000149
150 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000151 features[i].available = (u32)(dev->hw_features >> (32 * i));
152 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
153 features[i].active = (u32)(dev->features >> (32 * i));
154 features[i].never_changed =
155 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000156 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000157
158 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
159 if (get_user(copy_size, sizeaddr))
160 return -EFAULT;
161
162 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
163 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
164
165 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
166 return -EFAULT;
167 useraddr += sizeof(cmd);
168 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
169 return -EFAULT;
170
171 return 0;
172}
173
174static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
175{
176 struct ethtool_sfeatures cmd;
177 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000178 netdev_features_t wanted = 0, valid = 0;
179 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000180
181 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
182 return -EFAULT;
183 useraddr += sizeof(cmd);
184
185 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
186 return -EINVAL;
187
188 if (copy_from_user(features, useraddr, sizeof(features)))
189 return -EFAULT;
190
Michał Mirosław475414f2011-11-15 15:29:55 +0000191 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000192 valid |= (netdev_features_t)features[i].valid << (32 * i);
193 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000194 }
195
196 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000197 return -EINVAL;
198
Michał Mirosław475414f2011-11-15 15:29:55 +0000199 if (valid & ~dev->hw_features) {
200 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000201 ret |= ETHTOOL_F_UNSUPPORTED;
202 }
203
Michał Mirosław475414f2011-11-15 15:29:55 +0000204 dev->wanted_features &= ~valid;
205 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700206 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000207
Michał Mirosław475414f2011-11-15 15:29:55 +0000208 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000209 ret |= ETHTOOL_F_WISH;
210
211 return ret;
212}
213
Michał Mirosław340ae162011-02-15 16:59:16 +0000214static int __ethtool_get_sset_count(struct net_device *dev, int sset)
215{
216 const struct ethtool_ops *ops = dev->ethtool_ops;
217
Michał Mirosław5455c692011-02-15 16:59:17 +0000218 if (sset == ETH_SS_FEATURES)
219 return ARRAY_SIZE(netdev_features_strings);
220
Eyal Perry892311f2014-12-02 18:12:10 +0200221 if (sset == ETH_SS_RSS_HASH_FUNCS)
222 return ARRAY_SIZE(rss_hash_func_strings);
223
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300224 if (sset == ETH_SS_TUNABLES)
225 return ARRAY_SIZE(tunable_strings);
226
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100227 if (sset == ETH_SS_PHY_TUNABLES)
228 return ARRAY_SIZE(phy_tunable_strings);
229
Andrew Lunnf3a40942015-12-30 16:28:25 +0100230 if (sset == ETH_SS_PHY_STATS) {
231 if (dev->phydev)
Florian Fainellic59530d2018-04-25 12:12:47 -0700232 return phy_ethtool_get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100233 else
234 return -EOPNOTSUPP;
235 }
236
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000237 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000238 return ops->get_sset_count(dev, sset);
239 else
240 return -EOPNOTSUPP;
241}
242
243static void __ethtool_get_strings(struct net_device *dev,
244 u32 stringset, u8 *data)
245{
246 const struct ethtool_ops *ops = dev->ethtool_ops;
247
Michał Mirosław5455c692011-02-15 16:59:17 +0000248 if (stringset == ETH_SS_FEATURES)
249 memcpy(data, netdev_features_strings,
250 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200251 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
252 memcpy(data, rss_hash_func_strings,
253 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300254 else if (stringset == ETH_SS_TUNABLES)
255 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100256 else if (stringset == ETH_SS_PHY_TUNABLES)
257 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100258 else if (stringset == ETH_SS_PHY_STATS) {
Florian Fainellic59530d2018-04-25 12:12:47 -0700259 if (dev->phydev)
260 phy_ethtool_get_strings(dev->phydev, data);
261 else
Andrew Lunnf3a40942015-12-30 16:28:25 +0100262 return;
Andrew Lunnf3a40942015-12-30 16:28:25 +0100263 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000264 /* ops->get_strings is valid because checked earlier */
265 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000266}
267
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000268static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000269{
270 /* feature masks of legacy discrete ethtool ops */
271
272 switch (eth_cmd) {
273 case ETHTOOL_GTXCSUM:
274 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800275 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000276 case ETHTOOL_GRXCSUM:
277 case ETHTOOL_SRXCSUM:
278 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000279 case ETHTOOL_GSG:
280 case ETHTOOL_SSG:
281 return NETIF_F_SG;
282 case ETHTOOL_GTSO:
283 case ETHTOOL_STSO:
284 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000285 case ETHTOOL_GGSO:
286 case ETHTOOL_SGSO:
287 return NETIF_F_GSO;
288 case ETHTOOL_GGRO:
289 case ETHTOOL_SGRO:
290 return NETIF_F_GRO;
291 default:
292 BUG();
293 }
294}
295
Michał Mirosław0a417702011-02-15 16:59:17 +0000296static int ethtool_get_one_feature(struct net_device *dev,
297 char __user *useraddr, u32 ethcmd)
298{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000299 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000300 struct ethtool_value edata = {
301 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000302 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000303 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000304
Michał Mirosław0a417702011-02-15 16:59:17 +0000305 if (copy_to_user(useraddr, &edata, sizeof(edata)))
306 return -EFAULT;
307 return 0;
308}
309
Michał Mirosław0a417702011-02-15 16:59:17 +0000310static int ethtool_set_one_feature(struct net_device *dev,
311 void __user *useraddr, u32 ethcmd)
312{
313 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000314 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000315
316 if (copy_from_user(&edata, useraddr, sizeof(edata)))
317 return -EFAULT;
318
Michał Mirosław86794882011-02-15 16:59:17 +0000319 mask = ethtool_get_feature_mask(ethcmd);
320 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000321 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000322 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000323
324 if (edata.data)
325 dev->wanted_features |= mask;
326 else
327 dev->wanted_features &= ~mask;
328
329 __netdev_update_features(dev);
330
331 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000332}
333
Michał Mirosław02b3a552011-11-15 15:29:55 +0000334#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
335 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000336#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
337 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
338 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000339
340static u32 __ethtool_get_flags(struct net_device *dev)
341{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000342 u32 flags = 0;
343
Dragos Foianu8a731252013-07-13 14:43:00 +0100344 if (dev->features & NETIF_F_LRO)
345 flags |= ETH_FLAG_LRO;
346 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
347 flags |= ETH_FLAG_RXVLAN;
348 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
349 flags |= ETH_FLAG_TXVLAN;
350 if (dev->features & NETIF_F_NTUPLE)
351 flags |= ETH_FLAG_NTUPLE;
352 if (dev->features & NETIF_F_RXHASH)
353 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000354
355 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000356}
357
358static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000359{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000360 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000361
Michał Mirosław02b3a552011-11-15 15:29:55 +0000362 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000363 return -EINVAL;
364
Dragos Foianu8a731252013-07-13 14:43:00 +0100365 if (data & ETH_FLAG_LRO)
366 features |= NETIF_F_LRO;
367 if (data & ETH_FLAG_RXVLAN)
368 features |= NETIF_F_HW_VLAN_CTAG_RX;
369 if (data & ETH_FLAG_TXVLAN)
370 features |= NETIF_F_HW_VLAN_CTAG_TX;
371 if (data & ETH_FLAG_NTUPLE)
372 features |= NETIF_F_NTUPLE;
373 if (data & ETH_FLAG_RXHASH)
374 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000375
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000376 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000377 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000378 if (changed & ~dev->hw_features)
379 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
380
381 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000382 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000383
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700384 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000385
386 return 0;
387}
388
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700389/* Given two link masks, AND them together and save the result in dst. */
390void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
391 struct ethtool_link_ksettings *src)
392{
393 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
394 unsigned int idx = 0;
395
396 for (; idx < size; idx++) {
397 dst->link_modes.supported[idx] &=
398 src->link_modes.supported[idx];
399 dst->link_modes.advertising[idx] &=
400 src->link_modes.advertising[idx];
401 }
402}
403EXPORT_SYMBOL(ethtool_intersect_link_masks);
404
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200405void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
406 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800407{
408 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
409 dst[0] = legacy_u32;
410}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200411EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800412
413/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200414bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
415 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800416{
417 bool retval = true;
418
419 /* TODO: following test will soon always be true */
420 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
421 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
422
423 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
424 bitmap_fill(ext, 32);
425 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
426 if (bitmap_intersects(ext, src,
427 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
428 /* src mask goes beyond bit 31 */
429 retval = false;
430 }
431 }
432 *legacy_u32 = src[0];
433 return retval;
434}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200435EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800436
437/* return false if legacy contained non-0 deprecated fields
Niklas Söderlund95491e32017-10-20 01:32:08 +0200438 * maxtxpkt/maxrxpkt. rest of ksettings always updated
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800439 */
440static bool
441convert_legacy_settings_to_link_ksettings(
442 struct ethtool_link_ksettings *link_ksettings,
443 const struct ethtool_cmd *legacy_settings)
444{
445 bool retval = true;
446
447 memset(link_ksettings, 0, sizeof(*link_ksettings));
448
449 /* This is used to tell users that driver is still using these
450 * deprecated legacy fields, and they should not use
451 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
452 */
Niklas Söderlund95491e32017-10-20 01:32:08 +0200453 if (legacy_settings->maxtxpkt ||
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800454 legacy_settings->maxrxpkt)
455 retval = false;
456
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200457 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800458 link_ksettings->link_modes.supported,
459 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200460 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800461 link_ksettings->link_modes.advertising,
462 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200463 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800464 link_ksettings->link_modes.lp_advertising,
465 legacy_settings->lp_advertising);
466 link_ksettings->base.speed
467 = ethtool_cmd_speed(legacy_settings);
468 link_ksettings->base.duplex
469 = legacy_settings->duplex;
470 link_ksettings->base.port
471 = legacy_settings->port;
472 link_ksettings->base.phy_address
473 = legacy_settings->phy_address;
474 link_ksettings->base.autoneg
475 = legacy_settings->autoneg;
476 link_ksettings->base.mdio_support
477 = legacy_settings->mdio_support;
478 link_ksettings->base.eth_tp_mdix
479 = legacy_settings->eth_tp_mdix;
480 link_ksettings->base.eth_tp_mdix_ctrl
481 = legacy_settings->eth_tp_mdix_ctrl;
482 return retval;
483}
484
485/* return false if ksettings link modes had higher bits
486 * set. legacy_settings always updated (best effort)
487 */
488static bool
489convert_link_ksettings_to_legacy_settings(
490 struct ethtool_cmd *legacy_settings,
491 const struct ethtool_link_ksettings *link_ksettings)
492{
493 bool retval = true;
494
495 memset(legacy_settings, 0, sizeof(*legacy_settings));
496 /* this also clears the deprecated fields in legacy structure:
497 * __u8 transceiver;
498 * __u32 maxtxpkt;
499 * __u32 maxrxpkt;
500 */
501
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200502 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800503 &legacy_settings->supported,
504 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200505 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800506 &legacy_settings->advertising,
507 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200508 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800509 &legacy_settings->lp_advertising,
510 link_ksettings->link_modes.lp_advertising);
511 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
512 legacy_settings->duplex
513 = link_ksettings->base.duplex;
514 legacy_settings->port
515 = link_ksettings->base.port;
516 legacy_settings->phy_address
517 = link_ksettings->base.phy_address;
518 legacy_settings->autoneg
519 = link_ksettings->base.autoneg;
520 legacy_settings->mdio_support
521 = link_ksettings->base.mdio_support;
522 legacy_settings->eth_tp_mdix
523 = link_ksettings->base.eth_tp_mdix;
524 legacy_settings->eth_tp_mdix_ctrl
525 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700526 legacy_settings->transceiver
527 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800528 return retval;
529}
530
531/* number of 32-bit words to store the user's link mode bitmaps */
532#define __ETHTOOL_LINK_MODE_MASK_NU32 \
533 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
534
535/* layout of the struct passed from/to userland */
536struct ethtool_link_usettings {
537 struct ethtool_link_settings base;
538 struct {
539 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
540 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
541 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
542 } link_modes;
543};
544
545/* Internal kernel helper to query a device ethtool_link_settings.
546 *
547 * Backward compatibility note: for compatibility with legacy drivers
548 * that implement only the ethtool_cmd API, this has to work with both
549 * drivers implementing get_link_ksettings API and drivers
550 * implementing get_settings API. When drivers implement get_settings
551 * and report ethtool_cmd deprecated fields
552 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
553 * because the resulting struct ethtool_link_settings does not report them.
554 */
555int __ethtool_get_link_ksettings(struct net_device *dev,
556 struct ethtool_link_ksettings *link_ksettings)
557{
558 int err;
559 struct ethtool_cmd cmd;
560
561 ASSERT_RTNL();
562
563 if (dev->ethtool_ops->get_link_ksettings) {
564 memset(link_ksettings, 0, sizeof(*link_ksettings));
565 return dev->ethtool_ops->get_link_ksettings(dev,
566 link_ksettings);
567 }
568
569 /* driver doesn't support %ethtool_link_ksettings API. revert to
570 * legacy %ethtool_cmd API, unless it's not supported either.
571 * TODO: remove when ethtool_ops::get_settings disappears internally
572 */
David Decotigny3237fc62016-02-24 10:58:11 -0800573 if (!dev->ethtool_ops->get_settings)
574 return -EOPNOTSUPP;
575
576 memset(&cmd, 0, sizeof(cmd));
577 cmd.cmd = ETHTOOL_GSET;
578 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800579 if (err < 0)
580 return err;
581
582 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
583 */
584 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
585 return err;
586}
587EXPORT_SYMBOL(__ethtool_get_link_ksettings);
588
589/* convert ethtool_link_usettings in user space to a kernel internal
590 * ethtool_link_ksettings. return 0 on success, errno on error.
591 */
592static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
593 const void __user *from)
594{
595 struct ethtool_link_usettings link_usettings;
596
597 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
598 return -EFAULT;
599
600 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800601 bitmap_from_arr32(to->link_modes.supported,
602 link_usettings.link_modes.supported,
603 __ETHTOOL_LINK_MODE_MASK_NBITS);
604 bitmap_from_arr32(to->link_modes.advertising,
605 link_usettings.link_modes.advertising,
606 __ETHTOOL_LINK_MODE_MASK_NBITS);
607 bitmap_from_arr32(to->link_modes.lp_advertising,
608 link_usettings.link_modes.lp_advertising,
609 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800610
611 return 0;
612}
613
614/* convert a kernel internal ethtool_link_ksettings to
615 * ethtool_link_usettings in user space. return 0 on success, errno on
616 * error.
617 */
618static int
619store_link_ksettings_for_user(void __user *to,
620 const struct ethtool_link_ksettings *from)
621{
622 struct ethtool_link_usettings link_usettings;
623
624 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800625 bitmap_to_arr32(link_usettings.link_modes.supported,
626 from->link_modes.supported,
627 __ETHTOOL_LINK_MODE_MASK_NBITS);
628 bitmap_to_arr32(link_usettings.link_modes.advertising,
629 from->link_modes.advertising,
630 __ETHTOOL_LINK_MODE_MASK_NBITS);
631 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
632 from->link_modes.lp_advertising,
633 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800634
635 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
636 return -EFAULT;
637
638 return 0;
639}
640
641/* Query device for its ethtool_link_settings.
642 *
643 * Backward compatibility note: this function must fail when driver
644 * does not implement ethtool::get_link_ksettings, even if legacy
645 * ethtool_ops::get_settings is implemented. This tells new versions
646 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
647 * this driver, so that they can correctly access the ethtool_cmd
648 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
649 * implements ethtool_ops::get_settings anymore.
650 */
651static int ethtool_get_link_ksettings(struct net_device *dev,
652 void __user *useraddr)
653{
654 int err = 0;
655 struct ethtool_link_ksettings link_ksettings;
656
657 ASSERT_RTNL();
658
659 if (!dev->ethtool_ops->get_link_ksettings)
660 return -EOPNOTSUPP;
661
662 /* handle bitmap nbits handshake */
663 if (copy_from_user(&link_ksettings.base, useraddr,
664 sizeof(link_ksettings.base)))
665 return -EFAULT;
666
667 if (__ETHTOOL_LINK_MODE_MASK_NU32
668 != link_ksettings.base.link_mode_masks_nwords) {
669 /* wrong link mode nbits requested */
670 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000671 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800672 /* send back number of words required as negative val */
673 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
674 "need too many bits for link modes!");
675 link_ksettings.base.link_mode_masks_nwords
676 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
677
678 /* copy the base fields back to user, not the link
679 * mode bitmaps
680 */
681 if (copy_to_user(useraddr, &link_ksettings.base,
682 sizeof(link_ksettings.base)))
683 return -EFAULT;
684
685 return 0;
686 }
687
688 /* handshake successful: user/kernel agree on
689 * link_mode_masks_nwords
690 */
691
692 memset(&link_ksettings, 0, sizeof(link_ksettings));
693 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
694 if (err < 0)
695 return err;
696
697 /* make sure we tell the right values to user */
698 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
699 link_ksettings.base.link_mode_masks_nwords
700 = __ETHTOOL_LINK_MODE_MASK_NU32;
701
702 return store_link_ksettings_for_user(useraddr, &link_ksettings);
703}
704
705/* Update device ethtool_link_settings.
706 *
707 * Backward compatibility note: this function must fail when driver
708 * does not implement ethtool::set_link_ksettings, even if legacy
709 * ethtool_ops::set_settings is implemented. This tells new versions
710 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
711 * this driver, so that they can correctly update the ethtool_cmd
712 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
713 * implements ethtool_ops::get_settings anymore.
714 */
715static int ethtool_set_link_ksettings(struct net_device *dev,
716 void __user *useraddr)
717{
718 int err;
719 struct ethtool_link_ksettings link_ksettings;
720
721 ASSERT_RTNL();
722
723 if (!dev->ethtool_ops->set_link_ksettings)
724 return -EOPNOTSUPP;
725
726 /* make sure nbits field has expected value */
727 if (copy_from_user(&link_ksettings.base, useraddr,
728 sizeof(link_ksettings.base)))
729 return -EFAULT;
730
731 if (__ETHTOOL_LINK_MODE_MASK_NU32
732 != link_ksettings.base.link_mode_masks_nwords)
733 return -EINVAL;
734
735 /* copy the whole structure, now that we know it has expected
736 * format
737 */
738 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
739 if (err)
740 return err;
741
742 /* re-check nwords field, just in case */
743 if (__ETHTOOL_LINK_MODE_MASK_NU32
744 != link_ksettings.base.link_mode_masks_nwords)
745 return -EINVAL;
746
747 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
748}
749
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800750/* Query device for its ethtool_cmd settings.
751 *
752 * Backward compatibility note: for compatibility with legacy ethtool,
753 * this has to work with both drivers implementing get_link_ksettings
754 * API and drivers implementing get_settings API. When drivers
755 * implement get_link_ksettings and report higher link mode bits, a
756 * kernel warning is logged once (with name of 1st driver/device) to
757 * recommend user to upgrade ethtool, but the command is successful
758 * (only the lower link mode bits reported back to user).
759 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000760static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
761{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000762 struct ethtool_cmd cmd;
763
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800764 ASSERT_RTNL();
765
766 if (dev->ethtool_ops->get_link_ksettings) {
767 /* First, use link_ksettings API if it is supported */
768 int err;
769 struct ethtool_link_ksettings link_ksettings;
770
771 memset(&link_ksettings, 0, sizeof(link_ksettings));
772 err = dev->ethtool_ops->get_link_ksettings(dev,
773 &link_ksettings);
774 if (err < 0)
775 return err;
Stephen Hemminger71891e22017-12-29 10:02:52 -0800776 convert_link_ksettings_to_legacy_settings(&cmd,
777 &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800778
779 /* send a sensible cmd tag back to user */
780 cmd.cmd = ETHTOOL_GSET;
781 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800782 /* driver doesn't support %ethtool_link_ksettings
783 * API. revert to legacy %ethtool_cmd API, unless it's
784 * not supported either.
785 */
David Decotigny3237fc62016-02-24 10:58:11 -0800786 int err;
787
788 if (!dev->ethtool_ops->get_settings)
789 return -EOPNOTSUPP;
790
791 memset(&cmd, 0, sizeof(cmd));
792 cmd.cmd = ETHTOOL_GSET;
793 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800794 if (err < 0)
795 return err;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
799 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802}
803
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800804/* Update device link settings with given ethtool_cmd.
805 *
806 * Backward compatibility note: for compatibility with legacy ethtool,
807 * this has to work with both drivers implementing set_link_ksettings
808 * API and drivers implementing set_settings API. When drivers
809 * implement set_link_ksettings and user's request updates deprecated
810 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
811 * warning is logged once (with name of 1st driver/device) to
812 * recommend user to upgrade ethtool, and the request is rejected.
813 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
815{
816 struct ethtool_cmd cmd;
817
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800818 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
821 return -EFAULT;
822
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800823 /* first, try new %ethtool_link_ksettings API. */
824 if (dev->ethtool_ops->set_link_ksettings) {
825 struct ethtool_link_ksettings link_ksettings;
826
827 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
828 &cmd))
829 return -EINVAL;
830
831 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
832 link_ksettings.base.link_mode_masks_nwords
833 = __ETHTOOL_LINK_MODE_MASK_NU32;
834 return dev->ethtool_ops->set_link_ksettings(dev,
835 &link_ksettings);
836 }
837
838 /* legacy %ethtool_cmd API */
839
840 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
841 * disappears internally
842 */
843
844 if (!dev->ethtool_ops->set_settings)
845 return -EOPNOTSUPP;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return dev->ethtool_ops->set_settings(dev, &cmd);
848}
849
chavey97f8aef2010-04-07 21:54:42 -0700850static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
851 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700854 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 memset(&info, 0, sizeof(info));
857 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000858 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700859 ops->get_drvinfo(dev, &info);
860 } else if (dev->dev.parent && dev->dev.parent->driver) {
861 strlcpy(info.bus_info, dev_name(dev->dev.parent),
862 sizeof(info.bus_info));
863 strlcpy(info.driver, dev->dev.parent->driver->name,
864 sizeof(info.driver));
865 } else {
866 return -EOPNOTSUPP;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Jeff Garzik723b2f52010-03-03 22:51:50 +0000869 /*
870 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000871 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000872 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000873 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700874 int rc;
875
876 rc = ops->get_sset_count(dev, ETH_SS_TEST);
877 if (rc >= 0)
878 info.testinfo_len = rc;
879 rc = ops->get_sset_count(dev, ETH_SS_STATS);
880 if (rc >= 0)
881 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700882 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
883 if (rc >= 0)
884 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700885 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000886 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000888 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 info.eedump_len = ops->get_eeprom_len(dev);
890
891 if (copy_to_user(useraddr, &info, sizeof(info)))
892 return -EFAULT;
893 return 0;
894}
895
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800896static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700897 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000898{
899 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000900 u64 sset_mask;
901 int i, idx = 0, n_bits = 0, ret, rc;
902 u32 *info_buf = NULL;
903
Jeff Garzik723b2f52010-03-03 22:51:50 +0000904 if (copy_from_user(&info, useraddr, sizeof(info)))
905 return -EFAULT;
906
907 /* store copy of mask, because we zero struct later on */
908 sset_mask = info.sset_mask;
909 if (!sset_mask)
910 return 0;
911
912 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000913 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000914
915 memset(&info, 0, sizeof(info));
916 info.cmd = ETHTOOL_GSSET_INFO;
917
918 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
919 if (!info_buf)
920 return -ENOMEM;
921
922 /*
923 * fill return buffer based on input bitmask and successful
924 * get_sset_count return
925 */
926 for (i = 0; i < 64; i++) {
927 if (!(sset_mask & (1ULL << i)))
928 continue;
929
Michał Mirosław340ae162011-02-15 16:59:16 +0000930 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000931 if (rc >= 0) {
932 info.sset_mask |= (1ULL << i);
933 info_buf[idx++] = rc;
934 }
935 }
936
937 ret = -EFAULT;
938 if (copy_to_user(useraddr, &info, sizeof(info)))
939 goto out;
940
941 useraddr += offsetof(struct ethtool_sset_info, data);
942 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
943 goto out;
944
945 ret = 0;
946
947out:
948 kfree(info_buf);
949 return ret;
950}
951
chavey97f8aef2010-04-07 21:54:42 -0700952static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000953 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700954{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000955 struct ethtool_rxnfc info;
956 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000957 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700958
Santwona Behera59089d82009-02-20 00:58:13 -0800959 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700960 return -EOPNOTSUPP;
961
Ben Hutchingsbf988432010-06-28 08:45:58 +0000962 /* struct ethtool_rxnfc was originally defined for
963 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
964 * members. User-space might still be using that
965 * definition. */
966 if (cmd == ETHTOOL_SRXFH)
967 info_size = (offsetof(struct ethtool_rxnfc, data) +
968 sizeof(info.data));
969
970 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700971 return -EFAULT;
972
Ben Hutchings55664f32012-01-03 12:04:51 +0000973 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
974 if (rc)
975 return rc;
976
977 if (cmd == ETHTOOL_SRXCLSRLINS &&
978 copy_to_user(useraddr, &info, info_size))
979 return -EFAULT;
980
981 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700982}
983
chavey97f8aef2010-04-07 21:54:42 -0700984static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000985 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700986{
987 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000988 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800989 const struct ethtool_ops *ops = dev->ethtool_ops;
990 int ret;
991 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700992
Santwona Behera59089d82009-02-20 00:58:13 -0800993 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700994 return -EOPNOTSUPP;
995
Ben Hutchingsbf988432010-06-28 08:45:58 +0000996 /* struct ethtool_rxnfc was originally defined for
997 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
998 * members. User-space might still be using that
999 * definition. */
1000 if (cmd == ETHTOOL_GRXFH)
1001 info_size = (offsetof(struct ethtool_rxnfc, data) +
1002 sizeof(info.data));
1003
1004 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001005 return -EFAULT;
1006
Edward Cree84a1d9c42018-03-08 15:45:03 +00001007 /* If FLOW_RSS was requested then user-space must be using the
1008 * new definition, as FLOW_RSS is newer.
1009 */
1010 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1011 info_size = sizeof(info);
1012 if (copy_from_user(&info, useraddr, info_size))
1013 return -EFAULT;
1014 }
1015
Santwona Behera59089d82009-02-20 00:58:13 -08001016 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1017 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001018 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +00001019 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001020 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001021 if (!rule_buf)
1022 return -ENOMEM;
1023 }
1024 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001025
Santwona Behera59089d82009-02-20 00:58:13 -08001026 ret = ops->get_rxnfc(dev, &info, rule_buf);
1027 if (ret < 0)
1028 goto err_out;
1029
1030 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001031 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001032 goto err_out;
1033
1034 if (rule_buf) {
1035 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1036 if (copy_to_user(useraddr, rule_buf,
1037 info.rule_cnt * sizeof(u32)))
1038 goto err_out;
1039 }
1040 ret = 0;
1041
1042err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001043 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001044
1045 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001046}
1047
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301048static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1049 struct ethtool_rxnfc *rx_rings,
1050 u32 size)
1051{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001052 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301053
1054 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001055 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301056
1057 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001058 for (i = 0; i < size; i++)
1059 if (indir[i] >= rx_rings->data)
1060 return -EINVAL;
1061
1062 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301063}
1064
Kim Jonesba905f52016-02-02 03:51:16 +00001065u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001066
1067void netdev_rss_key_fill(void *buffer, size_t len)
1068{
1069 BUG_ON(len > sizeof(netdev_rss_key));
1070 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1071 memcpy(buffer, netdev_rss_key, len);
1072}
1073EXPORT_SYMBOL(netdev_rss_key_fill);
1074
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001075static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1076{
1077 u32 dev_size, current_max = 0;
1078 u32 *indir;
1079 int ret;
1080
1081 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1082 !dev->ethtool_ops->get_rxfh)
1083 return -EOPNOTSUPP;
1084 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1085 if (dev_size == 0)
1086 return -EOPNOTSUPP;
1087
1088 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1089 if (!indir)
1090 return -ENOMEM;
1091
1092 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1093 if (ret)
1094 goto out;
1095
1096 while (dev_size--)
1097 current_max = max(current_max, indir[dev_size]);
1098
1099 *max = current_max;
1100
1101out:
1102 kfree(indir);
1103 return ret;
1104}
1105
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001106static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1107 void __user *useraddr)
1108{
Ben Hutchings7850f632011-12-15 13:55:01 +00001109 u32 user_size, dev_size;
1110 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001111 int ret;
1112
Ben Hutchings7850f632011-12-15 13:55:01 +00001113 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001114 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001115 return -EOPNOTSUPP;
1116 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1117 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001118 return -EOPNOTSUPP;
1119
Ben Hutchings7850f632011-12-15 13:55:01 +00001120 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001121 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001122 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001123 return -EFAULT;
1124
Ben Hutchings7850f632011-12-15 13:55:01 +00001125 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1126 &dev_size, sizeof(dev_size)))
1127 return -EFAULT;
1128
1129 /* If the user buffer size is 0, this is just a query for the
1130 * device table size. Otherwise, if it's smaller than the
1131 * device table size it's an error.
1132 */
1133 if (user_size < dev_size)
1134 return user_size == 0 ? 0 : -EINVAL;
1135
1136 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001137 if (!indir)
1138 return -ENOMEM;
1139
Eyal Perry892311f2014-12-02 18:12:10 +02001140 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001141 if (ret)
1142 goto out;
1143
Ben Hutchings7850f632011-12-15 13:55:01 +00001144 if (copy_to_user(useraddr +
1145 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1146 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001147 ret = -EFAULT;
1148
1149out:
1150 kfree(indir);
1151 return ret;
1152}
1153
1154static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1155 void __user *useraddr)
1156{
Ben Hutchings7850f632011-12-15 13:55:01 +00001157 struct ethtool_rxnfc rx_rings;
1158 u32 user_size, dev_size, i;
1159 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001160 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001161 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301162 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001163
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001164 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001165 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001166 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001167
1168 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001169 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001170 return -EOPNOTSUPP;
1171
Ben Hutchings7850f632011-12-15 13:55:01 +00001172 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001173 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001174 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001175 return -EFAULT;
1176
Ben Hutchings278bc422011-12-15 13:56:49 +00001177 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001178 return -EINVAL;
1179
1180 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001181 if (!indir)
1182 return -ENOMEM;
1183
Ben Hutchings7850f632011-12-15 13:55:01 +00001184 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001185 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001186 if (ret)
1187 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001188
1189 if (user_size == 0) {
1190 for (i = 0; i < dev_size; i++)
1191 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1192 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301193 ret = ethtool_copy_validate_indir(indir,
1194 useraddr + ringidx_offset,
1195 &rx_rings,
1196 dev_size);
1197 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001198 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001199 }
1200
Eyal Perry892311f2014-12-02 18:12:10 +02001201 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001202 if (ret)
1203 goto out;
1204
1205 /* indicate whether rxfh was set to default */
1206 if (user_size == 0)
1207 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1208 else
1209 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001210
1211out:
1212 kfree(indir);
1213 return ret;
1214}
1215
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301216static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1217 void __user *useraddr)
1218{
1219 int ret;
1220 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001221 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301222 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001223 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301224 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001225 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301226 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001227 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301228 u8 *hkey = NULL;
1229 u8 *rss_config;
1230
Eyal Perry892311f2014-12-02 18:12:10 +02001231 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301232 return -EOPNOTSUPP;
1233
1234 if (ops->get_rxfh_indir_size)
1235 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301236 if (ops->get_rxfh_key_size)
1237 dev_key_size = ops->get_rxfh_key_size(dev);
1238
Ben Hutchingsf062a382014-05-15 16:28:07 +01001239 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301240 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001241 user_indir_size = rxfh.indir_size;
1242 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301243
Ben Hutchingsf062a382014-05-15 16:28:07 +01001244 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001245 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001246 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001247 /* Most drivers don't handle rss_context, check it's 0 as well */
1248 if (rxfh.rss_context && !ops->get_rxfh_context)
1249 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001250
1251 rxfh.indir_size = dev_indir_size;
1252 rxfh.key_size = dev_key_size;
1253 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301254 return -EFAULT;
1255
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301256 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1257 (user_key_size && (user_key_size != dev_key_size)))
1258 return -EINVAL;
1259
1260 indir_bytes = user_indir_size * sizeof(indir[0]);
1261 total_size = indir_bytes + user_key_size;
1262 rss_config = kzalloc(total_size, GFP_USER);
1263 if (!rss_config)
1264 return -ENOMEM;
1265
1266 if (user_indir_size)
1267 indir = (u32 *)rss_config;
1268
1269 if (user_key_size)
1270 hkey = rss_config + indir_bytes;
1271
Edward Cree84a1d9c42018-03-08 15:45:03 +00001272 if (rxfh.rss_context)
1273 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1274 &dev_hfunc,
1275 rxfh.rss_context);
1276 else
1277 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001278 if (ret)
1279 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301280
Eyal Perry892311f2014-12-02 18:12:10 +02001281 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1282 &dev_hfunc, sizeof(rxfh.hfunc))) {
1283 ret = -EFAULT;
1284 } else if (copy_to_user(useraddr +
1285 offsetof(struct ethtool_rxfh, rss_config[0]),
1286 rss_config, total_size)) {
1287 ret = -EFAULT;
1288 }
1289out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301290 kfree(rss_config);
1291
1292 return ret;
1293}
1294
1295static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1296 void __user *useraddr)
1297{
1298 int ret;
1299 const struct ethtool_ops *ops = dev->ethtool_ops;
1300 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001301 struct ethtool_rxfh rxfh;
1302 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301303 u32 *indir = NULL, indir_bytes = 0;
1304 u8 *hkey = NULL;
1305 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301306 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001307 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301308
Eyal Perry892311f2014-12-02 18:12:10 +02001309 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301310 return -EOPNOTSUPP;
1311
1312 if (ops->get_rxfh_indir_size)
1313 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301314 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001315 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301316
Ben Hutchingsf062a382014-05-15 16:28:07 +01001317 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301318 return -EFAULT;
1319
Ben Hutchingsf062a382014-05-15 16:28:07 +01001320 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001321 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001322 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001323 /* Most drivers don't handle rss_context, check it's 0 as well */
1324 if (rxfh.rss_context && !ops->set_rxfh_context)
1325 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001326
Eyal Perry892311f2014-12-02 18:12:10 +02001327 /* If either indir, hash key or function is valid, proceed further.
1328 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301329 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001330 if ((rxfh.indir_size &&
1331 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1332 rxfh.indir_size != dev_indir_size) ||
1333 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1334 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001335 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301336 return -EINVAL;
1337
Ben Hutchingsf062a382014-05-15 16:28:07 +01001338 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301339 indir_bytes = dev_indir_size * sizeof(indir[0]);
1340
Ben Hutchingsf062a382014-05-15 16:28:07 +01001341 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301342 if (!rss_config)
1343 return -ENOMEM;
1344
1345 rx_rings.cmd = ETHTOOL_GRXRINGS;
1346 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1347 if (ret)
1348 goto out;
1349
Edward Cree84a1d9c42018-03-08 15:45:03 +00001350 /* rxfh.indir_size == 0 means reset the indir table to default (master
1351 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001352 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301353 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001354 if (rxfh.indir_size &&
1355 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301356 indir = (u32 *)rss_config;
1357 ret = ethtool_copy_validate_indir(indir,
1358 useraddr + rss_cfg_offset,
1359 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001360 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301361 if (ret)
1362 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001363 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001364 if (rxfh.rss_context == 0) {
1365 indir = (u32 *)rss_config;
1366 for (i = 0; i < dev_indir_size; i++)
1367 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1368 } else {
1369 delete = true;
1370 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301371 }
1372
Ben Hutchingsf062a382014-05-15 16:28:07 +01001373 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301374 hkey = rss_config + indir_bytes;
1375 if (copy_from_user(hkey,
1376 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001377 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301378 ret = -EFAULT;
1379 goto out;
1380 }
1381 }
1382
Edward Cree84a1d9c42018-03-08 15:45:03 +00001383 if (rxfh.rss_context)
1384 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1385 &rxfh.rss_context, delete);
1386 else
1387 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001388 if (ret)
1389 goto out;
1390
Edward Cree84a1d9c42018-03-08 15:45:03 +00001391 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1392 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1393 ret = -EFAULT;
1394
1395 if (!rxfh.rss_context) {
1396 /* indicate whether rxfh was set to default */
1397 if (rxfh.indir_size == 0)
1398 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1399 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1400 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1401 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301402
1403out:
1404 kfree(rss_config);
1405 return ret;
1406}
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1409{
1410 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001411 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 void *regbuf;
1413 int reglen, ret;
1414
1415 if (!ops->get_regs || !ops->get_regs_len)
1416 return -EOPNOTSUPP;
1417
1418 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1419 return -EFAULT;
1420
1421 reglen = ops->get_regs_len(dev);
1422 if (regs.len > reglen)
1423 regs.len = reglen;
1424
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001425 regbuf = NULL;
1426 if (reglen) {
1427 regbuf = vzalloc(reglen);
1428 if (!regbuf)
1429 return -ENOMEM;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 ops->get_regs(dev, &regs, regbuf);
1433
1434 ret = -EFAULT;
1435 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1436 goto out;
1437 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001438 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto out;
1440 ret = 0;
1441
1442 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001443 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return ret;
1445}
1446
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001447static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1448{
1449 struct ethtool_value reset;
1450 int ret;
1451
1452 if (!dev->ethtool_ops->reset)
1453 return -EOPNOTSUPP;
1454
1455 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1456 return -EFAULT;
1457
1458 ret = dev->ethtool_ops->reset(dev, &reset.data);
1459 if (ret)
1460 return ret;
1461
1462 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1463 return -EFAULT;
1464 return 0;
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1468{
Roland Dreier8e557422010-02-11 12:14:23 -08001469 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 if (!dev->ethtool_ops->get_wol)
1472 return -EOPNOTSUPP;
1473
1474 dev->ethtool_ops->get_wol(dev, &wol);
1475
1476 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1477 return -EFAULT;
1478 return 0;
1479}
1480
1481static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1482{
1483 struct ethtool_wolinfo wol;
1484
1485 if (!dev->ethtool_ops->set_wol)
1486 return -EOPNOTSUPP;
1487
1488 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1489 return -EFAULT;
1490
1491 return dev->ethtool_ops->set_wol(dev, &wol);
1492}
1493
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001494static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1495{
1496 struct ethtool_eee edata;
1497 int rc;
1498
1499 if (!dev->ethtool_ops->get_eee)
1500 return -EOPNOTSUPP;
1501
1502 memset(&edata, 0, sizeof(struct ethtool_eee));
1503 edata.cmd = ETHTOOL_GEEE;
1504 rc = dev->ethtool_ops->get_eee(dev, &edata);
1505
1506 if (rc)
1507 return rc;
1508
1509 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1510 return -EFAULT;
1511
1512 return 0;
1513}
1514
1515static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1516{
1517 struct ethtool_eee edata;
1518
1519 if (!dev->ethtool_ops->set_eee)
1520 return -EOPNOTSUPP;
1521
1522 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1523 return -EFAULT;
1524
1525 return dev->ethtool_ops->set_eee(dev, &edata);
1526}
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528static int ethtool_nway_reset(struct net_device *dev)
1529{
1530 if (!dev->ethtool_ops->nway_reset)
1531 return -EOPNOTSUPP;
1532
1533 return dev->ethtool_ops->nway_reset(dev);
1534}
1535
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001536static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1537{
1538 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1539
1540 if (!dev->ethtool_ops->get_link)
1541 return -EOPNOTSUPP;
1542
1543 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1544
1545 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1546 return -EFAULT;
1547 return 0;
1548}
1549
Ben Hutchings081d0942012-04-12 00:42:12 +00001550static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1551 int (*getter)(struct net_device *,
1552 struct ethtool_eeprom *, u8 *),
1553 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001556 void __user *userbuf = useraddr + sizeof(eeprom);
1557 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001559 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1562 return -EFAULT;
1563
1564 /* Check for wrap and zero */
1565 if (eeprom.offset + eeprom.len <= eeprom.offset)
1566 return -EINVAL;
1567
1568 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001569 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return -EINVAL;
1571
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001572 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (!data)
1574 return -ENOMEM;
1575
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001576 bytes_remaining = eeprom.len;
1577 while (bytes_remaining > 0) {
1578 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Ben Hutchings081d0942012-04-12 00:42:12 +00001580 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001581 if (ret)
1582 break;
1583 if (copy_to_user(userbuf, data, eeprom.len)) {
1584 ret = -EFAULT;
1585 break;
1586 }
1587 userbuf += eeprom.len;
1588 eeprom.offset += eeprom.len;
1589 bytes_remaining -= eeprom.len;
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001592 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1593 eeprom.offset -= eeprom.len;
1594 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1595 ret = -EFAULT;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 kfree(data);
1598 return ret;
1599}
1600
Ben Hutchings081d0942012-04-12 00:42:12 +00001601static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1602{
1603 const struct ethtool_ops *ops = dev->ethtool_ops;
1604
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001605 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1606 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001607 return -EOPNOTSUPP;
1608
1609 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1610 ops->get_eeprom_len(dev));
1611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1614{
1615 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001616 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001617 void __user *userbuf = useraddr + sizeof(eeprom);
1618 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001620 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001622 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1623 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return -EOPNOTSUPP;
1625
1626 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1627 return -EFAULT;
1628
1629 /* Check for wrap and zero */
1630 if (eeprom.offset + eeprom.len <= eeprom.offset)
1631 return -EINVAL;
1632
1633 /* Check for exceeding total eeprom len */
1634 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1635 return -EINVAL;
1636
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001637 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (!data)
1639 return -ENOMEM;
1640
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001641 bytes_remaining = eeprom.len;
1642 while (bytes_remaining > 0) {
1643 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001645 if (copy_from_user(data, userbuf, eeprom.len)) {
1646 ret = -EFAULT;
1647 break;
1648 }
1649 ret = ops->set_eeprom(dev, &eeprom, data);
1650 if (ret)
1651 break;
1652 userbuf += eeprom.len;
1653 eeprom.offset += eeprom.len;
1654 bytes_remaining -= eeprom.len;
1655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 kfree(data);
1658 return ret;
1659}
1660
chavey97f8aef2010-04-07 21:54:42 -07001661static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1662 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Roland Dreier8e557422010-02-11 12:14:23 -08001664 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 if (!dev->ethtool_ops->get_coalesce)
1667 return -EOPNOTSUPP;
1668
1669 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1670
1671 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1672 return -EFAULT;
1673 return 0;
1674}
1675
chavey97f8aef2010-04-07 21:54:42 -07001676static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1677 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 struct ethtool_coalesce coalesce;
1680
David S. Millerfa04ae52005-06-06 15:07:19 -07001681 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return -EOPNOTSUPP;
1683
1684 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1685 return -EFAULT;
1686
1687 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1688}
1689
1690static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1691{
Roland Dreier8e557422010-02-11 12:14:23 -08001692 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 if (!dev->ethtool_ops->get_ringparam)
1695 return -EOPNOTSUPP;
1696
1697 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1698
1699 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1700 return -EFAULT;
1701 return 0;
1702}
1703
1704static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1705{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001706 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001708 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return -EOPNOTSUPP;
1710
1711 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1712 return -EFAULT;
1713
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001714 dev->ethtool_ops->get_ringparam(dev, &max);
1715
1716 /* ensure new ring parameters are within the maximums */
1717 if (ringparam.rx_pending > max.rx_max_pending ||
1718 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1719 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1720 ringparam.tx_pending > max.tx_max_pending)
1721 return -EINVAL;
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1724}
1725
amit salecha8b5933c2011-04-07 01:58:42 +00001726static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1727 void __user *useraddr)
1728{
1729 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1730
1731 if (!dev->ethtool_ops->get_channels)
1732 return -EOPNOTSUPP;
1733
1734 dev->ethtool_ops->get_channels(dev, &channels);
1735
1736 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1737 return -EFAULT;
1738 return 0;
1739}
1740
1741static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1742 void __user *useraddr)
1743{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001744 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001745 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001746
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001747 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001748 return -EOPNOTSUPP;
1749
1750 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1751 return -EFAULT;
1752
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001753 dev->ethtool_ops->get_channels(dev, &max);
1754
1755 /* ensure new counts are within the maximums */
1756 if ((channels.rx_count > max.max_rx) ||
1757 (channels.tx_count > max.max_tx) ||
1758 (channels.combined_count > max.max_combined) ||
1759 (channels.other_count > max.max_other))
1760 return -EINVAL;
1761
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001762 /* ensure the new Rx count fits within the configured Rx flow
1763 * indirection table settings */
1764 if (netif_is_rxfh_configured(dev) &&
1765 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1766 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1767 return -EINVAL;
1768
amit salecha8b5933c2011-04-07 01:58:42 +00001769 return dev->ethtool_ops->set_channels(dev, &channels);
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1773{
1774 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1775
1776 if (!dev->ethtool_ops->get_pauseparam)
1777 return -EOPNOTSUPP;
1778
1779 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1780
1781 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1782 return -EFAULT;
1783 return 0;
1784}
1785
1786static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1787{
1788 struct ethtool_pauseparam pauseparam;
1789
Jeff Garzike1b90c42006-07-17 12:54:40 -04001790 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return -EOPNOTSUPP;
1792
1793 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1794 return -EFAULT;
1795
1796 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1797}
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1800{
1801 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001802 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001804 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001806 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001807 return -EOPNOTSUPP;
1808
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001809 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001810 if (test_len < 0)
1811 return test_len;
1812 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 if (copy_from_user(&test, useraddr, sizeof(test)))
1815 return -EFAULT;
1816
Jeff Garzikff03d492007-08-15 16:01:08 -07001817 test.len = test_len;
1818 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (!data)
1820 return -ENOMEM;
1821
1822 ops->self_test(dev, &test, data);
1823
1824 ret = -EFAULT;
1825 if (copy_to_user(useraddr, &test, sizeof(test)))
1826 goto out;
1827 useraddr += sizeof(test);
1828 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1829 goto out;
1830 ret = 0;
1831
1832 out:
1833 kfree(data);
1834 return ret;
1835}
1836
1837static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1838{
1839 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 u8 *data;
1841 int ret;
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1844 return -EFAULT;
1845
Michał Mirosław340ae162011-02-15 16:59:16 +00001846 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001847 if (ret < 0)
1848 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001849 if (ret > S32_MAX / ETH_GSTRING_LEN)
1850 return -ENOMEM;
1851 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001852
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001853 gstrings.len = ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001854 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1855 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return -ENOMEM;
1857
Michał Mirosław340ae162011-02-15 16:59:16 +00001858 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 ret = -EFAULT;
1861 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1862 goto out;
1863 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001864 if (gstrings.len &&
1865 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto out;
1867 ret = 0;
1868
Michał Mirosław340ae162011-02-15 16:59:16 +00001869out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001870 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return ret;
1872}
1873
1874static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1875{
1876 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001877 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001878 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001879 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001881 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return -EOPNOTSUPP;
1883
Ben Hutchings68f512f2011-04-02 00:35:15 +01001884 if (busy)
1885 return -EBUSY;
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (copy_from_user(&id, useraddr, sizeof(id)))
1888 return -EFAULT;
1889
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001890 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001891 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001892 return rc;
1893
1894 /* Drop the RTNL lock while waiting, but prevent reentry or
1895 * removal of the device.
1896 */
1897 busy = true;
1898 dev_hold(dev);
1899 rtnl_unlock();
1900
1901 if (rc == 0) {
1902 /* Driver will handle this itself */
1903 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001904 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001905 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001906 /* Driver expects to be called at twice the frequency in rc */
1907 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001908
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001909 /* Count down seconds */
1910 do {
1911 /* Count down iterations per second */
1912 i = n;
1913 do {
1914 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001915 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001916 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1917 rtnl_unlock();
1918 if (rc)
1919 break;
1920 schedule_timeout_interruptible(interval);
1921 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001922 } while (!signal_pending(current) &&
1923 (id.data == 0 || --id.data != 0));
1924 }
1925
1926 rtnl_lock();
1927 dev_put(dev);
1928 busy = false;
1929
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001930 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001931 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
1934static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1935{
1936 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001937 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001939 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001941 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001942 return -EOPNOTSUPP;
1943
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001944 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001945 if (n_stats < 0)
1946 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001947 if (n_stats > S32_MAX / sizeof(u64))
1948 return -ENOMEM;
1949 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1951 return -EFAULT;
1952
Jeff Garzikff03d492007-08-15 16:01:08 -07001953 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001954 data = vzalloc(n_stats * sizeof(u64));
1955 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return -ENOMEM;
1957
1958 ops->get_ethtool_stats(dev, &stats, data);
1959
1960 ret = -EFAULT;
1961 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1962 goto out;
1963 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001964 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 goto out;
1966 ret = 0;
1967
1968 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001969 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return ret;
1971}
1972
Andrew Lunnf3a40942015-12-30 16:28:25 +01001973static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1974{
1975 struct ethtool_stats stats;
1976 struct phy_device *phydev = dev->phydev;
1977 u64 *data;
1978 int ret, n_stats;
1979
1980 if (!phydev)
1981 return -EOPNOTSUPP;
1982
Florian Fainellic59530d2018-04-25 12:12:47 -07001983 n_stats = phy_ethtool_get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001984 if (n_stats < 0)
1985 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001986 if (n_stats > S32_MAX / sizeof(u64))
1987 return -ENOMEM;
1988 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001989
1990 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1991 return -EFAULT;
1992
1993 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001994 data = vzalloc(n_stats * sizeof(u64));
1995 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01001996 return -ENOMEM;
1997
Florian Fainellic59530d2018-04-25 12:12:47 -07001998 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1999 if (ret < 0)
2000 return ret;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002001
2002 ret = -EFAULT;
2003 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2004 goto out;
2005 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002006 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002007 goto out;
2008 ret = 0;
2009
2010 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002011 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002012 return ret;
2013}
2014
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002015static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002016{
2017 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002018
Matthew Wilcox313674a2007-07-31 14:00:29 -07002019 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002020 return -EFAULT;
2021
Matthew Wilcox313674a2007-07-31 14:00:29 -07002022 if (epaddr.size < dev->addr_len)
2023 return -ETOOSMALL;
2024 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002025
Jon Wetzela6f9a702005-08-20 17:15:54 -07002026 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002027 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002028 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002029 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2030 return -EFAULT;
2031 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002032}
2033
Jeff Garzik13c99b22007-08-15 16:01:56 -07002034static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2035 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002036{
Roland Dreier8e557422010-02-11 12:14:23 -08002037 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002038
Jeff Garzik13c99b22007-08-15 16:01:56 -07002039 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002040 return -EOPNOTSUPP;
2041
Jeff Garzik13c99b22007-08-15 16:01:56 -07002042 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002043
2044 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2045 return -EFAULT;
2046 return 0;
2047}
2048
Jeff Garzik13c99b22007-08-15 16:01:56 -07002049static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2050 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002051{
2052 struct ethtool_value edata;
2053
Jeff Garzik13c99b22007-08-15 16:01:56 -07002054 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002055 return -EOPNOTSUPP;
2056
2057 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2058 return -EFAULT;
2059
Jeff Garzik13c99b22007-08-15 16:01:56 -07002060 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002061 return 0;
2062}
2063
Jeff Garzik13c99b22007-08-15 16:01:56 -07002064static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2065 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002066{
2067 struct ethtool_value edata;
2068
Jeff Garzik13c99b22007-08-15 16:01:56 -07002069 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002070 return -EOPNOTSUPP;
2071
2072 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2073 return -EFAULT;
2074
Jeff Garzik13c99b22007-08-15 16:01:56 -07002075 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002076}
2077
chavey97f8aef2010-04-07 21:54:42 -07002078static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2079 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002080{
2081 struct ethtool_flash efl;
2082
2083 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2084 return -EFAULT;
2085
2086 if (!dev->ethtool_ops->flash_device)
2087 return -EOPNOTSUPP;
2088
Ben Hutchings786f5282012-02-01 09:32:25 +00002089 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2090
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002091 return dev->ethtool_ops->flash_device(dev, &efl);
2092}
2093
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002094static int ethtool_set_dump(struct net_device *dev,
2095 void __user *useraddr)
2096{
2097 struct ethtool_dump dump;
2098
2099 if (!dev->ethtool_ops->set_dump)
2100 return -EOPNOTSUPP;
2101
2102 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2103 return -EFAULT;
2104
2105 return dev->ethtool_ops->set_dump(dev, &dump);
2106}
2107
2108static int ethtool_get_dump_flag(struct net_device *dev,
2109 void __user *useraddr)
2110{
2111 int ret;
2112 struct ethtool_dump dump;
2113 const struct ethtool_ops *ops = dev->ethtool_ops;
2114
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002115 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002116 return -EOPNOTSUPP;
2117
2118 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2119 return -EFAULT;
2120
2121 ret = ops->get_dump_flag(dev, &dump);
2122 if (ret)
2123 return ret;
2124
2125 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2126 return -EFAULT;
2127 return 0;
2128}
2129
2130static int ethtool_get_dump_data(struct net_device *dev,
2131 void __user *useraddr)
2132{
2133 int ret;
2134 __u32 len;
2135 struct ethtool_dump dump, tmp;
2136 const struct ethtool_ops *ops = dev->ethtool_ops;
2137 void *data = NULL;
2138
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002139 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002140 return -EOPNOTSUPP;
2141
2142 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2143 return -EFAULT;
2144
2145 memset(&tmp, 0, sizeof(tmp));
2146 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2147 ret = ops->get_dump_flag(dev, &tmp);
2148 if (ret)
2149 return ret;
2150
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002151 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002152 if (!len)
2153 return -EFAULT;
2154
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002155 /* Don't ever let the driver think there's more space available
2156 * than it requested with .get_dump_flag().
2157 */
2158 dump.len = len;
2159
2160 /* Always allocate enough space to hold the whole thing so that the
2161 * driver does not need to check the length and bother with partial
2162 * dumping.
2163 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002164 data = vzalloc(tmp.len);
2165 if (!data)
2166 return -ENOMEM;
2167 ret = ops->get_dump_data(dev, &dump, data);
2168 if (ret)
2169 goto out;
2170
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002171 /* There are two sane possibilities:
2172 * 1. The driver's .get_dump_data() does not touch dump.len.
2173 * 2. Or it may set dump.len to how much it really writes, which
2174 * should be tmp.len (or len if it can do a partial dump).
2175 * In any case respond to userspace with the actual length of data
2176 * it's receiving.
2177 */
2178 WARN_ON(dump.len != len && dump.len != tmp.len);
2179 dump.len = len;
2180
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002181 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2182 ret = -EFAULT;
2183 goto out;
2184 }
2185 useraddr += offsetof(struct ethtool_dump, data);
2186 if (copy_to_user(useraddr, data, len))
2187 ret = -EFAULT;
2188out:
2189 vfree(data);
2190 return ret;
2191}
2192
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002193static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2194{
2195 int err = 0;
2196 struct ethtool_ts_info info;
2197 const struct ethtool_ops *ops = dev->ethtool_ops;
2198 struct phy_device *phydev = dev->phydev;
2199
2200 memset(&info, 0, sizeof(info));
2201 info.cmd = ETHTOOL_GET_TS_INFO;
2202
2203 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002204 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002205 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002206 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002207 } else {
2208 info.so_timestamping =
2209 SOF_TIMESTAMPING_RX_SOFTWARE |
2210 SOF_TIMESTAMPING_SOFTWARE;
2211 info.phc_index = -1;
2212 }
2213
2214 if (err)
2215 return err;
2216
2217 if (copy_to_user(useraddr, &info, sizeof(info)))
2218 err = -EFAULT;
2219
2220 return err;
2221}
2222
Ed Swierk2f438362015-01-02 17:27:56 -08002223static int __ethtool_get_module_info(struct net_device *dev,
2224 struct ethtool_modinfo *modinfo)
2225{
2226 const struct ethtool_ops *ops = dev->ethtool_ops;
2227 struct phy_device *phydev = dev->phydev;
2228
Russell Kinge679c9c2018-03-28 15:44:16 -07002229 if (dev->sfp_bus)
2230 return sfp_get_module_info(dev->sfp_bus, modinfo);
2231
Ed Swierk2f438362015-01-02 17:27:56 -08002232 if (phydev && phydev->drv && phydev->drv->module_info)
2233 return phydev->drv->module_info(phydev, modinfo);
2234
2235 if (ops->get_module_info)
2236 return ops->get_module_info(dev, modinfo);
2237
2238 return -EOPNOTSUPP;
2239}
2240
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002241static int ethtool_get_module_info(struct net_device *dev,
2242 void __user *useraddr)
2243{
2244 int ret;
2245 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002246
2247 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2248 return -EFAULT;
2249
Ed Swierk2f438362015-01-02 17:27:56 -08002250 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002251 if (ret)
2252 return ret;
2253
2254 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2255 return -EFAULT;
2256
2257 return 0;
2258}
2259
Ed Swierk2f438362015-01-02 17:27:56 -08002260static int __ethtool_get_module_eeprom(struct net_device *dev,
2261 struct ethtool_eeprom *ee, u8 *data)
2262{
2263 const struct ethtool_ops *ops = dev->ethtool_ops;
2264 struct phy_device *phydev = dev->phydev;
2265
Russell Kinge679c9c2018-03-28 15:44:16 -07002266 if (dev->sfp_bus)
2267 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2268
Ed Swierk2f438362015-01-02 17:27:56 -08002269 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2270 return phydev->drv->module_eeprom(phydev, ee, data);
2271
2272 if (ops->get_module_eeprom)
2273 return ops->get_module_eeprom(dev, ee, data);
2274
2275 return -EOPNOTSUPP;
2276}
2277
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002278static int ethtool_get_module_eeprom(struct net_device *dev,
2279 void __user *useraddr)
2280{
2281 int ret;
2282 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002283
Ed Swierk2f438362015-01-02 17:27:56 -08002284 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002285 if (ret)
2286 return ret;
2287
Ed Swierk2f438362015-01-02 17:27:56 -08002288 return ethtool_get_any_eeprom(dev, useraddr,
2289 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002290 modinfo.eeprom_len);
2291}
2292
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302293static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2294{
2295 switch (tuna->id) {
2296 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002297 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302298 if (tuna->len != sizeof(u32) ||
2299 tuna->type_id != ETHTOOL_TUNABLE_U32)
2300 return -EINVAL;
2301 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002302 case ETHTOOL_PFC_PREVENTION_TOUT:
2303 if (tuna->len != sizeof(u16) ||
2304 tuna->type_id != ETHTOOL_TUNABLE_U16)
2305 return -EINVAL;
2306 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302307 default:
2308 return -EINVAL;
2309 }
2310
2311 return 0;
2312}
2313
2314static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2315{
2316 int ret;
2317 struct ethtool_tunable tuna;
2318 const struct ethtool_ops *ops = dev->ethtool_ops;
2319 void *data;
2320
2321 if (!ops->get_tunable)
2322 return -EOPNOTSUPP;
2323 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2324 return -EFAULT;
2325 ret = ethtool_tunable_valid(&tuna);
2326 if (ret)
2327 return ret;
2328 data = kmalloc(tuna.len, GFP_USER);
2329 if (!data)
2330 return -ENOMEM;
2331 ret = ops->get_tunable(dev, &tuna, data);
2332 if (ret)
2333 goto out;
2334 useraddr += sizeof(tuna);
2335 ret = -EFAULT;
2336 if (copy_to_user(useraddr, data, tuna.len))
2337 goto out;
2338 ret = 0;
2339
2340out:
2341 kfree(data);
2342 return ret;
2343}
2344
2345static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2346{
2347 int ret;
2348 struct ethtool_tunable tuna;
2349 const struct ethtool_ops *ops = dev->ethtool_ops;
2350 void *data;
2351
2352 if (!ops->set_tunable)
2353 return -EOPNOTSUPP;
2354 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2355 return -EFAULT;
2356 ret = ethtool_tunable_valid(&tuna);
2357 if (ret)
2358 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302359 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002360 data = memdup_user(useraddr, tuna.len);
2361 if (IS_ERR(data))
2362 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302363 ret = ops->set_tunable(dev, &tuna, data);
2364
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302365 kfree(data);
2366 return ret;
2367}
2368
Kan Liang421797b2016-02-19 09:24:02 -05002369static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2370 void __user *useraddr,
2371 struct ethtool_per_queue_op *per_queue_opt)
2372{
2373 u32 bit;
2374 int ret;
2375 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2376
2377 if (!dev->ethtool_ops->get_per_queue_coalesce)
2378 return -EOPNOTSUPP;
2379
2380 useraddr += sizeof(*per_queue_opt);
2381
Yury Norov3aa56882018-02-06 15:38:06 -08002382 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2383 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002384
2385 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2386 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2387
2388 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2389 if (ret != 0)
2390 return ret;
2391 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2392 return -EFAULT;
2393 useraddr += sizeof(coalesce);
2394 }
2395
2396 return 0;
2397}
2398
Kan Liangf38d1382016-02-19 09:24:03 -05002399static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2400 void __user *useraddr,
2401 struct ethtool_per_queue_op *per_queue_opt)
2402{
2403 u32 bit;
2404 int i, ret = 0;
2405 int n_queue;
2406 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2407 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2408
2409 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2410 (!dev->ethtool_ops->get_per_queue_coalesce))
2411 return -EOPNOTSUPP;
2412
2413 useraddr += sizeof(*per_queue_opt);
2414
Yury Norov3aa56882018-02-06 15:38:06 -08002415 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002416 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2417 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2418 if (!backup)
2419 return -ENOMEM;
2420
2421 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2422 struct ethtool_coalesce coalesce;
2423
2424 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2425 if (ret != 0)
2426 goto roll_back;
2427
2428 tmp++;
2429
2430 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2431 ret = -EFAULT;
2432 goto roll_back;
2433 }
2434
2435 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2436 if (ret != 0)
2437 goto roll_back;
2438
2439 useraddr += sizeof(coalesce);
2440 }
2441
2442roll_back:
2443 if (ret != 0) {
2444 tmp = backup;
2445 for_each_set_bit(i, queue_mask, bit) {
2446 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2447 tmp++;
2448 }
2449 }
2450 kfree(backup);
2451
2452 return ret;
2453}
2454
Kan Liangac2c7ad2016-02-19 09:24:01 -05002455static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2456{
2457 struct ethtool_per_queue_op per_queue_opt;
2458
2459 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2460 return -EFAULT;
2461
2462 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002463 case ETHTOOL_GCOALESCE:
2464 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002465 case ETHTOOL_SCOALESCE:
2466 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002467 default:
2468 return -EOPNOTSUPP;
2469 };
2470}
2471
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002472static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2473{
2474 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002475 case ETHTOOL_PHY_DOWNSHIFT:
2476 if (tuna->len != sizeof(u8) ||
2477 tuna->type_id != ETHTOOL_TUNABLE_U8)
2478 return -EINVAL;
2479 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002480 default:
2481 return -EINVAL;
2482 }
2483
2484 return 0;
2485}
2486
2487static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2488{
2489 int ret;
2490 struct ethtool_tunable tuna;
2491 struct phy_device *phydev = dev->phydev;
2492 void *data;
2493
2494 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2495 return -EOPNOTSUPP;
2496
2497 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2498 return -EFAULT;
2499 ret = ethtool_phy_tunable_valid(&tuna);
2500 if (ret)
2501 return ret;
2502 data = kmalloc(tuna.len, GFP_USER);
2503 if (!data)
2504 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002505 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002506 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002507 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002508 if (ret)
2509 goto out;
2510 useraddr += sizeof(tuna);
2511 ret = -EFAULT;
2512 if (copy_to_user(useraddr, data, tuna.len))
2513 goto out;
2514 ret = 0;
2515
2516out:
2517 kfree(data);
2518 return ret;
2519}
2520
2521static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2522{
2523 int ret;
2524 struct ethtool_tunable tuna;
2525 struct phy_device *phydev = dev->phydev;
2526 void *data;
2527
2528 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2529 return -EOPNOTSUPP;
2530 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2531 return -EFAULT;
2532 ret = ethtool_phy_tunable_valid(&tuna);
2533 if (ret)
2534 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002535 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002536 data = memdup_user(useraddr, tuna.len);
2537 if (IS_ERR(data))
2538 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002539 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002540 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002541 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002542
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002543 kfree(data);
2544 return ret;
2545}
2546
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002547static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2548{
2549 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002550 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002551
2552 if (!dev->ethtool_ops->get_fecparam)
2553 return -EOPNOTSUPP;
2554
Edward Creea6d50512018-02-28 19:15:58 +00002555 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2556 if (rc)
2557 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002558
2559 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2560 return -EFAULT;
2561 return 0;
2562}
2563
2564static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2565{
2566 struct ethtool_fecparam fecparam;
2567
2568 if (!dev->ethtool_ops->set_fecparam)
2569 return -EOPNOTSUPP;
2570
2571 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2572 return -EFAULT;
2573
2574 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2575}
2576
Yan Burman600fed52013-06-03 02:03:34 +00002577/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Eric W. Biederman881d9662007-09-17 11:56:21 -07002579int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002581 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002583 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002585 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (!dev || !netif_device_present(dev))
2588 return -ENODEV;
2589
chavey97f8aef2010-04-07 21:54:42 -07002590 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return -EFAULT;
2592
Kan Liangac2c7ad2016-02-19 09:24:01 -05002593 if (ethcmd == ETHTOOL_PERQUEUE) {
2594 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2595 return -EFAULT;
2596 } else {
2597 sub_cmd = ethcmd;
2598 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002599 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002600 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002601 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002602 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002603 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002604 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002605 case ETHTOOL_GCOALESCE:
2606 case ETHTOOL_GRINGPARAM:
2607 case ETHTOOL_GPAUSEPARAM:
2608 case ETHTOOL_GRXCSUM:
2609 case ETHTOOL_GTXCSUM:
2610 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002611 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002612 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002613 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002614 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002615 case ETHTOOL_GTSO:
2616 case ETHTOOL_GPERMADDR:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002617 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002618 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002619 case ETHTOOL_GFLAGS:
2620 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002621 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002622 case ETHTOOL_GRXRINGS:
2623 case ETHTOOL_GRXCLSRLCNT:
2624 case ETHTOOL_GRXCLSRULE:
2625 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002626 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302627 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002628 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002629 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002630 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002631 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302632 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002633 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002634 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002635 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002636 break;
2637 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002638 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002639 return -EPERM;
2640 }
2641
chavey97f8aef2010-04-07 21:54:42 -07002642 if (dev->ethtool_ops->begin) {
2643 rc = dev->ethtool_ops->begin(dev);
2644 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002646 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002647 old_features = dev->features;
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 switch (ethcmd) {
2650 case ETHTOOL_GSET:
2651 rc = ethtool_get_settings(dev, useraddr);
2652 break;
2653 case ETHTOOL_SSET:
2654 rc = ethtool_set_settings(dev, useraddr);
2655 break;
2656 case ETHTOOL_GDRVINFO:
2657 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 break;
2659 case ETHTOOL_GREGS:
2660 rc = ethtool_get_regs(dev, useraddr);
2661 break;
2662 case ETHTOOL_GWOL:
2663 rc = ethtool_get_wol(dev, useraddr);
2664 break;
2665 case ETHTOOL_SWOL:
2666 rc = ethtool_set_wol(dev, useraddr);
2667 break;
2668 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002669 rc = ethtool_get_value(dev, useraddr, ethcmd,
2670 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 break;
2672 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002673 rc = ethtool_set_value_void(dev, useraddr,
2674 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002676 case ETHTOOL_GEEE:
2677 rc = ethtool_get_eee(dev, useraddr);
2678 break;
2679 case ETHTOOL_SEEE:
2680 rc = ethtool_set_eee(dev, useraddr);
2681 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 case ETHTOOL_NWAY_RST:
2683 rc = ethtool_nway_reset(dev);
2684 break;
2685 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002686 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 break;
2688 case ETHTOOL_GEEPROM:
2689 rc = ethtool_get_eeprom(dev, useraddr);
2690 break;
2691 case ETHTOOL_SEEPROM:
2692 rc = ethtool_set_eeprom(dev, useraddr);
2693 break;
2694 case ETHTOOL_GCOALESCE:
2695 rc = ethtool_get_coalesce(dev, useraddr);
2696 break;
2697 case ETHTOOL_SCOALESCE:
2698 rc = ethtool_set_coalesce(dev, useraddr);
2699 break;
2700 case ETHTOOL_GRINGPARAM:
2701 rc = ethtool_get_ringparam(dev, useraddr);
2702 break;
2703 case ETHTOOL_SRINGPARAM:
2704 rc = ethtool_set_ringparam(dev, useraddr);
2705 break;
2706 case ETHTOOL_GPAUSEPARAM:
2707 rc = ethtool_get_pauseparam(dev, useraddr);
2708 break;
2709 case ETHTOOL_SPAUSEPARAM:
2710 rc = ethtool_set_pauseparam(dev, useraddr);
2711 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 case ETHTOOL_TEST:
2713 rc = ethtool_self_test(dev, useraddr);
2714 break;
2715 case ETHTOOL_GSTRINGS:
2716 rc = ethtool_get_strings(dev, useraddr);
2717 break;
2718 case ETHTOOL_PHYS_ID:
2719 rc = ethtool_phys_id(dev, useraddr);
2720 break;
2721 case ETHTOOL_GSTATS:
2722 rc = ethtool_get_stats(dev, useraddr);
2723 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002724 case ETHTOOL_GPERMADDR:
2725 rc = ethtool_get_perm_addr(dev, useraddr);
2726 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002727 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002728 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002729 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002730 break;
2731 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002732 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002733 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002734 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002735 rc = ethtool_get_value(dev, useraddr, ethcmd,
2736 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002737 break;
2738 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002739 rc = ethtool_set_value(dev, useraddr,
2740 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002741 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002742 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002743 case ETHTOOL_GRXRINGS:
2744 case ETHTOOL_GRXCLSRLCNT:
2745 case ETHTOOL_GRXCLSRULE:
2746 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002747 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002748 break;
2749 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002750 case ETHTOOL_SRXCLSRLDEL:
2751 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002752 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002753 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002754 case ETHTOOL_FLASHDEV:
2755 rc = ethtool_flash_device(dev, useraddr);
2756 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002757 case ETHTOOL_RESET:
2758 rc = ethtool_reset(dev, useraddr);
2759 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002760 case ETHTOOL_GSSET_INFO:
2761 rc = ethtool_get_sset_info(dev, useraddr);
2762 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002763 case ETHTOOL_GRXFHINDIR:
2764 rc = ethtool_get_rxfh_indir(dev, useraddr);
2765 break;
2766 case ETHTOOL_SRXFHINDIR:
2767 rc = ethtool_set_rxfh_indir(dev, useraddr);
2768 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302769 case ETHTOOL_GRSSH:
2770 rc = ethtool_get_rxfh(dev, useraddr);
2771 break;
2772 case ETHTOOL_SRSSH:
2773 rc = ethtool_set_rxfh(dev, useraddr);
2774 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002775 case ETHTOOL_GFEATURES:
2776 rc = ethtool_get_features(dev, useraddr);
2777 break;
2778 case ETHTOOL_SFEATURES:
2779 rc = ethtool_set_features(dev, useraddr);
2780 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002781 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002782 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002783 case ETHTOOL_GSG:
2784 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002785 case ETHTOOL_GGSO:
2786 case ETHTOOL_GGRO:
2787 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2788 break;
2789 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002790 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002791 case ETHTOOL_SSG:
2792 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002793 case ETHTOOL_SGSO:
2794 case ETHTOOL_SGRO:
2795 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2796 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002797 case ETHTOOL_GCHANNELS:
2798 rc = ethtool_get_channels(dev, useraddr);
2799 break;
2800 case ETHTOOL_SCHANNELS:
2801 rc = ethtool_set_channels(dev, useraddr);
2802 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002803 case ETHTOOL_SET_DUMP:
2804 rc = ethtool_set_dump(dev, useraddr);
2805 break;
2806 case ETHTOOL_GET_DUMP_FLAG:
2807 rc = ethtool_get_dump_flag(dev, useraddr);
2808 break;
2809 case ETHTOOL_GET_DUMP_DATA:
2810 rc = ethtool_get_dump_data(dev, useraddr);
2811 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002812 case ETHTOOL_GET_TS_INFO:
2813 rc = ethtool_get_ts_info(dev, useraddr);
2814 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002815 case ETHTOOL_GMODULEINFO:
2816 rc = ethtool_get_module_info(dev, useraddr);
2817 break;
2818 case ETHTOOL_GMODULEEEPROM:
2819 rc = ethtool_get_module_eeprom(dev, useraddr);
2820 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302821 case ETHTOOL_GTUNABLE:
2822 rc = ethtool_get_tunable(dev, useraddr);
2823 break;
2824 case ETHTOOL_STUNABLE:
2825 rc = ethtool_set_tunable(dev, useraddr);
2826 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002827 case ETHTOOL_GPHYSTATS:
2828 rc = ethtool_get_phy_stats(dev, useraddr);
2829 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002830 case ETHTOOL_PERQUEUE:
2831 rc = ethtool_set_per_queue(dev, useraddr);
2832 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002833 case ETHTOOL_GLINKSETTINGS:
2834 rc = ethtool_get_link_ksettings(dev, useraddr);
2835 break;
2836 case ETHTOOL_SLINKSETTINGS:
2837 rc = ethtool_set_link_ksettings(dev, useraddr);
2838 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002839 case ETHTOOL_PHY_GTUNABLE:
2840 rc = get_phy_tunable(dev, useraddr);
2841 break;
2842 case ETHTOOL_PHY_STUNABLE:
2843 rc = set_phy_tunable(dev, useraddr);
2844 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002845 case ETHTOOL_GFECPARAM:
2846 rc = ethtool_get_fecparam(dev, useraddr);
2847 break;
2848 case ETHTOOL_SFECPARAM:
2849 rc = ethtool_set_fecparam(dev, useraddr);
2850 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002852 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002854
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002855 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002857
2858 if (old_features != dev->features)
2859 netdev_features_change(dev);
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862}