blob: 1320e8dce559fd2ae19eb0f54e0dd7f261c7a7a2 [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>
Jakub Kicinskiddb6e992019-01-31 10:50:47 -080030#include <net/devlink.h>
Jakub Kicinski1661d342018-10-01 14:51:36 +020031#include <net/xdp_sock.h>
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +010032#include <net/flow_offload.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090034/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * Some useful ethtool_ops methods that're device independent.
36 * If we find that all drivers want to do the same thing here,
37 * we can turn these into dev_() function calls.
38 */
39
40u32 ethtool_op_get_link(struct net_device *dev)
41{
42 return netif_carrier_ok(dev) ? 1 : 0;
43}
chavey97f8aef2010-04-07 21:54:42 -070044EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Richard Cochran02eacbd2012-04-03 22:59:22 +000046int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
47{
48 info->so_timestamping =
49 SOF_TIMESTAMPING_TX_SOFTWARE |
50 SOF_TIMESTAMPING_RX_SOFTWARE |
51 SOF_TIMESTAMPING_SOFTWARE;
52 info->phc_index = -1;
53 return 0;
54}
55EXPORT_SYMBOL(ethtool_op_get_ts_info);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* Handlers for each ethtool command */
58
Michał Mirosław475414f2011-11-15 15:29:55 +000059#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000060
Michał Mirosław9d921542011-11-15 15:29:55 +000061static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
62 [NETIF_F_SG_BIT] = "tx-scatter-gather",
63 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000064 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
65 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
66 [NETIF_F_HIGHDMA_BIT] = "highdma",
67 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090068 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000069
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090070 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
71 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040072 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
73 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
74 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000075 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
76 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
77 [NETIF_F_LLTX_BIT] = "tx-lockless",
78 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
79 [NETIF_F_GRO_BIT] = "rx-gro",
Michael Chanfb1f5f72017-12-16 03:09:40 -050080 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
Michał Mirosław9d921542011-11-15 15:29:55 +000081 [NETIF_F_LRO_BIT] = "rx-lro",
82
83 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000084 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
85 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
Alexander Duyckcbc53e02016-04-10 21:44:51 -040086 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000087 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
88 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000089 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040090 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
Tom Herbert7e133182016-05-18 09:06:10 -070091 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
92 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000093 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040094 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
Alexander Duyck802ab552016-04-10 21:45:03 -040095 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030096 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +020097 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
Willem de Bruijn83aa0252018-04-26 13:42:21 -040098 [NETIF_F_GSO_UDP_L4_BIT] = "tx-udp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000099
100 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -0800101 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +0000102 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
103 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
104 [NETIF_F_RXHASH_BIT] = "rx-hashing",
105 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
106 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
107 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +0000108 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +0000109 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -0800110 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
John Fastabend1c78c642016-02-16 21:17:37 -0800111 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +0200112 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
113 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
Sabrina Dubrocad764a122017-07-21 12:49:28 +0200114 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
Atul Guptae0be6be2018-03-31 21:41:53 +0530115 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
Ilya Lesokhin2342a852018-04-30 10:16:14 +0300116 [NETIF_F_HW_TLS_TX_BIT] = "tls-hw-tx-offload",
Ilya Lesokhin14136562018-07-13 14:33:36 +0300117 [NETIF_F_HW_TLS_RX_BIT] = "tls-hw-rx-offload",
Michał Mirosław9d921542011-11-15 15:29:55 +0000118};
119
Eyal Perry892311f2014-12-02 18:12:10 +0200120static const char
121rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
122 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
123 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800124 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200125};
126
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300127static const char
128tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
129 [ETHTOOL_ID_UNSPEC] = "Unspec",
130 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
131 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
Inbar Karmye1577c12017-11-20 16:14:30 +0200132 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300133};
134
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100135static const char
136phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
137 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100138 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100139};
140
Michał Mirosław5455c692011-02-15 16:59:17 +0000141static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
142{
143 struct ethtool_gfeatures cmd = {
144 .cmd = ETHTOOL_GFEATURES,
145 .size = ETHTOOL_DEV_FEATURE_WORDS,
146 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000147 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000148 u32 __user *sizeaddr;
149 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000150 int i;
151
152 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000153 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000154
155 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000156 features[i].available = (u32)(dev->hw_features >> (32 * i));
157 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
158 features[i].active = (u32)(dev->features >> (32 * i));
159 features[i].never_changed =
160 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000161 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000162
163 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
164 if (get_user(copy_size, sizeaddr))
165 return -EFAULT;
166
167 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
168 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
169
170 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
171 return -EFAULT;
172 useraddr += sizeof(cmd);
173 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
174 return -EFAULT;
175
176 return 0;
177}
178
179static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
180{
181 struct ethtool_sfeatures cmd;
182 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000183 netdev_features_t wanted = 0, valid = 0;
184 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000185
186 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
187 return -EFAULT;
188 useraddr += sizeof(cmd);
189
190 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
191 return -EINVAL;
192
193 if (copy_from_user(features, useraddr, sizeof(features)))
194 return -EFAULT;
195
Michał Mirosław475414f2011-11-15 15:29:55 +0000196 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000197 valid |= (netdev_features_t)features[i].valid << (32 * i);
198 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000199 }
200
201 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000202 return -EINVAL;
203
Michał Mirosław475414f2011-11-15 15:29:55 +0000204 if (valid & ~dev->hw_features) {
205 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000206 ret |= ETHTOOL_F_UNSUPPORTED;
207 }
208
Michał Mirosław475414f2011-11-15 15:29:55 +0000209 dev->wanted_features &= ~valid;
210 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700211 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000212
Michał Mirosław475414f2011-11-15 15:29:55 +0000213 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000214 ret |= ETHTOOL_F_WISH;
215
216 return ret;
217}
218
Michał Mirosław340ae162011-02-15 16:59:16 +0000219static int __ethtool_get_sset_count(struct net_device *dev, int sset)
220{
221 const struct ethtool_ops *ops = dev->ethtool_ops;
222
Michał Mirosław5455c692011-02-15 16:59:17 +0000223 if (sset == ETH_SS_FEATURES)
224 return ARRAY_SIZE(netdev_features_strings);
225
Eyal Perry892311f2014-12-02 18:12:10 +0200226 if (sset == ETH_SS_RSS_HASH_FUNCS)
227 return ARRAY_SIZE(rss_hash_func_strings);
228
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300229 if (sset == ETH_SS_TUNABLES)
230 return ARRAY_SIZE(tunable_strings);
231
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100232 if (sset == ETH_SS_PHY_TUNABLES)
233 return ARRAY_SIZE(phy_tunable_strings);
234
Florian Fainelli99943382018-04-25 12:12:48 -0700235 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
236 !ops->get_ethtool_phy_stats)
237 return phy_ethtool_get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100238
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000239 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000240 return ops->get_sset_count(dev, sset);
241 else
242 return -EOPNOTSUPP;
243}
244
245static void __ethtool_get_strings(struct net_device *dev,
246 u32 stringset, u8 *data)
247{
248 const struct ethtool_ops *ops = dev->ethtool_ops;
249
Michał Mirosław5455c692011-02-15 16:59:17 +0000250 if (stringset == ETH_SS_FEATURES)
251 memcpy(data, netdev_features_strings,
252 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200253 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
254 memcpy(data, rss_hash_func_strings,
255 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300256 else if (stringset == ETH_SS_TUNABLES)
257 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100258 else if (stringset == ETH_SS_PHY_TUNABLES)
259 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Florian Fainelli99943382018-04-25 12:12:48 -0700260 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
261 !ops->get_ethtool_phy_stats)
262 phy_ethtool_get_strings(dev->phydev, data);
263 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
Michal Kubecek9b300492018-08-28 19:56:58 +0200545/* Internal kernel helper to query a device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800546int __ethtool_get_link_ksettings(struct net_device *dev,
547 struct ethtool_link_ksettings *link_ksettings)
548{
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800549 ASSERT_RTNL();
550
Michal Kubecek9b300492018-08-28 19:56:58 +0200551 if (!dev->ethtool_ops->get_link_ksettings)
David Decotigny3237fc62016-02-24 10:58:11 -0800552 return -EOPNOTSUPP;
553
Michal Kubecek9b300492018-08-28 19:56:58 +0200554 memset(link_ksettings, 0, sizeof(*link_ksettings));
555 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800556}
557EXPORT_SYMBOL(__ethtool_get_link_ksettings);
558
559/* convert ethtool_link_usettings in user space to a kernel internal
560 * ethtool_link_ksettings. return 0 on success, errno on error.
561 */
562static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
563 const void __user *from)
564{
565 struct ethtool_link_usettings link_usettings;
566
567 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
568 return -EFAULT;
569
570 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800571 bitmap_from_arr32(to->link_modes.supported,
572 link_usettings.link_modes.supported,
573 __ETHTOOL_LINK_MODE_MASK_NBITS);
574 bitmap_from_arr32(to->link_modes.advertising,
575 link_usettings.link_modes.advertising,
576 __ETHTOOL_LINK_MODE_MASK_NBITS);
577 bitmap_from_arr32(to->link_modes.lp_advertising,
578 link_usettings.link_modes.lp_advertising,
579 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800580
581 return 0;
582}
583
584/* convert a kernel internal ethtool_link_ksettings to
585 * ethtool_link_usettings in user space. return 0 on success, errno on
586 * error.
587 */
588static int
589store_link_ksettings_for_user(void __user *to,
590 const struct ethtool_link_ksettings *from)
591{
592 struct ethtool_link_usettings link_usettings;
593
594 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800595 bitmap_to_arr32(link_usettings.link_modes.supported,
596 from->link_modes.supported,
597 __ETHTOOL_LINK_MODE_MASK_NBITS);
598 bitmap_to_arr32(link_usettings.link_modes.advertising,
599 from->link_modes.advertising,
600 __ETHTOOL_LINK_MODE_MASK_NBITS);
601 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
602 from->link_modes.lp_advertising,
603 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800604
605 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
606 return -EFAULT;
607
608 return 0;
609}
610
Michal Kubecek9b300492018-08-28 19:56:58 +0200611/* Query device for its ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800612static int ethtool_get_link_ksettings(struct net_device *dev,
613 void __user *useraddr)
614{
615 int err = 0;
616 struct ethtool_link_ksettings link_ksettings;
617
618 ASSERT_RTNL();
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800619 if (!dev->ethtool_ops->get_link_ksettings)
620 return -EOPNOTSUPP;
621
622 /* handle bitmap nbits handshake */
623 if (copy_from_user(&link_ksettings.base, useraddr,
624 sizeof(link_ksettings.base)))
625 return -EFAULT;
626
627 if (__ETHTOOL_LINK_MODE_MASK_NU32
628 != link_ksettings.base.link_mode_masks_nwords) {
629 /* wrong link mode nbits requested */
630 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000631 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800632 /* send back number of words required as negative val */
633 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
634 "need too many bits for link modes!");
635 link_ksettings.base.link_mode_masks_nwords
636 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
637
638 /* copy the base fields back to user, not the link
639 * mode bitmaps
640 */
641 if (copy_to_user(useraddr, &link_ksettings.base,
642 sizeof(link_ksettings.base)))
643 return -EFAULT;
644
645 return 0;
646 }
647
648 /* handshake successful: user/kernel agree on
649 * link_mode_masks_nwords
650 */
651
652 memset(&link_ksettings, 0, sizeof(link_ksettings));
653 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
654 if (err < 0)
655 return err;
656
657 /* make sure we tell the right values to user */
658 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
659 link_ksettings.base.link_mode_masks_nwords
660 = __ETHTOOL_LINK_MODE_MASK_NU32;
661
662 return store_link_ksettings_for_user(useraddr, &link_ksettings);
663}
664
Michal Kubecek9b300492018-08-28 19:56:58 +0200665/* Update device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800666static int ethtool_set_link_ksettings(struct net_device *dev,
667 void __user *useraddr)
668{
669 int err;
670 struct ethtool_link_ksettings link_ksettings;
671
672 ASSERT_RTNL();
673
674 if (!dev->ethtool_ops->set_link_ksettings)
675 return -EOPNOTSUPP;
676
677 /* make sure nbits field has expected value */
678 if (copy_from_user(&link_ksettings.base, useraddr,
679 sizeof(link_ksettings.base)))
680 return -EFAULT;
681
682 if (__ETHTOOL_LINK_MODE_MASK_NU32
683 != link_ksettings.base.link_mode_masks_nwords)
684 return -EINVAL;
685
686 /* copy the whole structure, now that we know it has expected
687 * format
688 */
689 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
690 if (err)
691 return err;
692
693 /* re-check nwords field, just in case */
694 if (__ETHTOOL_LINK_MODE_MASK_NU32
695 != link_ksettings.base.link_mode_masks_nwords)
696 return -EINVAL;
697
698 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
699}
700
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800701/* Query device for its ethtool_cmd settings.
702 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200703 * Backward compatibility note: for compatibility with legacy ethtool, this is
704 * now implemented via get_link_ksettings. When driver reports higher link mode
705 * bits, a kernel warning is logged once (with name of 1st driver/device) to
706 * recommend user to upgrade ethtool, but the command is successful (only the
707 * lower link mode bits reported back to user). Deprecated fields from
708 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800709 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000710static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
711{
Michal Kubecek9b300492018-08-28 19:56:58 +0200712 struct ethtool_link_ksettings link_ksettings;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000713 struct ethtool_cmd cmd;
Michal Kubecek9b300492018-08-28 19:56:58 +0200714 int err;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000715
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800716 ASSERT_RTNL();
Michal Kubecek9b300492018-08-28 19:56:58 +0200717 if (!dev->ethtool_ops->get_link_ksettings)
718 return -EOPNOTSUPP;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800719
Michal Kubecek9b300492018-08-28 19:56:58 +0200720 memset(&link_ksettings, 0, sizeof(link_ksettings));
721 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
722 if (err < 0)
723 return err;
724 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800725
Michal Kubecek9b300492018-08-28 19:56:58 +0200726 /* send a sensible cmd tag back to user */
727 cmd.cmd = ETHTOOL_GSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
730 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 0;
733}
734
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800735/* Update device link settings with given ethtool_cmd.
736 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200737 * Backward compatibility note: for compatibility with legacy ethtool, this is
738 * now always implemented via set_link_settings. When user's request updates
739 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
740 * warning is logged once (with name of 1st driver/device) to recommend user to
741 * upgrade ethtool, and the request is rejected.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
744{
Michal Kubecek9b300492018-08-28 19:56:58 +0200745 struct ethtool_link_ksettings link_ksettings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct ethtool_cmd cmd;
747
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800748 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
751 return -EFAULT;
Michal Kubecek9b300492018-08-28 19:56:58 +0200752 if (!dev->ethtool_ops->set_link_ksettings)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800753 return -EOPNOTSUPP;
754
Michal Kubecek9b300492018-08-28 19:56:58 +0200755 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
756 return -EINVAL;
757 link_ksettings.base.link_mode_masks_nwords =
758 __ETHTOOL_LINK_MODE_MASK_NU32;
759 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
chavey97f8aef2010-04-07 21:54:42 -0700762static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
763 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700766 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 memset(&info, 0, sizeof(info));
769 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000770 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700771 ops->get_drvinfo(dev, &info);
772 } else if (dev->dev.parent && dev->dev.parent->driver) {
773 strlcpy(info.bus_info, dev_name(dev->dev.parent),
774 sizeof(info.bus_info));
775 strlcpy(info.driver, dev->dev.parent->driver->name,
776 sizeof(info.driver));
777 } else {
778 return -EOPNOTSUPP;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Jeff Garzik723b2f52010-03-03 22:51:50 +0000781 /*
782 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000783 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000784 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000785 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700786 int rc;
787
788 rc = ops->get_sset_count(dev, ETH_SS_TEST);
789 if (rc >= 0)
790 info.testinfo_len = rc;
791 rc = ops->get_sset_count(dev, ETH_SS_STATS);
792 if (rc >= 0)
793 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700794 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
795 if (rc >= 0)
796 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700797 }
Yunsheng Linf9fc54d2018-12-26 19:51:46 +0800798 if (ops->get_regs_len) {
799 int ret = ops->get_regs_len(dev);
800
801 if (ret > 0)
802 info.regdump_len = ret;
803 }
804
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000805 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 info.eedump_len = ops->get_eeprom_len(dev);
807
Jakub Kicinskiddb6e992019-01-31 10:50:47 -0800808 rtnl_unlock();
809 if (!info.fw_version[0])
810 devlink_compat_running_version(dev, info.fw_version,
811 sizeof(info.fw_version));
812 rtnl_lock();
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (copy_to_user(useraddr, &info, sizeof(info)))
815 return -EFAULT;
816 return 0;
817}
818
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800819static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700820 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000821{
822 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000823 u64 sset_mask;
824 int i, idx = 0, n_bits = 0, ret, rc;
825 u32 *info_buf = NULL;
826
Jeff Garzik723b2f52010-03-03 22:51:50 +0000827 if (copy_from_user(&info, useraddr, sizeof(info)))
828 return -EFAULT;
829
830 /* store copy of mask, because we zero struct later on */
831 sset_mask = info.sset_mask;
832 if (!sset_mask)
833 return 0;
834
835 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000836 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000837
838 memset(&info, 0, sizeof(info));
839 info.cmd = ETHTOOL_GSSET_INFO;
840
Kees Cook6396bb22018-06-12 14:03:40 -0700841 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000842 if (!info_buf)
843 return -ENOMEM;
844
845 /*
846 * fill return buffer based on input bitmask and successful
847 * get_sset_count return
848 */
849 for (i = 0; i < 64; i++) {
850 if (!(sset_mask & (1ULL << i)))
851 continue;
852
Michał Mirosław340ae162011-02-15 16:59:16 +0000853 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000854 if (rc >= 0) {
855 info.sset_mask |= (1ULL << i);
856 info_buf[idx++] = rc;
857 }
858 }
859
860 ret = -EFAULT;
861 if (copy_to_user(useraddr, &info, sizeof(info)))
862 goto out;
863
864 useraddr += offsetof(struct ethtool_sset_info, data);
865 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
866 goto out;
867
868 ret = 0;
869
870out:
871 kfree(info_buf);
872 return ret;
873}
874
chavey97f8aef2010-04-07 21:54:42 -0700875static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000876 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700877{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000878 struct ethtool_rxnfc info;
879 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000880 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700881
Santwona Behera59089d82009-02-20 00:58:13 -0800882 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700883 return -EOPNOTSUPP;
884
Ben Hutchingsbf988432010-06-28 08:45:58 +0000885 /* struct ethtool_rxnfc was originally defined for
886 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
887 * members. User-space might still be using that
888 * definition. */
889 if (cmd == ETHTOOL_SRXFH)
890 info_size = (offsetof(struct ethtool_rxnfc, data) +
891 sizeof(info.data));
892
893 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700894 return -EFAULT;
895
Ben Hutchings55664f32012-01-03 12:04:51 +0000896 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
897 if (rc)
898 return rc;
899
900 if (cmd == ETHTOOL_SRXCLSRLINS &&
901 copy_to_user(useraddr, &info, info_size))
902 return -EFAULT;
903
904 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700905}
906
chavey97f8aef2010-04-07 21:54:42 -0700907static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000908 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700909{
910 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000911 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800912 const struct ethtool_ops *ops = dev->ethtool_ops;
913 int ret;
914 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700915
Santwona Behera59089d82009-02-20 00:58:13 -0800916 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700917 return -EOPNOTSUPP;
918
Ben Hutchingsbf988432010-06-28 08:45:58 +0000919 /* struct ethtool_rxnfc was originally defined for
920 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
921 * members. User-space might still be using that
922 * definition. */
923 if (cmd == ETHTOOL_GRXFH)
924 info_size = (offsetof(struct ethtool_rxnfc, data) +
925 sizeof(info.data));
926
927 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700928 return -EFAULT;
929
Edward Cree84a1d9c42018-03-08 15:45:03 +0000930 /* If FLOW_RSS was requested then user-space must be using the
931 * new definition, as FLOW_RSS is newer.
932 */
933 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
934 info_size = sizeof(info);
935 if (copy_from_user(&info, useraddr, info_size))
936 return -EFAULT;
Wenwen Wangd656fe42018-04-30 12:31:13 -0500937 /* Since malicious users may modify the original data,
938 * we need to check whether FLOW_RSS is still requested.
939 */
940 if (!(info.flow_type & FLOW_RSS))
941 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +0000942 }
943
Wenwen Wang2bb32072018-10-09 08:15:38 -0500944 if (info.cmd != cmd)
945 return -EINVAL;
946
Santwona Behera59089d82009-02-20 00:58:13 -0800947 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
948 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000949 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cook6396bb22018-06-12 14:03:40 -0700950 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000951 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800952 if (!rule_buf)
953 return -ENOMEM;
954 }
955 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700956
Santwona Behera59089d82009-02-20 00:58:13 -0800957 ret = ops->get_rxnfc(dev, &info, rule_buf);
958 if (ret < 0)
959 goto err_out;
960
961 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000962 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800963 goto err_out;
964
965 if (rule_buf) {
966 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
967 if (copy_to_user(useraddr, rule_buf,
968 info.rule_cnt * sizeof(u32)))
969 goto err_out;
970 }
971 ret = 0;
972
973err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700974 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800975
976 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700977}
978
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530979static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
980 struct ethtool_rxnfc *rx_rings,
981 u32 size)
982{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100983 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530984
985 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100986 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530987
988 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100989 for (i = 0; i < size; i++)
990 if (indir[i] >= rx_rings->data)
991 return -EINVAL;
992
993 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530994}
995
Kim Jonesba905f52016-02-02 03:51:16 +0000996u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -0800997
998void netdev_rss_key_fill(void *buffer, size_t len)
999{
1000 BUG_ON(len > sizeof(netdev_rss_key));
1001 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1002 memcpy(buffer, netdev_rss_key, len);
1003}
1004EXPORT_SYMBOL(netdev_rss_key_fill);
1005
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001006static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1007{
1008 u32 dev_size, current_max = 0;
1009 u32 *indir;
1010 int ret;
1011
1012 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1013 !dev->ethtool_ops->get_rxfh)
1014 return -EOPNOTSUPP;
1015 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1016 if (dev_size == 0)
1017 return -EOPNOTSUPP;
1018
1019 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1020 if (!indir)
1021 return -ENOMEM;
1022
1023 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1024 if (ret)
1025 goto out;
1026
1027 while (dev_size--)
1028 current_max = max(current_max, indir[dev_size]);
1029
1030 *max = current_max;
1031
1032out:
1033 kfree(indir);
1034 return ret;
1035}
1036
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001037static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1038 void __user *useraddr)
1039{
Ben Hutchings7850f632011-12-15 13:55:01 +00001040 u32 user_size, dev_size;
1041 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001042 int ret;
1043
Ben Hutchings7850f632011-12-15 13:55:01 +00001044 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001045 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001046 return -EOPNOTSUPP;
1047 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1048 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001049 return -EOPNOTSUPP;
1050
Ben Hutchings7850f632011-12-15 13:55:01 +00001051 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001052 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001053 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001054 return -EFAULT;
1055
Ben Hutchings7850f632011-12-15 13:55:01 +00001056 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1057 &dev_size, sizeof(dev_size)))
1058 return -EFAULT;
1059
1060 /* If the user buffer size is 0, this is just a query for the
1061 * device table size. Otherwise, if it's smaller than the
1062 * device table size it's an error.
1063 */
1064 if (user_size < dev_size)
1065 return user_size == 0 ? 0 : -EINVAL;
1066
1067 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001068 if (!indir)
1069 return -ENOMEM;
1070
Eyal Perry892311f2014-12-02 18:12:10 +02001071 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001072 if (ret)
1073 goto out;
1074
Ben Hutchings7850f632011-12-15 13:55:01 +00001075 if (copy_to_user(useraddr +
1076 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1077 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001078 ret = -EFAULT;
1079
1080out:
1081 kfree(indir);
1082 return ret;
1083}
1084
1085static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1086 void __user *useraddr)
1087{
Ben Hutchings7850f632011-12-15 13:55:01 +00001088 struct ethtool_rxnfc rx_rings;
1089 u32 user_size, dev_size, i;
1090 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001091 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001092 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301093 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001094
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001095 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001096 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001097 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001098
1099 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001100 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001101 return -EOPNOTSUPP;
1102
Ben Hutchings7850f632011-12-15 13:55:01 +00001103 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001104 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001105 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001106 return -EFAULT;
1107
Ben Hutchings278bc422011-12-15 13:56:49 +00001108 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001109 return -EINVAL;
1110
1111 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001112 if (!indir)
1113 return -ENOMEM;
1114
Ben Hutchings7850f632011-12-15 13:55:01 +00001115 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001116 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001117 if (ret)
1118 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001119
1120 if (user_size == 0) {
1121 for (i = 0; i < dev_size; i++)
1122 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1123 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301124 ret = ethtool_copy_validate_indir(indir,
1125 useraddr + ringidx_offset,
1126 &rx_rings,
1127 dev_size);
1128 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001129 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001130 }
1131
Eyal Perry892311f2014-12-02 18:12:10 +02001132 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001133 if (ret)
1134 goto out;
1135
1136 /* indicate whether rxfh was set to default */
1137 if (user_size == 0)
1138 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1139 else
1140 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001141
1142out:
1143 kfree(indir);
1144 return ret;
1145}
1146
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301147static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1148 void __user *useraddr)
1149{
1150 int ret;
1151 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001152 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301153 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001154 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301155 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001156 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301157 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001158 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301159 u8 *hkey = NULL;
1160 u8 *rss_config;
1161
Eyal Perry892311f2014-12-02 18:12:10 +02001162 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301163 return -EOPNOTSUPP;
1164
1165 if (ops->get_rxfh_indir_size)
1166 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301167 if (ops->get_rxfh_key_size)
1168 dev_key_size = ops->get_rxfh_key_size(dev);
1169
Ben Hutchingsf062a382014-05-15 16:28:07 +01001170 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301171 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001172 user_indir_size = rxfh.indir_size;
1173 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301174
Ben Hutchingsf062a382014-05-15 16:28:07 +01001175 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001176 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001177 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001178 /* Most drivers don't handle rss_context, check it's 0 as well */
1179 if (rxfh.rss_context && !ops->get_rxfh_context)
1180 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001181
1182 rxfh.indir_size = dev_indir_size;
1183 rxfh.key_size = dev_key_size;
1184 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301185 return -EFAULT;
1186
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301187 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1188 (user_key_size && (user_key_size != dev_key_size)))
1189 return -EINVAL;
1190
1191 indir_bytes = user_indir_size * sizeof(indir[0]);
1192 total_size = indir_bytes + user_key_size;
1193 rss_config = kzalloc(total_size, GFP_USER);
1194 if (!rss_config)
1195 return -ENOMEM;
1196
1197 if (user_indir_size)
1198 indir = (u32 *)rss_config;
1199
1200 if (user_key_size)
1201 hkey = rss_config + indir_bytes;
1202
Edward Cree84a1d9c42018-03-08 15:45:03 +00001203 if (rxfh.rss_context)
1204 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1205 &dev_hfunc,
1206 rxfh.rss_context);
1207 else
1208 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001209 if (ret)
1210 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301211
Eyal Perry892311f2014-12-02 18:12:10 +02001212 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1213 &dev_hfunc, sizeof(rxfh.hfunc))) {
1214 ret = -EFAULT;
1215 } else if (copy_to_user(useraddr +
1216 offsetof(struct ethtool_rxfh, rss_config[0]),
1217 rss_config, total_size)) {
1218 ret = -EFAULT;
1219 }
1220out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301221 kfree(rss_config);
1222
1223 return ret;
1224}
1225
1226static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1227 void __user *useraddr)
1228{
1229 int ret;
1230 const struct ethtool_ops *ops = dev->ethtool_ops;
1231 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001232 struct ethtool_rxfh rxfh;
1233 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301234 u32 *indir = NULL, indir_bytes = 0;
1235 u8 *hkey = NULL;
1236 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301237 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001238 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301239
Eyal Perry892311f2014-12-02 18:12:10 +02001240 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301241 return -EOPNOTSUPP;
1242
1243 if (ops->get_rxfh_indir_size)
1244 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301245 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001246 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301247
Ben Hutchingsf062a382014-05-15 16:28:07 +01001248 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301249 return -EFAULT;
1250
Ben Hutchingsf062a382014-05-15 16:28:07 +01001251 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001252 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001253 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001254 /* Most drivers don't handle rss_context, check it's 0 as well */
1255 if (rxfh.rss_context && !ops->set_rxfh_context)
1256 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001257
Eyal Perry892311f2014-12-02 18:12:10 +02001258 /* If either indir, hash key or function is valid, proceed further.
1259 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301260 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001261 if ((rxfh.indir_size &&
1262 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1263 rxfh.indir_size != dev_indir_size) ||
1264 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1265 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001266 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301267 return -EINVAL;
1268
Ben Hutchingsf062a382014-05-15 16:28:07 +01001269 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301270 indir_bytes = dev_indir_size * sizeof(indir[0]);
1271
Ben Hutchingsf062a382014-05-15 16:28:07 +01001272 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301273 if (!rss_config)
1274 return -ENOMEM;
1275
1276 rx_rings.cmd = ETHTOOL_GRXRINGS;
1277 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1278 if (ret)
1279 goto out;
1280
Edward Cree84a1d9c42018-03-08 15:45:03 +00001281 /* rxfh.indir_size == 0 means reset the indir table to default (master
1282 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001283 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301284 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001285 if (rxfh.indir_size &&
1286 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301287 indir = (u32 *)rss_config;
1288 ret = ethtool_copy_validate_indir(indir,
1289 useraddr + rss_cfg_offset,
1290 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001291 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301292 if (ret)
1293 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001294 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001295 if (rxfh.rss_context == 0) {
1296 indir = (u32 *)rss_config;
1297 for (i = 0; i < dev_indir_size; i++)
1298 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1299 } else {
1300 delete = true;
1301 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301302 }
1303
Ben Hutchingsf062a382014-05-15 16:28:07 +01001304 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301305 hkey = rss_config + indir_bytes;
1306 if (copy_from_user(hkey,
1307 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001308 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301309 ret = -EFAULT;
1310 goto out;
1311 }
1312 }
1313
Edward Cree84a1d9c42018-03-08 15:45:03 +00001314 if (rxfh.rss_context)
1315 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1316 &rxfh.rss_context, delete);
1317 else
1318 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001319 if (ret)
1320 goto out;
1321
Edward Cree84a1d9c42018-03-08 15:45:03 +00001322 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1323 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1324 ret = -EFAULT;
1325
1326 if (!rxfh.rss_context) {
1327 /* indicate whether rxfh was set to default */
1328 if (rxfh.indir_size == 0)
1329 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1330 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1331 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1332 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301333
1334out:
1335 kfree(rss_config);
1336 return ret;
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1340{
1341 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001342 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 void *regbuf;
1344 int reglen, ret;
1345
1346 if (!ops->get_regs || !ops->get_regs_len)
1347 return -EOPNOTSUPP;
1348
1349 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1350 return -EFAULT;
1351
1352 reglen = ops->get_regs_len(dev);
Yunsheng Linf9fc54d2018-12-26 19:51:46 +08001353 if (reglen <= 0)
1354 return reglen;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (regs.len > reglen)
1357 regs.len = reglen;
1358
Dan Carpenteref76c772019-02-01 11:24:06 +03001359 regbuf = vzalloc(reglen);
1360 if (!regbuf)
1361 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 ops->get_regs(dev, &regs, regbuf);
1364
1365 ret = -EFAULT;
1366 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1367 goto out;
1368 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001369 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 goto out;
1371 ret = 0;
1372
1373 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001374 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return ret;
1376}
1377
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001378static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1379{
1380 struct ethtool_value reset;
1381 int ret;
1382
1383 if (!dev->ethtool_ops->reset)
1384 return -EOPNOTSUPP;
1385
1386 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1387 return -EFAULT;
1388
1389 ret = dev->ethtool_ops->reset(dev, &reset.data);
1390 if (ret)
1391 return ret;
1392
1393 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1394 return -EFAULT;
1395 return 0;
1396}
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1399{
Roland Dreier8e557422010-02-11 12:14:23 -08001400 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if (!dev->ethtool_ops->get_wol)
1403 return -EOPNOTSUPP;
1404
1405 dev->ethtool_ops->get_wol(dev, &wol);
1406
1407 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1408 return -EFAULT;
1409 return 0;
1410}
1411
1412static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1413{
1414 struct ethtool_wolinfo wol;
Heiner Kallweit61941142018-09-24 21:58:59 +02001415 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (!dev->ethtool_ops->set_wol)
1418 return -EOPNOTSUPP;
1419
1420 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1421 return -EFAULT;
1422
Heiner Kallweit61941142018-09-24 21:58:59 +02001423 ret = dev->ethtool_ops->set_wol(dev, &wol);
1424 if (ret)
1425 return ret;
1426
1427 dev->wol_enabled = !!wol.wolopts;
1428
1429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001432static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1433{
1434 struct ethtool_eee edata;
1435 int rc;
1436
1437 if (!dev->ethtool_ops->get_eee)
1438 return -EOPNOTSUPP;
1439
1440 memset(&edata, 0, sizeof(struct ethtool_eee));
1441 edata.cmd = ETHTOOL_GEEE;
1442 rc = dev->ethtool_ops->get_eee(dev, &edata);
1443
1444 if (rc)
1445 return rc;
1446
1447 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1448 return -EFAULT;
1449
1450 return 0;
1451}
1452
1453static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1454{
1455 struct ethtool_eee edata;
1456
1457 if (!dev->ethtool_ops->set_eee)
1458 return -EOPNOTSUPP;
1459
1460 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1461 return -EFAULT;
1462
1463 return dev->ethtool_ops->set_eee(dev, &edata);
1464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466static int ethtool_nway_reset(struct net_device *dev)
1467{
1468 if (!dev->ethtool_ops->nway_reset)
1469 return -EOPNOTSUPP;
1470
1471 return dev->ethtool_ops->nway_reset(dev);
1472}
1473
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001474static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1475{
1476 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1477
1478 if (!dev->ethtool_ops->get_link)
1479 return -EOPNOTSUPP;
1480
1481 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1482
1483 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1484 return -EFAULT;
1485 return 0;
1486}
1487
Ben Hutchings081d0942012-04-12 00:42:12 +00001488static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1489 int (*getter)(struct net_device *,
1490 struct ethtool_eeprom *, u8 *),
1491 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001494 void __user *userbuf = useraddr + sizeof(eeprom);
1495 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001497 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1500 return -EFAULT;
1501
1502 /* Check for wrap and zero */
1503 if (eeprom.offset + eeprom.len <= eeprom.offset)
1504 return -EINVAL;
1505
1506 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001507 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EINVAL;
1509
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001510 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (!data)
1512 return -ENOMEM;
1513
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001514 bytes_remaining = eeprom.len;
1515 while (bytes_remaining > 0) {
1516 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Ben Hutchings081d0942012-04-12 00:42:12 +00001518 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001519 if (ret)
1520 break;
1521 if (copy_to_user(userbuf, data, eeprom.len)) {
1522 ret = -EFAULT;
1523 break;
1524 }
1525 userbuf += eeprom.len;
1526 eeprom.offset += eeprom.len;
1527 bytes_remaining -= eeprom.len;
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001530 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1531 eeprom.offset -= eeprom.len;
1532 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1533 ret = -EFAULT;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 kfree(data);
1536 return ret;
1537}
1538
Ben Hutchings081d0942012-04-12 00:42:12 +00001539static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1540{
1541 const struct ethtool_ops *ops = dev->ethtool_ops;
1542
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001543 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1544 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001545 return -EOPNOTSUPP;
1546
1547 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1548 ops->get_eeprom_len(dev));
1549}
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1552{
1553 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001554 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001555 void __user *userbuf = useraddr + sizeof(eeprom);
1556 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001558 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001560 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1561 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return -EOPNOTSUPP;
1563
1564 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1565 return -EFAULT;
1566
1567 /* Check for wrap and zero */
1568 if (eeprom.offset + eeprom.len <= eeprom.offset)
1569 return -EINVAL;
1570
1571 /* Check for exceeding total eeprom len */
1572 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1573 return -EINVAL;
1574
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001575 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (!data)
1577 return -ENOMEM;
1578
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001579 bytes_remaining = eeprom.len;
1580 while (bytes_remaining > 0) {
1581 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001583 if (copy_from_user(data, userbuf, eeprom.len)) {
1584 ret = -EFAULT;
1585 break;
1586 }
1587 ret = ops->set_eeprom(dev, &eeprom, data);
1588 if (ret)
1589 break;
1590 userbuf += eeprom.len;
1591 eeprom.offset += eeprom.len;
1592 bytes_remaining -= eeprom.len;
1593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 kfree(data);
1596 return ret;
1597}
1598
chavey97f8aef2010-04-07 21:54:42 -07001599static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1600 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Roland Dreier8e557422010-02-11 12:14:23 -08001602 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 if (!dev->ethtool_ops->get_coalesce)
1605 return -EOPNOTSUPP;
1606
1607 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1608
1609 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1610 return -EFAULT;
1611 return 0;
1612}
1613
chavey97f8aef2010-04-07 21:54:42 -07001614static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1615 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct ethtool_coalesce coalesce;
1618
David S. Millerfa04ae52005-06-06 15:07:19 -07001619 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return -EOPNOTSUPP;
1621
1622 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1623 return -EFAULT;
1624
1625 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1626}
1627
1628static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1629{
Roland Dreier8e557422010-02-11 12:14:23 -08001630 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 if (!dev->ethtool_ops->get_ringparam)
1633 return -EOPNOTSUPP;
1634
1635 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1636
1637 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1638 return -EFAULT;
1639 return 0;
1640}
1641
1642static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1643{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001644 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001646 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return -EOPNOTSUPP;
1648
1649 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1650 return -EFAULT;
1651
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001652 dev->ethtool_ops->get_ringparam(dev, &max);
1653
1654 /* ensure new ring parameters are within the maximums */
1655 if (ringparam.rx_pending > max.rx_max_pending ||
1656 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1657 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1658 ringparam.tx_pending > max.tx_max_pending)
1659 return -EINVAL;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1662}
1663
amit salecha8b5933c2011-04-07 01:58:42 +00001664static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1665 void __user *useraddr)
1666{
1667 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1668
1669 if (!dev->ethtool_ops->get_channels)
1670 return -EOPNOTSUPP;
1671
1672 dev->ethtool_ops->get_channels(dev, &channels);
1673
1674 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1675 return -EFAULT;
1676 return 0;
1677}
1678
1679static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1680 void __user *useraddr)
1681{
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001682 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
Jakub Kicinski1661d342018-10-01 14:51:36 +02001683 u16 from_channel, to_channel;
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001684 u32 max_rx_in_use = 0;
Jakub Kicinski1661d342018-10-01 14:51:36 +02001685 unsigned int i;
amit salecha8b5933c2011-04-07 01:58:42 +00001686
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001687 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001688 return -EOPNOTSUPP;
1689
1690 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1691 return -EFAULT;
1692
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001693 dev->ethtool_ops->get_channels(dev, &curr);
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001694
1695 /* ensure new counts are within the maximums */
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001696 if (channels.rx_count > curr.max_rx ||
1697 channels.tx_count > curr.max_tx ||
1698 channels.combined_count > curr.max_combined ||
1699 channels.other_count > curr.max_other)
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001700 return -EINVAL;
1701
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001702 /* ensure the new Rx count fits within the configured Rx flow
1703 * indirection table settings */
1704 if (netif_is_rxfh_configured(dev) &&
1705 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1706 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1707 return -EINVAL;
1708
Jakub Kicinski1661d342018-10-01 14:51:36 +02001709 /* Disabling channels, query zero-copy AF_XDP sockets */
1710 from_channel = channels.combined_count +
1711 min(channels.rx_count, channels.tx_count);
1712 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1713 for (i = from_channel; i < to_channel; i++)
1714 if (xdp_get_umem_from_qid(dev, i))
1715 return -EINVAL;
1716
amit salecha8b5933c2011-04-07 01:58:42 +00001717 return dev->ethtool_ops->set_channels(dev, &channels);
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1721{
1722 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1723
1724 if (!dev->ethtool_ops->get_pauseparam)
1725 return -EOPNOTSUPP;
1726
1727 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1728
1729 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1730 return -EFAULT;
1731 return 0;
1732}
1733
1734static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1735{
1736 struct ethtool_pauseparam pauseparam;
1737
Jeff Garzike1b90c42006-07-17 12:54:40 -04001738 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -EOPNOTSUPP;
1740
1741 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1742 return -EFAULT;
1743
1744 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1748{
1749 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001750 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001752 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001754 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001755 return -EOPNOTSUPP;
1756
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001757 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001758 if (test_len < 0)
1759 return test_len;
1760 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 if (copy_from_user(&test, useraddr, sizeof(test)))
1763 return -EFAULT;
1764
Jeff Garzikff03d492007-08-15 16:01:08 -07001765 test.len = test_len;
Kees Cook6da2ec52018-06-12 13:55:00 -07001766 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (!data)
1768 return -ENOMEM;
1769
1770 ops->self_test(dev, &test, data);
1771
1772 ret = -EFAULT;
1773 if (copy_to_user(useraddr, &test, sizeof(test)))
1774 goto out;
1775 useraddr += sizeof(test);
1776 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1777 goto out;
1778 ret = 0;
1779
1780 out:
1781 kfree(data);
1782 return ret;
1783}
1784
1785static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1786{
1787 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 u8 *data;
1789 int ret;
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1792 return -EFAULT;
1793
Michał Mirosław340ae162011-02-15 16:59:16 +00001794 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001795 if (ret < 0)
1796 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001797 if (ret > S32_MAX / ETH_GSTRING_LEN)
1798 return -ENOMEM;
1799 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001800
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001801 gstrings.len = ret;
Kees Cookfad953c2018-06-12 14:27:37 -07001802 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001803 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return -ENOMEM;
1805
Michał Mirosław340ae162011-02-15 16:59:16 +00001806 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 ret = -EFAULT;
1809 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1810 goto out;
1811 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001812 if (gstrings.len &&
1813 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto out;
1815 ret = 0;
1816
Michał Mirosław340ae162011-02-15 16:59:16 +00001817out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001818 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return ret;
1820}
1821
1822static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1823{
1824 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001825 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001826 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001827 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001829 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return -EOPNOTSUPP;
1831
Ben Hutchings68f512f2011-04-02 00:35:15 +01001832 if (busy)
1833 return -EBUSY;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (copy_from_user(&id, useraddr, sizeof(id)))
1836 return -EFAULT;
1837
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001838 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001839 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001840 return rc;
1841
1842 /* Drop the RTNL lock while waiting, but prevent reentry or
1843 * removal of the device.
1844 */
1845 busy = true;
1846 dev_hold(dev);
1847 rtnl_unlock();
1848
1849 if (rc == 0) {
1850 /* Driver will handle this itself */
1851 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001852 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001853 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001854 /* Driver expects to be called at twice the frequency in rc */
1855 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001856
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001857 /* Count down seconds */
1858 do {
1859 /* Count down iterations per second */
1860 i = n;
1861 do {
1862 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001863 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001864 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1865 rtnl_unlock();
1866 if (rc)
1867 break;
1868 schedule_timeout_interruptible(interval);
1869 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001870 } while (!signal_pending(current) &&
1871 (id.data == 0 || --id.data != 0));
1872 }
1873
1874 rtnl_lock();
1875 dev_put(dev);
1876 busy = false;
1877
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001878 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001879 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1883{
1884 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001885 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001887 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001889 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001890 return -EOPNOTSUPP;
1891
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001892 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001893 if (n_stats < 0)
1894 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001895 if (n_stats > S32_MAX / sizeof(u64))
1896 return -ENOMEM;
1897 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1899 return -EFAULT;
1900
Jeff Garzikff03d492007-08-15 16:01:08 -07001901 stats.n_stats = n_stats;
Kees Cookfad953c2018-06-12 14:27:37 -07001902 data = vzalloc(array_size(n_stats, sizeof(u64)));
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001903 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return -ENOMEM;
1905
1906 ops->get_ethtool_stats(dev, &stats, data);
1907
1908 ret = -EFAULT;
1909 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1910 goto out;
1911 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001912 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 goto out;
1914 ret = 0;
1915
1916 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001917 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return ret;
1919}
1920
Andrew Lunnf3a40942015-12-30 16:28:25 +01001921static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1922{
Florian Fainelli99943382018-04-25 12:12:48 -07001923 const struct ethtool_ops *ops = dev->ethtool_ops;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001924 struct phy_device *phydev = dev->phydev;
Florian Fainelli99943382018-04-25 12:12:48 -07001925 struct ethtool_stats stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001926 u64 *data;
1927 int ret, n_stats;
1928
Florian Fainelli99943382018-04-25 12:12:48 -07001929 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
Andrew Lunnf3a40942015-12-30 16:28:25 +01001930 return -EOPNOTSUPP;
1931
Florian Fainelli99943382018-04-25 12:12:48 -07001932 if (dev->phydev && !ops->get_ethtool_phy_stats)
1933 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1934 else
1935 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001936 if (n_stats < 0)
1937 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001938 if (n_stats > S32_MAX / sizeof(u64))
1939 return -ENOMEM;
1940 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001941
1942 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1943 return -EFAULT;
1944
1945 stats.n_stats = n_stats;
Kees Cookfad953c2018-06-12 14:27:37 -07001946 data = vzalloc(array_size(n_stats, sizeof(u64)));
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001947 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01001948 return -ENOMEM;
1949
Florian Fainelli99943382018-04-25 12:12:48 -07001950 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1951 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1952 if (ret < 0)
1953 return ret;
1954 } else {
1955 ops->get_ethtool_phy_stats(dev, &stats, data);
1956 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01001957
1958 ret = -EFAULT;
1959 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1960 goto out;
1961 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001962 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01001963 goto out;
1964 ret = 0;
1965
1966 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001967 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001968 return ret;
1969}
1970
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001971static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001972{
1973 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001974
Matthew Wilcox313674a2007-07-31 14:00:29 -07001975 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001976 return -EFAULT;
1977
Matthew Wilcox313674a2007-07-31 14:00:29 -07001978 if (epaddr.size < dev->addr_len)
1979 return -ETOOSMALL;
1980 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001981
Jon Wetzela6f9a702005-08-20 17:15:54 -07001982 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001983 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001984 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001985 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1986 return -EFAULT;
1987 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001988}
1989
Jeff Garzik13c99b22007-08-15 16:01:56 -07001990static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1991 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001992{
Roland Dreier8e557422010-02-11 12:14:23 -08001993 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001994
Jeff Garzik13c99b22007-08-15 16:01:56 -07001995 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001996 return -EOPNOTSUPP;
1997
Jeff Garzik13c99b22007-08-15 16:01:56 -07001998 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001999
2000 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2001 return -EFAULT;
2002 return 0;
2003}
2004
Jeff Garzik13c99b22007-08-15 16:01:56 -07002005static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2006 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002007{
2008 struct ethtool_value edata;
2009
Jeff Garzik13c99b22007-08-15 16:01:56 -07002010 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002011 return -EOPNOTSUPP;
2012
2013 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2014 return -EFAULT;
2015
Jeff Garzik13c99b22007-08-15 16:01:56 -07002016 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002017 return 0;
2018}
2019
Jeff Garzik13c99b22007-08-15 16:01:56 -07002020static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2021 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002022{
2023 struct ethtool_value edata;
2024
Jeff Garzik13c99b22007-08-15 16:01:56 -07002025 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002026 return -EOPNOTSUPP;
2027
2028 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2029 return -EFAULT;
2030
Jeff Garzik13c99b22007-08-15 16:01:56 -07002031 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002032}
2033
chavey97f8aef2010-04-07 21:54:42 -07002034static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2035 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002036{
2037 struct ethtool_flash efl;
2038
2039 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2040 return -EFAULT;
Ben Hutchings786f5282012-02-01 09:32:25 +00002041 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2042
Jakub Kicinski4eceba12019-02-14 13:40:45 -08002043 if (!dev->ethtool_ops->flash_device) {
2044 int ret;
2045
2046 rtnl_unlock();
2047 ret = devlink_compat_flash_update(dev, efl.data);
2048 rtnl_lock();
2049
2050 return ret;
2051 }
2052
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002053 return dev->ethtool_ops->flash_device(dev, &efl);
2054}
2055
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002056static int ethtool_set_dump(struct net_device *dev,
2057 void __user *useraddr)
2058{
2059 struct ethtool_dump dump;
2060
2061 if (!dev->ethtool_ops->set_dump)
2062 return -EOPNOTSUPP;
2063
2064 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2065 return -EFAULT;
2066
2067 return dev->ethtool_ops->set_dump(dev, &dump);
2068}
2069
2070static int ethtool_get_dump_flag(struct net_device *dev,
2071 void __user *useraddr)
2072{
2073 int ret;
2074 struct ethtool_dump dump;
2075 const struct ethtool_ops *ops = dev->ethtool_ops;
2076
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002077 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002078 return -EOPNOTSUPP;
2079
2080 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2081 return -EFAULT;
2082
2083 ret = ops->get_dump_flag(dev, &dump);
2084 if (ret)
2085 return ret;
2086
2087 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2088 return -EFAULT;
2089 return 0;
2090}
2091
2092static int ethtool_get_dump_data(struct net_device *dev,
2093 void __user *useraddr)
2094{
2095 int ret;
2096 __u32 len;
2097 struct ethtool_dump dump, tmp;
2098 const struct ethtool_ops *ops = dev->ethtool_ops;
2099 void *data = NULL;
2100
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002101 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002102 return -EOPNOTSUPP;
2103
2104 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2105 return -EFAULT;
2106
2107 memset(&tmp, 0, sizeof(tmp));
2108 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2109 ret = ops->get_dump_flag(dev, &tmp);
2110 if (ret)
2111 return ret;
2112
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002113 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002114 if (!len)
2115 return -EFAULT;
2116
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002117 /* Don't ever let the driver think there's more space available
2118 * than it requested with .get_dump_flag().
2119 */
2120 dump.len = len;
2121
2122 /* Always allocate enough space to hold the whole thing so that the
2123 * driver does not need to check the length and bother with partial
2124 * dumping.
2125 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002126 data = vzalloc(tmp.len);
2127 if (!data)
2128 return -ENOMEM;
2129 ret = ops->get_dump_data(dev, &dump, data);
2130 if (ret)
2131 goto out;
2132
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002133 /* There are two sane possibilities:
2134 * 1. The driver's .get_dump_data() does not touch dump.len.
2135 * 2. Or it may set dump.len to how much it really writes, which
2136 * should be tmp.len (or len if it can do a partial dump).
2137 * In any case respond to userspace with the actual length of data
2138 * it's receiving.
2139 */
2140 WARN_ON(dump.len != len && dump.len != tmp.len);
2141 dump.len = len;
2142
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002143 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2144 ret = -EFAULT;
2145 goto out;
2146 }
2147 useraddr += offsetof(struct ethtool_dump, data);
2148 if (copy_to_user(useraddr, data, len))
2149 ret = -EFAULT;
2150out:
2151 vfree(data);
2152 return ret;
2153}
2154
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002155static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2156{
2157 int err = 0;
2158 struct ethtool_ts_info info;
2159 const struct ethtool_ops *ops = dev->ethtool_ops;
2160 struct phy_device *phydev = dev->phydev;
2161
2162 memset(&info, 0, sizeof(info));
2163 info.cmd = ETHTOOL_GET_TS_INFO;
2164
2165 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002166 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002167 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002168 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002169 } else {
2170 info.so_timestamping =
2171 SOF_TIMESTAMPING_RX_SOFTWARE |
2172 SOF_TIMESTAMPING_SOFTWARE;
2173 info.phc_index = -1;
2174 }
2175
2176 if (err)
2177 return err;
2178
2179 if (copy_to_user(useraddr, &info, sizeof(info)))
2180 err = -EFAULT;
2181
2182 return err;
2183}
2184
Ed Swierk2f438362015-01-02 17:27:56 -08002185static int __ethtool_get_module_info(struct net_device *dev,
2186 struct ethtool_modinfo *modinfo)
2187{
2188 const struct ethtool_ops *ops = dev->ethtool_ops;
2189 struct phy_device *phydev = dev->phydev;
2190
Russell Kinge679c9c2018-03-28 15:44:16 -07002191 if (dev->sfp_bus)
2192 return sfp_get_module_info(dev->sfp_bus, modinfo);
2193
Ed Swierk2f438362015-01-02 17:27:56 -08002194 if (phydev && phydev->drv && phydev->drv->module_info)
2195 return phydev->drv->module_info(phydev, modinfo);
2196
2197 if (ops->get_module_info)
2198 return ops->get_module_info(dev, modinfo);
2199
2200 return -EOPNOTSUPP;
2201}
2202
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002203static int ethtool_get_module_info(struct net_device *dev,
2204 void __user *useraddr)
2205{
2206 int ret;
2207 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002208
2209 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2210 return -EFAULT;
2211
Ed Swierk2f438362015-01-02 17:27:56 -08002212 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002213 if (ret)
2214 return ret;
2215
2216 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2217 return -EFAULT;
2218
2219 return 0;
2220}
2221
Ed Swierk2f438362015-01-02 17:27:56 -08002222static int __ethtool_get_module_eeprom(struct net_device *dev,
2223 struct ethtool_eeprom *ee, u8 *data)
2224{
2225 const struct ethtool_ops *ops = dev->ethtool_ops;
2226 struct phy_device *phydev = dev->phydev;
2227
Russell Kinge679c9c2018-03-28 15:44:16 -07002228 if (dev->sfp_bus)
2229 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2230
Ed Swierk2f438362015-01-02 17:27:56 -08002231 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2232 return phydev->drv->module_eeprom(phydev, ee, data);
2233
2234 if (ops->get_module_eeprom)
2235 return ops->get_module_eeprom(dev, ee, data);
2236
2237 return -EOPNOTSUPP;
2238}
2239
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002240static int ethtool_get_module_eeprom(struct net_device *dev,
2241 void __user *useraddr)
2242{
2243 int ret;
2244 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002245
Ed Swierk2f438362015-01-02 17:27:56 -08002246 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002247 if (ret)
2248 return ret;
2249
Ed Swierk2f438362015-01-02 17:27:56 -08002250 return ethtool_get_any_eeprom(dev, useraddr,
2251 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002252 modinfo.eeprom_len);
2253}
2254
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302255static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2256{
2257 switch (tuna->id) {
2258 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002259 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302260 if (tuna->len != sizeof(u32) ||
2261 tuna->type_id != ETHTOOL_TUNABLE_U32)
2262 return -EINVAL;
2263 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002264 case ETHTOOL_PFC_PREVENTION_TOUT:
2265 if (tuna->len != sizeof(u16) ||
2266 tuna->type_id != ETHTOOL_TUNABLE_U16)
2267 return -EINVAL;
2268 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302269 default:
2270 return -EINVAL;
2271 }
2272
2273 return 0;
2274}
2275
2276static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2277{
2278 int ret;
2279 struct ethtool_tunable tuna;
2280 const struct ethtool_ops *ops = dev->ethtool_ops;
2281 void *data;
2282
2283 if (!ops->get_tunable)
2284 return -EOPNOTSUPP;
2285 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2286 return -EFAULT;
2287 ret = ethtool_tunable_valid(&tuna);
2288 if (ret)
2289 return ret;
2290 data = kmalloc(tuna.len, GFP_USER);
2291 if (!data)
2292 return -ENOMEM;
2293 ret = ops->get_tunable(dev, &tuna, data);
2294 if (ret)
2295 goto out;
2296 useraddr += sizeof(tuna);
2297 ret = -EFAULT;
2298 if (copy_to_user(useraddr, data, tuna.len))
2299 goto out;
2300 ret = 0;
2301
2302out:
2303 kfree(data);
2304 return ret;
2305}
2306
2307static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2308{
2309 int ret;
2310 struct ethtool_tunable tuna;
2311 const struct ethtool_ops *ops = dev->ethtool_ops;
2312 void *data;
2313
2314 if (!ops->set_tunable)
2315 return -EOPNOTSUPP;
2316 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2317 return -EFAULT;
2318 ret = ethtool_tunable_valid(&tuna);
2319 if (ret)
2320 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302321 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002322 data = memdup_user(useraddr, tuna.len);
2323 if (IS_ERR(data))
2324 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302325 ret = ops->set_tunable(dev, &tuna, data);
2326
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302327 kfree(data);
2328 return ret;
2329}
2330
Kan Liang421797b2016-02-19 09:24:02 -05002331static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2332 void __user *useraddr,
2333 struct ethtool_per_queue_op *per_queue_opt)
2334{
2335 u32 bit;
2336 int ret;
2337 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2338
2339 if (!dev->ethtool_ops->get_per_queue_coalesce)
2340 return -EOPNOTSUPP;
2341
2342 useraddr += sizeof(*per_queue_opt);
2343
Yury Norov3aa56882018-02-06 15:38:06 -08002344 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2345 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002346
2347 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2348 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2349
2350 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2351 if (ret != 0)
2352 return ret;
2353 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2354 return -EFAULT;
2355 useraddr += sizeof(coalesce);
2356 }
2357
2358 return 0;
2359}
2360
Kan Liangf38d1382016-02-19 09:24:03 -05002361static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2362 void __user *useraddr,
2363 struct ethtool_per_queue_op *per_queue_opt)
2364{
2365 u32 bit;
2366 int i, ret = 0;
2367 int n_queue;
2368 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2369 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2370
2371 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2372 (!dev->ethtool_ops->get_per_queue_coalesce))
2373 return -EOPNOTSUPP;
2374
2375 useraddr += sizeof(*per_queue_opt);
2376
Yury Norov3aa56882018-02-06 15:38:06 -08002377 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002378 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2379 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2380 if (!backup)
2381 return -ENOMEM;
2382
2383 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2384 struct ethtool_coalesce coalesce;
2385
2386 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2387 if (ret != 0)
2388 goto roll_back;
2389
2390 tmp++;
2391
2392 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2393 ret = -EFAULT;
2394 goto roll_back;
2395 }
2396
2397 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2398 if (ret != 0)
2399 goto roll_back;
2400
2401 useraddr += sizeof(coalesce);
2402 }
2403
2404roll_back:
2405 if (ret != 0) {
2406 tmp = backup;
2407 for_each_set_bit(i, queue_mask, bit) {
2408 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2409 tmp++;
2410 }
2411 }
2412 kfree(backup);
2413
2414 return ret;
2415}
2416
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002417static int ethtool_set_per_queue(struct net_device *dev,
2418 void __user *useraddr, u32 sub_cmd)
Kan Liangac2c7ad2016-02-19 09:24:01 -05002419{
2420 struct ethtool_per_queue_op per_queue_opt;
2421
2422 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2423 return -EFAULT;
2424
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002425 if (per_queue_opt.sub_command != sub_cmd)
2426 return -EINVAL;
2427
Kan Liangac2c7ad2016-02-19 09:24:01 -05002428 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002429 case ETHTOOL_GCOALESCE:
2430 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002431 case ETHTOOL_SCOALESCE:
2432 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002433 default:
2434 return -EOPNOTSUPP;
2435 };
2436}
2437
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002438static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2439{
2440 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002441 case ETHTOOL_PHY_DOWNSHIFT:
2442 if (tuna->len != sizeof(u8) ||
2443 tuna->type_id != ETHTOOL_TUNABLE_U8)
2444 return -EINVAL;
2445 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002446 default:
2447 return -EINVAL;
2448 }
2449
2450 return 0;
2451}
2452
2453static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2454{
2455 int ret;
2456 struct ethtool_tunable tuna;
2457 struct phy_device *phydev = dev->phydev;
2458 void *data;
2459
2460 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2461 return -EOPNOTSUPP;
2462
2463 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2464 return -EFAULT;
2465 ret = ethtool_phy_tunable_valid(&tuna);
2466 if (ret)
2467 return ret;
2468 data = kmalloc(tuna.len, GFP_USER);
2469 if (!data)
2470 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002471 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002472 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002473 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002474 if (ret)
2475 goto out;
2476 useraddr += sizeof(tuna);
2477 ret = -EFAULT;
2478 if (copy_to_user(useraddr, data, tuna.len))
2479 goto out;
2480 ret = 0;
2481
2482out:
2483 kfree(data);
2484 return ret;
2485}
2486
2487static int set_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->set_tunable))
2495 return -EOPNOTSUPP;
2496 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2497 return -EFAULT;
2498 ret = ethtool_phy_tunable_valid(&tuna);
2499 if (ret)
2500 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002501 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002502 data = memdup_user(useraddr, tuna.len);
2503 if (IS_ERR(data))
2504 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002505 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002506 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002507 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002508
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002509 kfree(data);
2510 return ret;
2511}
2512
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002513static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2514{
2515 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002516 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002517
2518 if (!dev->ethtool_ops->get_fecparam)
2519 return -EOPNOTSUPP;
2520
Edward Creea6d50512018-02-28 19:15:58 +00002521 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2522 if (rc)
2523 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002524
2525 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2526 return -EFAULT;
2527 return 0;
2528}
2529
2530static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2531{
2532 struct ethtool_fecparam fecparam;
2533
2534 if (!dev->ethtool_ops->set_fecparam)
2535 return -EOPNOTSUPP;
2536
2537 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2538 return -EFAULT;
2539
2540 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2541}
2542
Yan Burman600fed52013-06-03 02:03:34 +00002543/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Eric W. Biederman881d9662007-09-17 11:56:21 -07002545int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002547 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002549 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002551 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (!dev || !netif_device_present(dev))
2554 return -ENODEV;
2555
chavey97f8aef2010-04-07 21:54:42 -07002556 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return -EFAULT;
2558
Kan Liangac2c7ad2016-02-19 09:24:01 -05002559 if (ethcmd == ETHTOOL_PERQUEUE) {
2560 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2561 return -EFAULT;
2562 } else {
2563 sub_cmd = ethcmd;
2564 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002565 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002566 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002567 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002568 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002569 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002570 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002571 case ETHTOOL_GCOALESCE:
2572 case ETHTOOL_GRINGPARAM:
2573 case ETHTOOL_GPAUSEPARAM:
2574 case ETHTOOL_GRXCSUM:
2575 case ETHTOOL_GTXCSUM:
2576 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002577 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002578 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002579 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002580 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002581 case ETHTOOL_GTSO:
2582 case ETHTOOL_GPERMADDR:
Maciej Żenczykowski474ff262018-09-22 01:34:01 -07002583 case ETHTOOL_GUFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002584 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002585 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002586 case ETHTOOL_GFLAGS:
2587 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002588 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002589 case ETHTOOL_GRXRINGS:
2590 case ETHTOOL_GRXCLSRLCNT:
2591 case ETHTOOL_GRXCLSRULE:
2592 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002593 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302594 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002595 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002596 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002597 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002598 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302599 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002600 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002601 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002602 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002603 break;
2604 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002605 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002606 return -EPERM;
2607 }
2608
chavey97f8aef2010-04-07 21:54:42 -07002609 if (dev->ethtool_ops->begin) {
2610 rc = dev->ethtool_ops->begin(dev);
2611 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002613 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002614 old_features = dev->features;
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 switch (ethcmd) {
2617 case ETHTOOL_GSET:
2618 rc = ethtool_get_settings(dev, useraddr);
2619 break;
2620 case ETHTOOL_SSET:
2621 rc = ethtool_set_settings(dev, useraddr);
2622 break;
2623 case ETHTOOL_GDRVINFO:
2624 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 break;
2626 case ETHTOOL_GREGS:
2627 rc = ethtool_get_regs(dev, useraddr);
2628 break;
2629 case ETHTOOL_GWOL:
2630 rc = ethtool_get_wol(dev, useraddr);
2631 break;
2632 case ETHTOOL_SWOL:
2633 rc = ethtool_set_wol(dev, useraddr);
2634 break;
2635 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002636 rc = ethtool_get_value(dev, useraddr, ethcmd,
2637 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 break;
2639 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002640 rc = ethtool_set_value_void(dev, useraddr,
2641 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002643 case ETHTOOL_GEEE:
2644 rc = ethtool_get_eee(dev, useraddr);
2645 break;
2646 case ETHTOOL_SEEE:
2647 rc = ethtool_set_eee(dev, useraddr);
2648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 case ETHTOOL_NWAY_RST:
2650 rc = ethtool_nway_reset(dev);
2651 break;
2652 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002653 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 break;
2655 case ETHTOOL_GEEPROM:
2656 rc = ethtool_get_eeprom(dev, useraddr);
2657 break;
2658 case ETHTOOL_SEEPROM:
2659 rc = ethtool_set_eeprom(dev, useraddr);
2660 break;
2661 case ETHTOOL_GCOALESCE:
2662 rc = ethtool_get_coalesce(dev, useraddr);
2663 break;
2664 case ETHTOOL_SCOALESCE:
2665 rc = ethtool_set_coalesce(dev, useraddr);
2666 break;
2667 case ETHTOOL_GRINGPARAM:
2668 rc = ethtool_get_ringparam(dev, useraddr);
2669 break;
2670 case ETHTOOL_SRINGPARAM:
2671 rc = ethtool_set_ringparam(dev, useraddr);
2672 break;
2673 case ETHTOOL_GPAUSEPARAM:
2674 rc = ethtool_get_pauseparam(dev, useraddr);
2675 break;
2676 case ETHTOOL_SPAUSEPARAM:
2677 rc = ethtool_set_pauseparam(dev, useraddr);
2678 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 case ETHTOOL_TEST:
2680 rc = ethtool_self_test(dev, useraddr);
2681 break;
2682 case ETHTOOL_GSTRINGS:
2683 rc = ethtool_get_strings(dev, useraddr);
2684 break;
2685 case ETHTOOL_PHYS_ID:
2686 rc = ethtool_phys_id(dev, useraddr);
2687 break;
2688 case ETHTOOL_GSTATS:
2689 rc = ethtool_get_stats(dev, useraddr);
2690 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002691 case ETHTOOL_GPERMADDR:
2692 rc = ethtool_get_perm_addr(dev, useraddr);
2693 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002694 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002695 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002696 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002697 break;
2698 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002699 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002700 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002701 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002702 rc = ethtool_get_value(dev, useraddr, ethcmd,
2703 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002704 break;
2705 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002706 rc = ethtool_set_value(dev, useraddr,
2707 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002708 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002709 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002710 case ETHTOOL_GRXRINGS:
2711 case ETHTOOL_GRXCLSRLCNT:
2712 case ETHTOOL_GRXCLSRULE:
2713 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002714 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002715 break;
2716 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002717 case ETHTOOL_SRXCLSRLDEL:
2718 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002719 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002720 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002721 case ETHTOOL_FLASHDEV:
2722 rc = ethtool_flash_device(dev, useraddr);
2723 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002724 case ETHTOOL_RESET:
2725 rc = ethtool_reset(dev, useraddr);
2726 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002727 case ETHTOOL_GSSET_INFO:
2728 rc = ethtool_get_sset_info(dev, useraddr);
2729 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002730 case ETHTOOL_GRXFHINDIR:
2731 rc = ethtool_get_rxfh_indir(dev, useraddr);
2732 break;
2733 case ETHTOOL_SRXFHINDIR:
2734 rc = ethtool_set_rxfh_indir(dev, useraddr);
2735 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302736 case ETHTOOL_GRSSH:
2737 rc = ethtool_get_rxfh(dev, useraddr);
2738 break;
2739 case ETHTOOL_SRSSH:
2740 rc = ethtool_set_rxfh(dev, useraddr);
2741 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002742 case ETHTOOL_GFEATURES:
2743 rc = ethtool_get_features(dev, useraddr);
2744 break;
2745 case ETHTOOL_SFEATURES:
2746 rc = ethtool_set_features(dev, useraddr);
2747 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002748 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002749 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002750 case ETHTOOL_GSG:
2751 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002752 case ETHTOOL_GGSO:
2753 case ETHTOOL_GGRO:
2754 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2755 break;
2756 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002757 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002758 case ETHTOOL_SSG:
2759 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002760 case ETHTOOL_SGSO:
2761 case ETHTOOL_SGRO:
2762 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2763 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002764 case ETHTOOL_GCHANNELS:
2765 rc = ethtool_get_channels(dev, useraddr);
2766 break;
2767 case ETHTOOL_SCHANNELS:
2768 rc = ethtool_set_channels(dev, useraddr);
2769 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002770 case ETHTOOL_SET_DUMP:
2771 rc = ethtool_set_dump(dev, useraddr);
2772 break;
2773 case ETHTOOL_GET_DUMP_FLAG:
2774 rc = ethtool_get_dump_flag(dev, useraddr);
2775 break;
2776 case ETHTOOL_GET_DUMP_DATA:
2777 rc = ethtool_get_dump_data(dev, useraddr);
2778 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002779 case ETHTOOL_GET_TS_INFO:
2780 rc = ethtool_get_ts_info(dev, useraddr);
2781 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002782 case ETHTOOL_GMODULEINFO:
2783 rc = ethtool_get_module_info(dev, useraddr);
2784 break;
2785 case ETHTOOL_GMODULEEEPROM:
2786 rc = ethtool_get_module_eeprom(dev, useraddr);
2787 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302788 case ETHTOOL_GTUNABLE:
2789 rc = ethtool_get_tunable(dev, useraddr);
2790 break;
2791 case ETHTOOL_STUNABLE:
2792 rc = ethtool_set_tunable(dev, useraddr);
2793 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002794 case ETHTOOL_GPHYSTATS:
2795 rc = ethtool_get_phy_stats(dev, useraddr);
2796 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002797 case ETHTOOL_PERQUEUE:
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002798 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002799 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002800 case ETHTOOL_GLINKSETTINGS:
2801 rc = ethtool_get_link_ksettings(dev, useraddr);
2802 break;
2803 case ETHTOOL_SLINKSETTINGS:
2804 rc = ethtool_set_link_ksettings(dev, useraddr);
2805 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002806 case ETHTOOL_PHY_GTUNABLE:
2807 rc = get_phy_tunable(dev, useraddr);
2808 break;
2809 case ETHTOOL_PHY_STUNABLE:
2810 rc = set_phy_tunable(dev, useraddr);
2811 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002812 case ETHTOOL_GFECPARAM:
2813 rc = ethtool_get_fecparam(dev, useraddr);
2814 break;
2815 case ETHTOOL_SFECPARAM:
2816 rc = ethtool_set_fecparam(dev, useraddr);
2817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002819 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002821
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002822 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002824
2825 if (old_features != dev->features)
2826 netdev_features_change(dev);
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01002830
2831struct ethtool_rx_flow_key {
2832 struct flow_dissector_key_basic basic;
2833 union {
2834 struct flow_dissector_key_ipv4_addrs ipv4;
2835 struct flow_dissector_key_ipv6_addrs ipv6;
2836 };
2837 struct flow_dissector_key_ports tp;
2838 struct flow_dissector_key_ip ip;
2839 struct flow_dissector_key_vlan vlan;
2840 struct flow_dissector_key_eth_addrs eth_addrs;
2841} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2842
2843struct ethtool_rx_flow_match {
2844 struct flow_dissector dissector;
2845 struct ethtool_rx_flow_key key;
2846 struct ethtool_rx_flow_key mask;
2847};
2848
2849struct ethtool_rx_flow_rule *
2850ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2851{
2852 const struct ethtool_rx_flow_spec *fs = input->fs;
2853 static struct in6_addr zero_addr = {};
2854 struct ethtool_rx_flow_match *match;
2855 struct ethtool_rx_flow_rule *flow;
2856 struct flow_action_entry *act;
2857
2858 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2859 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2860 if (!flow)
2861 return ERR_PTR(-ENOMEM);
2862
2863 /* ethtool_rx supports only one single action per rule. */
2864 flow->rule = flow_rule_alloc(1);
2865 if (!flow->rule) {
2866 kfree(flow);
2867 return ERR_PTR(-ENOMEM);
2868 }
2869
2870 match = (struct ethtool_rx_flow_match *)flow->priv;
2871 flow->rule->match.dissector = &match->dissector;
2872 flow->rule->match.mask = &match->mask;
2873 flow->rule->match.key = &match->key;
2874
2875 match->mask.basic.n_proto = htons(0xffff);
2876
2877 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2878 case TCP_V4_FLOW:
2879 case UDP_V4_FLOW: {
2880 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2881
2882 match->key.basic.n_proto = htons(ETH_P_IP);
2883
2884 v4_spec = &fs->h_u.tcp_ip4_spec;
2885 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2886
2887 if (v4_m_spec->ip4src) {
2888 match->key.ipv4.src = v4_spec->ip4src;
2889 match->mask.ipv4.src = v4_m_spec->ip4src;
2890 }
2891 if (v4_m_spec->ip4dst) {
2892 match->key.ipv4.dst = v4_spec->ip4dst;
2893 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2894 }
2895 if (v4_m_spec->ip4src ||
2896 v4_m_spec->ip4dst) {
2897 match->dissector.used_keys |=
2898 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2899 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2900 offsetof(struct ethtool_rx_flow_key, ipv4);
2901 }
2902 if (v4_m_spec->psrc) {
2903 match->key.tp.src = v4_spec->psrc;
2904 match->mask.tp.src = v4_m_spec->psrc;
2905 }
2906 if (v4_m_spec->pdst) {
2907 match->key.tp.dst = v4_spec->pdst;
2908 match->mask.tp.dst = v4_m_spec->pdst;
2909 }
2910 if (v4_m_spec->psrc ||
2911 v4_m_spec->pdst) {
2912 match->dissector.used_keys |=
2913 BIT(FLOW_DISSECTOR_KEY_PORTS);
2914 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2915 offsetof(struct ethtool_rx_flow_key, tp);
2916 }
2917 if (v4_m_spec->tos) {
2918 match->key.ip.tos = v4_spec->tos;
2919 match->mask.ip.tos = v4_m_spec->tos;
2920 match->dissector.used_keys |=
2921 BIT(FLOW_DISSECTOR_KEY_IP);
2922 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2923 offsetof(struct ethtool_rx_flow_key, ip);
2924 }
2925 }
2926 break;
2927 case TCP_V6_FLOW:
2928 case UDP_V6_FLOW: {
2929 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2930
2931 match->key.basic.n_proto = htons(ETH_P_IPV6);
2932
2933 v6_spec = &fs->h_u.tcp_ip6_spec;
2934 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2935 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2936 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2937 sizeof(match->key.ipv6.src));
2938 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2939 sizeof(match->mask.ipv6.src));
2940 }
2941 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2942 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2943 sizeof(match->key.ipv6.dst));
2944 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2945 sizeof(match->mask.ipv6.dst));
2946 }
2947 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2948 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2949 match->dissector.used_keys |=
2950 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2951 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2952 offsetof(struct ethtool_rx_flow_key, ipv6);
2953 }
2954 if (v6_m_spec->psrc) {
2955 match->key.tp.src = v6_spec->psrc;
2956 match->mask.tp.src = v6_m_spec->psrc;
2957 }
2958 if (v6_m_spec->pdst) {
2959 match->key.tp.dst = v6_spec->pdst;
2960 match->mask.tp.dst = v6_m_spec->pdst;
2961 }
2962 if (v6_m_spec->psrc ||
2963 v6_m_spec->pdst) {
2964 match->dissector.used_keys |=
2965 BIT(FLOW_DISSECTOR_KEY_PORTS);
2966 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2967 offsetof(struct ethtool_rx_flow_key, tp);
2968 }
2969 if (v6_m_spec->tclass) {
2970 match->key.ip.tos = v6_spec->tclass;
2971 match->mask.ip.tos = v6_m_spec->tclass;
2972 match->dissector.used_keys |=
2973 BIT(FLOW_DISSECTOR_KEY_IP);
2974 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2975 offsetof(struct ethtool_rx_flow_key, ip);
2976 }
2977 }
2978 break;
2979 default:
2980 ethtool_rx_flow_rule_destroy(flow);
2981 return ERR_PTR(-EINVAL);
2982 }
2983
2984 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2985 case TCP_V4_FLOW:
2986 case TCP_V6_FLOW:
2987 match->key.basic.ip_proto = IPPROTO_TCP;
2988 break;
2989 case UDP_V4_FLOW:
2990 case UDP_V6_FLOW:
2991 match->key.basic.ip_proto = IPPROTO_UDP;
2992 break;
2993 }
2994 match->mask.basic.ip_proto = 0xff;
2995
2996 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
2997 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
2998 offsetof(struct ethtool_rx_flow_key, basic);
2999
3000 if (fs->flow_type & FLOW_EXT) {
3001 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3002 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3003
3004 if (ext_m_spec->vlan_etype &&
3005 ext_m_spec->vlan_tci) {
3006 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3007 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3008
3009 match->key.vlan.vlan_id =
3010 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3011 match->mask.vlan.vlan_id =
3012 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3013
3014 match->key.vlan.vlan_priority =
3015 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3016 match->mask.vlan.vlan_priority =
3017 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3018
3019 match->dissector.used_keys |=
3020 BIT(FLOW_DISSECTOR_KEY_VLAN);
3021 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3022 offsetof(struct ethtool_rx_flow_key, vlan);
3023 }
3024 }
3025 if (fs->flow_type & FLOW_MAC_EXT) {
3026 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3027 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3028
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003029 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3030 ETH_ALEN);
3031 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3032 ETH_ALEN);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003033
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003034 match->dissector.used_keys |=
3035 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3036 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3037 offsetof(struct ethtool_rx_flow_key, eth_addrs);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003038 }
3039
3040 act = &flow->rule->action.entries[0];
3041 switch (fs->ring_cookie) {
3042 case RX_CLS_FLOW_DISC:
3043 act->id = FLOW_ACTION_DROP;
3044 break;
3045 case RX_CLS_FLOW_WAKE:
3046 act->id = FLOW_ACTION_WAKE;
3047 break;
3048 default:
3049 act->id = FLOW_ACTION_QUEUE;
3050 if (fs->flow_type & FLOW_RSS)
3051 act->queue.ctx = input->rss_ctx;
3052
3053 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3054 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3055 break;
3056 }
3057
3058 return flow;
3059}
3060EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3061
3062void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3063{
3064 kfree(flow->rule);
3065 kfree(flow);
3066}
3067EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);