blob: b5df90c981c263a12597bc93f7b2b9a811649368 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/core/ethtool.c - Ethtool ioctl handler
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 *
6 * This file is where we call all the ethtool_ops commands to get
Matthew Wilcox61a44b92007-07-31 14:00:02 -07007 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/module.h>
11#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/ethtool.h>
15#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000016#include <linux/net_tstamp.h>
17#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000018#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070019#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070020#include <linux/vmalloc.h>
Russell Kinge679c9c2018-03-28 15:44:16 -070021#include <linux/sfp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010023#include <linux/rtnetlink.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080025#include <linux/net.h>
Jakub Kicinskiddb6e992019-01-31 10:50:47 -080026#include <net/devlink.h>
Magnus Karlssona71506a2020-05-20 21:20:51 +020027#include <net/xdp_sock_drv.h>
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +010028#include <net/flow_offload.h>
Michal Kubecek73286732019-12-27 15:56:03 +010029#include <linux/ethtool_netlink.h>
Leon Romanovsky1c790312020-04-19 17:18:47 +030030#include <generated/utsrelease.h>
Michal Kubecekd44e1312019-12-11 10:58:29 +010031#include "common.h"
32
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * Some useful ethtool_ops methods that're device independent.
35 * If we find that all drivers want to do the same thing here,
36 * we can turn these into dev_() function calls.
37 */
38
39u32 ethtool_op_get_link(struct net_device *dev)
40{
41 return netif_carrier_ok(dev) ? 1 : 0;
42}
chavey97f8aef2010-04-07 21:54:42 -070043EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Richard Cochran02eacbd2012-04-03 22:59:22 +000045int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
46{
47 info->so_timestamping =
48 SOF_TIMESTAMPING_TX_SOFTWARE |
49 SOF_TIMESTAMPING_RX_SOFTWARE |
50 SOF_TIMESTAMPING_SOFTWARE;
51 info->phc_index = -1;
52 return 0;
53}
54EXPORT_SYMBOL(ethtool_op_get_ts_info);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* Handlers for each ethtool command */
57
Michał Mirosław5455c692011-02-15 16:59:17 +000058static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
59{
60 struct ethtool_gfeatures cmd = {
61 .cmd = ETHTOOL_GFEATURES,
62 .size = ETHTOOL_DEV_FEATURE_WORDS,
63 };
Michał Mirosław475414f2011-11-15 15:29:55 +000064 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +000065 u32 __user *sizeaddr;
66 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +000067 int i;
68
69 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +000070 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +000071
72 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +000073 features[i].available = (u32)(dev->hw_features >> (32 * i));
74 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
75 features[i].active = (u32)(dev->features >> (32 * i));
76 features[i].never_changed =
77 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +000078 }
Michał Mirosław5455c692011-02-15 16:59:17 +000079
80 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
81 if (get_user(copy_size, sizeaddr))
82 return -EFAULT;
83
84 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
85 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
86
87 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
88 return -EFAULT;
89 useraddr += sizeof(cmd);
90 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
91 return -EFAULT;
92
93 return 0;
94}
95
96static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
97{
98 struct ethtool_sfeatures cmd;
99 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000100 netdev_features_t wanted = 0, valid = 0;
101 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000102
103 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
104 return -EFAULT;
105 useraddr += sizeof(cmd);
106
107 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
108 return -EINVAL;
109
110 if (copy_from_user(features, useraddr, sizeof(features)))
111 return -EFAULT;
112
Michał Mirosław475414f2011-11-15 15:29:55 +0000113 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000114 valid |= (netdev_features_t)features[i].valid << (32 * i);
115 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000116 }
117
118 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000119 return -EINVAL;
120
Michał Mirosław475414f2011-11-15 15:29:55 +0000121 if (valid & ~dev->hw_features) {
122 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000123 ret |= ETHTOOL_F_UNSUPPORTED;
124 }
125
Michał Mirosław475414f2011-11-15 15:29:55 +0000126 dev->wanted_features &= ~valid;
127 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700128 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000129
Michał Mirosław475414f2011-11-15 15:29:55 +0000130 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000131 ret |= ETHTOOL_F_WISH;
132
133 return ret;
134}
135
Michał Mirosław340ae162011-02-15 16:59:16 +0000136static int __ethtool_get_sset_count(struct net_device *dev, int sset)
137{
138 const struct ethtool_ops *ops = dev->ethtool_ops;
139
Michał Mirosław5455c692011-02-15 16:59:17 +0000140 if (sset == ETH_SS_FEATURES)
141 return ARRAY_SIZE(netdev_features_strings);
142
Eyal Perry892311f2014-12-02 18:12:10 +0200143 if (sset == ETH_SS_RSS_HASH_FUNCS)
144 return ARRAY_SIZE(rss_hash_func_strings);
145
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300146 if (sset == ETH_SS_TUNABLES)
147 return ARRAY_SIZE(tunable_strings);
148
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100149 if (sset == ETH_SS_PHY_TUNABLES)
150 return ARRAY_SIZE(phy_tunable_strings);
151
Florian Fainelli99943382018-04-25 12:12:48 -0700152 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
153 !ops->get_ethtool_phy_stats)
154 return phy_ethtool_get_sset_count(dev->phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +0100155
Michal Kubecek428c1222019-12-11 10:58:34 +0100156 if (sset == ETH_SS_LINK_MODES)
157 return __ETHTOOL_LINK_MODE_MASK_NBITS;
158
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000159 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000160 return ops->get_sset_count(dev, sset);
161 else
162 return -EOPNOTSUPP;
163}
164
165static void __ethtool_get_strings(struct net_device *dev,
166 u32 stringset, u8 *data)
167{
168 const struct ethtool_ops *ops = dev->ethtool_ops;
169
Michał Mirosław5455c692011-02-15 16:59:17 +0000170 if (stringset == ETH_SS_FEATURES)
171 memcpy(data, netdev_features_strings,
172 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200173 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
174 memcpy(data, rss_hash_func_strings,
175 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300176 else if (stringset == ETH_SS_TUNABLES)
177 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100178 else if (stringset == ETH_SS_PHY_TUNABLES)
179 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Florian Fainelli99943382018-04-25 12:12:48 -0700180 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
181 !ops->get_ethtool_phy_stats)
182 phy_ethtool_get_strings(dev->phydev, data);
Michal Kubecek428c1222019-12-11 10:58:34 +0100183 else if (stringset == ETH_SS_LINK_MODES)
184 memcpy(data, link_mode_names,
185 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
Florian Fainelli99943382018-04-25 12:12:48 -0700186 else
Michał Mirosław5455c692011-02-15 16:59:17 +0000187 /* ops->get_strings is valid because checked earlier */
188 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000189}
190
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000191static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000192{
193 /* feature masks of legacy discrete ethtool ops */
194
195 switch (eth_cmd) {
196 case ETHTOOL_GTXCSUM:
197 case ETHTOOL_STXCSUM:
Vladyslav Tarasiuk9d648fb2020-03-24 13:57:08 +0200198 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
Michal Kubecekf70bb062020-03-12 21:07:43 +0100199 NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000200 case ETHTOOL_GRXCSUM:
201 case ETHTOOL_SRXCSUM:
202 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000203 case ETHTOOL_GSG:
204 case ETHTOOL_SSG:
Michal Kubecekf70bb062020-03-12 21:07:43 +0100205 return NETIF_F_SG | NETIF_F_FRAGLIST;
Michał Mirosław0a417702011-02-15 16:59:17 +0000206 case ETHTOOL_GTSO:
207 case ETHTOOL_STSO:
208 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000209 case ETHTOOL_GGSO:
210 case ETHTOOL_SGSO:
211 return NETIF_F_GSO;
212 case ETHTOOL_GGRO:
213 case ETHTOOL_SGRO:
214 return NETIF_F_GRO;
215 default:
216 BUG();
217 }
218}
219
Michał Mirosław0a417702011-02-15 16:59:17 +0000220static int ethtool_get_one_feature(struct net_device *dev,
221 char __user *useraddr, u32 ethcmd)
222{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000223 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000224 struct ethtool_value edata = {
225 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000226 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000227 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000228
Michał Mirosław0a417702011-02-15 16:59:17 +0000229 if (copy_to_user(useraddr, &edata, sizeof(edata)))
230 return -EFAULT;
231 return 0;
232}
233
Michał Mirosław0a417702011-02-15 16:59:17 +0000234static int ethtool_set_one_feature(struct net_device *dev,
235 void __user *useraddr, u32 ethcmd)
236{
237 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000238 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000239
240 if (copy_from_user(&edata, useraddr, sizeof(edata)))
241 return -EFAULT;
242
Michał Mirosław86794882011-02-15 16:59:17 +0000243 mask = ethtool_get_feature_mask(ethcmd);
244 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000245 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000246 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000247
248 if (edata.data)
249 dev->wanted_features |= mask;
250 else
251 dev->wanted_features &= ~mask;
252
253 __netdev_update_features(dev);
254
255 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000256}
257
Michał Mirosław02b3a552011-11-15 15:29:55 +0000258#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
259 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000260#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
261 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
262 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000263
264static u32 __ethtool_get_flags(struct net_device *dev)
265{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000266 u32 flags = 0;
267
Dragos Foianu8a731252013-07-13 14:43:00 +0100268 if (dev->features & NETIF_F_LRO)
269 flags |= ETH_FLAG_LRO;
270 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
271 flags |= ETH_FLAG_RXVLAN;
272 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
273 flags |= ETH_FLAG_TXVLAN;
274 if (dev->features & NETIF_F_NTUPLE)
275 flags |= ETH_FLAG_NTUPLE;
276 if (dev->features & NETIF_F_RXHASH)
277 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000278
279 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000280}
281
282static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000283{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000284 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000285
Michał Mirosław02b3a552011-11-15 15:29:55 +0000286 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000287 return -EINVAL;
288
Dragos Foianu8a731252013-07-13 14:43:00 +0100289 if (data & ETH_FLAG_LRO)
290 features |= NETIF_F_LRO;
291 if (data & ETH_FLAG_RXVLAN)
292 features |= NETIF_F_HW_VLAN_CTAG_RX;
293 if (data & ETH_FLAG_TXVLAN)
294 features |= NETIF_F_HW_VLAN_CTAG_TX;
295 if (data & ETH_FLAG_NTUPLE)
296 features |= NETIF_F_NTUPLE;
297 if (data & ETH_FLAG_RXHASH)
298 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000299
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000300 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000301 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000302 if (changed & ~dev->hw_features)
303 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
304
305 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000306 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000307
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700308 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000309
310 return 0;
311}
312
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700313/* Given two link masks, AND them together and save the result in dst. */
314void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
315 struct ethtool_link_ksettings *src)
316{
317 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
318 unsigned int idx = 0;
319
320 for (; idx < size; idx++) {
321 dst->link_modes.supported[idx] &=
322 src->link_modes.supported[idx];
323 dst->link_modes.advertising[idx] &=
324 src->link_modes.advertising[idx];
325 }
326}
327EXPORT_SYMBOL(ethtool_intersect_link_masks);
328
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200329void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
330 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800331{
332 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
333 dst[0] = legacy_u32;
334}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200335EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800336
337/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200338bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
339 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800340{
341 bool retval = true;
342
343 /* TODO: following test will soon always be true */
344 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
345 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
346
347 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
348 bitmap_fill(ext, 32);
349 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
350 if (bitmap_intersects(ext, src,
351 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
352 /* src mask goes beyond bit 31 */
353 retval = false;
354 }
355 }
356 *legacy_u32 = src[0];
357 return retval;
358}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200359EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800360
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800361/* return false if ksettings link modes had higher bits
362 * set. legacy_settings always updated (best effort)
363 */
364static bool
365convert_link_ksettings_to_legacy_settings(
366 struct ethtool_cmd *legacy_settings,
367 const struct ethtool_link_ksettings *link_ksettings)
368{
369 bool retval = true;
370
371 memset(legacy_settings, 0, sizeof(*legacy_settings));
372 /* this also clears the deprecated fields in legacy structure:
373 * __u8 transceiver;
374 * __u32 maxtxpkt;
375 * __u32 maxrxpkt;
376 */
377
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200378 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800379 &legacy_settings->supported,
380 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200381 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800382 &legacy_settings->advertising,
383 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200384 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800385 &legacy_settings->lp_advertising,
386 link_ksettings->link_modes.lp_advertising);
387 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
388 legacy_settings->duplex
389 = link_ksettings->base.duplex;
390 legacy_settings->port
391 = link_ksettings->base.port;
392 legacy_settings->phy_address
393 = link_ksettings->base.phy_address;
394 legacy_settings->autoneg
395 = link_ksettings->base.autoneg;
396 legacy_settings->mdio_support
397 = link_ksettings->base.mdio_support;
398 legacy_settings->eth_tp_mdix
399 = link_ksettings->base.eth_tp_mdix;
400 legacy_settings->eth_tp_mdix_ctrl
401 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700402 legacy_settings->transceiver
403 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800404 return retval;
405}
406
407/* number of 32-bit words to store the user's link mode bitmaps */
408#define __ETHTOOL_LINK_MODE_MASK_NU32 \
409 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
410
411/* layout of the struct passed from/to userland */
412struct ethtool_link_usettings {
413 struct ethtool_link_settings base;
414 struct {
415 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
416 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
417 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
418 } link_modes;
419};
420
Michal Kubecek9b300492018-08-28 19:56:58 +0200421/* Internal kernel helper to query a device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800422int __ethtool_get_link_ksettings(struct net_device *dev,
423 struct ethtool_link_ksettings *link_ksettings)
424{
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800425 ASSERT_RTNL();
426
Michal Kubecek9b300492018-08-28 19:56:58 +0200427 if (!dev->ethtool_ops->get_link_ksettings)
David Decotigny3237fc62016-02-24 10:58:11 -0800428 return -EOPNOTSUPP;
429
Michal Kubecek9b300492018-08-28 19:56:58 +0200430 memset(link_ksettings, 0, sizeof(*link_ksettings));
431 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800432}
433EXPORT_SYMBOL(__ethtool_get_link_ksettings);
434
435/* convert ethtool_link_usettings in user space to a kernel internal
436 * ethtool_link_ksettings. return 0 on success, errno on error.
437 */
438static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
439 const void __user *from)
440{
441 struct ethtool_link_usettings link_usettings;
442
443 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
444 return -EFAULT;
445
446 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800447 bitmap_from_arr32(to->link_modes.supported,
448 link_usettings.link_modes.supported,
449 __ETHTOOL_LINK_MODE_MASK_NBITS);
450 bitmap_from_arr32(to->link_modes.advertising,
451 link_usettings.link_modes.advertising,
452 __ETHTOOL_LINK_MODE_MASK_NBITS);
453 bitmap_from_arr32(to->link_modes.lp_advertising,
454 link_usettings.link_modes.lp_advertising,
455 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800456
457 return 0;
458}
459
Cris Forno70ae1e12020-02-28 14:12:04 -0600460/* Check if the user is trying to change anything besides speed/duplex */
461bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
462{
463 struct ethtool_link_settings base2 = {};
464
465 base2.speed = cmd->base.speed;
466 base2.port = PORT_OTHER;
467 base2.duplex = cmd->base.duplex;
468 base2.cmd = cmd->base.cmd;
469 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
470
471 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
472 bitmap_empty(cmd->link_modes.supported,
473 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
474 bitmap_empty(cmd->link_modes.lp_advertising,
475 __ETHTOOL_LINK_MODE_MASK_NBITS);
476}
477
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800478/* convert a kernel internal ethtool_link_ksettings to
479 * ethtool_link_usettings in user space. return 0 on success, errno on
480 * error.
481 */
482static int
483store_link_ksettings_for_user(void __user *to,
484 const struct ethtool_link_ksettings *from)
485{
486 struct ethtool_link_usettings link_usettings;
487
488 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800489 bitmap_to_arr32(link_usettings.link_modes.supported,
490 from->link_modes.supported,
491 __ETHTOOL_LINK_MODE_MASK_NBITS);
492 bitmap_to_arr32(link_usettings.link_modes.advertising,
493 from->link_modes.advertising,
494 __ETHTOOL_LINK_MODE_MASK_NBITS);
495 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
496 from->link_modes.lp_advertising,
497 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800498
499 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
500 return -EFAULT;
501
502 return 0;
503}
504
Michal Kubecek9b300492018-08-28 19:56:58 +0200505/* Query device for its ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800506static int ethtool_get_link_ksettings(struct net_device *dev,
507 void __user *useraddr)
508{
509 int err = 0;
510 struct ethtool_link_ksettings link_ksettings;
511
512 ASSERT_RTNL();
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800513 if (!dev->ethtool_ops->get_link_ksettings)
514 return -EOPNOTSUPP;
515
516 /* handle bitmap nbits handshake */
517 if (copy_from_user(&link_ksettings.base, useraddr,
518 sizeof(link_ksettings.base)))
519 return -EFAULT;
520
521 if (__ETHTOOL_LINK_MODE_MASK_NU32
522 != link_ksettings.base.link_mode_masks_nwords) {
523 /* wrong link mode nbits requested */
524 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000525 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800526 /* send back number of words required as negative val */
527 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
528 "need too many bits for link modes!");
529 link_ksettings.base.link_mode_masks_nwords
530 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
531
532 /* copy the base fields back to user, not the link
533 * mode bitmaps
534 */
535 if (copy_to_user(useraddr, &link_ksettings.base,
536 sizeof(link_ksettings.base)))
537 return -EFAULT;
538
539 return 0;
540 }
541
542 /* handshake successful: user/kernel agree on
543 * link_mode_masks_nwords
544 */
545
546 memset(&link_ksettings, 0, sizeof(link_ksettings));
547 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
548 if (err < 0)
549 return err;
550
551 /* make sure we tell the right values to user */
552 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
553 link_ksettings.base.link_mode_masks_nwords
554 = __ETHTOOL_LINK_MODE_MASK_NU32;
Oleksij Rempelbdbdac72020-05-05 08:35:05 +0200555 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
556 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800557
558 return store_link_ksettings_for_user(useraddr, &link_ksettings);
559}
560
Michal Kubecek9b300492018-08-28 19:56:58 +0200561/* Update device ethtool_link_settings. */
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800562static int ethtool_set_link_ksettings(struct net_device *dev,
563 void __user *useraddr)
564{
565 int err;
566 struct ethtool_link_ksettings link_ksettings;
567
568 ASSERT_RTNL();
569
570 if (!dev->ethtool_ops->set_link_ksettings)
571 return -EOPNOTSUPP;
572
573 /* make sure nbits field has expected value */
574 if (copy_from_user(&link_ksettings.base, useraddr,
575 sizeof(link_ksettings.base)))
576 return -EFAULT;
577
578 if (__ETHTOOL_LINK_MODE_MASK_NU32
579 != link_ksettings.base.link_mode_masks_nwords)
580 return -EINVAL;
581
582 /* copy the whole structure, now that we know it has expected
583 * format
584 */
585 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
586 if (err)
587 return err;
588
589 /* re-check nwords field, just in case */
590 if (__ETHTOOL_LINK_MODE_MASK_NU32
591 != link_ksettings.base.link_mode_masks_nwords)
592 return -EINVAL;
593
Oleksij Rempelbdbdac72020-05-05 08:35:05 +0200594 if (link_ksettings.base.master_slave_cfg ||
595 link_ksettings.base.master_slave_state)
596 return -EINVAL;
597
Michal Kubecek73286732019-12-27 15:56:03 +0100598 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100599 if (err >= 0) {
Michal Kubecek73286732019-12-27 15:56:03 +0100600 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100601 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
602 }
Michal Kubecek73286732019-12-27 15:56:03 +0100603 return err;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800604}
605
Cris Forno70ae1e12020-02-28 14:12:04 -0600606int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
607 const struct ethtool_link_ksettings *cmd,
608 u32 *dev_speed, u8 *dev_duplex)
609{
610 u32 speed;
611 u8 duplex;
612
613 speed = cmd->base.speed;
614 duplex = cmd->base.duplex;
615 /* don't allow custom speed and duplex */
616 if (!ethtool_validate_speed(speed) ||
617 !ethtool_validate_duplex(duplex) ||
618 !ethtool_virtdev_validate_cmd(cmd))
619 return -EINVAL;
620 *dev_speed = speed;
621 *dev_duplex = duplex;
622
623 return 0;
624}
625EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
626
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800627/* Query device for its ethtool_cmd settings.
628 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200629 * Backward compatibility note: for compatibility with legacy ethtool, this is
630 * now implemented via get_link_ksettings. When driver reports higher link mode
631 * bits, a kernel warning is logged once (with name of 1st driver/device) to
632 * recommend user to upgrade ethtool, but the command is successful (only the
633 * lower link mode bits reported back to user). Deprecated fields from
634 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800635 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000636static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
637{
Michal Kubecek9b300492018-08-28 19:56:58 +0200638 struct ethtool_link_ksettings link_ksettings;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000639 struct ethtool_cmd cmd;
Michal Kubecek9b300492018-08-28 19:56:58 +0200640 int err;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000641
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800642 ASSERT_RTNL();
Michal Kubecek9b300492018-08-28 19:56:58 +0200643 if (!dev->ethtool_ops->get_link_ksettings)
644 return -EOPNOTSUPP;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800645
Michal Kubecek9b300492018-08-28 19:56:58 +0200646 memset(&link_ksettings, 0, sizeof(link_ksettings));
647 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
648 if (err < 0)
649 return err;
650 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800651
Michal Kubecek9b300492018-08-28 19:56:58 +0200652 /* send a sensible cmd tag back to user */
653 cmd.cmd = ETHTOOL_GSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
656 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 0;
659}
660
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800661/* Update device link settings with given ethtool_cmd.
662 *
Michal Kubecek9b300492018-08-28 19:56:58 +0200663 * Backward compatibility note: for compatibility with legacy ethtool, this is
664 * now always implemented via set_link_settings. When user's request updates
665 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
666 * warning is logged once (with name of 1st driver/device) to recommend user to
667 * upgrade ethtool, and the request is rejected.
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
670{
Michal Kubecek9b300492018-08-28 19:56:58 +0200671 struct ethtool_link_ksettings link_ksettings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct ethtool_cmd cmd;
Michal Kubecek73286732019-12-27 15:56:03 +0100673 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800675 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
678 return -EFAULT;
Michal Kubecek9b300492018-08-28 19:56:58 +0200679 if (!dev->ethtool_ops->set_link_ksettings)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800680 return -EOPNOTSUPP;
681
Michal Kubecek9b300492018-08-28 19:56:58 +0200682 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
683 return -EINVAL;
684 link_ksettings.base.link_mode_masks_nwords =
685 __ETHTOOL_LINK_MODE_MASK_NU32;
Michal Kubecek73286732019-12-27 15:56:03 +0100686 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100687 if (ret >= 0) {
Michal Kubecek73286732019-12-27 15:56:03 +0100688 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
Michal Kubecek1b1b1842019-12-27 15:56:18 +0100689 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
690 }
Michal Kubecek73286732019-12-27 15:56:03 +0100691 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
chavey97f8aef2010-04-07 21:54:42 -0700694static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
695 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700698 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 memset(&info, 0, sizeof(info));
701 info.cmd = ETHTOOL_GDRVINFO;
Leon Romanovsky6a7e25c2020-01-27 09:20:28 +0200702 strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000703 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700704 ops->get_drvinfo(dev, &info);
705 } else if (dev->dev.parent && dev->dev.parent->driver) {
706 strlcpy(info.bus_info, dev_name(dev->dev.parent),
707 sizeof(info.bus_info));
708 strlcpy(info.driver, dev->dev.parent->driver->name,
709 sizeof(info.driver));
710 } else {
711 return -EOPNOTSUPP;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Jeff Garzik723b2f52010-03-03 22:51:50 +0000714 /*
715 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000716 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000717 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000718 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700719 int rc;
720
721 rc = ops->get_sset_count(dev, ETH_SS_TEST);
722 if (rc >= 0)
723 info.testinfo_len = rc;
724 rc = ops->get_sset_count(dev, ETH_SS_STATS);
725 if (rc >= 0)
726 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700727 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
728 if (rc >= 0)
729 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700730 }
Yunsheng Linf9fc54d2018-12-26 19:51:46 +0800731 if (ops->get_regs_len) {
732 int ret = ops->get_regs_len(dev);
733
734 if (ret > 0)
735 info.regdump_len = ret;
736 }
737
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000738 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 info.eedump_len = ops->get_eeprom_len(dev);
740
Jakub Kicinskiddb6e992019-01-31 10:50:47 -0800741 if (!info.fw_version[0])
742 devlink_compat_running_version(dev, info.fw_version,
743 sizeof(info.fw_version));
Jakub Kicinskiddb6e992019-01-31 10:50:47 -0800744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (copy_to_user(useraddr, &info, sizeof(info)))
746 return -EFAULT;
747 return 0;
748}
749
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800750static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700751 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000752{
753 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000754 u64 sset_mask;
755 int i, idx = 0, n_bits = 0, ret, rc;
756 u32 *info_buf = NULL;
757
Jeff Garzik723b2f52010-03-03 22:51:50 +0000758 if (copy_from_user(&info, useraddr, sizeof(info)))
759 return -EFAULT;
760
761 /* store copy of mask, because we zero struct later on */
762 sset_mask = info.sset_mask;
763 if (!sset_mask)
764 return 0;
765
766 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000767 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000768
769 memset(&info, 0, sizeof(info));
770 info.cmd = ETHTOOL_GSSET_INFO;
771
Kees Cook6396bb22018-06-12 14:03:40 -0700772 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000773 if (!info_buf)
774 return -ENOMEM;
775
776 /*
777 * fill return buffer based on input bitmask and successful
778 * get_sset_count return
779 */
780 for (i = 0; i < 64; i++) {
781 if (!(sset_mask & (1ULL << i)))
782 continue;
783
Michał Mirosław340ae162011-02-15 16:59:16 +0000784 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000785 if (rc >= 0) {
786 info.sset_mask |= (1ULL << i);
787 info_buf[idx++] = rc;
788 }
789 }
790
791 ret = -EFAULT;
792 if (copy_to_user(useraddr, &info, sizeof(info)))
793 goto out;
794
795 useraddr += offsetof(struct ethtool_sset_info, data);
796 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
797 goto out;
798
799 ret = 0;
800
801out:
802 kfree(info_buf);
803 return ret;
804}
805
chavey97f8aef2010-04-07 21:54:42 -0700806static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000807 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700808{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000809 struct ethtool_rxnfc info;
810 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000811 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700812
Santwona Behera59089d82009-02-20 00:58:13 -0800813 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700814 return -EOPNOTSUPP;
815
Ben Hutchingsbf988432010-06-28 08:45:58 +0000816 /* struct ethtool_rxnfc was originally defined for
817 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
818 * members. User-space might still be using that
819 * definition. */
820 if (cmd == ETHTOOL_SRXFH)
821 info_size = (offsetof(struct ethtool_rxnfc, data) +
822 sizeof(info.data));
823
824 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700825 return -EFAULT;
826
Ben Hutchings55664f32012-01-03 12:04:51 +0000827 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
828 if (rc)
829 return rc;
830
831 if (cmd == ETHTOOL_SRXCLSRLINS &&
832 copy_to_user(useraddr, &info, info_size))
833 return -EFAULT;
834
835 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700836}
837
chavey97f8aef2010-04-07 21:54:42 -0700838static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000839 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700840{
841 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000842 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800843 const struct ethtool_ops *ops = dev->ethtool_ops;
844 int ret;
845 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700846
Santwona Behera59089d82009-02-20 00:58:13 -0800847 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700848 return -EOPNOTSUPP;
849
Ben Hutchingsbf988432010-06-28 08:45:58 +0000850 /* struct ethtool_rxnfc was originally defined for
851 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
852 * members. User-space might still be using that
853 * definition. */
854 if (cmd == ETHTOOL_GRXFH)
855 info_size = (offsetof(struct ethtool_rxnfc, data) +
856 sizeof(info.data));
857
858 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700859 return -EFAULT;
860
Edward Cree84a1d9c42018-03-08 15:45:03 +0000861 /* If FLOW_RSS was requested then user-space must be using the
862 * new definition, as FLOW_RSS is newer.
863 */
864 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
865 info_size = sizeof(info);
866 if (copy_from_user(&info, useraddr, info_size))
867 return -EFAULT;
Wenwen Wangd656fe42018-04-30 12:31:13 -0500868 /* Since malicious users may modify the original data,
869 * we need to check whether FLOW_RSS is still requested.
870 */
871 if (!(info.flow_type & FLOW_RSS))
872 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +0000873 }
874
Wenwen Wang2bb32072018-10-09 08:15:38 -0500875 if (info.cmd != cmd)
876 return -EINVAL;
877
Santwona Behera59089d82009-02-20 00:58:13 -0800878 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
879 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000880 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cook6396bb22018-06-12 14:03:40 -0700881 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000882 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800883 if (!rule_buf)
884 return -ENOMEM;
885 }
886 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700887
Santwona Behera59089d82009-02-20 00:58:13 -0800888 ret = ops->get_rxnfc(dev, &info, rule_buf);
889 if (ret < 0)
890 goto err_out;
891
892 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000893 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800894 goto err_out;
895
896 if (rule_buf) {
897 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
898 if (copy_to_user(useraddr, rule_buf,
899 info.rule_cnt * sizeof(u32)))
900 goto err_out;
901 }
902 ret = 0;
903
904err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700905 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800906
907 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700908}
909
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530910static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
911 struct ethtool_rxnfc *rx_rings,
912 u32 size)
913{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100914 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530915
916 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100917 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530918
919 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100920 for (i = 0; i < size; i++)
921 if (indir[i] >= rx_rings->data)
922 return -EINVAL;
923
924 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530925}
926
Kim Jonesba905f52016-02-02 03:51:16 +0000927u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -0800928
929void netdev_rss_key_fill(void *buffer, size_t len)
930{
931 BUG_ON(len > sizeof(netdev_rss_key));
932 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
933 memcpy(buffer, netdev_rss_key, len);
934}
935EXPORT_SYMBOL(netdev_rss_key_fill);
936
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000937static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
938 void __user *useraddr)
939{
Ben Hutchings7850f632011-12-15 13:55:01 +0000940 u32 user_size, dev_size;
941 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000942 int ret;
943
Ben Hutchings7850f632011-12-15 13:55:01 +0000944 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +0100945 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +0000946 return -EOPNOTSUPP;
947 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
948 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000949 return -EOPNOTSUPP;
950
Ben Hutchings7850f632011-12-15 13:55:01 +0000951 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000952 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000953 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000954 return -EFAULT;
955
Ben Hutchings7850f632011-12-15 13:55:01 +0000956 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
957 &dev_size, sizeof(dev_size)))
958 return -EFAULT;
959
960 /* If the user buffer size is 0, this is just a query for the
961 * device table size. Otherwise, if it's smaller than the
962 * device table size it's an error.
963 */
964 if (user_size < dev_size)
965 return user_size == 0 ? 0 : -EINVAL;
966
967 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000968 if (!indir)
969 return -ENOMEM;
970
Eyal Perry892311f2014-12-02 18:12:10 +0200971 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000972 if (ret)
973 goto out;
974
Ben Hutchings7850f632011-12-15 13:55:01 +0000975 if (copy_to_user(useraddr +
976 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
977 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000978 ret = -EFAULT;
979
980out:
981 kfree(indir);
982 return ret;
983}
984
985static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
986 void __user *useraddr)
987{
Ben Hutchings7850f632011-12-15 13:55:01 +0000988 struct ethtool_rxnfc rx_rings;
989 u32 user_size, dev_size, i;
990 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000991 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000992 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530993 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000994
Ben Hutchingsfe62d002014-05-15 01:25:27 +0100995 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000996 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000997 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000998
999 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001000 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001001 return -EOPNOTSUPP;
1002
Ben Hutchings7850f632011-12-15 13:55:01 +00001003 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001004 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001005 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001006 return -EFAULT;
1007
Ben Hutchings278bc422011-12-15 13:56:49 +00001008 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001009 return -EINVAL;
1010
1011 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001012 if (!indir)
1013 return -ENOMEM;
1014
Ben Hutchings7850f632011-12-15 13:55:01 +00001015 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001016 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001017 if (ret)
1018 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001019
1020 if (user_size == 0) {
1021 for (i = 0; i < dev_size; i++)
1022 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1023 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301024 ret = ethtool_copy_validate_indir(indir,
1025 useraddr + ringidx_offset,
1026 &rx_rings,
1027 dev_size);
1028 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001029 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001030 }
1031
Eyal Perry892311f2014-12-02 18:12:10 +02001032 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001033 if (ret)
1034 goto out;
1035
1036 /* indicate whether rxfh was set to default */
1037 if (user_size == 0)
1038 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1039 else
1040 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001041
1042out:
1043 kfree(indir);
1044 return ret;
1045}
1046
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301047static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1048 void __user *useraddr)
1049{
1050 int ret;
1051 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001052 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301053 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001054 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301055 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001056 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301057 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001058 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301059 u8 *hkey = NULL;
1060 u8 *rss_config;
1061
Eyal Perry892311f2014-12-02 18:12:10 +02001062 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301063 return -EOPNOTSUPP;
1064
1065 if (ops->get_rxfh_indir_size)
1066 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301067 if (ops->get_rxfh_key_size)
1068 dev_key_size = ops->get_rxfh_key_size(dev);
1069
Ben Hutchingsf062a382014-05-15 16:28:07 +01001070 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301071 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001072 user_indir_size = rxfh.indir_size;
1073 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301074
Ben Hutchingsf062a382014-05-15 16:28:07 +01001075 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001076 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001077 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001078 /* Most drivers don't handle rss_context, check it's 0 as well */
1079 if (rxfh.rss_context && !ops->get_rxfh_context)
1080 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001081
1082 rxfh.indir_size = dev_indir_size;
1083 rxfh.key_size = dev_key_size;
1084 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301085 return -EFAULT;
1086
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301087 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1088 (user_key_size && (user_key_size != dev_key_size)))
1089 return -EINVAL;
1090
1091 indir_bytes = user_indir_size * sizeof(indir[0]);
1092 total_size = indir_bytes + user_key_size;
1093 rss_config = kzalloc(total_size, GFP_USER);
1094 if (!rss_config)
1095 return -ENOMEM;
1096
1097 if (user_indir_size)
1098 indir = (u32 *)rss_config;
1099
1100 if (user_key_size)
1101 hkey = rss_config + indir_bytes;
1102
Edward Cree84a1d9c42018-03-08 15:45:03 +00001103 if (rxfh.rss_context)
1104 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1105 &dev_hfunc,
1106 rxfh.rss_context);
1107 else
1108 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001109 if (ret)
1110 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301111
Eyal Perry892311f2014-12-02 18:12:10 +02001112 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1113 &dev_hfunc, sizeof(rxfh.hfunc))) {
1114 ret = -EFAULT;
1115 } else if (copy_to_user(useraddr +
1116 offsetof(struct ethtool_rxfh, rss_config[0]),
1117 rss_config, total_size)) {
1118 ret = -EFAULT;
1119 }
1120out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301121 kfree(rss_config);
1122
1123 return ret;
1124}
1125
1126static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1127 void __user *useraddr)
1128{
1129 int ret;
1130 const struct ethtool_ops *ops = dev->ethtool_ops;
1131 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001132 struct ethtool_rxfh rxfh;
1133 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301134 u32 *indir = NULL, indir_bytes = 0;
1135 u8 *hkey = NULL;
1136 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301137 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001138 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301139
Eyal Perry892311f2014-12-02 18:12:10 +02001140 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301141 return -EOPNOTSUPP;
1142
1143 if (ops->get_rxfh_indir_size)
1144 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301145 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001146 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301147
Ben Hutchingsf062a382014-05-15 16:28:07 +01001148 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301149 return -EFAULT;
1150
Ben Hutchingsf062a382014-05-15 16:28:07 +01001151 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001152 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001153 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001154 /* Most drivers don't handle rss_context, check it's 0 as well */
1155 if (rxfh.rss_context && !ops->set_rxfh_context)
1156 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001157
Eyal Perry892311f2014-12-02 18:12:10 +02001158 /* If either indir, hash key or function is valid, proceed further.
1159 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301160 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001161 if ((rxfh.indir_size &&
1162 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1163 rxfh.indir_size != dev_indir_size) ||
1164 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1165 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001166 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301167 return -EINVAL;
1168
Ben Hutchingsf062a382014-05-15 16:28:07 +01001169 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301170 indir_bytes = dev_indir_size * sizeof(indir[0]);
1171
Ben Hutchingsf062a382014-05-15 16:28:07 +01001172 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301173 if (!rss_config)
1174 return -ENOMEM;
1175
1176 rx_rings.cmd = ETHTOOL_GRXRINGS;
1177 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1178 if (ret)
1179 goto out;
1180
Edward Cree84a1d9c42018-03-08 15:45:03 +00001181 /* rxfh.indir_size == 0 means reset the indir table to default (master
1182 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001183 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301184 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001185 if (rxfh.indir_size &&
1186 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301187 indir = (u32 *)rss_config;
1188 ret = ethtool_copy_validate_indir(indir,
1189 useraddr + rss_cfg_offset,
1190 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001191 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301192 if (ret)
1193 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001194 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001195 if (rxfh.rss_context == 0) {
1196 indir = (u32 *)rss_config;
1197 for (i = 0; i < dev_indir_size; i++)
1198 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1199 } else {
1200 delete = true;
1201 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301202 }
1203
Ben Hutchingsf062a382014-05-15 16:28:07 +01001204 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301205 hkey = rss_config + indir_bytes;
1206 if (copy_from_user(hkey,
1207 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001208 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301209 ret = -EFAULT;
1210 goto out;
1211 }
1212 }
1213
Edward Cree84a1d9c42018-03-08 15:45:03 +00001214 if (rxfh.rss_context)
1215 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1216 &rxfh.rss_context, delete);
1217 else
1218 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001219 if (ret)
1220 goto out;
1221
Edward Cree84a1d9c42018-03-08 15:45:03 +00001222 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1223 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1224 ret = -EFAULT;
1225
1226 if (!rxfh.rss_context) {
1227 /* indicate whether rxfh was set to default */
1228 if (rxfh.indir_size == 0)
1229 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1230 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1231 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1232 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301233
1234out:
1235 kfree(rss_config);
1236 return ret;
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1240{
1241 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001242 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 void *regbuf;
1244 int reglen, ret;
1245
1246 if (!ops->get_regs || !ops->get_regs_len)
1247 return -EOPNOTSUPP;
1248
1249 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1250 return -EFAULT;
1251
1252 reglen = ops->get_regs_len(dev);
Yunsheng Linf9fc54d2018-12-26 19:51:46 +08001253 if (reglen <= 0)
1254 return reglen;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (regs.len > reglen)
1257 regs.len = reglen;
1258
Dan Carpenteref76c772019-02-01 11:24:06 +03001259 regbuf = vzalloc(reglen);
1260 if (!regbuf)
1261 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Vivien Didelot0ee4e762019-06-03 16:57:13 -04001263 if (regs.len < reglen)
1264 reglen = regs.len;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 ops->get_regs(dev, &regs, regbuf);
1267
1268 ret = -EFAULT;
1269 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1270 goto out;
1271 useraddr += offsetof(struct ethtool_regs, data);
Vivien Didelot0ee4e762019-06-03 16:57:13 -04001272 if (copy_to_user(useraddr, regbuf, reglen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto out;
1274 ret = 0;
1275
1276 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001277 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return ret;
1279}
1280
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001281static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1282{
1283 struct ethtool_value reset;
1284 int ret;
1285
1286 if (!dev->ethtool_ops->reset)
1287 return -EOPNOTSUPP;
1288
1289 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1290 return -EFAULT;
1291
1292 ret = dev->ethtool_ops->reset(dev, &reset.data);
1293 if (ret)
1294 return ret;
1295
1296 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1297 return -EFAULT;
1298 return 0;
1299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1302{
zhanglin5ff223e2019-10-26 15:54:16 +08001303 struct ethtool_wolinfo wol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (!dev->ethtool_ops->get_wol)
1306 return -EOPNOTSUPP;
1307
zhanglin5ff223e2019-10-26 15:54:16 +08001308 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1309 wol.cmd = ETHTOOL_GWOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 dev->ethtool_ops->get_wol(dev, &wol);
1311
1312 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1313 return -EFAULT;
1314 return 0;
1315}
1316
1317static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1318{
1319 struct ethtool_wolinfo wol;
Heiner Kallweit61941142018-09-24 21:58:59 +02001320 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (!dev->ethtool_ops->set_wol)
1323 return -EOPNOTSUPP;
1324
1325 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1326 return -EFAULT;
1327
Heiner Kallweit61941142018-09-24 21:58:59 +02001328 ret = dev->ethtool_ops->set_wol(dev, &wol);
1329 if (ret)
1330 return ret;
1331
1332 dev->wol_enabled = !!wol.wolopts;
Michal Kubecek67bffa72020-01-26 23:11:19 +01001333 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
Heiner Kallweit61941142018-09-24 21:58:59 +02001334
1335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001338static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1339{
1340 struct ethtool_eee edata;
1341 int rc;
1342
1343 if (!dev->ethtool_ops->get_eee)
1344 return -EOPNOTSUPP;
1345
1346 memset(&edata, 0, sizeof(struct ethtool_eee));
1347 edata.cmd = ETHTOOL_GEEE;
1348 rc = dev->ethtool_ops->get_eee(dev, &edata);
1349
1350 if (rc)
1351 return rc;
1352
1353 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1354 return -EFAULT;
1355
1356 return 0;
1357}
1358
1359static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1360{
1361 struct ethtool_eee edata;
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +01001362 int ret;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001363
1364 if (!dev->ethtool_ops->set_eee)
1365 return -EOPNOTSUPP;
1366
1367 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1368 return -EFAULT;
1369
Michal Kubecek6c5bc8f2020-03-28 00:01:48 +01001370 ret = dev->ethtool_ops->set_eee(dev, &edata);
1371 if (!ret)
1372 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1373 return ret;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static int ethtool_nway_reset(struct net_device *dev)
1377{
1378 if (!dev->ethtool_ops->nway_reset)
1379 return -EOPNOTSUPP;
1380
1381 return dev->ethtool_ops->nway_reset(dev);
1382}
1383
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001384static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1385{
1386 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001387 int link = __ethtool_get_link(dev);
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001388
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001389 if (link < 0)
1390 return link;
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001391
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001392 edata.data = link;
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001393 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1394 return -EFAULT;
1395 return 0;
1396}
1397
Ben Hutchings081d0942012-04-12 00:42:12 +00001398static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1399 int (*getter)(struct net_device *,
1400 struct ethtool_eeprom *, u8 *),
1401 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001404 void __user *userbuf = useraddr + sizeof(eeprom);
1405 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001407 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1410 return -EFAULT;
1411
1412 /* Check for wrap and zero */
1413 if (eeprom.offset + eeprom.len <= eeprom.offset)
1414 return -EINVAL;
1415
1416 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001417 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return -EINVAL;
1419
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001420 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (!data)
1422 return -ENOMEM;
1423
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001424 bytes_remaining = eeprom.len;
1425 while (bytes_remaining > 0) {
1426 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Ben Hutchings081d0942012-04-12 00:42:12 +00001428 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001429 if (ret)
1430 break;
1431 if (copy_to_user(userbuf, data, eeprom.len)) {
1432 ret = -EFAULT;
1433 break;
1434 }
1435 userbuf += eeprom.len;
1436 eeprom.offset += eeprom.len;
1437 bytes_remaining -= eeprom.len;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001440 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1441 eeprom.offset -= eeprom.len;
1442 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1443 ret = -EFAULT;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 kfree(data);
1446 return ret;
1447}
1448
Ben Hutchings081d0942012-04-12 00:42:12 +00001449static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1450{
1451 const struct ethtool_ops *ops = dev->ethtool_ops;
1452
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001453 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1454 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001455 return -EOPNOTSUPP;
1456
1457 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1458 ops->get_eeprom_len(dev));
1459}
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1462{
1463 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001464 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001465 void __user *userbuf = useraddr + sizeof(eeprom);
1466 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001468 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001470 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1471 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EOPNOTSUPP;
1473
1474 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1475 return -EFAULT;
1476
1477 /* Check for wrap and zero */
1478 if (eeprom.offset + eeprom.len <= eeprom.offset)
1479 return -EINVAL;
1480
1481 /* Check for exceeding total eeprom len */
1482 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1483 return -EINVAL;
1484
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001485 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (!data)
1487 return -ENOMEM;
1488
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001489 bytes_remaining = eeprom.len;
1490 while (bytes_remaining > 0) {
1491 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001493 if (copy_from_user(data, userbuf, eeprom.len)) {
1494 ret = -EFAULT;
1495 break;
1496 }
1497 ret = ops->set_eeprom(dev, &eeprom, data);
1498 if (ret)
1499 break;
1500 userbuf += eeprom.len;
1501 eeprom.offset += eeprom.len;
1502 bytes_remaining -= eeprom.len;
1503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 kfree(data);
1506 return ret;
1507}
1508
chavey97f8aef2010-04-07 21:54:42 -07001509static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1510 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Roland Dreier8e557422010-02-11 12:14:23 -08001512 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Heiner Kallweit31610712020-05-23 17:40:25 +02001513 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 if (!dev->ethtool_ops->get_coalesce)
1516 return -EOPNOTSUPP;
1517
Heiner Kallweit31610712020-05-23 17:40:25 +02001518 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce);
1519 if (ret)
1520 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1523 return -EFAULT;
1524 return 0;
1525}
1526
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001527static bool
1528ethtool_set_coalesce_supported(struct net_device *dev,
1529 struct ethtool_coalesce *coalesce)
1530{
1531 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1532 u32 nonzero_params = 0;
1533
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001534 if (coalesce->rx_coalesce_usecs)
1535 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1536 if (coalesce->rx_max_coalesced_frames)
1537 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1538 if (coalesce->rx_coalesce_usecs_irq)
1539 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1540 if (coalesce->rx_max_coalesced_frames_irq)
1541 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1542 if (coalesce->tx_coalesce_usecs)
1543 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1544 if (coalesce->tx_max_coalesced_frames)
1545 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1546 if (coalesce->tx_coalesce_usecs_irq)
1547 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1548 if (coalesce->tx_max_coalesced_frames_irq)
1549 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1550 if (coalesce->stats_block_coalesce_usecs)
1551 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1552 if (coalesce->use_adaptive_rx_coalesce)
1553 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1554 if (coalesce->use_adaptive_tx_coalesce)
1555 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1556 if (coalesce->pkt_rate_low)
1557 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1558 if (coalesce->rx_coalesce_usecs_low)
1559 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1560 if (coalesce->rx_max_coalesced_frames_low)
1561 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1562 if (coalesce->tx_coalesce_usecs_low)
1563 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1564 if (coalesce->tx_max_coalesced_frames_low)
1565 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1566 if (coalesce->pkt_rate_high)
1567 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1568 if (coalesce->rx_coalesce_usecs_high)
1569 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1570 if (coalesce->rx_max_coalesced_frames_high)
1571 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1572 if (coalesce->tx_coalesce_usecs_high)
1573 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1574 if (coalesce->tx_max_coalesced_frames_high)
1575 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1576 if (coalesce->rate_sample_interval)
1577 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1578
1579 return (supported_params & nonzero_params) == nonzero_params;
1580}
1581
chavey97f8aef2010-04-07 21:54:42 -07001582static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1583 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct ethtool_coalesce coalesce;
Michal Kubecek0cf3eac2020-03-28 00:01:18 +01001586 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
David S. Millerfa04ae52005-06-06 15:07:19 -07001588 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return -EOPNOTSUPP;
1590
1591 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1592 return -EFAULT;
1593
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08001594 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1595 return -EOPNOTSUPP;
1596
Michal Kubecek0cf3eac2020-03-28 00:01:18 +01001597 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce);
1598 if (!ret)
1599 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1600 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1604{
Roland Dreier8e557422010-02-11 12:14:23 -08001605 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 if (!dev->ethtool_ops->get_ringparam)
1608 return -EOPNOTSUPP;
1609
1610 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1611
1612 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1613 return -EFAULT;
1614 return 0;
1615}
1616
1617static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1618{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001619 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Michal Kubecekbc9d1c92020-03-12 21:08:33 +01001620 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001622 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return -EOPNOTSUPP;
1624
1625 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1626 return -EFAULT;
1627
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001628 dev->ethtool_ops->get_ringparam(dev, &max);
1629
1630 /* ensure new ring parameters are within the maximums */
1631 if (ringparam.rx_pending > max.rx_max_pending ||
1632 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1633 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1634 ringparam.tx_pending > max.tx_max_pending)
1635 return -EINVAL;
1636
Michal Kubecekbc9d1c92020-03-12 21:08:33 +01001637 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1638 if (!ret)
1639 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1640 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
amit salecha8b5933c2011-04-07 01:58:42 +00001643static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1644 void __user *useraddr)
1645{
1646 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1647
1648 if (!dev->ethtool_ops->get_channels)
1649 return -EOPNOTSUPP;
1650
1651 dev->ethtool_ops->get_channels(dev, &channels);
1652
1653 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1654 return -EFAULT;
1655 return 0;
1656}
1657
1658static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1659 void __user *useraddr)
1660{
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001661 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
Jakub Kicinski1661d342018-10-01 14:51:36 +02001662 u16 from_channel, to_channel;
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001663 u32 max_rx_in_use = 0;
Jakub Kicinski1661d342018-10-01 14:51:36 +02001664 unsigned int i;
Michal Kubecek546379b2020-03-12 21:08:48 +01001665 int ret;
amit salecha8b5933c2011-04-07 01:58:42 +00001666
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001667 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001668 return -EOPNOTSUPP;
1669
1670 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1671 return -EFAULT;
1672
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001673 dev->ethtool_ops->get_channels(dev, &curr);
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001674
Jakub Kicinski75c36db2020-05-15 12:49:02 -07001675 if (channels.rx_count == curr.rx_count &&
1676 channels.tx_count == curr.tx_count &&
1677 channels.combined_count == curr.combined_count &&
1678 channels.other_count == curr.other_count)
1679 return 0;
1680
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001681 /* ensure new counts are within the maximums */
Jakub Kicinskib8c8a2e2018-10-01 14:51:35 +02001682 if (channels.rx_count > curr.max_rx ||
1683 channels.tx_count > curr.max_tx ||
1684 channels.combined_count > curr.max_combined ||
1685 channels.other_count > curr.max_other)
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001686 return -EINVAL;
1687
Jakub Kicinski7be92512020-05-15 12:49:00 -07001688 /* ensure there is at least one RX and one TX channel */
1689 if (!channels.combined_count &&
1690 (!channels.rx_count || !channels.tx_count))
1691 return -EINVAL;
1692
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001693 /* ensure the new Rx count fits within the configured Rx flow
1694 * indirection table settings */
1695 if (netif_is_rxfh_configured(dev) &&
1696 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1697 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1698 return -EINVAL;
1699
Jakub Kicinski1661d342018-10-01 14:51:36 +02001700 /* Disabling channels, query zero-copy AF_XDP sockets */
1701 from_channel = channels.combined_count +
1702 min(channels.rx_count, channels.tx_count);
1703 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1704 for (i = from_channel; i < to_channel; i++)
1705 if (xdp_get_umem_from_qid(dev, i))
1706 return -EINVAL;
1707
Michal Kubecek546379b2020-03-12 21:08:48 +01001708 ret = dev->ethtool_ops->set_channels(dev, &channels);
1709 if (!ret)
1710 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1711 return ret;
amit salecha8b5933c2011-04-07 01:58:42 +00001712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1715{
Li RongQinge83887f2019-02-27 20:47:57 +08001716 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 if (!dev->ethtool_ops->get_pauseparam)
1719 return -EOPNOTSUPP;
1720
1721 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1722
1723 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1724 return -EFAULT;
1725 return 0;
1726}
1727
1728static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1729{
1730 struct ethtool_pauseparam pauseparam;
Michal Kubecekbf37faa2020-03-28 00:01:33 +01001731 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Jeff Garzike1b90c42006-07-17 12:54:40 -04001733 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return -EOPNOTSUPP;
1735
1736 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1737 return -EFAULT;
1738
Michal Kubecekbf37faa2020-03-28 00:01:33 +01001739 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1740 if (!ret)
1741 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1746{
1747 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001748 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001750 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001752 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001753 return -EOPNOTSUPP;
1754
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001755 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001756 if (test_len < 0)
1757 return test_len;
1758 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 if (copy_from_user(&test, useraddr, sizeof(test)))
1761 return -EFAULT;
1762
Jeff Garzikff03d492007-08-15 16:01:08 -07001763 test.len = test_len;
Kees Cook6da2ec52018-06-12 13:55:00 -07001764 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (!data)
1766 return -ENOMEM;
1767
Andrew Lunn77e9b2a2020-04-20 00:11:52 +02001768 netif_testing_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 ops->self_test(dev, &test, data);
Andrew Lunn77e9b2a2020-04-20 00:11:52 +02001770 netif_testing_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Li RongQing3d883022019-03-29 09:18:02 +08001803 if (gstrings.len) {
1804 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1805 if (!data)
1806 return -ENOMEM;
1807
1808 __ethtool_get_strings(dev, gstrings.string_set, data);
1809 } else {
1810 data = NULL;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 ret = -EFAULT;
1814 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1815 goto out;
1816 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001817 if (gstrings.len &&
1818 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 goto out;
1820 ret = 0;
1821
Michał Mirosław340ae162011-02-15 16:59:16 +00001822out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001823 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return ret;
1825}
1826
1827static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1828{
1829 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001830 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001831 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001832 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001834 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return -EOPNOTSUPP;
1836
Ben Hutchings68f512f2011-04-02 00:35:15 +01001837 if (busy)
1838 return -EBUSY;
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (copy_from_user(&id, useraddr, sizeof(id)))
1841 return -EFAULT;
1842
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001843 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001844 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001845 return rc;
1846
1847 /* Drop the RTNL lock while waiting, but prevent reentry or
1848 * removal of the device.
1849 */
1850 busy = true;
1851 dev_hold(dev);
1852 rtnl_unlock();
1853
1854 if (rc == 0) {
1855 /* Driver will handle this itself */
1856 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001857 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001858 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001859 /* Driver expects to be called at twice the frequency in rc */
1860 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001861
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001862 /* Count down seconds */
1863 do {
1864 /* Count down iterations per second */
1865 i = n;
1866 do {
1867 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001868 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001869 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1870 rtnl_unlock();
1871 if (rc)
1872 break;
1873 schedule_timeout_interruptible(interval);
1874 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001875 } while (!signal_pending(current) &&
1876 (id.data == 0 || --id.data != 0));
1877 }
1878
1879 rtnl_lock();
1880 dev_put(dev);
1881 busy = false;
1882
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001883 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001884 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
1887static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1888{
1889 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001890 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001892 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001894 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001895 return -EOPNOTSUPP;
1896
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001897 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001898 if (n_stats < 0)
1899 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001900 if (n_stats > S32_MAX / sizeof(u64))
1901 return -ENOMEM;
1902 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1904 return -EFAULT;
1905
Jeff Garzikff03d492007-08-15 16:01:08 -07001906 stats.n_stats = n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Li RongQing3d883022019-03-29 09:18:02 +08001908 if (n_stats) {
1909 data = vzalloc(array_size(n_stats, sizeof(u64)));
1910 if (!data)
1911 return -ENOMEM;
1912 ops->get_ethtool_stats(dev, &stats, data);
1913 } else {
1914 data = NULL;
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 ret = -EFAULT;
1918 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1919 goto out;
1920 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001921 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 goto out;
1923 ret = 0;
1924
1925 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001926 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return ret;
1928}
1929
Andrew Lunnf3a40942015-12-30 16:28:25 +01001930static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1931{
Florian Fainelli99943382018-04-25 12:12:48 -07001932 const struct ethtool_ops *ops = dev->ethtool_ops;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001933 struct phy_device *phydev = dev->phydev;
Florian Fainelli99943382018-04-25 12:12:48 -07001934 struct ethtool_stats stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001935 u64 *data;
1936 int ret, n_stats;
1937
Florian Fainelli99943382018-04-25 12:12:48 -07001938 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
Andrew Lunnf3a40942015-12-30 16:28:25 +01001939 return -EOPNOTSUPP;
1940
Florian Fainelli99943382018-04-25 12:12:48 -07001941 if (dev->phydev && !ops->get_ethtool_phy_stats)
1942 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1943 else
1944 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001945 if (n_stats < 0)
1946 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001947 if (n_stats > S32_MAX / sizeof(u64))
1948 return -ENOMEM;
1949 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001950
1951 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1952 return -EFAULT;
1953
1954 stats.n_stats = n_stats;
Andrew Lunnf3a40942015-12-30 16:28:25 +01001955
Li RongQing3d883022019-03-29 09:18:02 +08001956 if (n_stats) {
1957 data = vzalloc(array_size(n_stats, sizeof(u64)));
1958 if (!data)
1959 return -ENOMEM;
1960
1961 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1962 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1963 if (ret < 0)
1964 goto out;
1965 } else {
1966 ops->get_ethtool_phy_stats(dev, &stats, data);
1967 }
Florian Fainelli99943382018-04-25 12:12:48 -07001968 } else {
Li RongQing3d883022019-03-29 09:18:02 +08001969 data = NULL;
Florian Fainelli99943382018-04-25 12:12:48 -07001970 }
Andrew Lunnf3a40942015-12-30 16:28:25 +01001971
1972 ret = -EFAULT;
1973 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1974 goto out;
1975 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001976 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01001977 goto out;
1978 ret = 0;
1979
1980 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001981 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001982 return ret;
1983}
1984
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001985static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001986{
1987 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001988
Matthew Wilcox313674a2007-07-31 14:00:29 -07001989 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001990 return -EFAULT;
1991
Matthew Wilcox313674a2007-07-31 14:00:29 -07001992 if (epaddr.size < dev->addr_len)
1993 return -ETOOSMALL;
1994 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001995
Jon Wetzela6f9a702005-08-20 17:15:54 -07001996 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001997 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001998 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001999 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2000 return -EFAULT;
2001 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002002}
2003
Jeff Garzik13c99b22007-08-15 16:01:56 -07002004static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2005 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002006{
Roland Dreier8e557422010-02-11 12:14:23 -08002007 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002008
Jeff Garzik13c99b22007-08-15 16:01:56 -07002009 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002010 return -EOPNOTSUPP;
2011
Jeff Garzik13c99b22007-08-15 16:01:56 -07002012 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002013
2014 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2015 return -EFAULT;
2016 return 0;
2017}
2018
Jeff Garzik13c99b22007-08-15 16:01:56 -07002019static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2020 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002021{
2022 struct ethtool_value edata;
2023
Jeff Garzik13c99b22007-08-15 16:01:56 -07002024 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002025 return -EOPNOTSUPP;
2026
2027 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2028 return -EFAULT;
2029
Jeff Garzik13c99b22007-08-15 16:01:56 -07002030 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002031 return 0;
2032}
2033
Jeff Garzik13c99b22007-08-15 16:01:56 -07002034static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2035 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002036{
2037 struct ethtool_value edata;
2038
Jeff Garzik13c99b22007-08-15 16:01:56 -07002039 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002040 return -EOPNOTSUPP;
2041
2042 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2043 return -EFAULT;
2044
Jeff Garzik13c99b22007-08-15 16:01:56 -07002045 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002046}
2047
chavey97f8aef2010-04-07 21:54:42 -07002048static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2049 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002050{
2051 struct ethtool_flash efl;
2052
2053 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2054 return -EFAULT;
Ben Hutchings786f5282012-02-01 09:32:25 +00002055 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2056
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08002057 if (!dev->ethtool_ops->flash_device)
2058 return devlink_compat_flash_update(dev, efl.data);
Jakub Kicinski4eceba12019-02-14 13:40:45 -08002059
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002060 return dev->ethtool_ops->flash_device(dev, &efl);
2061}
2062
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002063static int ethtool_set_dump(struct net_device *dev,
2064 void __user *useraddr)
2065{
2066 struct ethtool_dump dump;
2067
2068 if (!dev->ethtool_ops->set_dump)
2069 return -EOPNOTSUPP;
2070
2071 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2072 return -EFAULT;
2073
2074 return dev->ethtool_ops->set_dump(dev, &dump);
2075}
2076
2077static int ethtool_get_dump_flag(struct net_device *dev,
2078 void __user *useraddr)
2079{
2080 int ret;
2081 struct ethtool_dump dump;
2082 const struct ethtool_ops *ops = dev->ethtool_ops;
2083
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002084 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002085 return -EOPNOTSUPP;
2086
2087 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2088 return -EFAULT;
2089
2090 ret = ops->get_dump_flag(dev, &dump);
2091 if (ret)
2092 return ret;
2093
2094 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2095 return -EFAULT;
2096 return 0;
2097}
2098
2099static int ethtool_get_dump_data(struct net_device *dev,
2100 void __user *useraddr)
2101{
2102 int ret;
2103 __u32 len;
2104 struct ethtool_dump dump, tmp;
2105 const struct ethtool_ops *ops = dev->ethtool_ops;
2106 void *data = NULL;
2107
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002108 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002109 return -EOPNOTSUPP;
2110
2111 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2112 return -EFAULT;
2113
2114 memset(&tmp, 0, sizeof(tmp));
2115 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2116 ret = ops->get_dump_flag(dev, &tmp);
2117 if (ret)
2118 return ret;
2119
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002120 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002121 if (!len)
2122 return -EFAULT;
2123
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002124 /* Don't ever let the driver think there's more space available
2125 * than it requested with .get_dump_flag().
2126 */
2127 dump.len = len;
2128
2129 /* Always allocate enough space to hold the whole thing so that the
2130 * driver does not need to check the length and bother with partial
2131 * dumping.
2132 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002133 data = vzalloc(tmp.len);
2134 if (!data)
2135 return -ENOMEM;
2136 ret = ops->get_dump_data(dev, &dump, data);
2137 if (ret)
2138 goto out;
2139
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002140 /* There are two sane possibilities:
2141 * 1. The driver's .get_dump_data() does not touch dump.len.
2142 * 2. Or it may set dump.len to how much it really writes, which
2143 * should be tmp.len (or len if it can do a partial dump).
2144 * In any case respond to userspace with the actual length of data
2145 * it's receiving.
2146 */
2147 WARN_ON(dump.len != len && dump.len != tmp.len);
2148 dump.len = len;
2149
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002150 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2151 ret = -EFAULT;
2152 goto out;
2153 }
2154 useraddr += offsetof(struct ethtool_dump, data);
2155 if (copy_to_user(useraddr, data, len))
2156 ret = -EFAULT;
2157out:
2158 vfree(data);
2159 return ret;
2160}
2161
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002162static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2163{
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002164 struct ethtool_ts_info info;
Michal Kubecek5b071c52020-03-28 00:01:58 +01002165 int err;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002166
Michal Kubecek5b071c52020-03-28 00:01:58 +01002167 err = __ethtool_get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002168 if (err)
2169 return err;
2170
2171 if (copy_to_user(useraddr, &info, sizeof(info)))
Michal Kubecek5b071c52020-03-28 00:01:58 +01002172 return -EFAULT;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002173
Michal Kubecek5b071c52020-03-28 00:01:58 +01002174 return 0;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002175}
2176
Ed Swierk2f438362015-01-02 17:27:56 -08002177static int __ethtool_get_module_info(struct net_device *dev,
2178 struct ethtool_modinfo *modinfo)
2179{
2180 const struct ethtool_ops *ops = dev->ethtool_ops;
2181 struct phy_device *phydev = dev->phydev;
2182
Russell Kinge679c9c2018-03-28 15:44:16 -07002183 if (dev->sfp_bus)
2184 return sfp_get_module_info(dev->sfp_bus, modinfo);
2185
Ed Swierk2f438362015-01-02 17:27:56 -08002186 if (phydev && phydev->drv && phydev->drv->module_info)
2187 return phydev->drv->module_info(phydev, modinfo);
2188
2189 if (ops->get_module_info)
2190 return ops->get_module_info(dev, modinfo);
2191
2192 return -EOPNOTSUPP;
2193}
2194
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002195static int ethtool_get_module_info(struct net_device *dev,
2196 void __user *useraddr)
2197{
2198 int ret;
2199 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002200
2201 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2202 return -EFAULT;
2203
Ed Swierk2f438362015-01-02 17:27:56 -08002204 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002205 if (ret)
2206 return ret;
2207
2208 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2209 return -EFAULT;
2210
2211 return 0;
2212}
2213
Ed Swierk2f438362015-01-02 17:27:56 -08002214static int __ethtool_get_module_eeprom(struct net_device *dev,
2215 struct ethtool_eeprom *ee, u8 *data)
2216{
2217 const struct ethtool_ops *ops = dev->ethtool_ops;
2218 struct phy_device *phydev = dev->phydev;
2219
Russell Kinge679c9c2018-03-28 15:44:16 -07002220 if (dev->sfp_bus)
2221 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2222
Ed Swierk2f438362015-01-02 17:27:56 -08002223 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2224 return phydev->drv->module_eeprom(phydev, ee, data);
2225
2226 if (ops->get_module_eeprom)
2227 return ops->get_module_eeprom(dev, ee, data);
2228
2229 return -EOPNOTSUPP;
2230}
2231
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002232static int ethtool_get_module_eeprom(struct net_device *dev,
2233 void __user *useraddr)
2234{
2235 int ret;
2236 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002237
Ed Swierk2f438362015-01-02 17:27:56 -08002238 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002239 if (ret)
2240 return ret;
2241
Ed Swierk2f438362015-01-02 17:27:56 -08002242 return ethtool_get_any_eeprom(dev, useraddr,
2243 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002244 modinfo.eeprom_len);
2245}
2246
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302247static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2248{
2249 switch (tuna->id) {
2250 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002251 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302252 if (tuna->len != sizeof(u32) ||
2253 tuna->type_id != ETHTOOL_TUNABLE_U32)
2254 return -EINVAL;
2255 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002256 case ETHTOOL_PFC_PREVENTION_TOUT:
2257 if (tuna->len != sizeof(u16) ||
2258 tuna->type_id != ETHTOOL_TUNABLE_U16)
2259 return -EINVAL;
2260 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302261 default:
2262 return -EINVAL;
2263 }
2264
2265 return 0;
2266}
2267
2268static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2269{
2270 int ret;
2271 struct ethtool_tunable tuna;
2272 const struct ethtool_ops *ops = dev->ethtool_ops;
2273 void *data;
2274
2275 if (!ops->get_tunable)
2276 return -EOPNOTSUPP;
2277 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2278 return -EFAULT;
2279 ret = ethtool_tunable_valid(&tuna);
2280 if (ret)
2281 return ret;
2282 data = kmalloc(tuna.len, GFP_USER);
2283 if (!data)
2284 return -ENOMEM;
2285 ret = ops->get_tunable(dev, &tuna, data);
2286 if (ret)
2287 goto out;
2288 useraddr += sizeof(tuna);
2289 ret = -EFAULT;
2290 if (copy_to_user(useraddr, data, tuna.len))
2291 goto out;
2292 ret = 0;
2293
2294out:
2295 kfree(data);
2296 return ret;
2297}
2298
2299static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2300{
2301 int ret;
2302 struct ethtool_tunable tuna;
2303 const struct ethtool_ops *ops = dev->ethtool_ops;
2304 void *data;
2305
2306 if (!ops->set_tunable)
2307 return -EOPNOTSUPP;
2308 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2309 return -EFAULT;
2310 ret = ethtool_tunable_valid(&tuna);
2311 if (ret)
2312 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302313 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002314 data = memdup_user(useraddr, tuna.len);
2315 if (IS_ERR(data))
2316 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302317 ret = ops->set_tunable(dev, &tuna, data);
2318
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302319 kfree(data);
2320 return ret;
2321}
2322
Arnd Bergmann3499e872019-03-07 16:58:35 +01002323static noinline_for_stack int
2324ethtool_get_per_queue_coalesce(struct net_device *dev,
2325 void __user *useraddr,
2326 struct ethtool_per_queue_op *per_queue_opt)
Kan Liang421797b2016-02-19 09:24:02 -05002327{
2328 u32 bit;
2329 int ret;
2330 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2331
2332 if (!dev->ethtool_ops->get_per_queue_coalesce)
2333 return -EOPNOTSUPP;
2334
2335 useraddr += sizeof(*per_queue_opt);
2336
Yury Norov3aa56882018-02-06 15:38:06 -08002337 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2338 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002339
2340 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2341 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2342
2343 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2344 if (ret != 0)
2345 return ret;
2346 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2347 return -EFAULT;
2348 useraddr += sizeof(coalesce);
2349 }
2350
2351 return 0;
2352}
2353
Arnd Bergmann3499e872019-03-07 16:58:35 +01002354static noinline_for_stack int
2355ethtool_set_per_queue_coalesce(struct net_device *dev,
2356 void __user *useraddr,
2357 struct ethtool_per_queue_op *per_queue_opt)
Kan Liangf38d1382016-02-19 09:24:03 -05002358{
2359 u32 bit;
2360 int i, ret = 0;
2361 int n_queue;
2362 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2363 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2364
2365 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2366 (!dev->ethtool_ops->get_per_queue_coalesce))
2367 return -EOPNOTSUPP;
2368
2369 useraddr += sizeof(*per_queue_opt);
2370
Yury Norov3aa56882018-02-06 15:38:06 -08002371 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002372 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2373 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2374 if (!backup)
2375 return -ENOMEM;
2376
2377 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2378 struct ethtool_coalesce coalesce;
2379
2380 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2381 if (ret != 0)
2382 goto roll_back;
2383
2384 tmp++;
2385
2386 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2387 ret = -EFAULT;
2388 goto roll_back;
2389 }
2390
Jakub Kicinski95cddcb2020-03-04 21:15:31 -08002391 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2392 ret = -EOPNOTSUPP;
2393 goto roll_back;
2394 }
2395
Kan Liangf38d1382016-02-19 09:24:03 -05002396 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2397 if (ret != 0)
2398 goto roll_back;
2399
2400 useraddr += sizeof(coalesce);
2401 }
2402
2403roll_back:
2404 if (ret != 0) {
2405 tmp = backup;
2406 for_each_set_bit(i, queue_mask, bit) {
2407 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2408 tmp++;
2409 }
2410 }
2411 kfree(backup);
2412
2413 return ret;
2414}
2415
Arnd Bergmann3499e872019-03-07 16:58:35 +01002416static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002417 void __user *useraddr, u32 sub_cmd)
Kan Liangac2c7ad2016-02-19 09:24:01 -05002418{
2419 struct ethtool_per_queue_op per_queue_opt;
2420
2421 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2422 return -EFAULT;
2423
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002424 if (per_queue_opt.sub_command != sub_cmd)
2425 return -EINVAL;
2426
Kan Liangac2c7ad2016-02-19 09:24:01 -05002427 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002428 case ETHTOOL_GCOALESCE:
2429 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002430 case ETHTOOL_SCOALESCE:
2431 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002432 default:
2433 return -EOPNOTSUPP;
2434 };
2435}
2436
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002437static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2438{
2439 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002440 case ETHTOOL_PHY_DOWNSHIFT:
Heiner Kallweit3aeb0802019-03-25 19:34:58 +01002441 case ETHTOOL_PHY_FAST_LINK_DOWN:
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002442 if (tuna->len != sizeof(u8) ||
2443 tuna->type_id != ETHTOOL_TUNABLE_U8)
2444 return -EINVAL;
2445 break;
Alexandru Ardelean9f2f13f2019-09-16 10:35:25 +03002446 case ETHTOOL_PHY_EDPD:
2447 if (tuna->len != sizeof(u16) ||
2448 tuna->type_id != ETHTOOL_TUNABLE_U16)
2449 return -EINVAL;
2450 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002451 default:
2452 return -EINVAL;
2453 }
2454
2455 return 0;
2456}
2457
2458static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2459{
2460 int ret;
2461 struct ethtool_tunable tuna;
2462 struct phy_device *phydev = dev->phydev;
2463 void *data;
2464
2465 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2466 return -EOPNOTSUPP;
2467
2468 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2469 return -EFAULT;
2470 ret = ethtool_phy_tunable_valid(&tuna);
2471 if (ret)
2472 return ret;
2473 data = kmalloc(tuna.len, GFP_USER);
2474 if (!data)
2475 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002476 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002477 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002478 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002479 if (ret)
2480 goto out;
2481 useraddr += sizeof(tuna);
2482 ret = -EFAULT;
2483 if (copy_to_user(useraddr, data, tuna.len))
2484 goto out;
2485 ret = 0;
2486
2487out:
2488 kfree(data);
2489 return ret;
2490}
2491
2492static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2493{
2494 int ret;
2495 struct ethtool_tunable tuna;
2496 struct phy_device *phydev = dev->phydev;
2497 void *data;
2498
2499 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2500 return -EOPNOTSUPP;
2501 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2502 return -EFAULT;
2503 ret = ethtool_phy_tunable_valid(&tuna);
2504 if (ret)
2505 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002506 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002507 data = memdup_user(useraddr, tuna.len);
2508 if (IS_ERR(data))
2509 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002510 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002511 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002512 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002513
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002514 kfree(data);
2515 return ret;
2516}
2517
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002518static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2519{
Li RongQinge83887f2019-02-27 20:47:57 +08002520 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002521 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002522
2523 if (!dev->ethtool_ops->get_fecparam)
2524 return -EOPNOTSUPP;
2525
Edward Creea6d50512018-02-28 19:15:58 +00002526 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2527 if (rc)
2528 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002529
2530 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2531 return -EFAULT;
2532 return 0;
2533}
2534
2535static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2536{
2537 struct ethtool_fecparam fecparam;
2538
2539 if (!dev->ethtool_ops->set_fecparam)
2540 return -EOPNOTSUPP;
2541
2542 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2543 return -EFAULT;
2544
2545 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2546}
2547
Yan Burman600fed52013-06-03 02:03:34 +00002548/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Eric W. Biederman881d9662007-09-17 11:56:21 -07002550int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002552 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002554 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002556 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (!dev || !netif_device_present(dev))
2559 return -ENODEV;
2560
chavey97f8aef2010-04-07 21:54:42 -07002561 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return -EFAULT;
2563
Kan Liangac2c7ad2016-02-19 09:24:01 -05002564 if (ethcmd == ETHTOOL_PERQUEUE) {
2565 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2566 return -EFAULT;
2567 } else {
2568 sub_cmd = ethcmd;
2569 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002570 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002571 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002572 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002573 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002574 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002575 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002576 case ETHTOOL_GCOALESCE:
2577 case ETHTOOL_GRINGPARAM:
2578 case ETHTOOL_GPAUSEPARAM:
2579 case ETHTOOL_GRXCSUM:
2580 case ETHTOOL_GTXCSUM:
2581 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002582 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002583 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002584 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002585 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002586 case ETHTOOL_GTSO:
2587 case ETHTOOL_GPERMADDR:
Maciej Żenczykowski474ff262018-09-22 01:34:01 -07002588 case ETHTOOL_GUFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002589 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002590 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002591 case ETHTOOL_GFLAGS:
2592 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002593 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002594 case ETHTOOL_GRXRINGS:
2595 case ETHTOOL_GRXCLSRLCNT:
2596 case ETHTOOL_GRXCLSRULE:
2597 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002598 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302599 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002600 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002601 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002602 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002603 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302604 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002605 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002606 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002607 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002608 break;
2609 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002610 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002611 return -EPERM;
2612 }
2613
chavey97f8aef2010-04-07 21:54:42 -07002614 if (dev->ethtool_ops->begin) {
2615 rc = dev->ethtool_ops->begin(dev);
2616 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002618 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002619 old_features = dev->features;
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 switch (ethcmd) {
2622 case ETHTOOL_GSET:
2623 rc = ethtool_get_settings(dev, useraddr);
2624 break;
2625 case ETHTOOL_SSET:
2626 rc = ethtool_set_settings(dev, useraddr);
2627 break;
2628 case ETHTOOL_GDRVINFO:
2629 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 break;
2631 case ETHTOOL_GREGS:
2632 rc = ethtool_get_regs(dev, useraddr);
2633 break;
2634 case ETHTOOL_GWOL:
2635 rc = ethtool_get_wol(dev, useraddr);
2636 break;
2637 case ETHTOOL_SWOL:
2638 rc = ethtool_set_wol(dev, useraddr);
2639 break;
2640 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002641 rc = ethtool_get_value(dev, useraddr, ethcmd,
2642 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 break;
2644 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002645 rc = ethtool_set_value_void(dev, useraddr,
2646 dev->ethtool_ops->set_msglevel);
Michal Kubecek0bda7af2020-01-26 23:11:10 +01002647 if (!rc)
2648 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002650 case ETHTOOL_GEEE:
2651 rc = ethtool_get_eee(dev, useraddr);
2652 break;
2653 case ETHTOOL_SEEE:
2654 rc = ethtool_set_eee(dev, useraddr);
2655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 case ETHTOOL_NWAY_RST:
2657 rc = ethtool_nway_reset(dev);
2658 break;
2659 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002660 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 break;
2662 case ETHTOOL_GEEPROM:
2663 rc = ethtool_get_eeprom(dev, useraddr);
2664 break;
2665 case ETHTOOL_SEEPROM:
2666 rc = ethtool_set_eeprom(dev, useraddr);
2667 break;
2668 case ETHTOOL_GCOALESCE:
2669 rc = ethtool_get_coalesce(dev, useraddr);
2670 break;
2671 case ETHTOOL_SCOALESCE:
2672 rc = ethtool_set_coalesce(dev, useraddr);
2673 break;
2674 case ETHTOOL_GRINGPARAM:
2675 rc = ethtool_get_ringparam(dev, useraddr);
2676 break;
2677 case ETHTOOL_SRINGPARAM:
2678 rc = ethtool_set_ringparam(dev, useraddr);
2679 break;
2680 case ETHTOOL_GPAUSEPARAM:
2681 rc = ethtool_get_pauseparam(dev, useraddr);
2682 break;
2683 case ETHTOOL_SPAUSEPARAM:
2684 rc = ethtool_set_pauseparam(dev, useraddr);
2685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 case ETHTOOL_TEST:
2687 rc = ethtool_self_test(dev, useraddr);
2688 break;
2689 case ETHTOOL_GSTRINGS:
2690 rc = ethtool_get_strings(dev, useraddr);
2691 break;
2692 case ETHTOOL_PHYS_ID:
2693 rc = ethtool_phys_id(dev, useraddr);
2694 break;
2695 case ETHTOOL_GSTATS:
2696 rc = ethtool_get_stats(dev, useraddr);
2697 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002698 case ETHTOOL_GPERMADDR:
2699 rc = ethtool_get_perm_addr(dev, useraddr);
2700 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002701 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002702 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002703 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002704 break;
2705 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002706 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002707 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002708 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002709 rc = ethtool_get_value(dev, useraddr, ethcmd,
2710 dev->ethtool_ops->get_priv_flags);
Michal Kubecek111dcba2020-03-12 21:08:18 +01002711 if (!rc)
2712 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
Jeff Garzik339bf022007-08-15 16:01:32 -07002713 break;
2714 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002715 rc = ethtool_set_value(dev, useraddr,
2716 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002717 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002718 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002719 case ETHTOOL_GRXRINGS:
2720 case ETHTOOL_GRXCLSRLCNT:
2721 case ETHTOOL_GRXCLSRULE:
2722 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002723 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002724 break;
2725 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002726 case ETHTOOL_SRXCLSRLDEL:
2727 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002728 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002729 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002730 case ETHTOOL_FLASHDEV:
2731 rc = ethtool_flash_device(dev, useraddr);
2732 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002733 case ETHTOOL_RESET:
2734 rc = ethtool_reset(dev, useraddr);
2735 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002736 case ETHTOOL_GSSET_INFO:
2737 rc = ethtool_get_sset_info(dev, useraddr);
2738 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002739 case ETHTOOL_GRXFHINDIR:
2740 rc = ethtool_get_rxfh_indir(dev, useraddr);
2741 break;
2742 case ETHTOOL_SRXFHINDIR:
2743 rc = ethtool_set_rxfh_indir(dev, useraddr);
2744 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302745 case ETHTOOL_GRSSH:
2746 rc = ethtool_get_rxfh(dev, useraddr);
2747 break;
2748 case ETHTOOL_SRSSH:
2749 rc = ethtool_set_rxfh(dev, useraddr);
2750 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002751 case ETHTOOL_GFEATURES:
2752 rc = ethtool_get_features(dev, useraddr);
2753 break;
2754 case ETHTOOL_SFEATURES:
2755 rc = ethtool_set_features(dev, useraddr);
2756 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002757 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002758 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002759 case ETHTOOL_GSG:
2760 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002761 case ETHTOOL_GGSO:
2762 case ETHTOOL_GGRO:
2763 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2764 break;
2765 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002766 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002767 case ETHTOOL_SSG:
2768 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002769 case ETHTOOL_SGSO:
2770 case ETHTOOL_SGRO:
2771 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2772 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002773 case ETHTOOL_GCHANNELS:
2774 rc = ethtool_get_channels(dev, useraddr);
2775 break;
2776 case ETHTOOL_SCHANNELS:
2777 rc = ethtool_set_channels(dev, useraddr);
2778 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002779 case ETHTOOL_SET_DUMP:
2780 rc = ethtool_set_dump(dev, useraddr);
2781 break;
2782 case ETHTOOL_GET_DUMP_FLAG:
2783 rc = ethtool_get_dump_flag(dev, useraddr);
2784 break;
2785 case ETHTOOL_GET_DUMP_DATA:
2786 rc = ethtool_get_dump_data(dev, useraddr);
2787 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002788 case ETHTOOL_GET_TS_INFO:
2789 rc = ethtool_get_ts_info(dev, useraddr);
2790 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002791 case ETHTOOL_GMODULEINFO:
2792 rc = ethtool_get_module_info(dev, useraddr);
2793 break;
2794 case ETHTOOL_GMODULEEEPROM:
2795 rc = ethtool_get_module_eeprom(dev, useraddr);
2796 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302797 case ETHTOOL_GTUNABLE:
2798 rc = ethtool_get_tunable(dev, useraddr);
2799 break;
2800 case ETHTOOL_STUNABLE:
2801 rc = ethtool_set_tunable(dev, useraddr);
2802 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002803 case ETHTOOL_GPHYSTATS:
2804 rc = ethtool_get_phy_stats(dev, useraddr);
2805 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002806 case ETHTOOL_PERQUEUE:
Wenwen Wang58f5bbe2018-10-08 10:49:35 -05002807 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002808 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002809 case ETHTOOL_GLINKSETTINGS:
2810 rc = ethtool_get_link_ksettings(dev, useraddr);
2811 break;
2812 case ETHTOOL_SLINKSETTINGS:
2813 rc = ethtool_set_link_ksettings(dev, useraddr);
2814 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002815 case ETHTOOL_PHY_GTUNABLE:
2816 rc = get_phy_tunable(dev, useraddr);
2817 break;
2818 case ETHTOOL_PHY_STUNABLE:
2819 rc = set_phy_tunable(dev, useraddr);
2820 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002821 case ETHTOOL_GFECPARAM:
2822 rc = ethtool_get_fecparam(dev, useraddr);
2823 break;
2824 case ETHTOOL_SFECPARAM:
2825 rc = ethtool_set_fecparam(dev, useraddr);
2826 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002828 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002830
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002831 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002833
2834 if (old_features != dev->features)
2835 netdev_features_change(dev);
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01002839
2840struct ethtool_rx_flow_key {
2841 struct flow_dissector_key_basic basic;
2842 union {
2843 struct flow_dissector_key_ipv4_addrs ipv4;
2844 struct flow_dissector_key_ipv6_addrs ipv6;
2845 };
2846 struct flow_dissector_key_ports tp;
2847 struct flow_dissector_key_ip ip;
2848 struct flow_dissector_key_vlan vlan;
2849 struct flow_dissector_key_eth_addrs eth_addrs;
2850} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2851
2852struct ethtool_rx_flow_match {
2853 struct flow_dissector dissector;
2854 struct ethtool_rx_flow_key key;
2855 struct ethtool_rx_flow_key mask;
2856};
2857
2858struct ethtool_rx_flow_rule *
2859ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2860{
2861 const struct ethtool_rx_flow_spec *fs = input->fs;
2862 static struct in6_addr zero_addr = {};
2863 struct ethtool_rx_flow_match *match;
2864 struct ethtool_rx_flow_rule *flow;
2865 struct flow_action_entry *act;
2866
2867 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2868 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2869 if (!flow)
2870 return ERR_PTR(-ENOMEM);
2871
2872 /* ethtool_rx supports only one single action per rule. */
2873 flow->rule = flow_rule_alloc(1);
2874 if (!flow->rule) {
2875 kfree(flow);
2876 return ERR_PTR(-ENOMEM);
2877 }
2878
2879 match = (struct ethtool_rx_flow_match *)flow->priv;
2880 flow->rule->match.dissector = &match->dissector;
2881 flow->rule->match.mask = &match->mask;
2882 flow->rule->match.key = &match->key;
2883
2884 match->mask.basic.n_proto = htons(0xffff);
2885
2886 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
Maxime Chevallier5b9469a2019-06-27 10:52:26 +02002887 case ETHER_FLOW: {
2888 const struct ethhdr *ether_spec, *ether_m_spec;
2889
2890 ether_spec = &fs->h_u.ether_spec;
2891 ether_m_spec = &fs->m_u.ether_spec;
2892
2893 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
2894 ether_addr_copy(match->key.eth_addrs.src,
2895 ether_spec->h_source);
2896 ether_addr_copy(match->mask.eth_addrs.src,
2897 ether_m_spec->h_source);
2898 }
2899 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
2900 ether_addr_copy(match->key.eth_addrs.dst,
2901 ether_spec->h_dest);
2902 ether_addr_copy(match->mask.eth_addrs.dst,
2903 ether_m_spec->h_dest);
2904 }
2905 if (ether_m_spec->h_proto) {
2906 match->key.basic.n_proto = ether_spec->h_proto;
2907 match->mask.basic.n_proto = ether_m_spec->h_proto;
2908 }
2909 }
2910 break;
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01002911 case TCP_V4_FLOW:
2912 case UDP_V4_FLOW: {
2913 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2914
2915 match->key.basic.n_proto = htons(ETH_P_IP);
2916
2917 v4_spec = &fs->h_u.tcp_ip4_spec;
2918 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2919
2920 if (v4_m_spec->ip4src) {
2921 match->key.ipv4.src = v4_spec->ip4src;
2922 match->mask.ipv4.src = v4_m_spec->ip4src;
2923 }
2924 if (v4_m_spec->ip4dst) {
2925 match->key.ipv4.dst = v4_spec->ip4dst;
2926 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2927 }
2928 if (v4_m_spec->ip4src ||
2929 v4_m_spec->ip4dst) {
2930 match->dissector.used_keys |=
2931 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2932 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2933 offsetof(struct ethtool_rx_flow_key, ipv4);
2934 }
2935 if (v4_m_spec->psrc) {
2936 match->key.tp.src = v4_spec->psrc;
2937 match->mask.tp.src = v4_m_spec->psrc;
2938 }
2939 if (v4_m_spec->pdst) {
2940 match->key.tp.dst = v4_spec->pdst;
2941 match->mask.tp.dst = v4_m_spec->pdst;
2942 }
2943 if (v4_m_spec->psrc ||
2944 v4_m_spec->pdst) {
2945 match->dissector.used_keys |=
2946 BIT(FLOW_DISSECTOR_KEY_PORTS);
2947 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2948 offsetof(struct ethtool_rx_flow_key, tp);
2949 }
2950 if (v4_m_spec->tos) {
2951 match->key.ip.tos = v4_spec->tos;
2952 match->mask.ip.tos = v4_m_spec->tos;
2953 match->dissector.used_keys |=
2954 BIT(FLOW_DISSECTOR_KEY_IP);
2955 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2956 offsetof(struct ethtool_rx_flow_key, ip);
2957 }
2958 }
2959 break;
2960 case TCP_V6_FLOW:
2961 case UDP_V6_FLOW: {
2962 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2963
2964 match->key.basic.n_proto = htons(ETH_P_IPV6);
2965
2966 v6_spec = &fs->h_u.tcp_ip6_spec;
2967 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2968 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2969 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2970 sizeof(match->key.ipv6.src));
2971 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2972 sizeof(match->mask.ipv6.src));
2973 }
2974 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2975 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2976 sizeof(match->key.ipv6.dst));
2977 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2978 sizeof(match->mask.ipv6.dst));
2979 }
2980 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2981 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2982 match->dissector.used_keys |=
2983 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2984 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2985 offsetof(struct ethtool_rx_flow_key, ipv6);
2986 }
2987 if (v6_m_spec->psrc) {
2988 match->key.tp.src = v6_spec->psrc;
2989 match->mask.tp.src = v6_m_spec->psrc;
2990 }
2991 if (v6_m_spec->pdst) {
2992 match->key.tp.dst = v6_spec->pdst;
2993 match->mask.tp.dst = v6_m_spec->pdst;
2994 }
2995 if (v6_m_spec->psrc ||
2996 v6_m_spec->pdst) {
2997 match->dissector.used_keys |=
2998 BIT(FLOW_DISSECTOR_KEY_PORTS);
2999 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3000 offsetof(struct ethtool_rx_flow_key, tp);
3001 }
3002 if (v6_m_spec->tclass) {
3003 match->key.ip.tos = v6_spec->tclass;
3004 match->mask.ip.tos = v6_m_spec->tclass;
3005 match->dissector.used_keys |=
3006 BIT(FLOW_DISSECTOR_KEY_IP);
3007 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3008 offsetof(struct ethtool_rx_flow_key, ip);
3009 }
3010 }
3011 break;
3012 default:
3013 ethtool_rx_flow_rule_destroy(flow);
3014 return ERR_PTR(-EINVAL);
3015 }
3016
3017 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3018 case TCP_V4_FLOW:
3019 case TCP_V6_FLOW:
3020 match->key.basic.ip_proto = IPPROTO_TCP;
3021 break;
3022 case UDP_V4_FLOW:
3023 case UDP_V6_FLOW:
3024 match->key.basic.ip_proto = IPPROTO_UDP;
3025 break;
3026 }
3027 match->mask.basic.ip_proto = 0xff;
3028
3029 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3030 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3031 offsetof(struct ethtool_rx_flow_key, basic);
3032
3033 if (fs->flow_type & FLOW_EXT) {
3034 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3035 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3036
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003037 if (ext_m_spec->vlan_etype) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003038 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3039 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003040 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003041
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003042 if (ext_m_spec->vlan_tci) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003043 match->key.vlan.vlan_id =
3044 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3045 match->mask.vlan.vlan_id =
3046 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3047
Maxime Chevallierf0d2ca12019-06-12 17:18:38 +02003048 match->key.vlan.vlan_dei =
3049 !!(ext_h_spec->vlan_tci & htons(0x1000));
3050 match->mask.vlan.vlan_dei =
3051 !!(ext_m_spec->vlan_tci & htons(0x1000));
3052
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003053 match->key.vlan.vlan_priority =
3054 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3055 match->mask.vlan.vlan_priority =
3056 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003057 }
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003058
Maxime Chevallierb73484b2019-05-30 16:08:40 +02003059 if (ext_m_spec->vlan_etype ||
3060 ext_m_spec->vlan_tci) {
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003061 match->dissector.used_keys |=
3062 BIT(FLOW_DISSECTOR_KEY_VLAN);
3063 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3064 offsetof(struct ethtool_rx_flow_key, vlan);
3065 }
3066 }
3067 if (fs->flow_type & FLOW_MAC_EXT) {
3068 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3069 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3070
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003071 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3072 ETH_ALEN);
3073 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3074 ETH_ALEN);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003075
Nathan Chancellor8b34ec62019-02-07 21:46:53 -07003076 match->dissector.used_keys |=
3077 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3078 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3079 offsetof(struct ethtool_rx_flow_key, eth_addrs);
Pablo Neira Ayusoeca42052019-02-02 12:50:51 +01003080 }
3081
3082 act = &flow->rule->action.entries[0];
3083 switch (fs->ring_cookie) {
3084 case RX_CLS_FLOW_DISC:
3085 act->id = FLOW_ACTION_DROP;
3086 break;
3087 case RX_CLS_FLOW_WAKE:
3088 act->id = FLOW_ACTION_WAKE;
3089 break;
3090 default:
3091 act->id = FLOW_ACTION_QUEUE;
3092 if (fs->flow_type & FLOW_RSS)
3093 act->queue.ctx = input->rss_ctx;
3094
3095 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3096 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3097 break;
3098 }
3099
3100 return flow;
3101}
3102EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3103
3104void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3105{
3106 kfree(flow->rule);
3107 kfree(flow);
3108}
3109EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);